-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgitops-cluster.go
189 lines (172 loc) · 7.39 KB
/
gitops-cluster.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"fmt"
"harness/client"
"harness/defaults"
"harness/shared"
"harness/telemetry"
. "harness/types"
. "harness/utils"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
// create or update a Gitops Cluster
func applyCluster(c *cli.Context) error {
filePath := c.String("file")
orgIdentifier := c.String("org-id")
projectIdentifier := c.String("project-id")
baseURL := GetBaseUrl(c, defaults.GITOPS_BASE_URL)
if filePath == "" {
fmt.Println("Please enter valid filename")
return nil
}
fmt.Println("Trying to create or update cluster using the yaml=",
GetColoredText(filePath, color.FgCyan))
var content, _ = ReadFromFile(c.String("file"))
agentIdentifier = c.String("agent-identifier")
if agentIdentifier == "" || agentIdentifier == defaults.GITOPS_AGENT_IDENTIFIER_PLACEHOLDER {
agentIdentifier = TextInput("Enter a valid AgentIdentifier:")
}
content = ReplacePlaceholderValues(content, defaults.GITOPS_AGENT_IDENTIFIER_PLACEHOLDER, agentIdentifier)
baseURL = baseURL + agentIdentifier
requestBody := GetJsonFromYaml(content)
if requestBody == nil {
println(GetColoredText("Please enter valid cluster yaml file", color.FgRed))
}
identifier := ValueToString(GetNestedValue(requestBody, "gitops", "identifier").(string))
if projectIdentifier == "" {
projectIdentifier = ValueToString(GetNestedValue(requestBody, "gitops", "projectIdentifier").(string))
}
if orgIdentifier == "" {
orgIdentifier = ValueToString(GetNestedValue(requestBody, "gitops", "orgIdentifier").(string))
}
createOrUpdateClusterURL := GetUrlWithQueryParams("", baseURL, defaults.GITOPS_CLUSTER_ENDPOINT, map[string]string{
"identifier": identifier,
"accountIdentifier": shared.CliCdRequestData.Account,
"orgIdentifier": orgIdentifier,
"projectIdentifier": projectIdentifier,
})
extraParams := map[string]string{
"query.name": ValueToString(GetNestedValue(requestBody, "gitops", "name").(string)),
}
entityExists := GetEntity(baseURL, fmt.Sprintf(defaults.GITOPS_CLUSTER_ENDPOINT+"/%s", identifier),
projectIdentifier, orgIdentifier, extraParams)
var _ ResponseBody
var err error
if !entityExists {
println("Creating cluster with id: ", GetColoredText(identifier, color.FgGreen))
clusterPayload := createClusterPayload(requestBody)
_, err = client.Post(createOrUpdateClusterURL, shared.CliCdRequestData.AuthToken, clusterPayload, defaults.CONTENT_TYPE_JSON, nil)
if err == nil {
println(GetColoredText("Successfully created cluster with id= ", color.FgGreen) +
GetColoredText(identifier, color.FgBlue))
telemetry.Track(telemetry.TrackEventInfoPayload{EventName: telemetry.CLUSTER_CREATED, UserId: shared.CliCdRequestData.UserId}, map[string]interface{}{
"accountId": shared.CliCdRequestData.Account,
"type": GetTypeFromYAML(content),
"userId": shared.CliCdRequestData.UserId,
})
return nil
}
} else {
println("Found GitOps Cluster with id=", GetColoredText(identifier, color.FgCyan))
println("Updating details of GitOps Cluster with id=", GetColoredText(identifier, color.FgBlue))
var clusterPUTUrl = GetUrlWithQueryParams("", baseURL,
fmt.Sprintf("%s/%s", defaults.GITOPS_CLUSTER_ENDPOINT, identifier), map[string]string{
"accountIdentifier": shared.CliCdRequestData.Account,
"orgIdentifier": orgIdentifier,
"projectIdentifier": projectIdentifier,
"agentIdentifier": agentIdentifier,
})
newClusterPayload := createClusterPUTPayload(requestBody)
_, err = client.Put(clusterPUTUrl, shared.CliCdRequestData.AuthToken, newClusterPayload, defaults.CONTENT_TYPE_JSON, nil)
if err == nil {
println(GetColoredText("Successfully updated GitOps Cluster with id= ", color.FgGreen) +
GetColoredText(identifier, color.FgBlue))
telemetry.Track(telemetry.TrackEventInfoPayload{EventName: telemetry.CLUSTER_UPDATED, UserId: shared.CliCdRequestData.UserId}, map[string]interface{}{
"accountId": shared.CliCdRequestData.Account,
"type": GetTypeFromYAML(content),
"userId": shared.CliCdRequestData.UserId,
"agentIdentifier": agentIdentifier,
})
return nil
}
}
return nil
}
func createClusterPayload(requestBody map[string]interface{}) GitOpsCluster {
newCluster := GitOpsCluster{
Cluster: Cluster{
Name: ValueToString(GetNestedValue(requestBody, "gitops", "name").(string)),
Server: ValueToString(GetNestedValue(requestBody, "gitops", "cluster", "server").(string)),
Config: Config{
ClusterConnectionType: ValueToString(GetNestedValue(requestBody, "gitops", "cluster", "config", "clusterConnectionType").(string)),
},
},
}
return newCluster
}
func createClusterEnvPayload(clusterId string, orgId string,
projId string, envId string) ClusterEnvLink {
return ClusterEnvLink{Identifier: clusterId,
AgentIdentifier: agentIdentifier,
OrgIdentifier: orgId,
ProjectIdentifier: projId,
EnvRef: envId,
Scope: "PROJECT",
}
}
func createClusterPUTPayload(requestBody map[string]interface{}) GitOpsClusterWithUpdatedFields {
clusterWithUpdateMask := GitOpsClusterWithUpdatedFields{
Cluster: Cluster{
Name: ValueToString(GetNestedValue(requestBody, "gitops", "name").(string)),
Server: ValueToString(GetNestedValue(requestBody, "gitops", "cluster", "server").(string)),
Config: Config{
ClusterConnectionType: ValueToString(GetNestedValue(requestBody, "gitops", "cluster", "config", "clusterConnectionType").(string)),
},
},
UpdatedFields: []string{"name", "tags", "authType", "credsType"},
}
return clusterWithUpdateMask
}
func linkClusterEnv(c *cli.Context) error {
agentIdentifier = c.String("agent-identifier")
clusterIdentifier := c.String("cluster-id")
envRef := c.String("environment-id")
orgIdentifier := c.String("org-id")
projectIdentifier := c.String("project-id")
fmt.Println("Trying to link a clusterId =", GetColoredText(clusterIdentifier, color.FgBlue),
"with an environmentId =", GetColoredText(envRef, color.FgBlue))
if agentIdentifier == "" {
agentIdentifier = TextInput("Enter a valid AgentIdentifier:")
}
if clusterIdentifier == "" {
clusterIdentifier = TextInput("Enter a valid ClusterIdentifier:")
}
if envRef == "" {
envRef = TextInput("Enter a valid environmentIdentifier:")
}
if projectIdentifier == "" {
projectIdentifier = defaults.DEFAULT_PROJECT
}
if orgIdentifier == "" {
orgIdentifier = defaults.DEFAULT_ORG
}
baseURL := GetNGBaseURL(c) + defaults.GITOPS_ENDPOINT
createOrUpdateClusterURL := GetUrlWithQueryParams("", baseURL, defaults.GITOPS_CLUSTER_ENDPOINT, map[string]string{
"accountIdentifier": shared.CliCdRequestData.Account,
})
var _ ResponseBody
var err error
clusterEnvPayload := createClusterEnvPayload(clusterIdentifier, orgIdentifier, projectIdentifier, envRef)
_, err = client.Post(createOrUpdateClusterURL, shared.CliCdRequestData.AuthToken, clusterEnvPayload, defaults.CONTENT_TYPE_JSON, nil)
if err == nil {
println(GetColoredText("Successfully linked clusterId = ", color.FgGreen)+GetColoredText(clusterIdentifier, color.FgBlue),
GetColoredText("with environmentId = ", color.FgGreen)+GetColoredText(envRef, color.FgBlue))
return nil
} else {
println(GetColoredText("Encountered an issue while trying to link the clusterId = ", color.FgRed)+GetColoredText(clusterIdentifier, color.FgBlue),
GetColoredText("with environmentId = ", color.FgRed)+GetColoredText(envRef, color.FgBlue))
println(GetColoredText("Try again with correct parameters...", color.FgGreen))
}
return nil
}