/
data_source_aviatrix_vpc.go
374 lines (342 loc) · 10.1 KB
/
data_source_aviatrix_vpc.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package aviatrix
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v3/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceAviatrixVpc() *schema.Resource {
return &schema.Resource{
Read: dataSourceAviatrixVpcRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the VPC created.",
},
"cloud_type": {
Type: schema.TypeInt,
Computed: true,
Description: "Type of cloud service provider.",
},
"account_name": {
Type: schema.TypeString,
Computed: true,
Description: "Account name of the VPC created.",
},
"region": {
Type: schema.TypeString,
Computed: true,
Description: "Region of the VPC created.",
},
"cidr": {
Type: schema.TypeString,
Computed: true,
Description: "Subnet of the VPC created.",
},
"subnet_size": {
Type: schema.TypeInt,
Computed: true,
Description: "Subnet size.",
},
"num_of_subnet_pairs": {
Type: schema.TypeInt,
Computed: true,
Description: "Number of public subnet and private subnet pair created.",
},
"aviatrix_transit_vpc": {
Type: schema.TypeBool,
Computed: true,
Description: "Switch if the VPC created is an Aviatrix Transit VPC or not.",
},
"aviatrix_firenet_vpc": {
Type: schema.TypeBool,
Computed: true,
Description: "Switch if the VPC created is an Aviatrix FireNet VPC or not.",
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the VPC created.",
},
"resource_group": {
Type: schema.TypeString,
Computed: true,
Description: "Resource group of the Azure VPC created.",
},
"subnets": {
Type: schema.TypeList,
Computed: true,
Description: "List of subnet of the VPC to be created.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr": {
Type: schema.TypeString,
Computed: true,
Description: "Subnet cidr.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Subnet name.",
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
Description: "Subnet ID.",
},
},
},
},
"public_subnets": {
Type: schema.TypeList,
Computed: true,
Description: "List of public subnet of the VPC to be created.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr": {
Type: schema.TypeString,
Computed: true,
Description: "Public subnet cidr.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Public subnet name.",
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
Description: "Public subnet ID.",
},
},
},
},
"private_subnets": {
Type: schema.TypeList,
Computed: true,
Description: "List of private subnet of the VPC to be created.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr": {
Type: schema.TypeString,
Computed: true,
Description: "Private subnet cidr.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Private subnet name.",
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
Description: "Private subnet ID.",
},
},
},
},
"route_tables": {
Type: schema.TypeList,
Computed: true,
Description: "List of AWS route table ids associated with this VPC. Only populated for AWS, AWSGov and Azure vpc.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"route_tables_filter": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"private", "public"}, false),
Description: "Filters the route_tables list to contain only public or private route tables. " +
"Valid values are 'private' or 'public'. If not set then route_tables are not filtered.",
},
"azure_vnet_resource_id": {
Type: schema.TypeString,
Computed: true,
Description: "Azure vnet resource ID.",
},
"availability_domains": {
Type: schema.TypeList,
Computed: true,
Description: "List of OCI availability domains.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"fault_domains": {
Type: schema.TypeList,
Computed: true,
Description: "List of OCI fault domains.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceAviatrixVpcRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
vpc := &goaviatrix.Vpc{
Name: d.Get("name").(string),
}
vC, err := client.GetVpc(vpc)
if err != nil {
if err == goaviatrix.ErrNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't find VPC: %s", err)
}
d.Set("cloud_type", vC.CloudType)
d.Set("account_name", vC.AccountName)
d.Set("region", vC.Region)
d.Set("name", vC.Name)
d.Set("cidr", vC.Cidr)
if vC.SubnetSize != 0 {
d.Set("subnet_size", vC.SubnetSize)
}
if vC.NumOfSubnetPairs != 0 {
d.Set("num_of_subnet_pairs", vC.NumOfSubnetPairs)
}
if vC.AviatrixTransitVpc == "yes" {
d.Set("aviatrix_transit_vpc", true)
} else {
d.Set("aviatrix_transit_vpc", false)
}
if vC.AviatrixFireNetVpc == "yes" {
d.Set("aviatrix_firenet_vpc", true)
} else {
d.Set("aviatrix_firenet_vpc", false)
}
if goaviatrix.IsCloudType(vC.CloudType, goaviatrix.GCPRelatedCloudTypes) {
d.Set("vpc_id", strings.Split(vC.VpcID, "~-~")[0])
} else {
d.Set("vpc_id", vC.VpcID)
}
if goaviatrix.IsCloudType(vC.CloudType, goaviatrix.AzureArmRelatedCloudTypes) {
account := &goaviatrix.Account{
AccountName: d.Get("account_name").(string),
}
acc, err := client.GetAccount(account)
if err != nil {
if err != goaviatrix.ErrNotFound {
return fmt.Errorf("aviatrix Account: %s", err)
}
}
var subscriptionId string
if acc != nil {
subscriptionId = acc.ArmSubscriptionId
}
var resourceGroup string
if vC.ResourceGroup != "" {
resourceGroup = vC.ResourceGroup
} else {
resourceGroup = strings.Split(vC.VpcID, ":")[1]
}
azureVnetResourceId := "/subscriptions/" + subscriptionId + "/resourceGroups/" + resourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vC.Name
d.Set("resource_group", resourceGroup)
d.Set("azure_vnet_resource_id", azureVnetResourceId)
}
var subnetList []map[string]string
for _, subnet := range vC.Subnets {
sub := make(map[string]string)
sub["cidr"] = subnet.Cidr
sub["name"] = subnet.Name
sub["subnet_id"] = subnet.SubnetID
subnetList = append(subnetList, sub)
}
if err := d.Set("subnets", subnetList); err != nil {
log.Printf("[WARN] Error setting subnets for (%s): %s", d.Id(), err)
}
var privateSubnetList []map[string]string
var publicSubnetList []map[string]string
for _, subnet := range vC.PrivateSubnets {
sub := make(map[string]string)
sub["cidr"] = subnet.Cidr
sub["name"] = subnet.Name
sub["subnet_id"] = subnet.SubnetID
privateSubnetList = append(privateSubnetList, sub)
}
for _, subnet := range vC.PublicSubnets {
sub := make(map[string]string)
sub["cidr"] = subnet.Cidr
sub["name"] = subnet.Name
sub["subnet_id"] = subnet.SubnetID
publicSubnetList = append(publicSubnetList, sub)
}
if err := d.Set("private_subnets", privateSubnetList); err != nil {
log.Printf("[WARN] Error setting 'private_subnets' for (%s): %s", d.Id(), err)
}
if err := d.Set("public_subnets", publicSubnetList); err != nil {
log.Printf("[WARN] Error setting 'public_subnets' for (%s): %s", d.Id(), err)
}
if goaviatrix.IsCloudType(vC.CloudType, goaviatrix.AWSRelatedCloudTypes|goaviatrix.AzureArmRelatedCloudTypes) {
var rtbs []string
routeTableFilter := d.Get("route_tables_filter")
if routeTableFilter == "private" {
rtbs, err = getPrivateRouteTables(vpc, client)
} else if routeTableFilter == "public" {
rtbs, err = getPublicRouteTables(vpc, client)
} else {
rtbs, err = getAllRouteTables(vpc, client)
}
if err != nil {
return fmt.Errorf("could not get vpc route table ids: %v", err)
}
if err := d.Set("route_tables", rtbs); err != nil {
log.Printf("[WARN] Error setting route tables for (%s): %s", d.Id(), err)
}
}
if goaviatrix.IsCloudType(vC.CloudType, goaviatrix.OCIRelatedCloudTypes) {
availabilityDomains, err := client.ListOciVpcAvailabilityDomains(vC)
if err != nil {
return fmt.Errorf("could not get OCI availability domains: %v", err)
}
d.Set("availability_domains", availabilityDomains)
faultDomains, err := client.ListOciVpcFaultDomains(vC)
if err != nil {
return fmt.Errorf("could not get OCI fault domains: %v", err)
}
d.Set("fault_domains", faultDomains)
}
d.SetId(vC.Name)
return nil
}
// To find all the private route tables we will remove the public route tables
// from the list of all route tables.
func getPrivateRouteTables(vpc *goaviatrix.Vpc, client *goaviatrix.Client) ([]string, error) {
all, err := getAllRouteTables(vpc, client)
if err != nil {
return nil, err
}
public, err := getPublicRouteTables(vpc, client)
if err != nil {
return nil, err
}
var rtbs []string
for _, rtb := range all {
if !goaviatrix.Contains(public, rtb) {
rtbs = append(rtbs, rtb)
}
}
return rtbs, nil
}
func getPublicRouteTables(vpc *goaviatrix.Vpc, client *goaviatrix.Client) ([]string, error) {
vpc.PublicRoutesOnly = true
rtbs, err := client.GetVpcRouteTableIDs(vpc)
if err != nil {
return nil, err
}
return rtbs, nil
}
func getAllRouteTables(vpc *goaviatrix.Vpc, client *goaviatrix.Client) ([]string, error) {
vpc.PublicRoutesOnly = false
rtbs, err := client.GetVpcRouteTableIDs(vpc)
if err != nil {
return nil, err
}
return rtbs, nil
}