-
Notifications
You must be signed in to change notification settings - Fork 656
/
data_source_ibm_is_subnet.go
362 lines (317 loc) · 9.95 KB
/
data_source_ibm_is_subnet.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMISSubnet() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISSubnetRead,
Schema: map[string]*schema.Schema{
"identifier": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{isSubnetName, "identifier"},
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_subnet", "identifier"),
},
isSubnetIpv4CidrBlock: {
Type: schema.TypeString,
Computed: true,
},
isSubnetAvailableIpv4AddressCount: {
Type: schema.TypeInt,
Computed: true,
},
isSubnetTotalIpv4AddressCount: {
Type: schema.TypeInt,
Computed: true,
},
isSubnetName: {
Type: schema.TypeString,
Computed: true,
Optional: true,
ExactlyOneOf: []string{isSubnetName, "identifier"},
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_subnet", isSubnetName),
},
isSubnetTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of tags",
},
isSubnetAccessTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access tags",
},
isSubnetCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
isSubnetNetworkACL: {
Type: schema.TypeString,
Computed: true,
},
isSubnetPublicGateway: {
Type: schema.TypeString,
Computed: true,
},
isSubnetStatus: {
Type: schema.TypeString,
Computed: true,
},
isSubnetVPC: {
Type: schema.TypeString,
Optional: true,
Computed: true,
RequiredWith: []string{isSubnetName},
ValidateFunc: validate.InvokeDataSourceValidator("ibm_is_subnet", "identifier"),
},
isSubnetVPCName: {
Type: schema.TypeString,
Computed: true,
},
isSubnetZone: {
Type: schema.TypeString,
Computed: true,
},
isSubnetResourceGroup: {
Type: schema.TypeString,
Computed: true,
},
flex.ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the IBM Cloud dashboard that can be used to explore and view details about this instance",
},
flex.ResourceName: {
Type: schema.TypeString,
Computed: true,
Description: "The name of the resource",
},
flex.ResourceCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
flex.ResourceStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the resource",
},
flex.ResourceGroupName: {
Type: schema.TypeString,
Computed: true,
Description: "The resource group name in which resource is provisioned",
},
"routing_table": {
Type: schema.TypeList,
Computed: true,
Description: "The routing table for this subnet",
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 routing table.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this routing table.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this routing table.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
},
},
},
},
}
}
func DataSourceIBMISSubnetValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "identifier",
ValidateFunctionIdentifier: validate.ValidateNoZeroValues,
Type: validate.TypeString})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isSubnetName,
ValidateFunctionIdentifier: validate.ValidateNoZeroValues,
Type: validate.TypeString})
ibmISSubnetDataSourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_subnet", Schema: validateSchema}
return &ibmISSubnetDataSourceValidator
}
func dataSourceIBMISSubnetRead(d *schema.ResourceData, meta interface{}) error {
err := subnetGetByNameOrID(d, meta)
if err != nil {
return err
}
return nil
}
func subnetGetByNameOrID(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
var subnet *vpcv1.Subnet
if v, ok := d.GetOk("identifier"); ok {
id := v.(string)
getSubnetOptions := &vpcv1.GetSubnetOptions{
ID: &id,
}
subnetinfo, response, err := sess.GetSubnet(getSubnetOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Getting Subnet (%s): %s\n%s", id, err, response)
}
subnet = subnetinfo
} else if v, ok := d.GetOk(isSubnetName); ok {
name := v.(string)
start := ""
allrecs := []vpcv1.Subnet{}
getSubnetsListOptions := &vpcv1.ListSubnetsOptions{}
for {
if start != "" {
getSubnetsListOptions.Start = &start
}
if vpcIdOk, ok := d.GetOk(isSubnetVPC); ok {
vpcIDOk := vpcIdOk.(string)
getSubnetsListOptions.VPCID = &vpcIDOk
}
subnetsCollection, response, err := sess.ListSubnets(getSubnetsListOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching subnets List %s\n%s", err, response)
}
start = flex.GetNext(subnetsCollection.Next)
allrecs = append(allrecs, subnetsCollection.Subnets...)
if start == "" {
break
}
}
for _, subnetInfo := range allrecs {
if *subnetInfo.Name == name {
subnet = &subnetInfo
break
}
}
if subnet == nil {
return fmt.Errorf("[ERROR] No subnet found with name (%s)", name)
}
}
d.SetId(*subnet.ID)
if subnet.RoutingTable != nil {
err = d.Set("routing_table", dataSourceSubnetFlattenroutingTable(*subnet.RoutingTable))
if err != nil {
return fmt.Errorf("Error setting routing_table %s", err)
}
}
d.Set(isSubnetName, *subnet.Name)
d.Set(isSubnetIpv4CidrBlock, *subnet.Ipv4CIDRBlock)
d.Set(isSubnetAvailableIpv4AddressCount, *subnet.AvailableIpv4AddressCount)
d.Set(isSubnetTotalIpv4AddressCount, *subnet.TotalIpv4AddressCount)
if subnet.NetworkACL != nil {
d.Set(isSubnetNetworkACL, *subnet.NetworkACL.ID)
}
if subnet.PublicGateway != nil {
d.Set(isSubnetPublicGateway, *subnet.PublicGateway.ID)
} else {
d.Set(isSubnetPublicGateway, nil)
}
d.Set(isSubnetStatus, *subnet.Status)
d.Set(isSubnetZone, *subnet.Zone.Name)
d.Set(isSubnetVPC, *subnet.VPC.ID)
d.Set(isSubnetVPCName, *subnet.VPC.Name)
controller, err := flex.GetBaseController(meta)
if err != nil {
return err
}
tags, err := flex.GetGlobalTagsUsingCRN(meta, *subnet.CRN, "", isUserTagType)
if err != nil {
log.Printf(
"An error occured during reading of subnet (%s) tags : %s", d.Id(), err)
}
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *subnet.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource subnet (%s) access tags: %s", d.Id(), err)
}
d.Set(isSubnetTags, tags)
d.Set(isSubnetAccessTags, accesstags)
d.Set(isSubnetCRN, *subnet.CRN)
d.Set(flex.ResourceControllerURL, controller+"/vpc-ext/network/subnets")
d.Set(flex.ResourceName, *subnet.Name)
d.Set(flex.ResourceCRN, *subnet.CRN)
d.Set(flex.ResourceStatus, *subnet.Status)
if subnet.ResourceGroup != nil {
d.Set(isSubnetResourceGroup, *subnet.ResourceGroup.ID)
d.Set(flex.ResourceGroupName, *subnet.ResourceGroup.Name)
}
return nil
}
func dataSourcesubnetRoutingTableDeletedToMap(deletedItem vpcv1.RoutingTableReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap["more_info"] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceSubnetFlattenroutingTable(result vpcv1.RoutingTableReference) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceSubnetRoutingTableToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceSubnetRoutingTableToMap(routingTableItem vpcv1.RoutingTableReference) (routingTableMap map[string]interface{}) {
routingTableMap = map[string]interface{}{}
if routingTableItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourcesubnetRoutingTableDeletedToMap(*routingTableItem.Deleted)
deletedList = append(deletedList, deletedMap)
routingTableMap["deleted"] = deletedList
}
if routingTableItem.Href != nil {
routingTableMap["href"] = routingTableItem.Href
}
if routingTableItem.ID != nil {
routingTableMap["id"] = routingTableItem.ID
}
if routingTableItem.Name != nil {
routingTableMap["name"] = routingTableItem.Name
}
if routingTableItem.ResourceType != nil {
routingTableMap["resource_type"] = routingTableItem.ResourceType
}
return routingTableMap
}