-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinfrastructure.go
141 lines (122 loc) · 5.5 KB
/
infrastructure.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
package main
import (
"fmt"
"harness/client"
"harness/defaults"
"harness/shared"
"harness/telemetry"
. "harness/types"
. "harness/utils"
"strings"
log "github.com/sirupsen/logrus"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
var cloudProjectName = ""
var cloudRegionName = ""
var cloudInstanceName = ""
// create or update Infra Definition
func applyInfraDefinition(c *cli.Context) error {
filePath := c.String("file")
baseURL := GetNGBaseURL(c)
if filePath == "" {
fmt.Println("Please enter valid filename")
return nil
}
cloudProjectName = c.String("cloud-project")
cloudRegionName = c.String("cloud-region")
cloudInstanceName = c.String("instance-name")
orgIdentifier := c.String("org-id")
projectIdentifier := c.String("project-id")
fmt.Println("Trying to create or update infrastructure using the given yaml=",
GetColoredText(filePath, color.FgCyan))
createOrUpdateInfraURL := GetUrlWithQueryParams("", baseURL, defaults.INFRA_ENDPOINT, map[string]string{
"accountIdentifier": shared.CliCdRequestData.Account,
})
var content, _ = ReadFromFile(c.String("file"))
content = updateInfraYamlContent(content)
if projectIdentifier != "" {
content = ReplacePlaceholderValues(content, defaults.DEFAULT_PROJECT, projectIdentifier)
}
if orgIdentifier != "" {
content = ReplacePlaceholderValues(content, defaults.DEFAULT_ORG, orgIdentifier)
}
requestBody := GetJsonFromYaml(content)
if requestBody == nil {
println(GetColoredText("Please enter a valid yaml and try again...", color.FgRed))
}
identifier := ValueToString(GetNestedValue(requestBody, "infrastructureDefinition", "identifier").(string))
name := ValueToString(GetNestedValue(requestBody, "infrastructureDefinition", "name").(string))
// Check if the project and org values are provided by the user otherwise default them
if projectIdentifier == "" {
projectIdentifier = ValueToString(GetNestedValue(requestBody, "infrastructureDefinition", "projectIdentifier").(string))
}
if orgIdentifier == "" {
orgIdentifier = ValueToString(GetNestedValue(requestBody, "infrastructureDefinition", "orgIdentifier").(string))
}
environmentRef := ValueToString(GetNestedValue(requestBody, "infrastructureDefinition", "environmentRef").(string))
//setup payload for Infra create / update
InfraPayload := HarnessInfra{Identifier: identifier, Name: name, ProjectIdentifier: projectIdentifier, OrgIdentifier: orgIdentifier, Yaml: content}
entityExists := GetEntity(baseURL, fmt.Sprintf("infrastructures/%s", identifier),
projectIdentifier, orgIdentifier, map[string]string{
"environmentIdentifier": environmentRef,
})
var err error
if !entityExists {
println("Creating infrastructure with id: ", GetColoredText(identifier, color.FgGreen))
_, err = client.Post(createOrUpdateInfraURL, shared.CliCdRequestData.AuthToken, InfraPayload, defaults.CONTENT_TYPE_JSON, nil)
if err == nil {
println(GetColoredText("Infrastructure Definition created successfully!", color.FgGreen))
telemetry.Track(telemetry.TrackEventInfoPayload{EventName: telemetry.INFRA_CREATED, UserId: shared.CliCdRequestData.UserId}, map[string]interface{}{
"accountId": shared.CliCdRequestData.Account,
"type": GetTypeFromYAML(content),
"userId": shared.CliCdRequestData.UserId,
})
return nil
}
} else {
println("Found infrastructure with id=", GetColoredText(identifier, color.FgCyan))
println("Updating details of infrastructure with id=", GetColoredText(identifier, color.FgBlue))
_, err = client.Put(createOrUpdateInfraURL, shared.CliCdRequestData.AuthToken, InfraPayload, defaults.CONTENT_TYPE_JSON, nil)
if err == nil {
println(GetColoredText("Successfully updated infrastructure definition with id= ", color.FgGreen) +
GetColoredText(identifier, color.FgBlue))
telemetry.Track(telemetry.TrackEventInfoPayload{EventName: telemetry.INFRA_UPDATED, UserId: shared.CliCdRequestData.UserId}, map[string]interface{}{
"accountId": shared.CliCdRequestData.Account,
"type": GetTypeFromYAML(content),
"userId": shared.CliCdRequestData.UserId,
})
return nil
}
}
return nil
}
func updateInfraYamlContent(content string) string {
hasRegionName := strings.Contains(content, defaults.REGION_NAME_PLACEHOLDER)
hasProjectName := strings.Contains(content, defaults.PROJECT_NAME_PLACEHOLDER)
hasInstanceName := strings.Contains(content, defaults.INSTANCE_NAME_PLACEHOLDER)
if hasProjectName && (cloudProjectName == "" || cloudProjectName == defaults.PROJECT_NAME_PLACEHOLDER) {
cloudProjectName = TextInput("Enter a valid project name:")
}
if hasInstanceName && (cloudInstanceName == "" || cloudInstanceName == defaults.INSTANCE_NAME_PLACEHOLDER) {
cloudInstanceName = TextInput("Enter a valid instance name:")
}
if hasRegionName && (cloudRegionName == "" || cloudRegionName == defaults.REGION_NAME_PLACEHOLDER) {
cloudRegionName = TextInput("Enter a valid region name:")
}
log.Info("Got your project and region info, let's create the infra now...")
content = ReplacePlaceholderValues(content, defaults.PROJECT_NAME_PLACEHOLDER, cloudProjectName)
content = ReplacePlaceholderValues(content, defaults.REGION_NAME_PLACEHOLDER, cloudRegionName)
content = ReplacePlaceholderValues(content, defaults.INSTANCE_NAME_PLACEHOLDER, cloudInstanceName)
return content
}
// Delete an existing Infra Definition
func deleteInfraDefinition(*cli.Context) error {
fmt.Println(defaults.NOT_IMPLEMENTED)
return nil
}
// Delete an existing Infra Definition
func listInfraDefinition(*cli.Context) error {
fmt.Println(defaults.NOT_IMPLEMENTED)
return nil
}