forked from harness/harness-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitops-cluster.go
104 lines (96 loc) · 4.12 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
package main
import (
"fmt"
"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")
baseURL := getBaseUrl(c, 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 == GITOPS_AGENT_IDENTIFIER_PLACEHOLDER {
agentIdentifier = TextInput("Enter a valid AgentIdentifier:")
}
content = replacePlaceholderValues(content, 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))
projectIdentifier := valueToString(GetNestedValue(requestBody, "gitops", "projectIdentifier").(string))
orgIdentifier := valueToString(GetNestedValue(requestBody, "gitops", "orgIdentifier").(string))
createOrUpdateClusterURL := GetUrlWithQueryParams("", baseURL, GITOPS_CLUSTER_ENDPOINT, map[string]string{
"identifier": identifier,
"accountIdentifier": cliCdRequestData.Account,
"orgIdentifier": orgIdentifier,
"projectIdentifier": projectIdentifier,
})
extraParams := map[string]string{
"query.name": valueToString(GetNestedValue(requestBody, "gitops", "name").(string)),
}
entityExists := getEntity(baseURL, fmt.Sprintf(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 = Post(createOrUpdateClusterURL, cliCdRequestData.AuthToken, clusterPayload, CONTENT_TYPE_JSON, nil)
if err == nil {
println(getColoredText("Successfully created cluster with id= ", color.FgGreen) +
getColoredText(identifier, color.FgBlue))
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", GITOPS_CLUSTER_ENDPOINT, identifier), map[string]string{
"accountIdentifier": cliCdRequestData.Account,
"orgIdentifier": orgIdentifier,
"projectIdentifier": projectIdentifier,
})
newClusterPayload := createClusterPUTPayload(requestBody)
_, err = Put(clusterPUTUrl, cliCdRequestData.AuthToken, newClusterPayload, CONTENT_TYPE_JSON, nil)
if err == nil {
println(getColoredText("Successfully updated GitOps Cluster with id= ", color.FgGreen) +
getColoredText(identifier, color.FgBlue))
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 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
}