Skip to content

Commit

Permalink
Changes for lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashvi Jain authored and Yashvi Jain committed Feb 8, 2022
1 parent 17e6475 commit 0aa1bf9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 26 deletions.
43 changes: 30 additions & 13 deletions components/infra-proxy-service/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()

Expand All @@ -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
}

Expand All @@ -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
}
}
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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

}
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 10 additions & 8 deletions components/infra-proxy-service/migrations/pipeline/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -103,29 +106,29 @@ 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
}

result.ParsedResult.Orgs = append(result.ParsedResult.Orgs, insertOrUpdateOrg(orgNames, orgsPresentInDB, orgPath)...)

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
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion components/infra-proxy-service/pipeline/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions components/infra-proxy-service/server/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
}
Expand Down
4 changes: 2 additions & 2 deletions components/infra-proxy-service/storage/testDB/testDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 0aa1bf9

Please sign in to comment.