forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_vswitch.go
220 lines (169 loc) · 4.61 KB
/
resource_alicloud_vswitch.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
package alicloud
import (
"fmt"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"log"
"time"
)
func resourceAliyunSubnet() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunSwitchCreate,
Read: resourceAliyunSwitchRead,
Update: resourceAliyunSwitchUpdate,
Delete: resourceAliyunSwitchDelete,
Schema: map[string]*schema.Schema{
"availability_zone": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"cidr_block": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateSwitchCIDRNetworkAddress,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceAliyunSwitchCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AliyunClient).ecsconn
args, err := buildAliyunSwitchArgs(d, meta)
if err != nil {
return err
}
vswitchID, err := conn.CreateVSwitch(args)
if err != nil {
return fmt.Errorf("Create subnet got a error :%s", err)
}
d.SetId(vswitchID)
err = conn.WaitForVSwitchAvailable(args.VpcId, vswitchID, 60)
if err != nil {
return fmt.Errorf("WaitForVSwitchAvailable got a error: %s", err)
}
return resourceAliyunSwitchRead(d, meta)
}
func resourceAliyunSwitchRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AliyunClient).ecsconn
args := &ecs.DescribeVSwitchesArgs{
VpcId: d.Get("vpc_id").(string),
VSwitchId: d.Id(),
}
vswitches, _, err := conn.DescribeVSwitches(args)
if err != nil {
if notFoundError(err) {
d.SetId("")
return nil
}
return err
}
if len(vswitches) == 0 {
d.SetId("")
return nil
}
vswitch := vswitches[0]
d.Set("availability_zone", vswitch.ZoneId)
d.Set("vpc_id", vswitch.VpcId)
d.Set("cidr_block", vswitch.CidrBlock)
d.Set("name", vswitch.VSwitchName)
d.Set("description", vswitch.Description)
return nil
}
func resourceAliyunSwitchUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AliyunClient).ecsconn
d.Partial(true)
attributeUpdate := false
args := &ecs.ModifyVSwitchAttributeArgs{
VSwitchId: d.Id(),
}
if d.HasChange("name") {
d.SetPartial("name")
args.VSwitchName = d.Get("name").(string)
attributeUpdate = true
}
if d.HasChange("description") {
d.SetPartial("description")
args.Description = d.Get("description").(string)
attributeUpdate = true
}
if attributeUpdate {
if err := conn.ModifyVSwitchAttribute(args); err != nil {
return err
}
}
d.Partial(false)
return nil
}
func resourceAliyunSwitchDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AliyunClient).ecsconn
return resource.Retry(5*time.Minute, func() *resource.RetryError {
err := conn.DeleteVSwitch(d.Id())
if err != nil {
e, _ := err.(*common.Error)
if e.ErrorResponse.Code == VswitcInvalidRegionId {
log.Printf("[ERROR] Delete Switch is failed.")
return resource.NonRetryableError(err)
}
return resource.RetryableError(fmt.Errorf("Switch in use. -- trying again while it is deleted."))
}
vsw, _, vswErr := conn.DescribeVSwitches(&ecs.DescribeVSwitchesArgs{
VpcId: d.Get("vpc_id").(string),
VSwitchId: d.Id(),
})
if vswErr != nil {
return resource.NonRetryableError(vswErr)
} else if vsw == nil || len(vsw) < 1 {
return nil
}
return resource.RetryableError(fmt.Errorf("Switch in use. -- trying again while it is deleted."))
})
}
func buildAliyunSwitchArgs(d *schema.ResourceData, meta interface{}) (*ecs.CreateVSwitchArgs, error) {
client := meta.(*AliyunClient)
vpcID := d.Get("vpc_id").(string)
vpc, err := client.DescribeVpc(vpcID)
if err != nil {
return nil, err
}
if vpc == nil {
return nil, fmt.Errorf("vpc_id not found")
}
zoneID := d.Get("availability_zone").(string)
zone, err := client.DescribeZone(zoneID)
if err != nil {
return nil, err
}
err = client.ResourceAvailable(zone, ecs.ResourceTypeVSwitch)
if err != nil {
return nil, err
}
cidrBlock := d.Get("cidr_block").(string)
args := &ecs.CreateVSwitchArgs{
VpcId: vpcID,
ZoneId: zoneID,
CidrBlock: cidrBlock,
}
if v, ok := d.GetOk("name"); ok && v != "" {
args.VSwitchName = v.(string)
}
if v, ok := d.GetOk("description"); ok && v != "" {
args.Description = v.(string)
}
return args, nil
}