-
Notifications
You must be signed in to change notification settings - Fork 669
/
data_source_ibm_is_flow_log.go
437 lines (393 loc) · 13.6 KB
/
data_source_ibm_is_flow_log.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMIsFlowLog() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsFlowLogRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"identifier", "name"},
Description: "The unique user-defined name for this flow log collector.",
},
"identifier": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"identifier", "name"},
Description: "The flow log collector identifier.",
},
"active": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this collector is active.",
},
"auto_delete": {
Type: schema.TypeBool,
Computed: true,
Description: "If set to `true`, this flow log collector will be automatically deleted when the target is deleted.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the flow log collector was created.",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this flow log collector.",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this flow log collector.",
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the flow log collector.",
},
"resource_group": {
Type: schema.TypeList,
Computed: true,
Description: "The resource group for this flow log collector.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this resource group.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this resource group.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this resource group.",
},
},
},
},
"storage_bucket": {
Type: schema.TypeList,
Computed: true,
Description: "The Cloud Object Storage bucket where the collected flows are logged.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name of this COS bucket.",
},
},
},
},
"target": {
Type: schema.TypeList,
Computed: true,
Description: "The target this collector is collecting flow logs for. If the target is an instance,subnet, or VPC, flow logs will not be collected for any network interfaces within thetarget that are themselves the target of a more specific flow log collector.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"deleted": {
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this network interface.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this network interface.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this network interface.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this virtual server instance.",
},
},
},
},
"vpc": {
Type: schema.TypeList,
Computed: true,
Description: "The VPC this flow log collector is associated with.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this VPC.",
},
"deleted": {
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this VPC.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this VPC.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this VPC.",
},
},
},
},
},
}
}
func dataSourceIBMIsFlowLogRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
name := d.Get("name").(string)
identifier := d.Get("identifier").(string)
var flowLogCollector *vpcv1.FlowLogCollector
if name != "" {
start := ""
allrecs := []vpcv1.FlowLogCollector{}
for {
listOptions := &vpcv1.ListFlowLogCollectorsOptions{}
if start != "" {
listOptions.Start = &start
}
flowlogCollectors, response, err := sess.ListFlowLogCollectors(listOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error Fetching Flow Logs for VPC %s\n%s", err, response))
}
start = flex.GetNext(flowlogCollectors.Next)
allrecs = append(allrecs, flowlogCollectors.FlowLogCollectors...)
if start == "" {
break
}
}
for _, flowlogCollector := range allrecs {
if *flowlogCollector.Name == name {
flowLogCollector = &flowlogCollector
break
}
}
if flowLogCollector == nil {
return diag.FromErr(fmt.Errorf("[ERROR] No flow log collector found with name (%s)", name))
}
} else if identifier != "" {
getFlowLogCollectorOptions := &vpcv1.GetFlowLogCollectorOptions{}
getFlowLogCollectorOptions.SetID(d.Get("identifier").(string))
flowlogCollector, response, err := sess.GetFlowLogCollectorWithContext(context, getFlowLogCollectorOptions)
if err != nil {
log.Printf("[DEBUG] GetFlowLogCollectorWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetFlowLogCollectorWithContext failed %s\n%s", err, response))
}
flowLogCollector = flowlogCollector
}
d.SetId(*flowLogCollector.ID)
if err = d.Set("active", flowLogCollector.Active); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting active: %s", err))
}
if err = d.Set("auto_delete", flowLogCollector.AutoDelete); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting auto_delete: %s", err))
}
if err = d.Set("created_at", flex.DateTimeToString(flowLogCollector.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set("crn", flowLogCollector.CRN); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting crn: %s", err))
}
if err = d.Set("href", flowLogCollector.Href); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting href: %s", err))
}
if err = d.Set("lifecycle_state", flowLogCollector.LifecycleState); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting lifecycle_state: %s", err))
}
if err = d.Set("name", flowLogCollector.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}
if err = d.Set("identifier", *flowLogCollector.ID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting identifier: %s", err))
}
if flowLogCollector.ResourceGroup != nil {
err = d.Set("resource_group", dataSourceFlowLogCollectorFlattenResourceGroup(*flowLogCollector.ResourceGroup))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_group %s", err))
}
}
if flowLogCollector.StorageBucket != nil {
err = d.Set("storage_bucket", dataSourceFlowLogCollectorFlattenStorageBucket(*flowLogCollector.StorageBucket))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting storage_bucket %s", err))
}
}
if flowLogCollector.Target != nil {
targetIntf := flowLogCollector.Target
target := targetIntf.(*vpcv1.FlowLogCollectorTarget)
err = d.Set("target", dataSourceFlowLogCollectorFlattenTarget(*target))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting target %s", err))
}
}
if flowLogCollector.VPC != nil {
err = d.Set("vpc", dataSourceFlowLogCollectorFlattenVPC(*flowLogCollector.VPC))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting vpc %s", err))
}
}
return nil
}
func dataSourceFlowLogCollectorFlattenResourceGroup(result vpcv1.ResourceGroupReference) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceFlowLogCollectorResourceGroupToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceFlowLogCollectorResourceGroupToMap(resourceGroupItem vpcv1.ResourceGroupReference) (resourceGroupMap map[string]interface{}) {
resourceGroupMap = map[string]interface{}{}
if resourceGroupItem.Href != nil {
resourceGroupMap["href"] = resourceGroupItem.Href
}
if resourceGroupItem.ID != nil {
resourceGroupMap["id"] = resourceGroupItem.ID
}
if resourceGroupItem.Name != nil {
resourceGroupMap["name"] = resourceGroupItem.Name
}
return resourceGroupMap
}
func dataSourceFlowLogCollectorFlattenStorageBucket(result vpcv1.CloudObjectStorageBucketReference) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceFlowLogCollectorStorageBucketToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceFlowLogCollectorStorageBucketToMap(storageBucketItem vpcv1.CloudObjectStorageBucketReference) (storageBucketMap map[string]interface{}) {
storageBucketMap = map[string]interface{}{}
if storageBucketItem.Name != nil {
storageBucketMap["name"] = storageBucketItem.Name
}
return storageBucketMap
}
func dataSourceFlowLogCollectorFlattenTarget(result vpcv1.FlowLogCollectorTarget) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceFlowLogCollectorTargetToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceFlowLogCollectorTargetToMap(targetItem vpcv1.FlowLogCollectorTarget) (targetMap map[string]interface{}) {
targetMap = map[string]interface{}{}
if targetItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceFlowLogCollectorTargetDeletedToMap(*targetItem.Deleted)
deletedList = append(deletedList, deletedMap)
targetMap["deleted"] = deletedList
}
if targetItem.Href != nil {
targetMap["href"] = targetItem.Href
}
if targetItem.ID != nil {
targetMap["id"] = targetItem.ID
}
if targetItem.Name != nil {
targetMap["name"] = targetItem.Name
}
if targetItem.ResourceType != nil {
targetMap["resource_type"] = targetItem.ResourceType
}
if targetItem.CRN != nil {
targetMap["crn"] = targetItem.CRN
}
return targetMap
}
func dataSourceFlowLogCollectorTargetDeletedToMap(deletedItem vpcv1.NetworkInterfaceReferenceTargetContextDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceFlowLogCollectorFlattenVPC(result vpcv1.VPCReference) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceFlowLogCollectorVPCToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceFlowLogCollectorVPCToMap(vpcItem vpcv1.VPCReference) (vpcMap map[string]interface{}) {
vpcMap = map[string]interface{}{}
if vpcItem.CRN != nil {
vpcMap["crn"] = vpcItem.CRN
}
if vpcItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceFlowLogCollectorVPCDeletedToMap(*vpcItem.Deleted)
deletedList = append(deletedList, deletedMap)
vpcMap["deleted"] = deletedList
}
if vpcItem.Href != nil {
vpcMap["href"] = vpcItem.Href
}
if vpcItem.ID != nil {
vpcMap["id"] = vpcItem.ID
}
if vpcItem.Name != nil {
vpcMap["name"] = vpcItem.Name
}
return vpcMap
}
func dataSourceFlowLogCollectorVPCDeletedToMap(deletedItem vpcv1.VPCReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}