-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemplates.go
135 lines (120 loc) · 4.07 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"strconv"
)
func BulkRemoveTemplates(*cli.Context) error {
promptConfirm := PromptEnvDetails()
names := Split(migrationReq.Names, ",")
identifiers := Split(migrationReq.Identifiers, ",")
if migrationReq.All {
identifiers = []string{}
templates := getTemplates(migrationReq.OrgIdentifier, migrationReq.ProjectIdentifier, []string{})
for _, template := range templates {
identifiers = append(identifiers, template.Identifier)
}
}
if len(names) == 0 && len(identifiers) == 0 {
log.Fatal("No names or identifiers for the templates provided. Aborting")
}
if len(names) > 0 && len(identifiers) > 0 {
log.Fatal("Both names and identifiers for the templates provided. Aborting")
}
n := len(identifiers)
if len(names) > 0 {
n = len(names)
}
if promptConfirm {
confirm := ConfirmInput("Are you sure you want to proceed with deletion of " + strconv.Itoa(n) + " templates?")
if !confirm {
log.Fatal("Aborting...")
}
}
if len(names) > 0 {
templates := getTemplates(migrationReq.OrgIdentifier, migrationReq.ProjectIdentifier, []string{})
for _, name := range names {
id := findTemplateIdByName(templates, name)
if len(id) > 0 {
identifiers = append(identifiers, id)
}
}
log.Debugf("Valid identifiers for the given names are - %s", identifiers)
}
for _, identifier := range identifiers {
deleteTemplate(migrationReq.OrgIdentifier, migrationReq.ProjectIdentifier, identifier, migrationReq.Force)
}
log.Info("Finished operation for all given templates")
return nil
}
func deleteTemplate(orgId string, projectId string, templateId string, force bool) {
templates := getTemplates(orgId, projectId, []string{templateId})
var versions []string
for _, template := range templates {
versions = append(versions, template.VersionLabel)
}
queryParams := map[string]string{
AccountIdentifier: migrationReq.Account,
"forceDelete": strconv.FormatBool(force),
}
if len(orgId) > 0 {
queryParams[OrgIdentifier] = orgId
}
if len(projectId) > 0 {
queryParams[ProjectIdentifier] = projectId
}
url := GetUrlWithQueryParams(migrationReq.Environment, TemplateService, fmt.Sprintf("api/templates/%s", templateId), queryParams)
log.Infof("Deleting the template with identifier %s", templateId)
_, err := Delete(url, migrationReq.Auth, TemplateDeleteBody{TemplateVersionLabels: versions})
if err == nil {
log.Infof("Successfully deleted the template - %s", templateId)
} else {
log.Errorf("Failed to delete the template - %s", templateId)
}
}
func getTemplates(orgId string, projectId string, templateIdentifiers []string) []TemplateDetails {
queryParams := map[string]string{
AccountIdentifier: migrationReq.Account,
"size": "1000",
"templateListType": "LastUpdated",
}
if len(orgId) > 0 {
queryParams[OrgIdentifier] = orgId
}
if len(projectId) > 0 {
queryParams[ProjectIdentifier] = projectId
}
url := GetUrlWithQueryParams(migrationReq.Environment, TemplateService, "api/templates/list-metadata", queryParams)
resp, err := Post(url, migrationReq.Auth, FilterRequestBody{FilterType: TemplateService, TemplateIdentifiers: templateIdentifiers})
if err != nil || resp.Status != "SUCCESS" {
log.Fatal("Failed to fetch templates", err)
}
byteData, err := json.Marshal(resp.Data)
if err != nil {
log.Fatal("Failed to fetch templates", err)
}
var templateListBody TemplateListBody
err = json.Unmarshal(byteData, &templateListBody)
if err != nil {
log.Fatal("Failed to fetch templates", err)
}
return templateListBody.Templates
}
func findTemplateIdByName(templates []TemplateDetails, templateName string) string {
for _, o := range templates {
if o.Name == templateName {
return o.Identifier
}
}
return ""
}
func MigrateTemplates(*cli.Context) (err error) {
promptConfirm := PromptDefaultInputs()
err = MigrateEntities(promptConfirm, []string{migrationReq.TemplateScope, migrationReq.SecretScope, migrationReq.ConnectorScope}, "templates", Template)
if err != nil {
log.Fatal("Failed to migrate templates")
}
return
}