Skip to content

Commit

Permalink
Merge pull request #9 from Fernando-Dourado/fernando/overrides-v2
Browse files Browse the repository at this point in the history
add support to move overrides v2 entities
  • Loading branch information
aleksa11010 authored Aug 5, 2024
2 parents 67fc694 + 5ef4445 commit ba51db1
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 2 deletions.
78 changes: 78 additions & 0 deletions harness/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,52 @@ func (i *Infrastructure) MoveInfrastructureToRemote(api *APIRequest, c Config, e
return err
}

func (ov *OverridesV2Content) MoveToRemote(api *APIRequest, c Config) error {

params := map[string]string{
"accountIdentifier": c.AccountIdentifier,
"projectIdentifier": ov.ProjectIdentifier,
"orgIdentifier": ov.OrgIdentifier,
"connectorRef": c.GitDetails.ConnectorRef,
"repoName": c.GitDetails.RepoName,
"branch": c.GitDetails.BranchName,
"isNewBranch": "false",
"isHarnessCodeRepo": "false",
"filePath": c.GitDetails.FilePath,
"commitMsg": c.GitDetails.CommitMessage,
"moveConfigType": "INLINE_TO_REMOTE",
"serviceOverridesType": string(ov.Type),
"identifier": ov.Identifier,
}

if len(ov.EnvironmentRef) != 0 {
params["environmentRef"] = ov.EnvironmentRef
}
if len(ov.ServiceRef) > 0 {
params["serviceRef"] = ov.ServiceRef
}
if len(ov.InfraIdentifier) > 0 {
params["infraIdentifier"] = ov.InfraIdentifier
}

resp, err := api.Client.R().
SetHeader("x-api-key", api.APIKey).
SetQueryParams(params).
Post(api.BaseURL + "/gateway/ng/api/serviceOverrides/move-config")

if resp.StatusCode() != 200 {
ar := ApiResponse{}
err = json.Unmarshal(resp.Body(), &ar)
if err != nil {
return err
}
errMsg := fmt.Sprintf("CorrelationId: %s, ResponseMessages: %+v", ar.CorrelationID, ar.ResponseMessages)
return fmt.Errorf(errMsg)
}

return err
}

func (api *APIRequest) GetAllOrgs(account string) (Organizations, error) {
resp, err := api.Client.R().
SetHeader("x-api-key", api.APIKey).
Expand Down Expand Up @@ -709,3 +755,35 @@ func (api *APIRequest) GetServiceOverrides(environment, account, org, project st

return overrideList, nil
}

func (api *APIRequest) GetOverridesV2(account, org, project string, ovType OverridesV2Type) ([]OverridesV2Content, error) {

params := map[string]string{
"accountIdentifier": account,
"orgIdentifier": org,
"projectIdentifier": project,
"page": "0",
"size": "1000",
"type": string(ovType),
}

resp, err := api.Client.R().
SetHeader("x-api-key", api.APIKey).
SetHeader("Content-Type", "application/json").
SetQueryParams(params).
Post(api.BaseURL + "/ng/api/serviceOverrides/v2/list")
if err != nil {
return nil, err
}

result := OverridesV2Response{}
err = json.Unmarshal(resp.Body(), &result)
if err != nil {
return nil, err
}

var list []OverridesV2Content
list = append(list, result.Data.Content...)

return list, nil
}
25 changes: 25 additions & 0 deletions harness/git_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ func GetInfrastructureFilePath(gitX bool, customGitDetailsFilePath string, p Pro
}
}

