-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_is_instance_network_interface_reserved_ips.go
172 lines (161 loc) · 5.35 KB
/
data_source_ibm_is_instance_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
163
164
165
166
167
168
169
170
171
172
// 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-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"
)
// Define all the constants that matches with the given terrafrom attribute
const (
// Request Param Constants
isInstanceNICReservedIPLimit = "limit"
isInstanceNICReservedIPSort = "sort"
isInstanceNICReservedIPs = "reserved_ips"
isInstanceNICReservedIPsCount = "total_count"
)
func DataSourceIBMISInstanceNICReservedIPs() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISInstanceNICReservedIPsRead,
Schema: map[string]*schema.Schema{
/*
Request Parameters
==================
These are mandatory req parameters
*/
isInstanceID: {
Type: schema.TypeString,
Required: true,
Description: "The instance identifier.",
},
isInstanceNICID: {
Type: schema.TypeString,
Required: true,
Description: "The instance network interface identifier.",
},
/*
Response Parameters
===================
All of these are computed and an user doesn't need to provide
these from outside.
*/
isInstanceNICReservedIPs: {
Type: schema.TypeList,
Description: "Collection of reserved IPs in this subnet.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceNICReservedIPAddress: {
Type: schema.TypeString,
Computed: true,
Description: "The IP address",
},
isInstanceNICReservedIPAutoDelete: {
Type: schema.TypeBool,
Computed: true,
Description: "If reserved ip shall be deleted automatically",
},
isInstanceNICReservedIPCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the reserved IP was created.",
},
isInstanceNICReservedIPhref: {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this reserved IP.",
},
isInstanceNICReservedIPID: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this reserved IP",
},
isInstanceNICReservedIPName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined or system-provided name for this reserved IP.",
},
isInstanceNICReservedIPOwner: {
Type: schema.TypeString,
Computed: true,
Description: "The owner of a reserved IP, defining whether it is managed by the user or the provider.",
},
isInstanceNICReservedIPType: {
Type: schema.TypeString,
Computed: true,
Description: "The resource type.",
},
isInstanceNICReservedIPTarget: {
Type: schema.TypeString,
Computed: true,
Description: "Reserved IP target id",
},
},
},
},
isInstanceNICReservedIPsCount: {
Type: schema.TypeInt,
Computed: true,
Description: "The total number of resources across all pages",
},
},
}
}
func dataSourceIBMISInstanceNICReservedIPsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
instanceID := d.Get(isInstanceID).(string)
nicID := d.Get(isInstanceNICID).(string)
// Flatten all the reserved IPs
start := ""
allrecs := []vpcv1.ReservedIP{}
for {
options := &vpcv1.ListInstanceNetworkInterfaceIpsOptions{
InstanceID: &instanceID,
NetworkInterfaceID: &nicID,
}
if start != "" {
options.Start = &start
}
result, response, err := sess.ListInstanceNetworkInterfaceIpsWithContext(context, options)
if err != nil || response == nil || result == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error fetching reserved ips %s\n%s", err, response))
}
start = flex.GetNext(result.Next)
allrecs = append(allrecs, result.Ips...)
if start == "" {
break
}
}
// Now store all the reserved IP info with their response tags
reservedIPs := []map[string]interface{}{}
for _, data := range allrecs {
ipsOutput := map[string]interface{}{}
ipsOutput[isInstanceNICReservedIPAddress] = *data.Address
ipsOutput[isInstanceNICReservedIPAutoDelete] = *data.AutoDelete
ipsOutput[isInstanceNICReservedIPCreatedAt] = (*data.CreatedAt).String()
ipsOutput[isInstanceNICReservedIPhref] = *data.Href
ipsOutput[isInstanceNICReservedIPID] = *data.ID
ipsOutput[isInstanceNICReservedIPName] = *data.Name
ipsOutput[isInstanceNICReservedIPOwner] = *data.Owner
ipsOutput[isInstanceNICReservedIPType] = *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 instance id but state id
d.Set(isInstanceNICReservedIPs, reservedIPs)
d.Set(isInstanceNICReservedIPsCount, len(reservedIPs))
d.Set(isInstanceID, instanceID)
d.Set(isInstanceNICID, nicID)
return nil
}