forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_eip_association.go
151 lines (134 loc) · 4.33 KB
/
resource_alicloud_eip_association.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
package alicloud
import (
"strings"
"time"
"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 resourceAliyunEipAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunEipAssociationCreate,
Read: resourceAliyunEipAssociationRead,
Delete: resourceAliyunEipAssociationDelete,
Schema: map[string]*schema.Schema{
"allocation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
},
}
}
func resourceAliyunEipAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
request := vpc.CreateAssociateEipAddressRequest()
request.AllocationId = Trim(d.Get("allocation_id").(string))
request.InstanceId = Trim(d.Get("instance_id").(string))
request.InstanceType = EcsInstance
if strings.HasPrefix(request.InstanceId, "lb-") {
request.InstanceType = SlbInstance
}
if strings.HasPrefix(request.InstanceId, "ngw-") {
request.InstanceType = Nat
}
if instanceType, ok := d.GetOk("instance_type"); ok {
request.InstanceType = instanceType.(string)
}
if err := resource.Retry(3*time.Minute, func() *resource.RetryError {
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.AssociateEipAddress(request)
})
if err != nil {
if IsExceptedError(err, TaskConflict) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(request.GetActionName(), raw)
return nil
}); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_eip_association", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
if err := vpcService.WaitForEip(request.AllocationId, InUse, 60); err != nil {
return WrapError(err)
}
// There is at least 30 seconds delay for ecs instance
if request.InstanceType == EcsInstance {
time.Sleep(30 * time.Second)
}
d.SetId(request.AllocationId + ":" + request.InstanceId)
return resourceAliyunEipAssociationRead(d, meta)
}
func resourceAliyunEipAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
object, err := vpcService.DescribeEipAssociation(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("instance_id", object.InstanceId)
d.Set("allocation_id", object.AllocationId)
d.Set("instance_type", object.InstanceType)
return nil
}
func resourceAliyunEipAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
allocationId, instanceId := parts[0], parts[1]
if err != nil {
return WrapError(err)
}
request := vpc.CreateUnassociateEipAddressRequest()
request.AllocationId = allocationId
request.InstanceId = instanceId
request.InstanceType = EcsInstance
if strings.HasPrefix(instanceId, "lb-") {
request.InstanceType = SlbInstance
}
if strings.HasPrefix(instanceId, "ngw-") {
request.InstanceType = Nat
}
if instanceType, ok := d.GetOk("instance_type"); ok {
request.InstanceType = instanceType.(string)
}
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.UnassociateEipAddress(request)
})
if err != nil {
if IsExceptedErrors(err, []string{InstanceIncorrectStatus, HaVipIncorrectStatus, TaskConflict,
HasBeenUsedBySnatTable, HasBeenUsedByForwardEntry}) {
return resource.RetryableError(err)
}
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.WaitForEipAssociation(d.Id(), Available, DefaultTimeoutMedium))
}