-
Notifications
You must be signed in to change notification settings - Fork 670
/
data_source_ibm_is_flow_logs.go
145 lines (133 loc) · 4.14 KB
/
data_source_ibm_is_flow_logs.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isFlowLogs = "flow_log_collectors"
)
func DataSourceIBMISFlowLogs() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISFlowLogsRead,
Schema: map[string]*schema.Schema{
isFlowLogs: {
Type: schema.TypeList,
Description: "Collection of flow log collectors",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this flow log collector",
},
"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",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Flow Log Collector name",
},
"resource_group": {
Type: schema.TypeString,
Computed: true,
Description: "The resource group of flow log",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time flow log was created",
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
Description: "The lifecycle state of the flow log collector",
},
"storage_bucket": {
Type: schema.TypeString,
Computed: true,
Description: "The Cloud Object Storage bucket name where the collected flows will be logged",
},
"active": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this collector is active",
},
"target": {
Type: schema.TypeString,
Computed: true,
Description: "The target id that the flow log collector is to collect flow logs",
},
"vpc": {
Type: schema.TypeString,
Computed: true,
Description: "The VPC this flow log collector is associated with",
},
},
},
},
},
}
}
func dataSourceIBMISFlowLogsRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
start := ""
allrecs := []vpcv1.FlowLogCollector{}
for {
listOptions := &vpcv1.ListFlowLogCollectorsOptions{}
if start != "" {
listOptions.Start = &start
}
flowlogCollectors, response, err := sess.ListFlowLogCollectors(listOptions)
if err != nil {
return 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
}
}
flowlogsInfo := make([]map[string]interface{}, 0)
for _, flowlogCollector := range allrecs {
targetIntf := flowlogCollector.Target
target := targetIntf.(*vpcv1.FlowLogCollectorTarget)
l := map[string]interface{}{
"id": *flowlogCollector.ID,
"crn": *flowlogCollector.CRN,
"href": *flowlogCollector.Href,
"name": *flowlogCollector.Name,
"resource_group": *flowlogCollector.ResourceGroup.ID,
"created_at": flowlogCollector.CreatedAt.String(),
"lifecycle_state": *flowlogCollector.LifecycleState,
"storage_bucket": *flowlogCollector.StorageBucket.Name,
"active": *flowlogCollector.Active,
"vpc": *flowlogCollector.VPC.ID,
"target": *target.ID,
}
flowlogsInfo = append(flowlogsInfo, l)
}
d.SetId(dataSourceIBMISFlowLogsID(d))
d.Set(isFlowLogs, flowlogsInfo)
return nil
}
// dataSourceIBMISFlowLogsID returns a reasonable ID for a flowlogCollector list.
func dataSourceIBMISFlowLogsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}