forked from aliyun/terraform-provider-alicloud
/
resource_alicloud_eip.go
203 lines (187 loc) · 6.03 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
package alicloud
import (
"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": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateInstanceName,
},
"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateInstanceDescription,
},
"bandwidth": {
Type: schema.TypeInt,
Optional: true,
Default: 5,
},
"internet_charge_type": {
Type: schema.TypeString,
Default: "PayByTraffic",
Optional: true,
ForceNew: true,
ValidateFunc: validateInternetChargeType,
},
"instance_charge_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateInstanceChargeType,
Default: PostPaid,
ForceNew: true,
},
"period": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ForceNew: true,
ValidateFunc: validateEipChargeTypePeriod,
DiffSuppressFunc: ecsPostPaidDiffSuppressFunc,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"isp": {
Type: schema.TypeString,
Optional: true,
ForceNew: 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)
request.ISP = d.Get("isp").(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(request.GetActionName())
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 WrapErrorf(err, "Your account is international and it can only create '%s' elastic IP. Please change it and try again. %s", PayByTraffic, AlibabaCloudSdkGoERROR)
}
return WrapErrorf(err, DefaultErrorMsg, "alicloud_eip", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
response, _ := raw.(*vpc.AllocateEipAddressResponse)
d.SetId(response.AllocationId)
err = vpcService.WaitForEip(d.Id(), Available, DefaultTimeoutMedium)
if err != nil {
return WrapError(err)
}
return resourceAliyunEipUpdate(d, meta)
}
func resourceAliyunEipRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
object, err := vpcService.DescribeEip(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("name", object.Name)
d.Set("description", object.Descritpion)
bandwidth, _ := strconv.Atoi(object.Bandwidth)
d.Set("bandwidth", bandwidth)
d.Set("internet_charge_type", object.InternetChargeType)
d.Set("instance_charge_type", object.ChargeType)
d.Set("isp", object.ISP)
d.Set("ip_address", object.IpAddress)
d.Set("status", object.Status)
return nil
}
func resourceAliyunEipUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
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))
}
if d.HasChange("name") {
update = true
request.Name = d.Get("name").(string)
}
if d.HasChange("description") {
update = true
request.Description = d.Get("description").(string)
}
if update {
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.ModifyEipAddressAttribute(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
}
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()
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.ReleaseEipAddress(request)
})
if err != nil {
if IsExceptedError(err, EipIncorrectStatus) {
return resource.RetryableError(err)
} else if IsExceptedError(err, AllocationIdNotFound) {
return nil
}
return resource.NonRetryableError(err)
}
addDebug(request.GetActionName(), raw)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
return WrapError(vpcService.WaitForEip(d.Id(), Deleted, DefaultTimeout))
}