forked from harness/harness-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitops-application.go
153 lines (143 loc) · 7.08 KB
/
gitops-application.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
package main
import (
"fmt"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
func applyGitopsApplications(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 gitops-application 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 repository yaml file", color.FgRed))
}
orgIdentifier := valueToString(GetNestedValue(requestBody, "gitops", "orgIdentifier").(string))
projectIdentifier := valueToString(GetNestedValue(requestBody, "gitops", "projectIdentifier").(string))
clusterIdentifier := valueToString(GetNestedValue(requestBody, "gitops", "clusterIdentifier").(string))
repoIdentifier := valueToString(GetNestedValue(requestBody, "gitops", "repoIdentifier").(string))
createOrUpdateApplicationURL := GetUrlWithQueryParams("", baseURL, GITOPS_APPLICATION_ENDPOINT, map[string]string{
"accountIdentifier": cliCdRequestData.Account,
"orgIdentifier": orgIdentifier,
"projectIdentifier": projectIdentifier,
"clusterIdentifier": clusterIdentifier,
"repoIdentifier": repoIdentifier,
})
applicationName := valueToString(GetNestedValue(requestBody, "gitops", "name").(string))
syncApplicationURL := GetUrlWithQueryParams("", baseURL,
fmt.Sprintf(GITOPS_APPLICATION_ENDPOINT+"/%s", applicationName+"/sync"), map[string]string{
"routingId": cliCdRequestData.Account,
"accountIdentifier": cliCdRequestData.Account,
"orgIdentifier": orgIdentifier,
"projectIdentifier": projectIdentifier,
})
extraParams := map[string]string{
"agentIdentifier": agentIdentifier,
}
entityExists := getEntity(baseURL, fmt.Sprintf(GITOPS_APPLICATION_ENDPOINT+"/%s", applicationName),
projectIdentifier, orgIdentifier, extraParams)
var _ ResponseBody
var err error
if !entityExists {
println("Creating GitOps-Application with id: ", getColoredText(applicationName, color.FgGreen))
applicationPayload := createGitOpsApplicationPayload(requestBody)
_, err = Post(createOrUpdateApplicationURL, cliCdRequestData.AuthToken, applicationPayload, CONTENT_TYPE_JSON, nil)
if err == nil {
println(getColoredText("Successfully created GitOps-Application with id= ", color.FgGreen) +
getColoredText(applicationName, color.FgBlue))
return nil
}
} else {
println("Found GitOps Application with id=", getColoredText(applicationName, color.FgCyan))
println("Updating details of GitOps Application with id=", getColoredText(applicationName, color.FgBlue))
var appPUTUrl = GetUrlWithQueryParams("", baseURL,
fmt.Sprintf(GITOPS_APPLICATION_ENDPOINT+"/%s", applicationName), map[string]string{
"routingId": cliCdRequestData.Account,
"accountIdentifier": cliCdRequestData.Account,
"orgIdentifier": orgIdentifier,
"projectIdentifier": projectIdentifier,
"repoIdentifier": repoIdentifier,
"clusterIdentifier": clusterIdentifier,
})
newAppPayload := createGitOpsApplicationPUTPayload(requestBody)
syncPayload := createGitOpsApplicationPayload(requestBody)
println("Syncing the GitOps Application before updating the spec:", getColoredText(applicationName, color.FgGreen))
_, err = Post(syncApplicationURL, cliCdRequestData.AuthToken, syncPayload, CONTENT_TYPE_JSON, nil)
if err == nil {
println(getColoredText("Successfully synced GitOps app with id= ", color.FgGreen) +
getColoredText(applicationName, color.FgBlue))
_, err = Put(appPUTUrl, cliCdRequestData.AuthToken, newAppPayload, CONTENT_TYPE_JSON, nil)
if err == nil {
println(getColoredText("Successfully updated GitOps app with id= ", color.FgGreen) +
getColoredText(applicationName, color.FgBlue))
return nil
}
return nil
}
}
return nil
}
func createGitOpsApplicationPayload(requestBody map[string]interface{}) GitOpsApplication {
newApplication := GitOpsApplication{
Application: Application{
Metadata: Metadata{
Labels: Labels{
Envref: valueToString(GetNestedValue(requestBody, "gitops", "application", "metadata", "labels", "harness.io/envRef").(string)),
Serviceref: valueToString(GetNestedValue(requestBody, "gitops", "application", "metadata", "labels", "harness.io/serviceRef").(string)),
},
Name: valueToString(GetNestedValue(requestBody, "gitops", "name").(string)),
Namespace: valueToString(GetNestedValue(requestBody, "gitops", "application", "metadata", "namespace").(string)),
ClusterName: valueToString(GetNestedValue(requestBody, "gitops", "application", "metadata", "clusterName").(string)),
},
Spec: Spec{
Source: Source{
RepoURL: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "source", "repoURL").(string)),
Path: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "source", "path").(string)),
TargetRevision: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "source", "targetRevision").(string)),
},
Destination: Destination{
Server: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "destination", "server").(string)),
Namespace: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "destination", "namespace").(string)),
},
},
},
}
return newApplication
}
func createGitOpsApplicationPUTPayload(requestBody map[string]interface{}) GitOpsApplication {
putApp := GitOpsApplication{
Application{
Metadata: Metadata{
Namespace: valueToString(GetNestedValue(requestBody, "gitops", "application", "metadata", "namespace").(string)),
Labels: Labels{
Serviceref: valueToString(GetNestedValue(requestBody, "gitops", "application", "metadata", "labels", "harness.io/serviceRef").(string)),
Envref: valueToString(GetNestedValue(requestBody, "gitops", "application", "metadata", "labels", "harness.io/envRef").(string)),
},
},
Spec: Spec{
Source: Source{
RepoURL: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "source", "repoURL").(string)),
Path: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "source", "path").(string)),
TargetRevision: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "source", "targetRevision").(string)),
},
Destination: Destination{
Server: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "destination", "server").(string)),
Namespace: valueToString(GetNestedValue(requestBody, "gitops", "application", "spec", "destination", "namespace").(string)),
},
},
},
}
return putApp
}