-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stalwart 24 Pipeline function to store the orgs to database (#6658)
* Pipeline function to store the orgs Signed-off-by: sonali wale <sonali.wale@progress.com> * Added minor changes Signed-off-by: sonali wale <sonali.wale@progress.com> * Code formatting and setting up tests DB Signed-off-by: Kallol Roy <karoy@progress.com> * Added migration status changes Signed-off-by: sonali wale <sonali.wale@progress.com> * Added minor changes Signed-off-by: sonali wale <sonali.wale@progress.com> * Comment removed Signed-off-by: sonali wale <sonali.wale@progress.com> * Logs added Signed-off-by: sonali wale <sonali.wale@progress.com> * Minor changes added Signed-off-by: sonali wale <sonali.wale@progress.com> * Removed message variable Signed-off-by: sonali wale <sonali.wale@progress.com> * Incorporating review comments Signed-off-by: sonali wale <sonali.wale@progress.com> Co-authored-by: Kallol Roy <karoy@progress.com>
- Loading branch information
Showing
10 changed files
with
493 additions
and
2 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
api/external/infra_proxy/migrations/migrations.pb.client_mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
components/infra-proxy-service/migrations/pipeline/utility.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package pipeline | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/chef/automate/components/infra-proxy-service/storage" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// StoreOrgs reads the Result struct and populate the orgs table | ||
func StoreOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationStorage, res Result) (Result, error) { | ||
var err error | ||
var msg string | ||
var totalSucceeded, totalSkipped, totalFailed int64 | ||
_, err = mst.StartOrgMigration(ctx, res.Meta.MigrationID, res.Meta.ServerID) | ||
if err != nil { | ||
return res, err | ||
} | ||
log.Info("Starting the organisation migration phase for migration id: ", res.Meta.MigrationID) | ||
for _, org := range res.ParsedResult.Orgs { | ||
err, _ = StoreOrg(ctx, st, org, res.Meta.ServerID) | ||
if err != nil { | ||
totalFailed++ | ||
msg = err.Error() | ||
continue | ||
} | ||
if org.ActionOps == Skip { | ||
totalSkipped++ | ||
continue | ||
} | ||
totalSucceeded++ | ||
} | ||
if len(res.ParsedResult.Orgs) == int(totalFailed) { | ||
log.Errorf("Failed to migrate orgs for migration id %s : %s", res.Meta.MigrationID, err.Error()) | ||
_, _ = mst.FailedOrgMigration(ctx, res.Meta.MigrationID, res.Meta.ServerID, msg, totalSucceeded, totalSkipped, totalFailed) | ||
return res, err | ||
} | ||
_, err = mst.CompleteOrgMigration(ctx, res.Meta.MigrationID, res.Meta.ServerID, totalSucceeded, totalSkipped, totalFailed) | ||
if err != nil { | ||
log.Errorf("Failed to update the status for migration id %s : %s", res.Meta.MigrationID, err.Error()) | ||
return res, err | ||
} | ||
log.Info("Successfully completed the organisation migration phase for migration id: ", res.Meta.MigrationID) | ||
return res, err | ||
} | ||
|
||
// StoreOrg stores a single Org into DB | ||
func StoreOrg(ctx context.Context, st storage.Storage, org Org, serverID string) (error, ActionOps) { | ||
var actionTaken ActionOps | ||
var err error | ||
switch org.ActionOps { | ||
case Insert: | ||
_, err = st.StoreOrg(ctx, org.Name, org.FullName, "", "", serverID, nil) | ||
actionTaken = Insert | ||
case Delete: | ||
_, err = st.DeleteOrg(ctx, org.Name, serverID) | ||
actionTaken = Delete | ||
case Update: | ||
_, err = st.EditOrg(ctx, org.Name, org.FullName, "", serverID, nil) | ||
actionTaken = Update | ||
default: | ||
} | ||
return err, actionTaken | ||
} |
40 changes: 40 additions & 0 deletions
40
components/infra-proxy-service/migrations/pipeline/utility_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package pipeline | ||
|
||
import ( | ||
"context" | ||
"github.com/chef/automate/components/infra-proxy-service/storage" | ||
"github.com/chef/automate/components/infra-proxy-service/storage/testDB" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestStoreOrg(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
st storage.Storage | ||
org Org | ||
serverID string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want error | ||
want1 ActionOps | ||
}{ | ||
// TODO: Add test cases. | ||
{name: "Test Delete Org", args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: Org{Name: "org1", FullName: "Org 1", ActionOps: Delete}, serverID: "server1"}, want: nil, want1: Delete}, | ||
{name: "Test Store Org", args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: Org{Name: "org1", FullName: "Org 1", ActionOps: Insert}, serverID: "server1"}, want: nil, want1: Insert}, | ||
{name: "Test Edit Org", args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: Org{Name: "org1", FullName: "Org 1", ActionOps: Update}, serverID: "server1"}, want: nil, want1: Update}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, got1 := StoreOrg(tt.args.ctx, tt.args.st, tt.args.org, tt.args.serverID) | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("StoreOrg() got = %v, want %v", got, tt.want) | ||
} | ||
if got1 != tt.want1 { | ||
t.Errorf("StoreOrg() got1 = %v, want %v", got1, tt.want1) | ||
} | ||
}) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
components/infra-proxy-service/storage/testDB/testDB.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package testDB | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/chef/automate/components/infra-proxy-service/storage" | ||
) | ||
|
||
type TestDB struct { | ||
Orgs map[string]storage.Org | ||
Servers map[string]storage.Server | ||
Users map[string]storage.User | ||
NeedError bool | ||
} | ||
|
||
func (t *TestDB) GetServer(_ context.Context, id string) (storage.Server, error) { | ||
if t.NeedError { | ||
return storage.Server{}, errors.New("failed to fetch Server") | ||
} | ||
if _, ok := t.Servers[id]; !ok { | ||
return storage.Server{}, errors.New("No such server found") | ||
} | ||
return t.Servers[id], nil | ||
} | ||
|
||
func (t *TestDB) GetServers(ctx context.Context) ([]storage.Server, error) { | ||
if t.NeedError { | ||
return []storage.Server{}, errors.New("failed to fetch Server") | ||
} | ||
var x []storage.Server | ||
for _, v := range t.Servers { | ||
x = append(x, v) | ||
} | ||
return x, nil | ||
} | ||
|
||
func (t *TestDB) StoreServer(ctx context.Context, id string, name string, fqdn string, ipAddress string, credentialId string) (storage.Server, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) DeleteServer(ctx context.Context, id string) (storage.Server, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) EditServer(ctx context.Context, id string, name string, fqdn string, ipAddress string) (storage.Server, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) EditServerWebuiKey(ctx context.Context, id, credentialId string) (storage.Server, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) GetOrg(ctx context.Context, orgID string, serverID string) (storage.Org, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) GetOrgs(ctx context.Context, serverID string) ([]storage.Org, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) StoreOrg(ctx context.Context, id string, name string, adminUser string, adminKey string, serverID string, projects []string) (storage.Org, error) { | ||
if t.NeedError { | ||
return storage.Org{}, errors.New("failed to store org") | ||
} | ||
return storage.Org{ID: id, Name: name, AdminUser: adminUser, CredentialID: adminKey, ServerID: serverID, Projects: projects}, nil | ||
} | ||
|
||
func (t *TestDB) DeleteOrg(ctx context.Context, orgID string, serverID string) (storage.Org, error) { | ||
if t.NeedError { | ||
return storage.Org{}, errors.New("failed to delete org") | ||
} | ||
return storage.Org{ID: orgID}, nil | ||
} | ||
|
||
func (t *TestDB) EditOrg(ctx context.Context, id string, name string, adminUser string, serverID string, projects []string) (storage.Org, error) { | ||
if t.NeedError { | ||
return storage.Org{}, errors.New("failed to edit org") | ||
} | ||
return storage.Org{ID: id, Name: name, AdminUser: adminUser, ServerID: serverID, Projects: projects}, nil | ||
} | ||
|
||
func (t *TestDB) TouchOrg(ctx context.Context, id string, serverID string) (storage.Org, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) InsertUser(ctx context.Context, id, serverID, infraServerUsername, credentialID, Connector, automateUserID string, IsServerAdmin bool) (storage.User, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) GetUser(ctx context.Context, id string) (storage.User, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) EditUser(ctx context.Context, id, serverID, infraServerUsername, credentialID, Connector, automateUserID string, IsServerAdmin bool) (storage.User, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) DeleteUser(ctx context.Context, id string) (storage.User, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t *TestDB) GetAutomateInfraServerUsers(ctx context.Context, serverId string) ([]storage.User, error) { | ||
panic("implement me") | ||
} |
Oops, something went wrong.