-
Notifications
You must be signed in to change notification settings - Fork 650
/
data_source_ibm_is_virtual_network_interface_floating_ips.go
134 lines (122 loc) · 4.3 KB
/
data_source_ibm_is_virtual_network_interface_floating_ips.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
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"time"
"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/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func DataSourceIBMIsVirtualNetworkInterfaceFloatingIPs() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIsVirtualNetworkInterfaceFloatingIPsRead,
Schema: map[string]*schema.Schema{
"virtual_network_interface": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The virtual network interface identifier",
},
"floating_ips": {
Type: schema.TypeList,
Description: "List of floating ips associated with the virtual network interface id",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The floating IP identifier",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name for this floating IP. The name is unique across all floating IPs in the region.",
},
"deleted": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted, and provides some supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources",
},
},
},
},
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The globally unique IP address.",
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this floating IP.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The URL for this floating IP.",
},
},
},
},
},
}
}
func dataSourceIBMIsVirtualNetworkInterfaceFloatingIPsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := vpcClient(meta)
if err != nil {
return diag.FromErr(err)
}
vniId := d.Get("virtual_network_interface").(string)
start := ""
allrecs := []vpcv1.FloatingIPReference{}
for {
listNetworkInterfaceFloatingIpsOptions := &vpcv1.ListNetworkInterfaceFloatingIpsOptions{}
listNetworkInterfaceFloatingIpsOptions.SetVirtualNetworkInterfaceID(vniId)
if start != "" {
listNetworkInterfaceFloatingIpsOptions.Start = &start
}
floatingIPCollection, response, err := sess.ListNetworkInterfaceFloatingIpsWithContext(context, listNetworkInterfaceFloatingIpsOptions)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error ListNetworkInterfaceFloatingIpsWithContext %s\n%s", err, response))
}
start = flex.GetNext(floatingIPCollection.Next)
allrecs = append(allrecs, floatingIPCollection.FloatingIps...)
if start == "" {
break
}
}
floatingIpsInfo := make([]map[string]interface{}, 0)
for _, floatingIP := range allrecs {
l := map[string]interface{}{}
l["id"] = *floatingIP.ID
if !core.IsNil(floatingIP.Name) {
l["name"] = floatingIP.Name
}
l["address"] = floatingIP.Address
l["crn"] = floatingIP.CRN
l["href"] = floatingIP.Href
deleted := make(map[string]interface{})
if floatingIP.Deleted != nil && floatingIP.Deleted.MoreInfo != nil {
deleted["more_info"] = floatingIP.Deleted
}
l["deleted"] = []map[string]interface{}{deleted}
floatingIpsInfo = append(floatingIpsInfo, l)
}
d.SetId(dataSourceIBMISVirtualNetworkInterfaceFloatingIPsID(d))
d.Set("floating_ips", floatingIpsInfo)
return nil
}
// dataSourceIBMISVirtualNetworkInterfaceFloatingIPsID returns a reasonable ID for a Virtual Network Interface FloatingIPs ID list.
func dataSourceIBMISVirtualNetworkInterfaceFloatingIPsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}