Skip to content

Commit

Permalink
Stalwart 24 Pipeline function to store the orgs to database (#6658)
Browse files Browse the repository at this point in the history
* 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
2 people authored and vinay033 committed Mar 9, 2022
1 parent f8e5d07 commit 77a1fb5
Show file tree
Hide file tree
Showing 10 changed files with 493 additions and 2 deletions.
95 changes: 95 additions & 0 deletions 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.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ type Meta struct {

// UnzipFolder for unzipped folder's location
UnzipFolder string `json:"unzip_folder"`

// Chef Infra Server ID
ServerID string `json:"server_id"`

// Migration ID
MigrationID string `json:"migration_id"`
}

type StageResult struct {
Expand Down
64 changes: 64 additions & 0 deletions components/infra-proxy-service/migrations/pipeline/utility.go
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 components/infra-proxy-service/migrations/pipeline/utility_test.go
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)
}
})
}
}
3 changes: 1 addition & 2 deletions components/infra-proxy-service/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/chef/automate/components/infra-proxy-service/migrations"
"net"
"net/http"

Expand All @@ -17,8 +18,6 @@ import (
grpc_s "github.com/chef/automate/api/interservice/infra_proxy/service"

grpc_migration "github.com/chef/automate/api/interservice/infra_proxy/migrations/service"
migrations "github.com/chef/automate/components/infra-proxy-service/server/migrations"

"github.com/chef/automate/components/infra-proxy-service/service"
"github.com/chef/automate/lib/grpc/health"
"github.com/chef/automate/lib/tracing"
Expand Down
104 changes: 104 additions & 0 deletions components/infra-proxy-service/storage/testDB/testDB.go
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")
}
Loading

0 comments on commit 77a1fb5

Please sign in to comment.