-
Notifications
You must be signed in to change notification settings - Fork 658
/
resource_ibm_scc_provider_type_instance.go
276 lines (234 loc) · 10.3 KB
/
resource_ibm_scc_provider_type_instance.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
// ng Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package scc
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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/go-sdk-core/v5/core"
"github.com/IBM/scc-go-sdk/v5/securityandcompliancecenterapiv3"
)
func ResourceIbmSccProviderTypeInstance() *schema.Resource {
return AddSchemaData(&schema.Resource{
CreateContext: resourceIbmSccProviderTypeInstanceCreate,
ReadContext: resourceIbmSccProviderTypeInstanceRead,
UpdateContext: resourceIbmSccProviderTypeInstanceUpdate,
DeleteContext: resourceIbmSccProviderTypeInstanceDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"provider_type_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.InvokeValidator("ibm_scc_provider_type_instance", "provider_type_id"),
Description: "The provider type ID.",
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_scc_provider_type_instance", "name"),
Description: "The name of the provider type instance.",
},
"attributes": {
Type: schema.TypeMap,
Optional: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the provider type.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Time at which resource was created.",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "Time at which resource was updated.",
},
"provider_type_instance_id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier of the provider type instance.",
},
},
})
}
func ResourceIbmSccProviderTypeInstanceValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "provider_type_id",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `^[a-zA-Z0-9 ,\-_]+$`,
MinValueLength: 32,
MaxValueLength: 36,
},
validate.ValidateSchema{
Identifier: "name",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `[A-Za-z0-9]+`,
MinValueLength: 1,
MaxValueLength: 64,
},
)
resourceValidator := validate.ResourceValidator{ResourceName: "ibm_scc_provider_type_instance", Schema: validateSchema}
return &resourceValidator
}
func resourceIbmSccProviderTypeInstanceCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
securityAndComplianceCenterApIsClient, err := meta.(conns.ClientSession).SecurityAndComplianceCenterV3()
if err != nil {
return diag.FromErr(err)
}
createProviderTypeInstanceOptions := &securityandcompliancecenterapiv3.CreateProviderTypeInstanceOptions{}
instanceID := d.Get("instance_id").(string)
createProviderTypeInstanceOptions.SetProviderTypeID(d.Get("provider_type_id").(string))
createProviderTypeInstanceOptions.SetName(d.Get("name").(string))
createProviderTypeInstanceOptions.SetInstanceID(instanceID)
attributesModel, err := resourceIbmSccProviderTypeInstanceMapToProviderTypeInstanceAttributes(d.Get("attributes").(map[string]interface{}))
if err != nil {
return diag.FromErr(err)
}
createProviderTypeInstanceOptions.SetAttributes(attributesModel)
providerTypeInstanceItem, response, err := securityAndComplianceCenterApIsClient.CreateProviderTypeInstanceWithContext(context, createProviderTypeInstanceOptions)
if err != nil {
log.Printf("[DEBUG] CreateProviderTypeInstanceWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("CreateProviderTypeInstanceWithContext failed %s\n%s", err, response))
}
d.SetId(fmt.Sprintf("%s/%s/%s", instanceID, *createProviderTypeInstanceOptions.ProviderTypeID, *providerTypeInstanceItem.ID))
return resourceIbmSccProviderTypeInstanceRead(context, d, meta)
}
func resourceIbmSccProviderTypeInstanceRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
securityAndComplianceCenterApIsClient, err := meta.(conns.ClientSession).SecurityAndComplianceCenterV3()
if err != nil {
return diag.FromErr(err)
}
getProviderTypeInstanceOptions := &securityandcompliancecenterapiv3.GetProviderTypeInstanceOptions{}
parts, err := flex.SepIdParts(d.Id(), "/")
if err != nil {
return diag.FromErr(err)
}
getProviderTypeInstanceOptions.SetInstanceID(parts[0])
getProviderTypeInstanceOptions.SetProviderTypeID(parts[1])
getProviderTypeInstanceOptions.SetProviderTypeInstanceID(parts[2])
providerTypeInstanceItem, response, err := securityAndComplianceCenterApIsClient.GetProviderTypeInstanceWithContext(context, getProviderTypeInstanceOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetProviderTypeInstanceWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetProviderTypeInstanceWithContext failed %s\n%s", err, response))
}
if err = d.Set("instance_id", parts[0]); err != nil {
return diag.FromErr(fmt.Errorf("Error setting instance_id: %s", err))
}
if err = d.Set("name", providerTypeInstanceItem.Name); err != nil {
return diag.FromErr(fmt.Errorf("Error setting name: %s", err))
}
attributesMap, err := resourceIbmSccProviderTypeInstanceProviderTypeInstanceAttributesToMap(providerTypeInstanceItem.Attributes)
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("attributes", attributesMap); err != nil {
return diag.FromErr(fmt.Errorf("Error setting attributes: %s", err))
}
if !core.IsNil(providerTypeInstanceItem.Type) {
if err = d.Set("type", providerTypeInstanceItem.Type); err != nil {
return diag.FromErr(fmt.Errorf("Error setting type: %s", err))
}
}
if !core.IsNil(providerTypeInstanceItem.CreatedAt) {
if err = d.Set("created_at", flex.DateTimeToString(providerTypeInstanceItem.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
}
if !core.IsNil(providerTypeInstanceItem.UpdatedAt) {
if err = d.Set("updated_at", flex.DateTimeToString(providerTypeInstanceItem.UpdatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting updated_at: %s", err))
}
}
if !core.IsNil(providerTypeInstanceItem.ID) {
if err = d.Set("provider_type_instance_id", providerTypeInstanceItem.ID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting provider_type_instance_id: %s", err))
}
}
if err = d.Set("provider_type_id", parts[1]); err != nil {
return diag.FromErr(fmt.Errorf("Error setting provider_type_id: %s", err))
}
return nil
}
func resourceIbmSccProviderTypeInstanceUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
securityAndComplianceCenterApIsClient, err := meta.(conns.ClientSession).SecurityAndComplianceCenterV3()
if err != nil {
return diag.FromErr(err)
}
updateProviderTypeInstanceOptions := &securityandcompliancecenterapiv3.UpdateProviderTypeInstanceOptions{}
parts, err := flex.SepIdParts(d.Id(), "/")
if err != nil {
return diag.FromErr(err)
}
// TODO: add updateProviderTypeInstanceOptions.SetInstanceID to scc-go-sdk
updateProviderTypeInstanceOptions.SetInstanceID(parts[0])
updateProviderTypeInstanceOptions.SetProviderTypeID(parts[1])
updateProviderTypeInstanceOptions.SetProviderTypeInstanceID(parts[2])
hasChange := false
if d.HasChange("provider_type_id") {
return diag.FromErr(fmt.Errorf("Cannot update resource property \"%s\" with the ForceNew annotation."+
" The resource must be re-created to update this property.", "provider_type_id"))
}
if d.HasChange("attributes") {
updateProviderTypeInstanceOptions.SetAttributes(d.Get("attributes").(map[string]interface{}))
hasChange = true
}
if d.HasChange("name") {
updateProviderTypeInstanceOptions.SetName(d.Get("name").(string))
hasChange = true
}
if hasChange {
_, response, err := securityAndComplianceCenterApIsClient.UpdateProviderTypeInstanceWithContext(context, updateProviderTypeInstanceOptions)
if err != nil {
log.Printf("[DEBUG] UpdateProviderTypeInstanceWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("UpdateProviderTypeInstanceWithContext failed %s\n%s", err, response))
}
}
return resourceIbmSccProviderTypeInstanceRead(context, d, meta)
}
func resourceIbmSccProviderTypeInstanceDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
securityAndComplianceCenterApIsClient, err := meta.(conns.ClientSession).SecurityAndComplianceCenterV3()
if err != nil {
return diag.FromErr(err)
}
deleteProviderTypeInstanceOptions := &securityandcompliancecenterapiv3.DeleteProviderTypeInstanceOptions{}
parts, err := flex.SepIdParts(d.Id(), "/")
if err != nil {
return diag.FromErr(err)
}
deleteProviderTypeInstanceOptions.SetInstanceID(parts[0])
deleteProviderTypeInstanceOptions.SetProviderTypeID(parts[1])
deleteProviderTypeInstanceOptions.SetProviderTypeInstanceID(parts[2])
response, err := securityAndComplianceCenterApIsClient.DeleteProviderTypeInstanceWithContext(context, deleteProviderTypeInstanceOptions)
if err != nil {
log.Printf("[DEBUG] DeleteProviderTypeInstanceWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("DeleteProviderTypeInstanceWithContext failed %s\n%s", err, response))
}
d.SetId("")
return nil
}
func resourceIbmSccProviderTypeInstanceMapToProviderTypeInstanceAttributes(modelMap map[string]interface{}) (map[string]interface{}, error) {
return modelMap, nil
}
func resourceIbmSccProviderTypeInstanceProviderTypeInstanceAttributesToMap(modelMap map[string]interface{}) (map[string]interface{}, error) {
return modelMap, nil
}