-
Notifications
You must be signed in to change notification settings - Fork 7
/
org.go
140 lines (120 loc) · 3.66 KB
/
org.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
136
137
138
139
140
package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"strconv"
)
func createOrg(*cli.Context) error {
promptConfirm := PromptEnvDetails()
if len(migrationReq.OrgName) == 0 {
promptConfirm = true
migrationReq.OrgName = TextInput("Name of the Org - ")
}
if len(migrationReq.OrgIdentifier) == 0 {
promptConfirm = true
migrationReq.OrgIdentifier = TextInput("Identifier for the Org - ")
}
log.WithFields(log.Fields{
"Account": migrationReq.Account,
"OrgIdentifier": migrationReq.OrgIdentifier,
"OrgName": migrationReq.OrgName,
}).Info("Org creation details")
if promptConfirm {
confirm := ConfirmInput("Do you want to proceed with org creation?")
if !confirm {
log.Fatal("Aborting...")
}
}
url := fmt.Sprintf("%s/api/organizations?accountIdentifier=%s", GetBaseUrl(migrationReq.Environment, NextGenService), migrationReq.Account)
log.Info("Creating the org....")
_, err := Post(url, migrationReq.Auth, OrgBody{
Org: OrgDetails{
Identifier: migrationReq.OrgIdentifier,
Name: migrationReq.OrgName,
Description: "",
}})
if err == nil {
log.Info("Created the org!")
} else {
log.Error("There was an error creating the org")
return err
}
return nil
}
func bulkRemoveOrg(*cli.Context) error {
promptConfirm := PromptEnvDetails()
names := Split(migrationReq.Names, ",")
identifiers := Split(migrationReq.Identifiers, ",")
if len(names) == 0 && len(identifiers) == 0 {
log.Fatal("No names or identifiers for the organisations provided. Aborting")
}
if len(names) > 0 && len(identifiers) > 0 {
log.Fatal("Both names and identifiers for the organisations 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) + " organisations?")
if !confirm {
log.Fatal("Aborting...")
}
}
if len(names) > 0 {
organisations := getOrganisations()
for _, name := range names {
id := findOrgIdByName(organisations, name)
if len(id) > 0 {
identifiers = append(identifiers, id)
}
}
log.Debugf("Valid identifiers for the given names are - %s", identifiers)
}
for _, identifier := range identifiers {
deleteOrg(identifier)
}
log.Info("Finished operation for all given organisations")
return nil
}
func deleteOrg(orgId string) {
url := fmt.Sprintf("%s/api/organizations/%s?accountIdentifier=%s", GetBaseUrl(migrationReq.Environment, NextGenService), orgId, migrationReq.Account)
log.Infof("Deleting the org with identifier %s", orgId)
_, err := Delete(url, migrationReq.Auth, nil)
if err == nil {
log.Infof("Successfully deleted the org - %s", orgId)
} else {
log.Errorf("Failed to delete the org - %s", orgId)
}
}
func getOrganisations() []OrgDetails {
url := fmt.Sprintf("%s/api/aggregate/organizations?accountIdentifier=%s&pageSize=1000", GetBaseUrl(migrationReq.Environment, NextGenService), migrationReq.Account)
resp, err := Get(url, migrationReq.Auth)
if err != nil || resp.Status != "SUCCESS" {
log.Fatal("Failed to fetch organisations", err)
}
byteData, err := json.Marshal(resp.Data)
if err != nil {
log.Fatal("Failed to fetch organisations", err)
}
var orgListBody OrgListBody
err = json.Unmarshal(byteData, &orgListBody)
if err != nil {
log.Fatal("Failed to fetch organisations", err)
}
var details []OrgDetails
for _, o := range orgListBody.Organisations {
details = append(details, o.Org.Org)
}
return details
}
func findOrgIdByName(organisations []OrgDetails, orgName string) string {
for _, o := range organisations {
if o.Name == orgName {
return o.Identifier
}
}
return ""
}