-
Notifications
You must be signed in to change notification settings - Fork 650
/
data_source_ibm_is_floating_ip.go
399 lines (366 loc) · 12.6 KB
/
data_source_ibm_is_floating_ip.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"reflect"
"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 (
floatingIPName = "name"
floatingIPAddress = "address"
floatingIPStatus = "status"
floatingIPZone = "zone"
floatingIPTarget = "target"
floatingIPTargets = "target_list"
floatingIPTargetsHref = "href"
floatingIPTargetsCrn = "crn"
floatingIPTargetsDeleted = "deleted"
floatingIPTargetsMoreInfo = "more_info"
floatingIPTargetsId = "id"
floatingIPTargetsName = "name"
floatingIPTargetsResourceType = "resource_type"
floatingIPTags = "tags"
floatingIPCRN = "crn"
floatingIpPrimaryIP = "primary_ip"
floatingIpPrimaryIpAddress = "address"
floatingIpPrimaryIpHref = "href"
floatingIpPrimaryIpName = "name"
floatingIpPrimaryIpId = "reserved_ip"
floatingIpPrimaryIpResourceType = "resource_type"
)
func DataSourceIBMISFloatingIP() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMISFloatingIPRead,
Schema: map[string]*schema.Schema{
floatingIPName: {
Type: schema.TypeString,
Required: true,
Description: "Name of the floating IP",
},
floatingIPAddress: {
Type: schema.TypeString,
Computed: true,
Description: "Floating IP address",
},
floatingIPStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Floating IP status",
},
floatingIPZone: {
Type: schema.TypeString,
Computed: true,
Description: "Zone name",
},
floatingIPTarget: {
Type: schema.TypeString,
Computed: true,
Description: "Target info",
},
floatingIPTargets: {
Type: schema.TypeList,
Computed: true,
Description: "The target of this floating IP.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
floatingIPTargetsDeleted: {
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{
floatingIPTargetsMoreInfo: {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
floatingIPTargetsHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this network interface.",
},
floatingIPTargetsId: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this network interface.",
},
floatingIPTargetsName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this network interface.",
},
floatingIpPrimaryIP: {
Type: schema.TypeList,
Computed: true,
Description: "The primary IP address to bind to the network interface. This can be specified using an existing reserved IP, or a prototype object for a new reserved IP.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
floatingIpPrimaryIpAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The IP address to reserve, which must not already be reserved on the subnet.",
},
floatingIpPrimaryIpHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP",
},
floatingIpPrimaryIpName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this reserved IP. If unspecified, the name will be a hyphenated list of randomly-selected words. Names must be unique within the subnet the reserved IP resides in. ",
},
floatingIpPrimaryIpId: {
Type: schema.TypeString,
Computed: true,
Description: "Identifies a reserved IP by a unique property.",
},
floatingIpPrimaryIpResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type",
},
},
},
},
floatingIPTargetsResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
floatingIPTargetsCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this public gateway.",
},
},
},
},
floatingIPCRN: {
Type: schema.TypeString,
Computed: true,
Description: "Floating IP crn",
},
floatingIPTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "Floating IP tags",
},
isFloatingIPAccessTags: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: flex.ResourceIBMVPCHash,
Description: "List of access management tags",
},
},
}
}
func dataSourceIBMISFloatingIPRead(d *schema.ResourceData, meta interface{}) error {
floatingIPName := d.Get(isFloatingIPName).(string)
err := floatingIPGet(d, meta, floatingIPName)
if err != nil {
return err
}
return nil
}
func floatingIPGet(d *schema.ResourceData, meta interface{}, name string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
start := ""
allFloatingIPs := []vpcv1.FloatingIP{}
for {
floatingIPOptions := &vpcv1.ListFloatingIpsOptions{}
if start != "" {
floatingIPOptions.Start = &start
}
floatingIPs, response, err := sess.ListFloatingIps(floatingIPOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Fetching floating IPs %s\n%s", err, response)
}
start = flex.GetNext(floatingIPs.Next)
allFloatingIPs = append(allFloatingIPs, floatingIPs.FloatingIps...)
if start == "" {
break
}
}
for _, ip := range allFloatingIPs {
if *ip.Name == name {
d.Set(floatingIPName, *ip.Name)
d.Set(floatingIPAddress, *ip.Address)
d.Set(floatingIPStatus, *ip.Status)
d.Set(floatingIPZone, *ip.Zone.Name)
d.Set(floatingIPCRN, *ip.CRN)
if ip.Target != nil {
targetId, targetMap := dataSourceFloatingIPCollectionFloatingIpTargetToMap(ip.Target)
d.Set(floatingIPTarget, targetId)
targetList := []map[string]interface{}{}
targetList = append(targetList, targetMap)
d.Set(floatingIPTargets, targetList)
}
tags, err := flex.GetGlobalTagsUsingCRN(meta, *ip.CRN, "", isUserTagType)
if err != nil {
fmt.Printf("[ERROR] Error on get of vpc Floating IP (%s) tags: %s", *ip.Address, err)
}
d.Set(floatingIPTags, tags)
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *ip.CRN, "", isAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource floating ip (%s) access tags: %s", d.Id(), err)
}
d.Set(isFloatingIPAccessTags, accesstags)
d.SetId(*ip.ID)
return nil
}
}
return fmt.Errorf("[ERROR] No floatingIP found with name %s", name)
}
func dataSourceFloatingIPCollectionFloatingIpTargetToMap(targetItemIntf vpcv1.FloatingIPTargetIntf) (targetId string, targetMap map[string]interface{}) {
targetMap = map[string]interface{}{}
targetId = ""
switch reflect.TypeOf(targetItemIntf).String() {
case "*vpcv1.FloatingIPTargetNetworkInterfaceReference":
{
targetItem := targetItemIntf.(*vpcv1.FloatingIPTargetNetworkInterfaceReference)
targetId = *targetItem.ID
if targetItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceFloatingIPTargetNicDeletedToMap(*targetItem.Deleted)
deletedList = append(deletedList, deletedMap)
targetMap[floatingIPTargetsDeleted] = deletedList
}
if targetItem.Href != nil {
targetMap[floatingIPTargetsHref] = targetItem.Href
}
if targetItem.ID != nil {
targetMap[floatingIPTargetsId] = targetItem.ID
}
if targetItem.Name != nil {
targetMap[floatingIPTargetsName] = targetItem.Name
}
if targetItem.PrimaryIP != nil {
primaryIpList := make([]map[string]interface{}, 0)
currentIP := map[string]interface{}{}
if targetItem.PrimaryIP.Address != nil {
currentIP[floatingIpPrimaryIpAddress] = *targetItem.PrimaryIP.Address
}
if targetItem.PrimaryIP.Href != nil {
currentIP[floatingIpPrimaryIpHref] = *targetItem.PrimaryIP.Href
}
if targetItem.PrimaryIP.Name != nil {
currentIP[floatingIpPrimaryIpName] = *targetItem.PrimaryIP.Name
}
if targetItem.PrimaryIP.ID != nil {
currentIP[floatingIpPrimaryIpId] = *targetItem.PrimaryIP.ID
}
if targetItem.PrimaryIP.ResourceType != nil {
currentIP[floatingIpPrimaryIpResourceType] = *targetItem.PrimaryIP.ResourceType
}
primaryIpList = append(primaryIpList, currentIP)
targetMap[floatingIpPrimaryIP] = primaryIpList
}
if targetItem.ResourceType != nil {
targetMap[floatingIPTargetsResourceType] = targetItem.ResourceType
}
}
case "*vpcv1.FloatingIPTargetPublicGatewayReference":
{
targetItem := targetItemIntf.(*vpcv1.FloatingIPTargetPublicGatewayReference)
targetId = *targetItem.ID
if targetItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceFloatingIPTargetPgDeletedToMap(*targetItem.Deleted)
deletedList = append(deletedList, deletedMap)
targetMap[floatingIPTargetsDeleted] = deletedList
}
if targetItem.Href != nil {
targetMap[floatingIPTargetsHref] = targetItem.Href
}
if targetItem.ID != nil {
targetMap[floatingIPTargetsId] = targetItem.ID
}
if targetItem.Name != nil {
targetMap[floatingIPTargetsName] = targetItem.Name
}
if targetItem.ResourceType != nil {
targetMap[floatingIPTargetsResourceType] = targetItem.ResourceType
}
if targetItem.CRN != nil {
targetMap[floatingIPTargetsCrn] = targetItem.CRN
}
}
case "*vpcv1.FloatingIPTarget":
{
targetItem := targetItemIntf.(*vpcv1.FloatingIPTarget)
targetId = *targetItem.ID
if targetItem.Deleted != nil {
deletedList := []map[string]interface{}{}
deletedMap := dataSourceFloatingIPTargetNicDeletedToMap(*targetItem.Deleted)
deletedList = append(deletedList, deletedMap)
targetMap[floatingIPTargetsDeleted] = deletedList
}
if targetItem.Href != nil {
targetMap[floatingIPTargetsHref] = targetItem.Href
}
if targetItem.ID != nil {
targetMap[floatingIPTargetsId] = targetItem.ID
}
if targetItem.Name != nil {
targetMap[floatingIPTargetsName] = targetItem.Name
}
if targetItem.PrimaryIP != nil && targetItem.PrimaryIP.Address != nil {
primaryIpList := make([]map[string]interface{}, 0)
currentIP := map[string]interface{}{}
if targetItem.PrimaryIP.Address != nil {
currentIP[floatingIpPrimaryIpAddress] = *targetItem.PrimaryIP.Address
}
if targetItem.PrimaryIP.Href != nil {
currentIP[floatingIpPrimaryIpHref] = *targetItem.PrimaryIP.Href
}
if targetItem.PrimaryIP.Name != nil {
currentIP[floatingIpPrimaryIpName] = *targetItem.PrimaryIP.Name
}
if targetItem.PrimaryIP.ID != nil {
currentIP[floatingIpPrimaryIpId] = *targetItem.PrimaryIP.ID
}
if targetItem.PrimaryIP.ResourceType != nil {
currentIP[floatingIpPrimaryIpResourceType] = *targetItem.PrimaryIP.ResourceType
}
primaryIpList = append(primaryIpList, currentIP)
targetMap[floatingIpPrimaryIP] = primaryIpList
}
if targetItem.ResourceType != nil {
targetMap[floatingIPTargetsResourceType] = targetItem.ResourceType
}
if targetItem.CRN != nil {
targetMap[floatingIPTargetsCrn] = targetItem.CRN
}
}
}
return targetId, targetMap
}
func dataSourceFloatingIPTargetNicDeletedToMap(deletedItem vpcv1.NetworkInterfaceReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap[floatingIPTargetsMoreInfo] = deletedItem.MoreInfo
}
return deletedMap
}
func dataSourceFloatingIPTargetPgDeletedToMap(deletedItem vpcv1.PublicGatewayReferenceDeleted) (deletedMap map[string]interface{}) {
deletedMap = map[string]interface{}{}
if deletedItem.MoreInfo != nil {
deletedMap[floatingIPTargetsMoreInfo] = deletedItem.MoreInfo
}
return deletedMap
}