/
cac_environment.go
89 lines (70 loc) · 2.19 KB
/
cac_environment.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
package cd
import (
"errors"
"fmt"
"github.com/harness/harness-go-sdk/harness/cd/cac"
"github.com/harness/harness-go-sdk/harness/utils"
)
func (c *ConfigAsCodeClient) UpsertEnvironment(input *cac.Environment) (*cac.Environment, error) {
if input == nil {
return nil, errors.New("cannot create environment. input is nil")
}
if ok, err := input.Validate(); !ok {
return nil, err
}
app, err := c.ApiClient.ApplicationClient.GetApplicationById(input.ApplicationId)
if err != nil {
return nil, fmt.Errorf("could not find application '%s'", app.Id)
}
output := &cac.Environment{}
filePath := cac.GetEnvironmentYamlPath(app.Name, input.Name)
err = c.ApiClient.ConfigAsCodeClient.UpsertObject(input, filePath, output)
if err != nil {
return nil, err
}
return output, nil
}
func (c *ConfigAsCodeClient) GetEnvironmentByName(applicationId string, environmentName string) (*cac.Environment, error) {
if ok, err := utils.CheckRequiredParameters(applicationId, ""); !ok {
return nil, err
}
if ok, err := utils.CheckRequiredParameters(environmentName, ""); !ok {
return nil, err
}
app, err := c.ApiClient.ApplicationClient.GetApplicationById(applicationId)
if err != nil {
return nil, err
} else if app == nil {
return nil, fmt.Errorf("could not find application '%s'", applicationId)
}
output := &cac.Environment{}
yamlPath := cac.GetEnvironmentYamlPath(app.Name, environmentName)
err = c.FindObjectByPath(applicationId, yamlPath, output)
if err != nil {
return nil, err
}
if output.IsEmpty() {
return nil, nil
}
return output, nil
}
func (c *ConfigAsCodeClient) GetEnvironmentById(applicationId string, environmentId string) (*cac.Environment, error) {
if ok, err := utils.CheckRequiredParameters(applicationId, ""); !ok {
return nil, err
}
if ok, err := utils.CheckRequiredParameters(environmentId, ""); !ok {
return nil, err
}
env := &cac.Environment{}
err := c.FindObjectById(applicationId, environmentId, env)
if err != nil {
return nil, err
}
if env.IsEmpty() {
return nil, nil
}
return env, nil
}
func (c *ConfigAsCodeClient) DeleteEnvironment(appName string, envName string) error {
return c.DeleteEntity(cac.GetEnvironmentYamlPath(appName, envName))
}