Skip to content

Commit

Permalink
Support deletion of Templates & Pipelines (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
puthrayaharness authored May 2, 2023
1 parent d4ef147 commit 0448819
Show file tree
Hide file tree
Showing 13 changed files with 504 additions and 50 deletions.
31 changes: 28 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func Post(reqUrl string, auth string, body interface{}) (respBodyObj ResponseBod
postBody, _ := json.Marshal(body)
requestBody := bytes.NewBuffer(postBody)
log.WithFields(log.Fields{
"url": reqUrl,
"body": string(postBody),
}).Debug("The request body")
}).Debug("The request details")
req, err := http.NewRequest("POST", reqUrl, requestBody)
if err != nil {
return
Expand All @@ -31,18 +32,42 @@ func Get(reqUrl string, auth string) (respBodyObj ResponseBody, err error) {
if err != nil {
return
}
log.WithFields(log.Fields{
"url": reqUrl,
}).Debug("The request details")
req.Header.Set("Content-Type", "application/json")
req.Header.Set(AuthHeaderKey(auth), auth)
return handleResp(req)
}

func Delete(reqUrl string, auth string) (respBodyObj ResponseBody, err error) {
req, err := http.NewRequest("DELETE", reqUrl, nil)
func Delete(reqUrl string, auth string, body interface{}) (respBodyObj ResponseBody, err error) {
var requestBody *bytes.Buffer
if body != nil {
postBody, _ := json.Marshal(body)
requestBody = bytes.NewBuffer(postBody)
log.WithFields(log.Fields{
"url": reqUrl,
"body": string(postBody),
}).Debug("The request details")
} else {
log.WithFields(log.Fields{
"url": reqUrl,
}).Debug("The request details")
}
var req *http.Request
if requestBody != nil {
req, err = http.NewRequest("DELETE", reqUrl, requestBody)
} else {
req, err = http.NewRequest("DELETE", reqUrl, nil)
}
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set(AuthHeaderKey(auth), auth)
if requestBody == nil {
req.ContentLength = 0
}
return handleResp(req)
}

Expand Down
6 changes: 6 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ const (
)

var scopes = []string{"project", "org", "account"}

const (
AccountIdentifier = "accountIdentifier"
OrgIdentifier = "orgIdentifier"
ProjectIdentifier = "projectIdentifier"
)
89 changes: 89 additions & 0 deletions docs/docs/advanced/utility-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,93 @@ harness-upgrade --api-key SAT_API_KEY \
--env ENV \
--org ORG \
project --identifiers identifier1,identifier2 rm
```

## Pipelines Management

### Remove pipelines
To remove pipelines, you can provide their names or identifiers.

To remove pipelines by name use `--names`
```shell
harness-upgrade --api-key SAT_API_KEY \
--account ACCOUNT_ID \
--org ORG_ID \
--project PROJECT_ID \
--env ENV \
pipelines --names name1,name2 rm
```

To remove pipelines by identifier use `--identifiers`
```shell
harness-upgrade --api-key SAT_API_KEY \
--account ACCOUNT_ID \
--org ORG_ID \
--project PROJECT_ID \
--env ENV \
pipelines --identifiers identifier1,identifier2 rm
```

To remove all pipelines
```shell
harness-upgrade --api-key SAT_API_KEY \
--account ACCOUNT_ID \
--org ORG_ID \
--project PROJECT_ID \
--env ENV \
pipelines --all rm
```

## Templates Management

### Remove templates
To remove templates, you can provide their names or identifiers.

To remove templates by name use `--names`
```shell
harness-upgrade --api-key SAT_API_KEY \
--account ACCOUNT_ID \
--org ORG_ID \
--project PROJECT_ID \
--env ENV \
templates --names name1,name2 rm
```

To remove templates by identifier use `--identifiers`
```shell
harness-upgrade --api-key SAT_API_KEY \
--account ACCOUNT_ID \
--org ORG_ID \
--project PROJECT_ID \
--env ENV \
templates --identifiers identifier1,identifier2 rm
```

To remove all templates
```shell
harness-upgrade --api-key SAT_API_KEY \
--account ACCOUNT_ID \
--org ORG_ID \
--project PROJECT_ID \
--env ENV \
templates --all rm
```

If the templates are being referenced, the deletion may fail. Use the `--force` flag to force delete the templates:

```shell
harness-upgrade --api-key SAT_API_KEY \
--account ACCOUNT_ID \
--org ORG_ID \
--project PROJECT_ID \
--env ENV \
templates --all --force rm
```

:::info
The above commands are for removing project level templates.

If you want to remove org level templates, do not pass the --project flag.

If you want to remove global level templates, do not pass the --project and --org flags.
:::
4 changes: 2 additions & 2 deletions docs/docs/upgrade/migrate-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ harness-upgrade --api-key SAT_API_KEY \
--template-scope SCOPE \
--workflow-scope SCOPE \
--env ENV \
workflows --all
pipelines --all import
```

### Migrating specific pipelines
Expand All @@ -126,7 +126,7 @@ harness-upgrade --api-key SAT_API_KEY \
--template-scope SCOPE \
--workflow-scope SCOPE \
--env ENV \
pipelines --pipelines PIPELINE_IDS
pipelines --pipelines PIPELINE_IDS import
```

## Migrating triggers
Expand Down
8 changes: 4 additions & 4 deletions entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func CreateEntities(body RequestBody) {
}

func QueueCreateEntity(body RequestBody) (reqId string, err error) {
url := GetUrl(migrationReq.Environment, MIGRATOR, "save/async", migrationReq.Account)
url := GetUrl(migrationReq.Environment, MigratorService, "save/async", migrationReq.Account)
resp, err := Post(url, migrationReq.Auth, body)
if err != nil {
log.Fatal("Failed to create the entities", err)
Expand All @@ -41,9 +41,9 @@ func PollForCompletion(reqId string) {
s.Start()
for {
time.Sleep(time.Second)
url := GetUrlWithQueryParams(migrationReq.Environment, MIGRATOR, "save/async-result", map[string]string{
"accountIdentifier": migrationReq.Account,
"requestId": reqId,
url := GetUrlWithQueryParams(migrationReq.Environment, MigratorService, "save/async-result", map[string]string{
AccountIdentifier: migrationReq.Account,
"requestId": reqId,
})
resp, err := Get(url, migrationReq.Auth)
if err != nil {
Expand Down
50 changes: 32 additions & 18 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,36 @@ const (
)

const (
MIGRATOR string = "Migrator"
NG = "NextGen"
MigratorService string = "Migrator"
NextGenService = "NextGen"
TemplateService = "Template"
PipelineService = "Pipeline"
)

var urlMap = map[string]map[string]string{
Prod: {
MIGRATOR: "https://app.harness.io/gateway/ng-migration",
NG: "https://app.harness.io/gateway/ng",
PipelineService: "https://app.harness.io/gateway/pipeline",
TemplateService: "https://app.harness.io/gateway/template",
MigratorService: "https://app.harness.io/gateway/ng-migration/api/ng-migration",
NextGenService: "https://app.harness.io/gateway/ng",
},
QA: {
MIGRATOR: "https://qa.harness.io/gateway/ng-migration",
NG: "https://qa.harness.io/gateway/ng",
PipelineService: "https://qa.harness.io/gateway/pipeline",
TemplateService: "https://qa.harness.io/gateway/template",
MigratorService: "https://qa.harness.io/gateway/ng-migration/api/ng-migration",
NextGenService: "https://qa.harness.io/gateway/ng",
},
Dev: {
MIGRATOR: "https://localhost:9080",
NG: "https://localhost:8181/ng",
PipelineService: "https://localhost:8181/pipeline",
TemplateService: "https://localhost:8181/template",
MigratorService: "https://localhost:9080/api/ng-migration",
NextGenService: "https://localhost:8181/ng",
},
Prod3: {
MIGRATOR: "https://app3.harness.io/gateway/ng-migration",
NG: "https://app3.harness.io/gateway/ng",
PipelineService: "https://app3.harness.io/gateway/pipeline",
TemplateService: "https://app3.harness.io/gateway/template",
MigratorService: "https://app3.harness.io/gateway/ng-migration/api/ng-migration",
NextGenService: "https://app3.harness.io/gateway/ng",
},
}

Expand Down Expand Up @@ -87,11 +97,11 @@ func GetUrlWithQueryParams(environment string, service string, endpoint string,
params = params + k + "=" + v + "&"
}

return fmt.Sprintf("%s/api/ng-migration/%s?%s", GetBaseUrl(environment, service), endpoint, params)
return fmt.Sprintf("%s/%s?%s", GetBaseUrl(environment, service), endpoint, params)
}

func GetUrl(environment string, service string, path string, accountId string) string {
return fmt.Sprintf("%s/api/ng-migration/%s?accountIdentifier=%s", GetBaseUrl(environment, service), path, accountId)
return fmt.Sprintf("%s/%s?accountIdentifier=%s", GetBaseUrl(environment, service), path, accountId)
}

func getOrDefault(value string, defaultValue string) string {
Expand Down Expand Up @@ -213,9 +223,9 @@ func Split(str string, sep string) (result []string) {
}

func listEntities(entity string) (data []BaseEntityDetail, err error) {
url := GetUrlWithQueryParams(migrationReq.Environment, MIGRATOR, entity, map[string]string{
"accountIdentifier": migrationReq.Account,
"appId": migrationReq.AppId,
url := GetUrlWithQueryParams(migrationReq.Environment, MigratorService, entity, map[string]string{
AccountIdentifier: migrationReq.Account,
"appId": migrationReq.AppId,
})
resp, err := Get(url, migrationReq.Auth)
if err != nil {
Expand Down Expand Up @@ -246,10 +256,14 @@ func GetBaseUrl(environment string, service string) string {
}
var url string
switch service {
case NG:
case PipelineService:
url = migrationReq.BaseUrl + "/pipeline"
case TemplateService:
url = migrationReq.BaseUrl + "/template"
case NextGenService:
url = migrationReq.BaseUrl + "/ng"
case MIGRATOR:
url = migrationReq.BaseUrl + "/ng-migration"
case MigratorService:
url = migrationReq.BaseUrl + "/ng-migration/api/ng-migration"
default:
panic("Unknown service! Please contact Harness support")
}
Expand Down
67 changes: 63 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var migrationReq = struct {
TargetAccount string `survey:"targetAccount"`
TargetAuthToken string `survey:"targetAuth"`
BaseUrl string `survey:"baseUrl"`
Force bool `survey:"force"`
}{}

func getReqBody(entityType EntityType, filter Filter) RequestBody {
Expand Down Expand Up @@ -397,17 +398,40 @@ func main() {
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Usage: "if all pipelines in the app need to be migrated",
Usage: "all pipelines",
Destination: &migrationReq.All,
},
altsrc.NewStringFlag(&cli.StringFlag{
Name: "pipelines",
Usage: "pipelines as comma separated values `pipeline1,pipeline2`",
Usage: "first gen pipeline ids as comma separated values `pipeline1,pipeline2`",
Destination: &migrationReq.PipelineIds,
}),
&cli.StringFlag{
Name: "identifiers",
Usage: "`IDENTIFIERS` of the next gen pipelines",
Destination: &migrationReq.Identifiers,
},
&cli.StringFlag{
Name: "names",
Usage: "`NAMES` of the next gen pipeline",
Destination: &migrationReq.Names,
},
},
Action: func(context *cli.Context) error {
return cliWrapper(migratePipelines, context)
Subcommands: []*cli.Command{
{
Name: "rm",
Usage: "Remove nextgen pipelines",
Action: func(context *cli.Context) error {
return cliWrapper(BulkRemovePipelines, context)
},
},
{
Name: "import",
Usage: "import first gen pipelines to next gen",
Action: func(context *cli.Context) error {
return cliWrapper(migratePipelines, context)
},
},
},
},
{
Expand Down Expand Up @@ -565,6 +589,41 @@ func main() {
},
},
},
{
Name: "templates",
Usage: "Template specific commands.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "identifiers",
Usage: "`IDENTIFIERS` of the template",
Destination: &migrationReq.Identifiers,
},
&cli.StringFlag{
Name: "names",
Usage: "`NAMES` of the template",
Destination: &migrationReq.Names,
},
&cli.BoolFlag{
Name: "force",
Usage: "to force delete template",
Destination: &migrationReq.Force,
},
&cli.BoolFlag{
Name: "all",
Usage: "if set will delete all templates",
Destination: &migrationReq.All,
},
},
Subcommands: []*cli.Command{
{
Name: "rm",
Usage: "Remove templates",
Action: func(context *cli.Context) error {
return cliWrapper(BulkRemoveTemplates, context)
},
},
},
},
},
Before: altsrc.InitInputSourceWithContext(globalFlags, altsrc.NewYamlSourceFromFlagFunc("load")),
Flags: globalFlags,
Expand Down
Loading

0 comments on commit 0448819

Please sign in to comment.