generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 44
/
resource_manual_freeze.go
311 lines (279 loc) · 8.88 KB
/
resource_manual_freeze.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package manual_freeze
import (
"context"
"net/http"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceManualFreeze() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for Manual Deployment Freeze Window.",
ReadContext: resourceManualFreezeRead,
UpdateContext: resourceManualFreezeCreateOrUpdate,
DeleteContext: resourceManualFreezeDelete,
CreateContext: resourceManualFreezeCreateOrUpdate,
Importer: helpers.MultiLevelResourceImporter,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Identifier of the freeze",
Type: schema.TypeString,
Required: true,
},
"description": {
Description: "Description of the freeze",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "Name of the freeze",
Type: schema.TypeString,
Computed: true,
},
"org_id": {
Description: "Organization identifier of the freeze",
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Description: "Project identifier of the freeze",
Type: schema.TypeString,
Optional: true,
},
"tags": {
Description: "Tags associated with the freeze",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"type": {
Description: "Type of freeze",
Type: schema.TypeString,
Computed: true,
},
"account_id": {
Description: "Account Identifier of the freeze",
Type: schema.TypeString,
Required: true,
},
"status": {
Description: "Status of the freeze",
Type: schema.TypeString,
Computed: true,
},
"scope": {
Description: "Scope of the freeze",
Type: schema.TypeString,
Computed: true,
},
"yaml": {
Description: "Yaml of the freeze",
Type: schema.TypeString,
Required: true,
},
"current_or_upcoming_windows": {
Description: "Current or upcoming windows",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_time": {
Description: "Start time of the freeze window",
Type: schema.TypeInt,
Computed: true,
},
"end_time": {
Description: "End time of the freeze window",
Type: schema.TypeInt,
Computed: true,
},
}},
},
"freeze_windows": {
Description: "Freeze windows in the freeze response",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"time_zone": {
Description: "Time zone of the freeze window",
Type: schema.TypeString,
Computed: true,
},
"start_time": {
Description: "Start Time of the freeze window",
Type: schema.TypeString,
Computed: true,
},
"duration": {
Description: "Duration of the freeze window",
Type: schema.TypeString,
Computed: true,
},
"end_time": {
Description: "End Time of the freeze window",
Type: schema.TypeString,
Computed: true,
},
"recurrence": {
Description: "Recurrence of the freeze window",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Description: "Type of the recurrence",
Type: schema.TypeString,
Computed: true,
},
"recurrence_spec": {
Description: "Used to filter resources on their attributes",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"until": {
Description: "Time till which freeze window recurrs",
Type: schema.TypeString,
Computed: true,
},
"value": {
Description: "Every n months recurrence",
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
},
},
},
},
}
return resource
}
func resourceManualFreezeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Get("identifier").(string)
resp, httpResp, err := c.FreezeCRUDApi.GetFreeze(ctx, c.AccountId, id, &nextgen.FreezeCRUDApiGetFreezeOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
if httpResp.StatusCode == 404 {
d.SetId("")
d.MarkNewResource()
return nil
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
if resp.Data == nil {
return nil
}
readFreezeResponse(d, resp.Data)
return nil
}
func resourceManualFreezeCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var resp nextgen.ResponseDtoFreezeResponse
var httpResp *http.Response
var yaml string
id := d.Id()
if attr, ok := d.GetOk("yaml"); ok {
yaml = attr.(string)
}
if id == "" {
resp, httpResp, err = c.FreezeCRUDApi.CreateFreeze(ctx, yaml, c.AccountId, &nextgen.FreezeCRUDApiCreateFreezeOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
} else {
resp, httpResp, err = c.FreezeCRUDApi.UpdateFreeze(ctx, yaml, c.AccountId, d.Id(), &nextgen.FreezeCRUDApiUpdateFreezeOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
// The create/update methods don't return the upcoming windows in the response, so we need to query for it again.
respGet, httpResp, err := c.FreezeCRUDApi.GetFreeze(ctx, c.AccountId, resp.Data.Identifier, &nextgen.FreezeCRUDApiGetFreezeOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
readFreezeResponse(d, respGet.Data)
return nil
}
func resourceManualFreezeDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
httpResp, err := c.FreezeCRUDApi.DeleteFreeze(ctx, c.AccountId, d.Id(), &nextgen.FreezeCRUDApiDeleteFreezeOpts{
OrgIdentifier: helpers.BuildField(d, "org_id"),
ProjectIdentifier: helpers.BuildField(d, "project_id"),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func readFreezeResponse(d *schema.ResourceData, freezeResponse *nextgen.FreezeDetailedResponse) {
d.SetId(freezeResponse.Identifier)
d.Set("identifier", freezeResponse.Identifier)
d.Set("name", freezeResponse.Name)
d.Set("org_id", freezeResponse.OrgIdentifier)
d.Set("project_id", freezeResponse.ProjectIdentifier)
d.Set("account_id", freezeResponse.AccountId)
d.Set("type", freezeResponse.Type_)
d.Set("tags", helpers.FlattenTags(freezeResponse.Tags))
d.Set("status", freezeResponse.Status)
d.Set("scope", freezeResponse.FreezeScope)
d.Set("yaml", freezeResponse.Yaml)
d.Set("description", freezeResponse.Description)
d.Set("current_or_upcoming_windows", []interface{}{
map[string]interface{}{
"start_time": freezeResponse.CurrentOrUpcomingWindow.StartTime,
"end_time": freezeResponse.CurrentOrUpcomingWindow.EndTime,
},
})
d.Set("freeze_windows", expandFreezeWindows(freezeResponse.Windows))
}
func expandFreezeWindows(freezeWindows []nextgen.FreezeWindow) []interface{} {
var result []interface{}
for _, window := range freezeWindows {
result = append(result, map[string]interface{}{
"time_zone": window.TimeZone,
"start_time": window.StartTime,
"duration": window.Duration,
"end_time": window.EndTime,
"recurrence": expandRecurrence(window),
})
}
return result
}
func expandRecurrence(window nextgen.FreezeWindow) []interface{} {
var result []interface{}
if window.Recurrence != nil {
result = append(result, map[string]interface{}{
"type": window.Recurrence.Type_,
"recurrence_spec": expandRecurrenceSpec(*window.Recurrence),
})
}
return result
}
func expandRecurrenceSpec(recurrence nextgen.Recurrence) []interface{} {
var result []interface{}
if recurrence.Spec != nil {
result = append(result, map[string]interface{}{
"value": recurrence.Spec.Value,
"until": recurrence.Spec.Until,
})
}
return result
}