/
resource_digitalocean_vpc.go
172 lines (146 loc) · 4.62 KB
/
resource_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
171
172
package digitalocean
import (
"context"
"fmt"
"log"
"time"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceDigitalOceanVPC() *schema.Resource {
return &schema.Resource{
Create: resourceDigitalOceanVPCCreate,
Read: resourceDigitalOceanVPCRead,
Update: resourceDigitalOceanVPCUpdate,
Delete: resourceDigitalOceanVPCDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the VPC",
ValidateFunc: validation.NoZeroValues,
},
"region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "DigitalOcean region slug for the VPC's location",
ValidateFunc: validation.NoZeroValues,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "A free-form description for the VPC",
ValidateFunc: validation.StringLenBetween(0, 255),
},
"ip_range": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "The range of IP addresses for the VPC in CIDR notation",
ValidateFunc: validation.IsCIDR,
},
// Computed attributes
"urn": {
Type: schema.TypeString,
Computed: true,
Description: "The uniform resource name (URN) for the VPC",
},
"default": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether or not the VPC is the default one for the region",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time of when the VPC was created",
},
},
Timeouts: &schema.ResourceTimeout{
Delete: schema.DefaultTimeout(2 * time.Minute),
},
}
}
func resourceDigitalOceanVPCCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
vpcRequest := &godo.VPCCreateRequest{
Name: d.Get("name").(string),
RegionSlug: d.Get("region").(string),
}
if v, ok := d.GetOk("description"); ok {
vpcRequest.Description = v.(string)
}
if v, ok := d.GetOk("ip_range"); ok {
vpcRequest.IPRange = v.(string)
}
log.Printf("[DEBUG] VPC create request: %#v", vpcRequest)
vpc, _, err := client.VPCs.Create(context.Background(), vpcRequest)
if err != nil {
return fmt.Errorf("Error creating VPC: %s", err)
}
d.SetId(vpc.ID)
log.Printf("[INFO] VPC created, ID: %s", d.Id())
return resourceDigitalOceanVPCRead(d, meta)
}
func resourceDigitalOceanVPCRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
vpc, resp, err := client.VPCs.Get(context.Background(), d.Id())
if err != nil {
if resp != nil && resp.StatusCode == 404 {
log.Printf("[DEBUG] VPC (%s) was not found - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading VPC: %s", err)
}
d.SetId(vpc.ID)
d.Set("name", vpc.Name)
d.Set("region", vpc.RegionSlug)
d.Set("description", vpc.Description)
d.Set("ip_range", vpc.IPRange)
d.Set("urn", vpc.URN)
d.Set("default", vpc.Default)
d.Set("created_at", vpc.CreatedAt.UTC().String())
return err
}
func resourceDigitalOceanVPCUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
if d.HasChange("name") || d.HasChange("description") {
vpcUpdateRequest := &godo.VPCUpdateRequest{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
_, _, err := client.VPCs.Update(context.Background(), d.Id(), vpcUpdateRequest)
if err != nil {
return fmt.Errorf("Error updating VPC : %s", err)
}
log.Printf("[INFO] Updated VPC")
}
return resourceDigitalOceanVPCRead(d, meta)
}
func resourceDigitalOceanVPCDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).godoClient()
vpcID := d.Id()
return resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
resp, err := client.VPCs.Delete(context.Background(), vpcID)
if err != nil {
// Retry if VPC still contains member resources to prevent race condition
// with database cluster deletion.
if resp.StatusCode == 403 {
return resource.RetryableError(err)
} else {
return resource.NonRetryableError(fmt.Errorf("Error deleting VPC: %s", err))
}
}
d.SetId("")
log.Printf("[INFO] VPC deleted, ID: %s", vpcID)
return nil
})
}