-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_bare_metal_server_network_interface_floating_ip.go
112 lines (95 loc) · 3.06 KB
/
data_source_ibm_is_bare_metal_server_network_interface_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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isBareMetalServerNetworkInterfaceFloatingIPID = "floating_ip"
)
func DataSourceIBMIsBareMetalServerNetworkInterfaceFloatingIP() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISBareMetalServerNetworkInterfaceFloatingIPRead,
Schema: map[string]*schema.Schema{
isBareMetalServerID: {
Type: schema.TypeString,
Required: true,
Description: "The bare metal server identifier",
},
isBareMetalServerNetworkInterface: {
Type: schema.TypeString,
Required: true,
Description: "The network interface identifier of bare metal server",
},
isBareMetalServerNetworkInterfaceFloatingIPID: {
Type: schema.TypeString,
Required: true,
Description: "The floating ip identifier of the network interface associated with the bare metal server",
},
floatingIPName: {
Type: schema.TypeString,
Computed: 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",
},
floatingIPCRN: {
Type: schema.TypeString,
Computed: true,
Description: "Floating IP crn",
},
},
}
}
func dataSourceIBMISBareMetalServerNetworkInterfaceFloatingIPRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
bareMetalServerID := d.Get(isBareMetalServerID).(string)
nicID := d.Get(isBareMetalServerNetworkInterface).(string)
fipID := d.Get(isBareMetalServerNetworkInterfaceFloatingIPID).(string)
sess, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
options := &vpcv1.GetBareMetalServerNetworkInterfaceFloatingIPOptions{
BareMetalServerID: &bareMetalServerID,
NetworkInterfaceID: &nicID,
ID: &fipID,
}
ip, response, err := sess.GetBareMetalServerNetworkInterfaceFloatingIPWithContext(context, options)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error fetching floating IP for bare metal server %s\n%s", err, response))
}
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)
target, ok := ip.Target.(*vpcv1.FloatingIPTarget)
if ok {
d.Set(floatingIPTarget, target.ID)
}
d.SetId(*ip.ID)
return nil
}