generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
environment.go
234 lines (202 loc) · 6.34 KB
/
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package environment
import (
"context"
"strings"
"github.com/harness/harness-go-sdk/harness/cd/cac"
"github.com/harness/terraform-provider-harness/internal"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceEnvironment() *schema.Resource {
return &schema.Resource{
Description: "Resource for creating an environment",
CreateContext: resourceEnvironmentCreateOrUpdate,
ReadContext: resourceEnvironmentRead,
UpdateContext: resourceEnvironmentCreateOrUpdate,
DeleteContext: resourceEnvironmentDelete,
Schema: map[string]*schema.Schema{
"id": {
Description: "The id of the environment.",
Type: schema.TypeString,
Computed: true,
},
"app_id": {
Description: "The id of the application.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Description: "The name of the environment.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Description: "The description of the environment.",
Type: schema.TypeString,
Optional: true,
},
"type": {
Description: "The type of the environment. Valid values are `PROD` and `NON_PROD`",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"PROD", "NON_PROD"}, false),
},
"variable_override": {
Description: "Override for a service variable",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Description: "The name of the variable",
Type: schema.TypeString,
Required: true,
},
"service_name": {
Description: "The name of the service",
Type: schema.TypeString,
Optional: true,
},
"value": {
Description: "The value of the service variable",
Type: schema.TypeString,
Required: true,
},
"type": {
Description: "The type of the service variable. Valid values are `TEXT` and `ENCRYPTED_TEXT`",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"TEXT", "ENCRYPTED_TEXT"}, false),
},
},
},
},
},
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// <app_id>/<env_id>
parts := strings.Split(d.Id(), "/")
d.Set("app_id", parts[0])
d.SetId(parts[1])
return []*schema.ResourceData{d}, nil
},
},
}
}
func resourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
var env *cac.Environment
var err error
envId := d.Get("id").(string)
appId := d.Get("app_id").(string)
if env, err = c.ConfigAsCodeClient.GetEnvironmentById(appId, envId); err != nil {
return diag.FromErr(err)
} else if env == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
return readEnvironment(d, env)
}
func readEnvironment(d *schema.ResourceData, env *cac.Environment) diag.Diagnostics {
d.SetId(env.Id)
d.Set("app_id", env.ApplicationId)
d.Set("name", env.Name)
d.Set("app_id", env.ApplicationId)
d.Set("type", env.EnvironmentType)
d.Set("description", env.Description)
if overrides := flattenVariableOverrides(env.VariableOverrides); len(overrides) > 0 {
d.Set("variable_override", overrides)
}
return nil
}
func resourceEnvironmentCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
appId := d.Get("app_id").(string)
id := d.Get("id").(string)
var env *cac.Environment
var err error
if d.IsNewResource() {
env = cac.NewEntity(cac.ObjectTypes.Environment).(*cac.Environment)
} else {
if env, err = c.ConfigAsCodeClient.GetEnvironmentById(appId, id); err != nil {
return diag.FromErr(err)
} else if env == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
}
env.Id = id
env.ApplicationId = appId
env.Name = d.Get("name").(string)
env.EnvironmentType = cac.EnvironmentType(d.Get("type").(string))
env.ApplicationId = d.Get("app_id").(string)
env.Description = d.Get("description").(string)
if overrides := d.Get("variable_override"); overrides != nil {
env.VariableOverrides = expandVariableOverrides(overrides.(*schema.Set).List())
}
newEnv, err := c.ConfigAsCodeClient.UpsertEnvironment(env)
if err != nil {
return diag.FromErr(err)
}
return readEnvironment(d, newEnv)
}
func flattenVariableOverrides(overrides []*cac.VariableOverride) []map[string]interface{} {
if len(overrides) == 0 {
return make([]map[string]interface{}, 0)
}
var results = make([]map[string]interface{}, len(overrides))
for i, override := range overrides {
results[i] = map[string]interface{}{
"name": override.Name,
"service_name": override.ServiceName,
"value": override.Value,
"type": override.ValueType,
}
}
return results
}
func expandVariableOverrides(d []interface{}) []*cac.VariableOverride {
if len(d) == 0 {
return make([]*cac.VariableOverride, 0)
}
overrides := make([]*cac.VariableOverride, len(d))
for i, override := range d {
data := override.(map[string]interface{})
overrides[i] = &cac.VariableOverride{
Name: data["name"].(string),
ServiceName: data["service_name"].(string),
Value: data["value"].(string),
ValueType: cac.VariableValueType(data["type"].(string)),
}
}
return overrides
}
func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
envName := d.Get("name").(string)
appId := d.Get("app_id").(string)
app, err := c.ApplicationClient.GetApplicationById(appId)
if err != nil {
return diag.FromErr(err)
}
err = c.ConfigAsCodeClient.DeleteEnvironment(app.Name, envName)
if err != nil {
return diag.FromErr(err)
}
return nil
}