func GetOverridesV2FilePath(gitX bool, customGitDetailsFilePath string, p Project, ov OverridesV2Content) string {
if len(customGitDetailsFilePath) == 0 {
if gitX {
switch ov.Type {
case OV2_Global:
return fmt.Sprintf(".harness/orgs/%s/projects/%s/overrides/%s/overrides.yaml", string(p.OrgIdentifier), p.Identifier, ov.EnvironmentRef)
case OV2_Service:
return fmt.Sprintf(".harness/orgs/%s/projects/%s/overrides/%s/services/%s/overrides.yaml",
string(p.OrgIdentifier), p.Identifier, ov.EnvironmentRef, ov.ServiceRef)
case OV2_Infra:
return fmt.Sprintf(".harness/orgs/%s/projects/%s/overrides/%s/infras/%s/overrides.yaml",
string(p.OrgIdentifier), p.Identifier, ov.EnvironmentRef, ov.InfraIdentifier)
case OV2_ServiceInfra:
return fmt.Sprintf(".harness/orgs/%s/projects/%s/overrides/%s/services/%s/infras/%s/overrides.yaml",
string(p.OrgIdentifier), p.Identifier, ov.EnvironmentRef, ov.ServiceRef, ov.InfraIdentifier)
default:
panic(fmt.Sprintf("unrecognized overrides V2 type %s", ov.Type))
}
}
return "overrides/" + string(p.OrgIdentifier) + "/" + p.Identifier + "/" + ov.Identifier + ".yaml"
} else {
return customGitDetailsFilePath + "/" + ov.Identifier + ".yaml"
}
}

