forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_eip_association.go
167 lines (140 loc) · 4.45 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package alicloud
import (
"fmt"
"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": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"instance_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceAliyunEipAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
args := vpc.CreateAssociateEipAddressRequest()
args.AllocationId = Trim(d.Get("allocation_id").(string))
args.InstanceId = Trim(d.Get("instance_id").(string))
args.InstanceType = EcsInstance
if strings.HasPrefix(args.InstanceId, "lb-") {
args.InstanceType = SlbInstance
}
if strings.HasPrefix(args.InstanceId, "ngw-") {
args.InstanceType = Nat
}
if err := resource.Retry(3*time.Minute, func() *resource.RetryError {
ar := args
_, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.AssociateEipAddress(ar)
})
if err != nil {
if IsExceptedError(err, TaskConflict) {
return resource.RetryableError(fmt.Errorf("AssociateEip got an error: %#v", err))
}
return resource.NonRetryableError(fmt.Errorf("AssociateEip got an error: %#v", err))
}
return nil
}); err != nil {
return err
}
if err := vpcService.WaitForEip(args.AllocationId, InUse, 60); err != nil {
return fmt.Errorf("Error Waitting for EIP allocated: %#v", err)
}
// There is at least 30 seconds delay for ecs instance
if args.InstanceType == EcsInstance {
time.Sleep(30 * time.Second)
}
d.SetId(args.AllocationId + ":" + args.InstanceId)
return resourceAliyunEipAssociationRead(d, meta)
}
func resourceAliyunEipAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
allocationId, instanceId, err := getAllocationIdAndInstanceId(d, meta)
if err != nil {
return err
}
eip, err := vpcService.DescribeEipAddress(allocationId)
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error Describe Eip Attribute: %#v", err)
}
if eip.InstanceId != instanceId {
d.SetId("")
return nil
}
d.Set("instance_id", eip.InstanceId)
d.Set("allocation_id", allocationId)
return nil
}
func resourceAliyunEipAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
allocationId, instanceId, err := getAllocationIdAndInstanceId(d, meta)
if err != nil {
return 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
}
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.UnassociateEipAddress(request)
})
if err != nil {
if IsExceptedError(err, InstanceIncorrectStatus) ||
IsExceptedError(err, HaVipIncorrectStatus) ||
IsExceptedError(err, TaskConflict) {
return resource.RetryableError(fmt.Errorf("Unassociate EIP timeout and got an error:%#v.", err))
}
}
eip, descErr := vpcService.DescribeEipAddress(allocationId)
if descErr != nil {
if NotFoundError(err) {
return nil
}
return resource.NonRetryableError(descErr)
}
if eip.InstanceId == instanceId {
return resource.RetryableError(fmt.Errorf("Unassociate EIP timeout and got an error:%#v.", err))
}
return nil
})
}
func getAllocationIdAndInstanceId(d *schema.ResourceData, meta interface{}) (string, string, error) {
parts := strings.Split(d.Id(), ":")
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid resource id")
}
return parts[0], parts[1], nil
}