forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_huaweicloud_vpc_v1.go
229 lines (189 loc) · 5.61 KB
/
resource_huaweicloud_vpc_v1.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
package huaweicloud
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/networking/v1/vpcs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/huaweicloud/golangsdk"
)
func resourceVirtualPrivateCloudV1() *schema.Resource {
return &schema.Resource{
Create: resourceVirtualPrivateCloudV1Create,
Read: resourceVirtualPrivateCloudV1Read,
Update: resourceVirtualPrivateCloudV1Update,
Delete: resourceVirtualPrivateCloudV1Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(3 * time.Minute),
},
Schema: map[string]*schema.Schema{ //request and response parameters
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
ValidateFunc: validateName,
},
"cidr": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
ValidateFunc: validateCIDR,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"shared": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
func resourceVirtualPrivateCloudV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vpcClient, err := config.networkingV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating Huaweicloud vpc client: %s", err)
}
createOpts := vpcs.CreateOpts{
Name: d.Get("name").(string),
CIDR: d.Get("cidr").(string),
}
n, err := vpcs.Create(vpcClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating Huaweicloud VPC: %s", err)
}
d.SetId(n.ID)
log.Printf("[INFO] Vpc ID: %s", n.ID)
stateConf := &resource.StateChangeConf{
Pending: []string{"CREATING"},
Target: []string{"ACTIVE"},
Refresh: waitForVpcActive(vpcClient, n.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, stateErr := stateConf.WaitForState()
if stateErr != nil {
return fmt.Errorf(
"Error waiting for Vpc (%s) to become ACTIVE: %s",
n.ID, stateErr)
}
return resourceVirtualPrivateCloudV1Read(d, meta)
}
func resourceVirtualPrivateCloudV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vpcClient, err := config.networkingV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating Huaweicloud Vpc client: %s", err)
}
n, err := vpcs.Get(vpcClient, d.Id()).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving Huaweicloud Vpc: %s", err)
}
d.Set("id", n.ID)
d.Set("name", n.Name)
d.Set("cidr", n.CIDR)
d.Set("status", n.Status)
d.Set("shared", n.EnableSharedSnat)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceVirtualPrivateCloudV1Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vpcClient, err := config.networkingV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating Huaweicloud Vpc: %s", err)
}
var updateOpts vpcs.UpdateOpts
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
}
if d.HasChange("cidr") {
updateOpts.CIDR = d.Get("cidr").(string)
}
_, err = vpcs.Update(vpcClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating Huaweicloud Vpc: %s", err)
}
return resourceVirtualPrivateCloudV1Read(d, meta)
}
func resourceVirtualPrivateCloudV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vpcClient, err := config.networkingV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating Huaweicloud vpc: %s", err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE"},
Target: []string{"DELETED"},
Refresh: waitForVpcDelete(vpcClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error deleting Huaweicloud Vpc: %s", err)
}
d.SetId("")
return nil
}
func waitForVpcActive(vpcClient *golangsdk.ServiceClient, vpcId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
n, err := vpcs.Get(vpcClient, vpcId).Extract()
if err != nil {
return nil, "", err
}
if n.Status == "OK" {
return n, "ACTIVE", nil
}
//If vpc status is other than Ok, send error
if n.Status == "DOWN" {
return nil, "", fmt.Errorf("Vpc status: '%s'", n.Status)
}
return n, n.Status, nil
}
}
func waitForVpcDelete(vpcClient *golangsdk.ServiceClient, vpcId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
r, err := vpcs.Get(vpcClient, vpcId).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
log.Printf("[INFO] Successfully deleted Huaweicloud vpc %s", vpcId)
return r, "DELETED", nil
}
return r, "ACTIVE", err
}
err = vpcs.Delete(vpcClient, vpcId).ExtractErr()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
log.Printf("[INFO] Successfully deleted Huaweicloud vpc %s", vpcId)
return r, "DELETED", nil
}
if errCode, ok := err.(golangsdk.ErrUnexpectedResponseCode); ok {
if errCode.Actual == 409 {
return r, "ACTIVE", nil
}
}
return r, "ACTIVE", err
}
return r, "ACTIVE", nil
}
}