forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource_digitalocean_vpc.go
170 lines (146 loc) · 4 KB
/
datasource_digitalocean_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
package digitalocean
import (
"context"
"fmt"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceDigitalOceanVPC() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDigitalOceanVPCRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "name", "region"},
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "name", "region"},
},
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "name", "region"},
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"ip_range": {
Type: schema.TypeString,
Computed: true,
},
"urn": {
Type: schema.TypeString,
Computed: true,
},
"default": {
Type: schema.TypeBool,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceDigitalOceanVPCRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*CombinedConfig).godoClient()
var foundVPC *godo.VPC
if id, ok := d.GetOk("id"); ok {
vpc, _, err := client.VPCs.Get(context.Background(), id.(string))
if err != nil {
return diag.Errorf("Error retrieving VPC: %s", err)
}
foundVPC = vpc
} else if slug, ok := d.GetOk("region"); ok {
vpcs, err := listVPCs(client)
if err != nil {
return diag.Errorf("Error retrieving VPC: %s", err)
}
vpc, err := findRegionDefaultVPC(vpcs, slug.(string))
if err != nil {
return diag.Errorf("Error retrieving VPC: %s", err)
}
foundVPC = vpc
} else if name, ok := d.GetOk("name"); ok {
vpcs, err := listVPCs(client)
if err != nil {
return diag.Errorf("Error retrieving VPC: %s", err)
}
vpc, err := findVPCByName(vpcs, name.(string))
if err != nil {
return diag.Errorf("Error retrieving VPC: %s", err)
}
foundVPC = vpc
}
d.SetId(foundVPC.ID)
d.Set("name", foundVPC.Name)
d.Set("region", foundVPC.RegionSlug)
d.Set("description", foundVPC.Description)
d.Set("ip_range", foundVPC.IPRange)
d.Set("urn", foundVPC.URN)
d.Set("default", foundVPC.Default)
d.Set("created_at", foundVPC.CreatedAt.UTC().String())
return nil
}
func listVPCs(client *godo.Client) ([]*godo.VPC, error) {
vpcList := []*godo.VPC{}
opts := &godo.ListOptions{
Page: 1,
PerPage: 200,
}
for {
vpcs, resp, err := client.VPCs.List(context.Background(), opts)
if err != nil {
return vpcList, fmt.Errorf("Error retrieving VPCs: %s", err)
}
vpcList = append(vpcList, vpcs...)
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return vpcList, fmt.Errorf("Error retrieving VPCs: %s", err)
}
opts.Page = page + 1
}
return vpcList, nil
}
func findRegionDefaultVPC(vpcs []*godo.VPC, region string) (*godo.VPC, error) {
results := make([]*godo.VPC, 0)
for _, v := range vpcs {
if v.RegionSlug == region && v.Default {
results = append(results, v)
}
}
if len(results) == 1 {
return results[0], nil
}
return nil, fmt.Errorf("unable to find default VPC in %s region", region)
}
func findVPCByName(vpcs []*godo.VPC, name string) (*godo.VPC, error) {
results := make([]*godo.VPC, 0)
for _, v := range vpcs {
if v.Name == name {
results = append(results, v)
}
}
if len(results) == 1 {
return results[0], nil
} else if len(results) == 0 {
return nil, fmt.Errorf("no VPCs found with name %s", name)
}
return nil, fmt.Errorf("too many VPCs found with name %s (found %d, expected 1)", name, len(results))
}