-
Notifications
You must be signed in to change notification settings - Fork 650
/
data_source_ibm_is_bare_metal_server_network_interface_reserved_ips.go
162 lines (150 loc) · 5.39 KB
/
data_source_ibm_is_bare_metal_server_network_interface_reserved_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
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
// Copyright IBM Corp. 2017, 2021 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/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"
)
// Define all the constants that matches with the given terrafrom attribute
const (
// Request Param Constants
isBareMetalServerNICReservedIPLimit = "limit"
isBareMetalServerNICReservedIPSort = "sort"
isBareMetalServerNICReservedIPs = "reserved_ips"
isBareMetalServerNICReservedIPsCount = "total_count"
)
func DataSourceIBMISBareMetalServerNICReservedIPs() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISBareMetalServerNICReservedIPsRead,
Schema: map[string]*schema.Schema{
/*
Request Parameters
==================
These are mandatory req parameters
*/
isBareMetalServerID: {
Type: schema.TypeString,
Required: true,
Description: "The BareMetalServer identifier.",
},
isBareMetalServerNicID: {
Type: schema.TypeString,
Required: true,
Description: "The BareMetalServer network interface identifier.",
},
/*
Response Parameters
===================
All of these are computed and an user doesn't need to provide
these from outside.
*/
isBareMetalServerNICReservedIPs: {
Type: schema.TypeList,
Description: "Collection of all reserved IPs bound to a network interface.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerNicIpAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The IP address",
},
isBareMetalServerNicIpAutoDelete: {
Type: schema.TypeBool,
Computed: true,
Description: "If reserved ip shall be deleted automatically",
},
isBareMetalServerNICReservedIPCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the reserved IP was created.",
},
isBareMetalServerNICReservedIPhref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP.",
},
isBareMetalServerNicIpID: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this reserved IP",
},
isBareMetalServerNicIpName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined or system-provided name for this reserved IP.",
},
isBareMetalServerNICReservedIPOwner: {
Type: schema.TypeString,
Computed: true,
Description: "The owner of a reserved IP, defining whether it is managed by the user or the provider.",
},
isBareMetalServerNICReservedIPType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
isBareMetalServerNICReservedIPTarget: {
Type: schema.TypeString,
Computed: true,
Description: "Reserved IP target id",
},
},
},
},
isBareMetalServerNICReservedIPsCount: {
Type: schema.TypeInt,
Computed: true,
Description: "The total number of resources across all pages",
},
},
}
}
func dataSourceIBMISBareMetalServerNICReservedIPsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
bareMetalServerID := d.Get(isBareMetalServerID).(string)
nicID := d.Get(isBareMetalServerNicID).(string)
// Flatten all the reserved IPs
allrecs := []vpcv1.ReservedIP{}
options := &vpcv1.ListBareMetalServerNetworkInterfaceIpsOptions{
BareMetalServerID: &bareMetalServerID,
NetworkInterfaceID: &nicID,
}
result, response, err := sess.ListBareMetalServerNetworkInterfaceIpsWithContext(context, options)
if err != nil || response == nil || result == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error fetching reserved ips %s\n%s", err, response))
}
allrecs = append(allrecs, result.Ips...)
// Now store all the reserved IP info with their response tags
reservedIPs := []map[string]interface{}{}
for _, data := range allrecs {
ipsOutput := map[string]interface{}{}
ipsOutput[isBareMetalServerNicIpAddress] = *data.Address
ipsOutput[isBareMetalServerNicIpAutoDelete] = *data.AutoDelete
ipsOutput[isBareMetalServerNICReservedIPCreatedAt] = (*data.CreatedAt).String()
ipsOutput[isBareMetalServerNICReservedIPhref] = *data.Href
ipsOutput[isBareMetalServerNicIpID] = *data.ID
ipsOutput[isBareMetalServerNicIpName] = *data.Name
ipsOutput[isBareMetalServerNICReservedIPOwner] = *data.Owner
ipsOutput[isBareMetalServerNICReservedIPType] = *data.ResourceType
target, ok := data.Target.(*vpcv1.ReservedIPTarget)
if ok {
ipsOutput[isReservedIPTarget] = target.ID
}
reservedIPs = append(reservedIPs, ipsOutput)
}
d.SetId(time.Now().UTC().String()) // This is not any reserved ip or BareMetalServer id but state id
d.Set(isBareMetalServerNICReservedIPs, reservedIPs)
d.Set(isBareMetalServerNICReservedIPsCount, len(reservedIPs))
d.Set(isBareMetalServerID, bareMetalServerID)
d.Set(isBareMetalServerNicID, nicID)
return nil
}