-
Notifications
You must be signed in to change notification settings - Fork 645
/
resource_ibm_cd_toolchain.go
333 lines (297 loc) · 11.2 KB
/
resource_ibm_cd_toolchain.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package cdtoolchain
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/continuous-delivery-go-sdk/cdtoolchainv2"
"github.com/IBM/go-sdk-core/v5/core"
)
func ResourceIBMCdToolchain() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMCdToolchainCreate,
ReadContext: resourceIBMCdToolchainRead,
UpdateContext: resourceIBMCdToolchainUpdate,
DeleteContext: resourceIBMCdToolchainDelete,
Importer: &schema.ResourceImporter{},
CustomizeDiff: customdiff.Sequence(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
return flex.ResourceTagsCustomizeDiff(diff)
},
),
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_cd_toolchain", "name"),
Description: "Toolchain name.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.InvokeValidator("ibm_cd_toolchain", "description"),
Description: "Describes the toolchain.",
},
"resource_group_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.InvokeValidator("ibm_cd_toolchain", "resource_group_id"),
Description: "Resource group where the toolchain is located.",
},
"account_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Account ID where toolchain can be found.",
},
"location": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Toolchain region.",
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Toolchain CRN.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "URI that can be used to retrieve toolchain.",
},
"ui_href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "URL of a user-facing user interface for this toolchain.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Toolchain creation timestamp.",
},
"updated_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Latest toolchain update timestamp.",
},
"created_by": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Identity that created the toolchain.",
},
"tags": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString, ValidateFunc: validate.InvokeValidator("ibm_cd_toolchain", "tags")},
Set: flex.ResourceIBMVPCHash,
ValidateFunc: validate.InvokeValidator("ibm_cd_toolchain", "tags"),
Description: "Toolchain tags.",
},
},
}
}
func ResourceIBMCdToolchainValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "name",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `^([^\x00-\x7F]|[a-zA-Z0-9-._ ])+$`,
MinValueLength: 0,
MaxValueLength: 128,
},
validate.ValidateSchema{
Identifier: "description",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^(.*?)$`,
MinValueLength: 0,
MaxValueLength: 500,
},
validate.ValidateSchema{
Identifier: "resource_group_id",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `^[0-9a-f]{32}$`,
MinValueLength: 32,
MaxValueLength: 32,
},
validate.ValidateSchema{
Identifier: "tags",
ValidateFunctionIdentifier: validate.ValidateCloudData,
Type: validate.TypeString,
CloudDataType: "tags",
Optional: true,
},
)
resourceValidator := validate.ResourceValidator{ResourceName: "ibm_cd_toolchain", Schema: validateSchema}
return &resourceValidator
}
func resourceIBMCdToolchainCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cdToolchainClient, err := meta.(conns.ClientSession).CdToolchainV2()
if err != nil {
return diag.FromErr(err)
}
createToolchainOptions := &cdtoolchainv2.CreateToolchainOptions{}
createToolchainOptions.SetName(d.Get("name").(string))
createToolchainOptions.SetResourceGroupID(d.Get("resource_group_id").(string))
if _, ok := d.GetOk("description"); ok {
createToolchainOptions.SetDescription(d.Get("description").(string))
}
toolchainPost, response, err := cdToolchainClient.CreateToolchainWithContext(context, createToolchainOptions)
if err != nil {
log.Printf("[DEBUG] CreateToolchainWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("CreateToolchainWithContext failed %s\n%s", err, response))
}
d.SetId(*toolchainPost.ID)
v := os.Getenv("IC_ENV_TAGS")
if _, ok := d.GetOk("tags"); ok || v != "" {
oldList, newList := d.GetChange("tags")
err = flex.UpdateTagsUsingCRN(oldList, newList, meta, *toolchainPost.CRN)
if err != nil {
log.Printf(
"Error on create of toolchain (%s) tags: %s", d.Id(), err)
}
}
return resourceIBMCdToolchainRead(context, d, meta)
}
func resourceIBMCdToolchainRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cdToolchainClient, err := meta.(conns.ClientSession).CdToolchainV2()
if err != nil {
return diag.FromErr(err)
}
getToolchainByIDOptions := &cdtoolchainv2.GetToolchainByIDOptions{}
getToolchainByIDOptions.SetToolchainID(d.Id())
var toolchain *cdtoolchainv2.Toolchain
var response *core.DetailedResponse
err = resource.RetryContext(context, 10*time.Second, func() *resource.RetryError {
toolchain, response, err = cdToolchainClient.GetToolchainByIDWithContext(context, getToolchainByIDOptions)
if err != nil || toolchain == nil {
if response != nil && response.StatusCode == 404 {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if conns.IsResourceTimeoutError(err) {
toolchain, response, err = cdToolchainClient.GetToolchainByIDWithContext(context, getToolchainByIDOptions)
}
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetToolchainByIDWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetToolchainByIDWithContext failed %s\n%s", err, response))
}
tags, err := flex.GetTagsUsingCRN(meta, *toolchain.CRN)
if err != nil {
log.Printf(
"Error on get of toolchain (%s) tags: %s", d.Id(), err)
}
d.Set("tags", tags)
if err = d.Set("name", toolchain.Name); err != nil {
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
}
if !core.IsNil(toolchain.Description) {
if err = d.Set("description", toolchain.Description); err != nil {
return diag.FromErr(fmt.Errorf("Error setting description: %s", err))
}
}
if err = d.Set("resource_group_id", toolchain.ResourceGroupID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting resource_group_id: %s", err))
}
if err = d.Set("account_id", toolchain.AccountID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting account_id: %s", err))
}
if err = d.Set("location", toolchain.Location); err != nil {
return diag.FromErr(fmt.Errorf("Error setting location: %s", err))
}
if err = d.Set("crn", toolchain.CRN); err != nil {
return diag.FromErr(fmt.Errorf("Error setting crn: %s", err))
}
if err = d.Set("href", toolchain.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("ui_href", toolchain.UIHref); err != nil {
return diag.FromErr(fmt.Errorf("Error setting ui_href: %s", err))
}
if err = d.Set("created_at", flex.DateTimeToString(toolchain.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
if err = d.Set("updated_at", flex.DateTimeToString(toolchain.UpdatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting updated_at: %s", err))
}
if err = d.Set("created_by", toolchain.CreatedBy); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_by: %s", err))
}
return nil
}
func resourceIBMCdToolchainUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cdToolchainClient, err := meta.(conns.ClientSession).CdToolchainV2()
if err != nil {
return diag.FromErr(err)
}
updateToolchainOptions := &cdtoolchainv2.UpdateToolchainOptions{}
updateToolchainOptions.SetToolchainID(d.Id())
hasChange := false
patchVals := &cdtoolchainv2.ToolchainPrototypePatch{}
if d.HasChange("name") {
newName := d.Get("name").(string)
patchVals.Name = &newName
hasChange = true
}
if d.HasChange("description") {
newDescription := d.Get("description").(string)
patchVals.Description = &newDescription
hasChange = true
}
if d.HasChange("tags") {
oldList, newList := d.GetChange("tags")
err = flex.UpdateTagsUsingCRN(oldList, newList, meta, d.Get("crn").(string))
if err != nil {
log.Printf(
"Error on update of toolchain (%s) tags: %s", d.Id(), err)
}
}
if hasChange {
updateToolchainOptions.ToolchainPrototypePatch, _ = patchVals.AsPatch()
_, response, err := cdToolchainClient.UpdateToolchainWithContext(context, updateToolchainOptions)
if err != nil {
log.Printf("[DEBUG] UpdateToolchainWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("UpdateToolchainWithContext failed %s\n%s", err, response))
}
}
return resourceIBMCdToolchainRead(context, d, meta)
}
func resourceIBMCdToolchainDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cdToolchainClient, err := meta.(conns.ClientSession).CdToolchainV2()
if err != nil {
return diag.FromErr(err)
}
deleteToolchainOptions := &cdtoolchainv2.DeleteToolchainOptions{}
deleteToolchainOptions.SetToolchainID(d.Id())
response, err := cdToolchainClient.DeleteToolchainWithContext(context, deleteToolchainOptions)
if err != nil {
log.Printf("[DEBUG] DeleteToolchainWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("DeleteToolchainWithContext failed %s\n%s", err, response))
}
d.SetId("")
return nil
}