From 9f8a19785e196c6e8c244d2d4bc33dc221c9bb78 Mon Sep 17 00:00:00 2001 From: Deepak Puthraya Date: Fri, 20 Jan 2023 18:45:20 +0530 Subject: [PATCH] Add support for skipping workflows & pipelines (#9) --- client.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ helper.go | 42 ++---------------------------------------- main.go | 16 +++++++++++++++- pipelines.go | 13 ++++++++++--- summary.go | 21 +++++++++++++++++++++ workflows.go | 21 ++++++++++++++------- 6 files changed, 106 insertions(+), 51 deletions(-) create mode 100644 client.go create mode 100644 summary.go diff --git a/client.go b/client.go new file mode 100644 index 0000000..9f4ad76 --- /dev/null +++ b/client.go @@ -0,0 +1,44 @@ +package main + +import ( + "bytes" + "encoding/json" + "errors" + log "github.com/sirupsen/logrus" + "io" + "net/http" + "strconv" +) + +func Post(reqUrl string, auth string, body interface{}) ([]byte, error) { + postBody, _ := json.Marshal(body) + requestBody := bytes.NewBuffer(postBody) + log.WithFields(log.Fields{ + "body": string(postBody), + }).Debug("The request body") + req, err := http.NewRequest("POST", reqUrl, requestBody) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("x-api-key", auth) + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer func(Body io.ReadCloser) { + _ = Body.Close() + }(resp.Body) + respBody, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + log.WithFields(log.Fields{ + "body": string(respBody), + }).Debug("The response body") + if resp.StatusCode != 200 { + return nil, errors.New("received non 200 response code. The response code was " + strconv.Itoa(resp.StatusCode)) + } + return respBody, nil +} diff --git a/helper.go b/helper.go index 59c6190..ccd68ad 100644 --- a/helper.go +++ b/helper.go @@ -1,18 +1,13 @@ package main import ( - "bytes" "encoding/json" - "errors" "fmt" "github.com/AlecAivazis/survey/v2" "github.com/jedib0t/go-pretty/v6/table" log "github.com/sirupsen/logrus" "golang.org/x/exp/slices" - "io" - "net/http" "os" - "strconv" ) const ( @@ -83,45 +78,12 @@ func ConfirmInput(question string) bool { return confirm } -func PostReq(reqUrl string, auth string, body interface{}) ([]byte, error) { - postBody, _ := json.Marshal(body) - requestBody := bytes.NewBuffer(postBody) - log.WithFields(log.Fields{ - "body": string(postBody), - }).Debug("The request body") - req, err := http.NewRequest("POST", reqUrl, requestBody) - if err != nil { - return nil, err - } - req.Header.Set("Content-Type", "application/json") - req.Header.Set("x-api-key", auth) - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - return nil, err - } - if resp.StatusCode != 200 { - return nil, errors.New("received non 200 response code. The response code was " + strconv.Itoa(resp.StatusCode)) - } - defer func(Body io.ReadCloser) { - _ = Body.Close() - }(resp.Body) - respBody, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - log.WithFields(log.Fields{ - "body": string(respBody), - }).Debug("The response body") - return respBody, nil -} - func GetUrl(environment string, service string, path string, accountId string) string { return fmt.Sprintf("%s/api/ng-migration/%s?accountIdentifier=%s", urlMap[environment][service], path, accountId) } func MakeAPICall(url string, auth string, body interface{}) ([]byte, error) { - resp, err := PostReq(url, auth, body) + resp, err := Post(url, auth, body) if err != nil { log.Fatalln("There was error. Exiting...", err) } @@ -129,7 +91,7 @@ func MakeAPICall(url string, auth string, body interface{}) ([]byte, error) { } func CreateEntity(url string, auth string, body RequestBody) { - resp, err := PostReq(url, auth, body) + resp, err := Post(url, auth, body) if err != nil { log.Fatalln("There was error while migrating. Exiting...", err) } diff --git a/main.go b/main.go index 1b82952..8b08de4 100644 --- a/main.go +++ b/main.go @@ -146,7 +146,7 @@ func main() { altsrc.NewStringFlag(&cli.StringFlag{ Name: "pipelines", Usage: "pipelines as comma separated values `pipeline1,pipeline2`", - Destination: &migrationReq.WorkflowIds, + Destination: &migrationReq.PipelineIds, }), altsrc.NewStringFlag(&cli.StringFlag{ Name: "load", @@ -176,6 +176,20 @@ func main() { EnableBashCompletion: true, Suggest: true, Commands: []*cli.Command{ + { + Name: "account-summary", + Usage: "Get a summary of an account", + Action: func(context *cli.Context) error { + return cliWrapper(getAccountSummary, context) + }, + }, + { + Name: "application-summary", + Usage: "Get a summary of an app", + Action: func(context *cli.Context) error { + return cliWrapper(getAppSummary, context) + }, + }, { Name: "app", Usage: "Import an app into an existing project by providing the `appId`", diff --git a/pipelines.go b/pipelines.go index 0df6f1d..e1f798f 100644 --- a/pipelines.go +++ b/pipelines.go @@ -19,8 +19,11 @@ func migratePipelines(*cli.Context) error { } if len(migrationReq.PipelineIds) == 0 { - promptConfirm = true - migrationReq.PipelineIds = TextInput("Provide the pipelines that you wish to import as template as comma separated values(e.g. pipeline1,pipeline2)") + allPipelinesConfirm := ConfirmInput("No pipelines provided. This defaults to migrating all pipelines within the application. Do you want to proceed?") + if !allPipelinesConfirm { + promptConfirm = true + migrationReq.PipelineIds = TextInput("Provide the pipelines that you wish to import as template as comma separated values(e.g. pipeline1,pipeline2)") + } } promptConfirm = PromptOrgAndProject([]string{Project}) || promptConfirm @@ -37,8 +40,12 @@ func migratePipelines(*cli.Context) error { url := GetUrl(migrationReq.Environment, MIGRATOR, "save/v2", migrationReq.Account) // Migrating the pipelines log.Info("Importing the pipelines....") + var pipelineIds []string + if len(migrationReq.PipelineIds) > 0 { + pipelineIds = strings.Split(migrationReq.PipelineIds, ",") + } CreateEntity(url, migrationReq.Auth, getReqBody(Pipeline, Filter{ - PipelineIds: strings.Split(migrationReq.PipelineIds, ","), + PipelineIds: pipelineIds, AppId: migrationReq.AppId, })) log.Info("Imported the pipelines.") diff --git a/summary.go b/summary.go new file mode 100644 index 0000000..23910d8 --- /dev/null +++ b/summary.go @@ -0,0 +1,21 @@ +package main + +import "github.com/urfave/cli/v2" + +func getAccountSummary(*cli.Context) error { + _ = PromptDefaultInputs() + logMigrationDetails() + + _ = GetUrl(migrationReq.Environment, MIGRATOR, "save/v2", migrationReq.Account) + return nil +} + +func getAppSummary(*cli.Context) error { + _ = PromptDefaultInputs() + if len(migrationReq.AppId) == 0 { + migrationReq.AppId = TextInput("Please provide the application ID - ") + } + + _ = GetUrl(migrationReq.Environment, MIGRATOR, "save/v2", migrationReq.Account) + return nil +} diff --git a/workflows.go b/workflows.go index c07a5d2..26ff309 100644 --- a/workflows.go +++ b/workflows.go @@ -13,16 +13,19 @@ func migrateWorkflows(*cli.Context) error { migrationReq.AppId = TextInput("Please provide the application ID of the app containing the workflows -") } - if len(migrationReq.WorkflowIds) == 0 { - promptConfirm = true - migrationReq.WorkflowIds = TextInput("Provide the workflows that you wish to import as template as comma separated values(e.g. workflow1,workflow2)") - } - if len(migrationReq.WorkflowScope) == 0 { promptConfirm = true migrationReq.WorkflowScope = SelectInput("Scope for workflows:", scopes, Project) } + if len(migrationReq.WorkflowIds) == 0 { + allWorkflowConfirm := ConfirmInput("No workflows provided. This defaults to migrating all workflows within the application. Do you want to proceed?") + if !allWorkflowConfirm { + promptConfirm = true + migrationReq.WorkflowIds = TextInput("Provide the workflows that you wish to import as template as comma separated values(e.g. workflow1,workflow2)") + } + } + promptConfirm = PromptOrgAndProject([]string{migrationReq.WorkflowScope, migrationReq.SecretScope, migrationReq.ConnectorScope, migrationReq.TemplateScope}) || promptConfirm logMigrationDetails() @@ -35,10 +38,14 @@ func migrateWorkflows(*cli.Context) error { } url := GetUrl(migrationReq.Environment, MIGRATOR, "save/v2", migrationReq.Account) - // Migrating the app + // Migrating the workflows + var workflowIds []string + if len(migrationReq.WorkflowIds) > 0 { + workflowIds = strings.Split(migrationReq.WorkflowIds, ",") + } log.Info("Importing the workflows....") CreateEntity(url, migrationReq.Auth, getReqBody(Workflow, Filter{ - WorkflowIds: strings.Split(migrationReq.WorkflowIds, ","), + WorkflowIds: workflowIds, AppId: migrationReq.AppId, })) log.Info("Imported the workflows.")