-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_bare_metal_server_network_interfaces.go
312 lines (291 loc) · 11.9 KB
/
data_source_ibm_is_bare_metal_server_network_interfaces.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"reflect"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMIsBareMetalServerNetworkInterfaces() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISBareMetalServerNetworkInterfacesRead,
Schema: map[string]*schema.Schema{
isBareMetalServerID: {
Type: schema.TypeString,
Required: true,
Description: "The bare metal server identifier",
},
//network interface properties
isBareMetalServerNetworkInterfaces: {
Type: schema.TypeList,
Description: "A list of all network interfaces on a bare metal server. A network interface is an abstract representation of a network interface card and connects a bare metal server to a subnet. While each network interface can attach to only one subnet, multiple network interfaces can be created to attach to multiple subnets. Multiple interfaces may also attach to the same subnet.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerNicAllowIPSpoofing: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether source IP spoofing is allowed on this interface. If false, source IP spoofing is prevented on this interface. If true, source IP spoofing is allowed on this interface.",
},
isBareMetalServerNicEnableInfraNAT: {
Type: schema.TypeBool,
Computed: true,
Description: "If true, the VPC infrastructure performs any needed NAT operations. If false, the packet is passed unmodified to/from the network interface, allowing the workload to perform any needed NAT operations.",
},
isBareMetalServerNicFloatingIPs: {
Type: schema.TypeList,
Computed: true,
Description: "The floating IPs associated with this network interface.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerNicIpAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique IP address",
},
isBareMetalServerNicIpCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this floating IP",
},
isBareMetalServerNicIpHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this floating IP",
},
isBareMetalServerNicIpID: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this floating IP",
},
isBareMetalServerNicIpName: {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this floating IP",
},
},
},
},
isBareMetalServerNicHref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this network interface",
},
isBareMetalServerNicInterfaceType: {
Type: schema.TypeString,
Computed: true,
Description: "The network interface type: [ pci, vlan ]",
},
isBareMetalServerNicMacAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The MAC address of the interface. If absent, the value is not known.",
},
isBareMetalServerNicName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this network interface",
},
isBareMetalServerNicPortSpeed: {
Type: schema.TypeInt,
Computed: true,
Description: "The network interface port speed in Mbps",
},
isBareMetalServerNicPrimaryIP: {
Type: schema.TypeList,
Computed: true,
Description: "title: IPv4, The IP address. ",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerNicIpAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique IP address",
},
},
},
},
isBareMetalServerNicResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type : [ subnet_reserved_ip ]",
},
isBareMetalServerNicSecurityGroups: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Collection of security groups ids",
},
isBareMetalServerNicStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the network interface : [ available, deleting, failed, pending ]",
},
isBareMetalServerNicSubnet: {
Type: schema.TypeString,
Computed: true,
Description: "The id of the associated subnet",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The id of the network interface",
},
isBareMetalServerNicType: {
Type: schema.TypeString,
Computed: true,
Description: "The type of this bare metal server network interface : [ primary, secondary ]",
},
isBareMetalServerNicAllowedVlans: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeInt},
Set: schema.HashInt,
Description: "Indicates what VLAN IDs (for VLAN type only) can use this physical (PCI type) interface. A given VLAN can only be in the allowed_vlans array for one PCI type adapter per bare metal server.",
},
isBareMetalServerNicAllowInterfaceToFloat: {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates if the interface can float to any other server within the same resource_group. The interface will float automatically if the network detects a GARP or RARP on another bare metal server in the resource group. Applies only to vlan type interfaces.",
},
isBareMetalServerNicVlan: {
Type: schema.TypeInt,
Computed: true,
Description: "Indicates the 802.1Q VLAN ID tag that must be used for all traffic on this interface",
},
},
},
},
},
}
}
func dataSourceIBMISBareMetalServerNetworkInterfacesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
bareMetalServerID := d.Get(isBareMetalServerID).(string)
sess, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
options := &vpcv1.ListBareMetalServerNetworkInterfacesOptions{
BareMetalServerID: &bareMetalServerID,
}
nics := []vpcv1.BareMetalServerNetworkInterfaceIntf{}
bmsNics, response, err := sess.ListBareMetalServerNetworkInterfacesWithContext(context, options)
if err != nil || bmsNics == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error listing Bare Metal Server (%s) network interfaces : %s\n%s", bareMetalServerID, err, response))
}
nics = append(nics, bmsNics.NetworkInterfaces...)
nicsInfo := make([]map[string]interface{}, 0)
for _, nicIntf := range nics {
l := map[string]interface{}{}
switch reflect.TypeOf(nicIntf).String() {
case "*vpcv1.BareMetalServerNetworkInterfaceByPci":
{
nic := nicIntf.(*vpcv1.BareMetalServerNetworkInterfaceByPci)
l[isBareMetalServerNicAllowIPSpoofing] = *nic.AllowIPSpoofing
l[isBareMetalServerNicEnableInfraNAT] = *nic.EnableInfrastructureNat
if nic.FloatingIps != nil {
floatingIPList := make([]map[string]interface{}, 0)
for _, ip := range nic.FloatingIps {
currentIP := map[string]interface{}{
isBareMetalServerNicIpID: *ip.ID,
isBareMetalServerNicIpAddress: *ip.Address,
}
floatingIPList = append(floatingIPList, currentIP)
}
l[isBareMetalServerNicFloatingIPs] = floatingIPList
}
l[isBareMetalServerNicHref] = *nic.Href
l[isBareMetalServerNicInterfaceType] = *nic.InterfaceType
l[isBareMetalServerNicMacAddress] = *nic.MacAddress
l[isBareMetalServerNicName] = *nic.Name
if nic.PortSpeed != nil {
l[isBareMetalServerNicPortSpeed] = *nic.PortSpeed
}
primaryIpList := make([]map[string]interface{}, 0)
currentIP := map[string]interface{}{
isBareMetalServerNicIpAddress: *nic.PrimaryIpv4Address,
}
primaryIpList = append(primaryIpList, currentIP)
l[isBareMetalServerNicPrimaryIP] = primaryIpList
l[isBareMetalServerNicResourceType] = *nic.ResourceType
if nic.SecurityGroups != nil && len(nic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(nic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(nic.SecurityGroups[i].ID)))
}
l[isBareMetalServerNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
l[isBareMetalServerNicStatus] = *nic.Status
l[isBareMetalServerNicSubnet] = *nic.Subnet.ID
l[isBareMetalServerNicType] = *nic.Type
l["id"] = *nic.ID
if nic.AllowedVlans != nil {
var out = make([]interface{}, len(nic.AllowedVlans), len(nic.AllowedVlans))
for i, v := range nic.AllowedVlans {
out[i] = int(v)
}
l[isBareMetalServerNicAllowedVlans] = schema.NewSet(schema.HashInt, out)
}
}
case "*vpcv1.BareMetalServerNetworkInterfaceByVlan":
{
nic := nicIntf.(*vpcv1.BareMetalServerNetworkInterfaceByVlan)
l[isBareMetalServerNicAllowIPSpoofing] = *nic.AllowIPSpoofing
l[isBareMetalServerNicEnableInfraNAT] = *nic.EnableInfrastructureNat
if nic.FloatingIps != nil {
floatingIPList := make([]map[string]interface{}, 0)
for _, ip := range nic.FloatingIps {
currentIP := map[string]interface{}{
isBareMetalServerNicIpID: *ip.ID,
isBareMetalServerNicIpAddress: *ip.Address,
}
floatingIPList = append(floatingIPList, currentIP)
}
l[isBareMetalServerNicFloatingIPs] = floatingIPList
}
l[isBareMetalServerNicHref] = *nic.Href
l[isBareMetalServerNicInterfaceType] = *nic.InterfaceType
l[isBareMetalServerNicMacAddress] = *nic.MacAddress
l[isBareMetalServerNicName] = *nic.Name
if nic.PortSpeed != nil {
l[isBareMetalServerNicPortSpeed] = *nic.PortSpeed
}
primaryIpList := make([]map[string]interface{}, 0)
currentIP := map[string]interface{}{
isBareMetalServerNicIpAddress: *nic.PrimaryIpv4Address,
}
primaryIpList = append(primaryIpList, currentIP)
l[isBareMetalServerNicPrimaryIP] = primaryIpList
l[isBareMetalServerNicResourceType] = *nic.ResourceType
if nic.SecurityGroups != nil && len(nic.SecurityGroups) != 0 {
secgrpList := []string{}
for i := 0; i < len(nic.SecurityGroups); i++ {
secgrpList = append(secgrpList, string(*(nic.SecurityGroups[i].ID)))
}
l[isBareMetalServerNicSecurityGroups] = flex.NewStringSet(schema.HashString, secgrpList)
}
l[isBareMetalServerNicStatus] = *nic.Status
l[isBareMetalServerNicSubnet] = *nic.Subnet.ID
l[isBareMetalServerNicType] = *nic.Type
l["id"] = *nic.ID
l[isBareMetalServerNicAllowInterfaceToFloat] = *nic.AllowInterfaceToFloat
l[isBareMetalServerNicVlan] = *nic.Vlan
}
}
nicsInfo = append(nicsInfo, l)
}
d.SetId(dataSourceIBMISBareMetalServerNetworkInterfacesID(d))
d.Set(isBareMetalServerNetworkInterfaces, nicsInfo)
return nil
}
// dataSourceIBMISBMSProfilesID returns a reasonable ID for a BMS Profile list.
func dataSourceIBMISBareMetalServerNetworkInterfacesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}