forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_eip.go
223 lines (196 loc) · 6.18 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
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
package alicloud
import (
"fmt"
"strconv"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAliyunEip() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunEipCreate,
Read: resourceAliyunEipRead,
Update: resourceAliyunEipUpdate,
Delete: resourceAliyunEipDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateInstanceName,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateInstanceDescription,
},
"bandwidth": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 5,
},
"internet_charge_type": &schema.Schema{
Type: schema.TypeString,
Default: "PayByTraffic",
Optional: true,
ForceNew: true,
ValidateFunc: validateInternetChargeType,
},
"instance_charge_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateInstanceChargeType,
Default: PostPaid,
ForceNew: true,
},
"period": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 1,
ForceNew: true,
ValidateFunc: validateEipChargeTypePeriod,
DiffSuppressFunc: ecsPostPaidDiffSuppressFunc,
},
"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 {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
request := vpc.CreateAllocateEipAddressRequest()
request.RegionId = string(client.Region)
request.Bandwidth = strconv.Itoa(d.Get("bandwidth").(int))
request.InternetChargeType = d.Get("internet_charge_type").(string)
request.InstanceChargeType = d.Get("instance_charge_type").(string)
if request.InstanceChargeType == string(PrePaid) {
period := d.Get("period").(int)
request.Period = requests.NewInteger(period)
request.PricingCycle = string(Month)
if period > 9 {
request.Period = requests.NewInteger(period / 12)
request.PricingCycle = string(Year)
}
request.AutoPay = requests.NewBoolean(true)
}
request.ClientToken = buildClientToken("TF-AllocateEip")
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.AllocateEipAddress(request)
})
if err != nil {
if IsExceptedError(err, COMMODITYINVALID_COMPONENT) && request.InternetChargeType == string(PayByBandwidth) {
return fmt.Errorf("Your account is international and it can only create '%s' elastic IP. Please change it and try again.", PayByTraffic)
}
return err
}
eip, _ := raw.(*vpc.AllocateEipAddressResponse)
err = vpcService.WaitForEip(eip.AllocationId, Available, 60)
if err != nil {
return fmt.Errorf("Error Waitting for EIP available: %#v", err)
}
d.SetId(eip.AllocationId)
return resourceAliyunEipUpdate(d, meta)
}
func resourceAliyunEipRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
eip, err := vpcService.DescribeEipAddress(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error Describe Eip Attribute: %#v", err)
}
// Output parameter 'instance' would be deprecated in the next version.
if eip.InstanceId != "" {
d.Set("instance", eip.InstanceId)
} else {
d.Set("instance", "")
}
d.Set("name", eip.Name)
d.Set("description", eip.Descritpion)
bandwidth, _ := strconv.Atoi(eip.Bandwidth)
d.Set("bandwidth", bandwidth)
d.Set("internet_charge_type", eip.InternetChargeType)
d.Set("instance_charge_type", eip.ChargeType)
d.Set("ip_address", eip.IpAddress)
d.Set("status", eip.Status)
return nil
}
func resourceAliyunEipUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
d.Partial(true)
update := false
request := vpc.CreateModifyEipAddressAttributeRequest()
request.AllocationId = d.Id()
if d.HasChange("bandwidth") && !d.IsNewResource() {
update = true
request.Bandwidth = strconv.Itoa(d.Get("bandwidth").(int))
d.SetPartial("bandwidth")
}
if d.HasChange("name") {
update = true
request.Name = d.Get("name").(string)
d.SetPartial("name")
}
if d.HasChange("description") {
update = true
request.Description = d.Get("description").(string)
d.SetPartial("description")
}
if update {
_, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.ModifyEipAddressAttribute(request)
})
if err != nil {
return err
}
}
d.Partial(false)
return resourceAliyunEipRead(d, meta)
}
func resourceAliyunEipDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
request := vpc.CreateReleaseEipAddressRequest()
request.AllocationId = d.Id()
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.ReleaseEipAddress(request)
})
if err != nil {
if IsExceptedError(err, EipIncorrectStatus) {
return resource.RetryableError(fmt.Errorf("Delete EIP timeout and got an error:%#v.", err))
}
return resource.NonRetryableError(err)
}
eip, descErr := vpcService.DescribeEipAddress(d.Id())
if descErr != nil {
if NotFoundError(descErr) {
return nil
}
return resource.NonRetryableError(descErr)
} else if eip.AllocationId == d.Id() {
return resource.RetryableError(fmt.Errorf("Delete EIP timeout and it still exists."))
}
return nil
})
}