-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_vpc_address_prefixes.go
262 lines (226 loc) · 8.05 KB
/
data_source_ibm_is_vpc_address_prefixes.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
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"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/vpc-go-sdk/vpcv1"
)
func DataSourceIbmIsVpcAddressPrefixes() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmIsVpcAddressPrefixRead,
Schema: map[string]*schema.Schema{
"vpc": {
Type: schema.TypeString,
Required: true,
Description: "The VPC identifier.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The user-defined name for this address prefix. Names must be unique within the VPC the address prefix resides in.",
},
"address_prefixes": {
Type: schema.TypeList,
Computed: true,
Description: "Collection of address prefixes.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr": {
Type: schema.TypeString,
Computed: true,
Description: "The CIDR block for this prefix.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the prefix was created.",
},
"has_subnets": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether subnets exist with addresses from this prefix.",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this address prefix.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this address prefix.",
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether this is the default prefix for this zone in this VPC. If a default prefix was automatically created when the VPC was created, the prefix is automatically named using a hyphenated list of randomly-selected words, but may be updated with a user-specified name.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this address prefix. Names must be unique within the VPC the address prefix resides in.",
},
"zone": {
Type: schema.TypeList,
Computed: true,
Description: "The zone this address prefix resides in.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this zone.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for this zone.",
},
},
},
},
},
},
},
},
}
}
func dataSourceIbmIsVpcAddressPrefixRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
start := ""
allrecs := []vpcv1.AddressPrefix{}
for {
listVpcAddressPrefixesOptions := &vpcv1.ListVPCAddressPrefixesOptions{}
listVpcAddressPrefixesOptions.SetVPCID(d.Get("vpc").(string))
if start != "" {
listVpcAddressPrefixesOptions.Start = &start
}
addressPrefixCollection, response, err := vpcClient.ListVPCAddressPrefixesWithContext(context, listVpcAddressPrefixesOptions)
if err != nil {
log.Printf("[DEBUG] ListVpcAddressPrefixesWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListVpcAddressPrefixesWithContext failed %s\n%s", err, response))
}
start = flex.GetNext(addressPrefixCollection.Next)
allrecs = append(allrecs, addressPrefixCollection.AddressPrefixes...)
if start == "" {
break
}
}
// Use the provided filter argument and construct a new list with only the requested resource(s)
var matchAddressPrefixes []vpcv1.AddressPrefix
var name string
var suppliedFilter bool
if v, ok := d.GetOk("name"); ok {
name = v.(string)
suppliedFilter = true
for _, data := range allrecs {
if *data.Name == name {
matchAddressPrefixes = append(matchAddressPrefixes, data)
}
}
} else {
matchAddressPrefixes = allrecs
}
if suppliedFilter {
if len(matchAddressPrefixes) == 0 {
return diag.FromErr(fmt.Errorf("no AddressPrefixes found with name %s", name))
}
d.SetId(name)
} else {
d.SetId(dataSourceIbmIsVpcAddressPrefixID(d))
}
if matchAddressPrefixes != nil {
err = d.Set("address_prefixes", dataSourceAddressPrefixCollectionFlattenAddressPrefixes(matchAddressPrefixes))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting address_prefixes %s", err))
}
}
return nil
}
// dataSourceIbmIsVpcAddressPrefixID returns a reasonable ID for the list.
func dataSourceIbmIsVpcAddressPrefixID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceAddressPrefixCollectionFlattenAddressPrefixes(result []vpcv1.AddressPrefix) (addressPrefixes []map[string]interface{}) {
for _, addressPrefixesItem := range result {
addressPrefixes = append(addressPrefixes, dataSourceAddressPrefixCollectionAddressPrefixesToMap(addressPrefixesItem))
}
return addressPrefixes
}
func dataSourceAddressPrefixCollectionAddressPrefixesToMap(addressPrefixesItem vpcv1.AddressPrefix) (addressPrefixesMap map[string]interface{}) {
addressPrefixesMap = map[string]interface{}{}
if addressPrefixesItem.CIDR != nil {
addressPrefixesMap["cidr"] = addressPrefixesItem.CIDR
}
if addressPrefixesItem.CreatedAt != nil {
addressPrefixesMap["created_at"] = addressPrefixesItem.CreatedAt.String()
}
if addressPrefixesItem.HasSubnets != nil {
addressPrefixesMap["has_subnets"] = addressPrefixesItem.HasSubnets
}
if addressPrefixesItem.Href != nil {
addressPrefixesMap["href"] = addressPrefixesItem.Href
}
if addressPrefixesItem.ID != nil {
addressPrefixesMap["id"] = addressPrefixesItem.ID
}
if addressPrefixesItem.IsDefault != nil {
addressPrefixesMap["is_default"] = addressPrefixesItem.IsDefault
}
if addressPrefixesItem.Name != nil {
addressPrefixesMap["name"] = addressPrefixesItem.Name
}
if addressPrefixesItem.Zone != nil {
zoneList := []map[string]interface{}{}
zoneMap := dataSourceAddressPrefixCollectionAddressPrefixesZoneToMap(*addressPrefixesItem.Zone)
zoneList = append(zoneList, zoneMap)
addressPrefixesMap["zone"] = zoneList
}
return addressPrefixesMap
}
func dataSourceAddressPrefixCollectionAddressPrefixesZoneToMap(zoneItem vpcv1.ZoneReference) (zoneMap map[string]interface{}) {
zoneMap = map[string]interface{}{}
if zoneItem.Href != nil {
zoneMap["href"] = zoneItem.Href
}
if zoneItem.Name != nil {
zoneMap["name"] = zoneItem.Name
}
return zoneMap
}
func dataSourceAddressPrefixCollectionFlattenFirst(result vpcv1.AddressPrefixCollectionFirst) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceAddressPrefixCollectionFirstToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceAddressPrefixCollectionFirstToMap(firstItem vpcv1.AddressPrefixCollectionFirst) (firstMap map[string]interface{}) {
firstMap = map[string]interface{}{}
if firstItem.Href != nil {
firstMap["href"] = firstItem.Href
}
return firstMap
}
func dataSourceAddressPrefixCollectionFlattenNext(result vpcv1.AddressPrefixCollectionNext) (finalList []map[string]interface{}) {
finalList = []map[string]interface{}{}
finalMap := dataSourceAddressPrefixCollectionNextToMap(result)
finalList = append(finalList, finalMap)
return finalList
}
func dataSourceAddressPrefixCollectionNextToMap(nextItem vpcv1.AddressPrefixCollectionNext) (nextMap map[string]interface{}) {
nextMap = map[string]interface{}{}
if nextItem.Href != nil {
nextMap["href"] = nextItem.Href
}
return nextMap
}