forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_alicloud_eip.go
157 lines (125 loc) · 3.51 KB
/
resource_alicloud_eip.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
package alicloud
import (
"strconv"
"fmt"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"time"
)
func resourceAliyunEip() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunEipCreate,
Read: resourceAliyunEipRead,
Update: resourceAliyunEipUpdate,
Delete: resourceAliyunEipDelete,
Schema: map[string]*schema.Schema{
"bandwidth": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 5,
},
"internet_charge_type": &schema.Schema{
Type: schema.TypeString,
Default: "PayByBandwidth",
Optional: true,
ForceNew: true,
ValidateFunc: validateInternetChargeType,
},
"ip_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"instance": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceAliyunEipCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AliyunClient).ecsconn
args, err := buildAliyunEipArgs(d, meta)
if err != nil {
return err
}
_, allocationID, err := conn.AllocateEipAddress(args)
if err != nil {
return err
}
d.SetId(allocationID)
return resourceAliyunEipRead(d, meta)
}
func resourceAliyunEipRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*AliyunClient)
eip, err := client.DescribeEipAddress(d.Id())
if err != nil {
if notFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error Describe Eip Attribute: %#v", err)
}
if eip.InstanceId != "" {
d.Set("instance", eip.InstanceId)
} else {
d.Set("instance", "")
return nil
}
bandwidth, _ := strconv.Atoi(eip.Bandwidth)
d.Set("bandwidth", bandwidth)
d.Set("internet_charge_type", eip.InternetChargeType)
d.Set("ip_address", eip.IpAddress)
d.Set("status", eip.Status)
return nil
}
func resourceAliyunEipUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AliyunClient).ecsconn
d.Partial(true)
if d.HasChange("bandwidth") {
err := conn.ModifyEipAddressAttribute(d.Id(), d.Get("bandwidth").(int))
if err != nil {
return err
}
d.SetPartial("bandwidth")
}
d.Partial(false)
return nil
}
func resourceAliyunEipDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AliyunClient).ecsconn
return resource.Retry(5*time.Minute, func() *resource.RetryError {
err := conn.ReleaseEipAddress(d.Id())
if err != nil {
e, _ := err.(*common.Error)
if e.ErrorResponse.Code == EipIncorrectStatus {
return resource.RetryableError(fmt.Errorf("EIP in use - trying again while it is deleted."))
}
}
args := &ecs.DescribeEipAddressesArgs{
RegionId: getRegion(d, meta),
AllocationId: d.Id(),
}
eips, _, descErr := conn.DescribeEipAddresses(args)
if descErr != nil {
return resource.NonRetryableError(descErr)
} else if eips == nil || len(eips) < 1 {
return nil
}
return resource.RetryableError(fmt.Errorf("EIP in use - trying again while it is deleted."))
})
}
func buildAliyunEipArgs(d *schema.ResourceData, meta interface{}) (*ecs.AllocateEipAddressArgs, error) {
args := &ecs.AllocateEipAddressArgs{
RegionId: getRegion(d, meta),
Bandwidth: d.Get("bandwidth").(int),
InternetChargeType: common.InternetChargeType(d.Get("internet_charge_type").(string)),
}
return args, nil
}