func GetInputsetFilePath(gitX bool, customGitDetailsFilePath string, p Project, is *InputsetContent) string {
if len(customGitDetailsFilePath) == 0 {
if gitX {
Expand Down
39 changes: 39 additions & 0 deletions harness/overrides_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package harness

type OverridesV2Response struct {
Status string `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
Data OverridesV2Data `json:"data"`
}

type OverridesV2Data struct {
TotalPages int64 `json:"totalPages"`
TotalItems int64 `json:"totalItems"`
PageItemCount int64 `json:"pageItemCount"`
PageSize int64 `json:"pageSize"`
Content []OverridesV2Content `json:"content"`
PageIndex int64 `json:"pageIndex"`
Empty bool `json:"empty"`
}

type OverridesV2Content struct {
Identifier string `json:"identifier"`
AccountID string `json:"accountId"`
OrgIdentifier string `json:"orgIdentifier"`
ProjectIdentifier string `json:"projectIdentifier"`
EnvironmentRef string `json:"environmentRef"`
ServiceRef string `json:"serviceRef,omitempty"`
InfraIdentifier string `json:"infraIdentifier,omitempty"`
Type OverridesV2Type `json:"type"`
StoreType string `json:"storeType,omitempty"`
}

type OverridesV2Type string

const (
OV2_Global OverridesV2Type = "ENV_GLOBAL_OVERRIDE"
OV2_Service OverridesV2Type = "ENV_SERVICE_OVERRIDE"
OV2_Infra OverridesV2Type = "INFRA_GLOBAL_OVERRIDE"
OV2_ServiceInfra OverridesV2Type = "INFRA_SERVICE_OVERRIDE"
)
53 changes: 51 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func main() {
serviceManifests := flag.Bool("service", false, "Migrate service manifests.")
forceServiceUpdate := flag.Bool("update-service", false, "Force update remote service manifests")
overridesFlag := flag.Bool("overrides", false, "Migrate service overrides")
overridesV2Flag := flag.Bool("overrides-v2", false, "Migrate service overrides V2")
urlEncoding := flag.Bool("url-encode-string", false, "Encode Paths as URL friendly strings")
cgFolderStructure := flag.Bool("alt-path", false, "CG-like folder structure for Git")
prod3 := flag.Bool("prod3", false, "User Prod3 base URL for API calls")
Expand All @@ -63,6 +64,7 @@ func main() {
ServiceManifests bool
ForceUpdateManifests bool
Overrides bool
OverridesV2 bool
UrlEncoding bool
CGFolderStructure bool
Prod3 bool
Expand All @@ -82,6 +84,7 @@ func main() {
ServiceManifests: true,
ForceUpdateManifests: *forceServiceUpdate,
Overrides: true,
OverridesV2: true,
UrlEncoding: *urlEncoding,
CGFolderStructure: false,
Prod3: false,
Expand All @@ -99,6 +102,7 @@ func main() {
ServiceManifests: *serviceManifests,
ForceUpdateManifests: *forceServiceUpdate,
Overrides: *overridesFlag,
OverridesV2: *overridesV2Flag,
UrlEncoding: *urlEncoding,
CGFolderStructure: *cgFolderStructure,
Prod3: *prod3,
Expand Down Expand Up @@ -138,9 +142,9 @@ func main() {
APIKey: accountConfig.ApiKey,
}

if !scope.Pipelines && !scope.Templates && !scope.FileStore && !scope.Overrides && !scope.Services && !scope.Environments && !scope.InfraDef && !scope.Inputsets {
if !scope.Pipelines && !scope.Templates && !scope.FileStore && !scope.Overrides && !scope.Services && !scope.Environments && !scope.InfraDef && !scope.Inputsets && !scope.OverridesV2 {
log.Errorf(color.RedString("You need to specify at least one type of entity to migrate!"))
log.Errorf(color.RedString("Please use -pipelines, -templates, -services, -environments, -filestore or -overrides flags"))
log.Errorf(color.RedString("Please use -pipelines, -templates, -services, -environments, -overrides-v2, -filestore or -overrides flags"))
log.Errorf(color.RedString("If you want to migrate all entities, use -all flag"))
return
}
Expand Down Expand Up @@ -411,6 +415,14 @@ func main() {
log.Errorf(color.RedString("Unable to inline-to-remote infrastructure - %s", err))
}
}

// OVERRIDES-V2 MOVE TO REMOTE
if scope.OverridesV2 {
err := processOverridesV2(log, api, *customGitDetailsFilePath, accountConfig, p, scope.GitX)
if err != nil {
log.Errorf(color.RedString("Unable to inline-to-remote overrides v2 - %s", err))
}
}
}
if scope.Pipelines {
pipelinesSummary(log, boldCyan, failedPipelines, pipelines)
Expand Down Expand Up @@ -964,6 +976,43 @@ func processInfraDefScope(log *logrus.Logger, api harness.APIRequest, customGitD
return nil
}

func processOverridesV2(log *logrus.Logger, api harness.APIRequest, customGitDetailsFilePath string, cfg harness.Config, p harness.Project, gitX bool) error {

overrideTypes := []harness.OverridesV2Type{harness.OV2_Global, harness.OV2_Service, harness.OV2_Infra, harness.OV2_ServiceInfra}
var overrides []harness.OverridesV2Content

for _, ovType := range overrideTypes {
ov, err := api.GetOverridesV2(cfg.AccountIdentifier, string(p.OrgIdentifier), p.Identifier, ovType)

if err != nil {
log.Errorf("Failed to get service overrides V2 type %s - %s", ovType, err)
} else {
overrides = append(overrides, ov...)
}
}

if len(overrides) > 0 {
log.Infof("Moving %d service overrides V2 to remote", len(overrides))

pbTemplate := `{{ blue "Processing: " }} {{ bar . "<" "-" (cycle . "↖" "↗" "↘" "↙" ) "." ">"}} {{percent .}} `
pbBar := pb.ProgressBarTemplate(pbTemplate).Start(len(overrides))

for _, override := range overrides {
if override.StoreType != "REMOTE" {
cfg.GitDetails.FilePath = harness.GetOverridesV2FilePath(gitX, customGitDetailsFilePath, p, override)

if err := override.MoveToRemote(&api, cfg); err != nil {
log.Errorf(color.RedString("Unable to move overrides V2 [%s] - %s", override, err))
}
}
pbBar.Increment()
}
pbBar.Finish()
}

return nil
}

func servicesSummary(log *logrus.Logger, boldCyan *color.Color, failedTemplates []string, failedServices []string, alreadyRemoteServices []string, services []*harness.ServiceClass) {
log.Infof(boldCyan.Sprintf("---Services---"))
if len(failedTemplates) > 0 {
Expand Down

0 comments on commit ba51db1

Please sign in to comment.