Skip to content

Commit

Permalink
Add support for skipping workflows & pipelines (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
puthrayaharness authored Jan 20, 2023
1 parent fd2fdda commit 9f8a197
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 51 deletions.
44 changes: 44 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 2 additions & 40 deletions helper.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -83,53 +78,20 @@ 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)
}
return resp, err
}

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)
}
Expand Down
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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`",
Expand Down
13 changes: 10 additions & 3 deletions pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.")
Expand Down
21 changes: 21 additions & 0 deletions summary.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 14 additions & 7 deletions workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.")
Expand Down

0 comments on commit 9f8a197

Please sign in to comment.