From 0aa1bf9ba02a2906e9d801caf0e329c264f736fe Mon Sep 17 00:00:00 2001 From: Yashvi Jain Date: Tue, 8 Feb 2022 20:21:59 +0530 Subject: [PATCH] Changes for lint --- .../migrations/migrations.go | 43 +++++++++++++------ .../migrations/pipeline/models.go | 2 +- .../migrations/pipeline/phaseonemigration.go | 2 +- .../migrations/pipeline/utility.go | 18 ++++---- .../infra-proxy-service/pipeline/models.go | 2 +- .../infra-proxy-service/server/servers.go | 4 ++ .../storage/testDB/testDB.go | 4 +- 7 files changed, 49 insertions(+), 26 deletions(-) diff --git a/components/infra-proxy-service/migrations/migrations.go b/components/infra-proxy-service/migrations/migrations.go index 5fa787d037a8..7987485a85ab 100644 --- a/components/infra-proxy-service/migrations/migrations.go +++ b/components/infra-proxy-service/migrations/migrations.go @@ -12,7 +12,6 @@ import ( "github.com/chef/automate/api/interservice/infra_proxy/migrations/response" "github.com/chef/automate/api/interservice/infra_proxy/migrations/service" "github.com/chef/automate/components/infra-proxy-service/constants" - "github.com/chef/automate/components/infra-proxy-service/migrations/pipeline" pipeline_model "github.com/chef/automate/components/infra-proxy-service/pipeline" "github.com/chef/automate/components/infra-proxy-service/validation" @@ -31,13 +30,22 @@ func (s *MigrationServer) UploadFile(stream service.MigrationDataService_UploadF if err != nil { log.WithError(err).Error("Unable to create migration id") res := handleErrorForUploadFileAndMigration(err, migrationId, serverId, s, ctx) - stream.SendAndClose(res) + errStream := stream.SendAndClose(res) + if errStream != nil { + log.Errorf("Failed to send and close strean file for migration id %s : %s", migrationId, err.Error()) + } return err } log.Info("Starting with migration phase with the upload file for migration id: ", migrationId) _, err = s.service.Migration.StartMigration(ctx, migrationId, serverId) + if err != nil { + log.Errorf("Unable to insert the migration status Start Migration for migration id : %s", migrationId) + } fileData := bytes.Buffer{} - s.service.Migration.StartFileUpload(ctx, migrationId, serverId) + _, err = s.service.Migration.StartFileUpload(ctx, migrationId, serverId) + if err != nil { + log.Errorf("Unable to insert the migration status Start File upload for migration id : %s", migrationId) + } for { req, err := stream.Recv() @@ -48,7 +56,10 @@ func (s *MigrationServer) UploadFile(stream service.MigrationDataService_UploadF if err != nil { log.Errorf("Failed to upload file for migration id %s : %s", migrationId, err.Error()) res := handleErrorForUploadFileAndMigration(err, migrationId, serverId, s, ctx) - stream.SendAndClose(res) + errStream := stream.SendAndClose(res) + if errStream != nil { + log.Errorf("Failed to send and close strean file for migration id %s : %s", migrationId, err.Error()) + } return err } @@ -57,7 +68,10 @@ func (s *MigrationServer) UploadFile(stream service.MigrationDataService_UploadF if err != nil { log.Errorf("Failed to upload file for migration id %s : %s", migrationId, err.Error()) res := handleErrorForUploadFileAndMigration(err, migrationId, serverId, s, ctx) - stream.SendAndClose(res) + errStream := stream.SendAndClose(res) + if errStream != nil { + log.Errorf("Failed to send and close strean file for migration id %s : %s", migrationId, err.Error()) + } return err } } @@ -66,7 +80,10 @@ func (s *MigrationServer) UploadFile(stream service.MigrationDataService_UploadF if err != nil { log.Errorf("Failed to save uploaded file for migration id %s : %s", migrationId, err.Error()) res := handleErrorForUploadFileAndMigration(err, migrationId, serverId, s, ctx) - stream.SendAndClose(res) + errStream := stream.SendAndClose(res) + if errStream != nil { + log.Errorf("Failed to send and close strean file for migration id %s : %s", migrationId, err.Error()) + } return err } log.Info("File successfully saved in the directory for the requested file for migration id: ", migrationId) @@ -75,7 +92,7 @@ func (s *MigrationServer) UploadFile(stream service.MigrationDataService_UploadF MigrationId: migrationId, Success: true, } - s.service.Migration.CompleteFileUpload(ctx, migrationId, serverId, 0, 0, 0) + _, _ = s.service.Migration.CompleteFileUpload(ctx, migrationId, serverId, 0, 0, 0) log.Info("File successfully uploaded in the directory for the requested file for migration id: ", migrationId) err = stream.SendAndClose(res) if err != nil { @@ -84,7 +101,7 @@ func (s *MigrationServer) UploadFile(stream service.MigrationDataService_UploadF return err } - pipelineResult := pipeline.Result{Meta: pipeline.Meta{ZipFile: fileName}} + pipelineResult := pipeline_model.Result{Meta: pipeline_model.Meta{ZipFile: fileName}} s.phaseOnePipeline.Run(pipelineResult) return nil } @@ -151,9 +168,9 @@ func createMigrationId() (string, error) { //handleErrorForUploadFileAndMigration handles the error for the file upload func handleErrorForUploadFileAndMigration(err error, migrationId string, serviceId string, s *MigrationServer, ctx context.Context) *response.UploadZipFileResponse { response := createResponseWithErrors(err, migrationId) - s.service.Migration.FailedFileUpload(ctx, migrationId, serviceId, err.Error(), 0, 0, 0) + _, _ = s.service.Migration.FailedFileUpload(ctx, migrationId, serviceId, err.Error(), 0, 0, 0) //ToDo to add the Failed migration status as well - s.service.Migration.FailedMigration(ctx, migrationId, serviceId, err.Error(), 0, 0, 0) + _, _ = s.service.Migration.FailedMigration(ctx, migrationId, serviceId, err.Error(), 0, 0, 0) return response } @@ -191,21 +208,21 @@ func (s *MigrationServer) CancelMigration(ctx context.Context, req *request.Canc folderPath := path.Join("/hab/svc/infra-proxy-service/data", req.MigrationId) err = os.RemoveAll(folderPath) if err != nil { - s.service.Migration.FailedCancelMigration(ctx, req.MigrationId, req.ServerId, err.Error(), 0, 0, 0) + _, _ = s.service.Migration.FailedCancelMigration(ctx, req.MigrationId, req.ServerId, err.Error(), 0, 0, 0) return nil, err } // Clear up the stage table _, err = s.service.Migration.DeleteMigrationStage(ctx, req.MigrationId) if err != nil { - s.service.Migration.FailedCancelMigration(ctx, req.MigrationId, req.ServerId, err.Error(), 0, 0, 0) + _, _ = s.service.Migration.FailedCancelMigration(ctx, req.MigrationId, req.ServerId, err.Error(), 0, 0, 0) return nil, err } // Update the migration status _, err = s.service.Migration.CancelMigration(ctx, req.MigrationId, req.ServerId, 0, 0, 0) if err != nil { - s.service.Migration.FailedCancelMigration(ctx, req.MigrationId, req.ServerId, err.Error(), 0, 0, 0) + _, _ = s.service.Migration.FailedCancelMigration(ctx, req.MigrationId, req.ServerId, err.Error(), 0, 0, 0) return nil, err } diff --git a/components/infra-proxy-service/migrations/pipeline/models.go b/components/infra-proxy-service/migrations/pipeline/models.go index b730e76df522..3f16370d98b5 100644 --- a/components/infra-proxy-service/migrations/pipeline/models.go +++ b/components/infra-proxy-service/migrations/pipeline/models.go @@ -113,7 +113,7 @@ type User struct { // Connector ldap user Connector string `json:"connector"` - // IsConflicting for user's existance in db + // IsConflicting for user's existence in db IsConflicting bool `json:"is_conflicting"` // IsAdmin (user is admin or not) diff --git a/components/infra-proxy-service/migrations/pipeline/phaseonemigration.go b/components/infra-proxy-service/migrations/pipeline/phaseonemigration.go index 559776f1d44d..5b0fa0fb041f 100644 --- a/components/infra-proxy-service/migrations/pipeline/phaseonemigration.go +++ b/components/infra-proxy-service/migrations/pipeline/phaseonemigration.go @@ -216,7 +216,7 @@ func SetupPhaseOnePipeline() PhaseOnePipleine { return PhaseOnePipleine{in: c} } -func (p *PhaseOnePipleine) Run(result Result) { +func (p *PhaseOnePipleine) Run(result pipeline.Result) { go func() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/components/infra-proxy-service/migrations/pipeline/utility.go b/components/infra-proxy-service/migrations/pipeline/utility.go index dbcc8411345d..735a668e201e 100644 --- a/components/infra-proxy-service/migrations/pipeline/utility.go +++ b/components/infra-proxy-service/migrations/pipeline/utility.go @@ -62,6 +62,9 @@ func StoreOrg(ctx context.Context, st storage.Storage, org Org, serverID string, return err, actionTaken } _, err = st.StoreOrg(ctx, org.Name, org.FullName, "", "", serverID, projects) + if err != nil { + log.Errorf("Unable to insert org for server id: %s", serverID) + } actionTaken = Insert case Delete: _, err = st.DeleteOrg(ctx, org.Name, serverID) @@ -103,21 +106,21 @@ func ParseOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationSto folder, err := os.Open(orgPath) if err != nil { log.Errorf("Failed to open the folder for the file path %s : %s", orgPath, err.Error()) - mst.FailedOrgParsing(ctx, result.Meta.MigrationID, result.Meta.ServerID, err.Error(), 0, 0, 0) + _, _ = mst.FailedOrgParsing(ctx, result.Meta.MigrationID, result.Meta.ServerID, err.Error(), 0, 0, 0) return result, err } - defer folder.Close() orgNames, err := folder.Readdir(0) if err != nil { log.Errorf("Failed to read the files for the file path %s : %s", orgPath, err.Error()) - mst.FailedOrgParsing(ctx, result.Meta.MigrationID, result.Meta.ServerID, err.Error(), 0, 0, 0) + _, _ = mst.FailedOrgParsing(ctx, result.Meta.MigrationID, result.Meta.ServerID, err.Error(), 0, 0, 0) return result, err } + _ = folder.Close() orgsPresentInDB, err := st.GetOrgs(ctx, result.Meta.ServerID) if err != nil { log.Errorf("Failed to read orgs from database for %s:%s", result.Meta.ServerID, err.Error()) - mst.FailedOrgParsing(ctx, result.Meta.MigrationID, result.Meta.ServerID, err.Error(), 0, 0, 0) + _, _ = mst.FailedOrgParsing(ctx, result.Meta.MigrationID, result.Meta.ServerID, err.Error(), 0, 0, 0) return result, err } @@ -125,7 +128,7 @@ func ParseOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationSto result.ParsedResult.Orgs = append(result.ParsedResult.Orgs, deleteOrgsIfNotPresentInCurrentFile(orgNames, orgsPresentInDB)...) - mst.CompleteOrgParsing(ctx, result.Meta.MigrationID, result.Meta.ServerID, 0, 0, 0) + _, err = mst.CompleteOrgParsing(ctx, result.Meta.MigrationID, result.Meta.ServerID, 0, 0, 0) if err != nil { log.Errorf("Failed to update the complete status while parsing for migration id %s : %s", result.Meta.MigrationID, err.Error()) return result, err @@ -210,10 +213,9 @@ func openOrgFolder(org os.FileInfo, fileLocation string) OrgJson { } log.Info("Successfully opened the file at location", jsonPath) // defer the closing of our jsonFile so that we can parse it later on - defer jsonFile.Close() byteValue, _ := ioutil.ReadAll(jsonFile) - json.Unmarshal(byteValue, &orgJson) - + _ = json.Unmarshal(byteValue, &orgJson) + _ = jsonFile.Close() return orgJson } diff --git a/components/infra-proxy-service/pipeline/models.go b/components/infra-proxy-service/pipeline/models.go index b730e76df522..3f16370d98b5 100644 --- a/components/infra-proxy-service/pipeline/models.go +++ b/components/infra-proxy-service/pipeline/models.go @@ -113,7 +113,7 @@ type User struct { // Connector ldap user Connector string `json:"connector"` - // IsConflicting for user's existance in db + // IsConflicting for user's existence in db IsConflicting bool `json:"is_conflicting"` // IsAdmin (user is admin or not) diff --git a/components/infra-proxy-service/server/servers.go b/components/infra-proxy-service/server/servers.go index c709d927f6eb..b6f238956950 100644 --- a/components/infra-proxy-service/server/servers.go +++ b/components/infra-proxy-service/server/servers.go @@ -3,6 +3,7 @@ package server import ( "context" "encoding/json" + "github.com/prometheus/common/log" "io/ioutil" "github.com/chef/automate/api/external/common/query" @@ -178,6 +179,9 @@ func (s *Server) GetServer(ctx context.Context, req *request.GetServer) (*respon return nil, service.ParseStorageError(err, *req, "server") } migration, err := s.service.Migration.GetActiveMigration(ctx, req.Id) + if err != nil { + log.Errorf("Unable to fetch migration status for server id:%s", req.Id) + } resp := &response.GetServer{ Server: fromStorageServerWithMigrationDetails(server, migration), } diff --git a/components/infra-proxy-service/storage/testDB/testDB.go b/components/infra-proxy-service/storage/testDB/testDB.go index 864e2c114cf2..2b460ef59779 100644 --- a/components/infra-proxy-service/storage/testDB/testDB.go +++ b/components/infra-proxy-service/storage/testDB/testDB.go @@ -25,10 +25,10 @@ func (t *TestDB) GetServer(_ context.Context, id string) (storage.Server, error) } func (t *TestDB) GetServers(ctx context.Context) ([]storage.Server, error) { + x := make([]storage.Server, 10) if t.NeedError { - return []storage.Server{}, errors.New("failed to fetch Server") + return x, errors.New("failed to fetch Server") } - var x []storage.Server for _, v := range t.Servers { x = append(x, v) }