This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 27
/
resource_alicloud_havip_attachment.go
126 lines (110 loc) · 3.86 KB
/
resource_alicloud_havip_attachment.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
package alicloud
import (
"fmt"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAliyunHaVipAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunHaVipAttachmentCreate,
Read: resourceAliyunHaVipAttachmentRead,
Delete: resourceAliyunHaVipAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"havip_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAliyunHaVipAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
haVipService := HaVipService{client}
request := vpc.CreateAssociateHaVipRequest()
request.RegionId = client.RegionId
request.HaVipId = Trim(d.Get("havip_id").(string))
request.InstanceId = Trim(d.Get("instance_id").(string))
if err := resource.Retry(5*time.Minute, func() *resource.RetryError {
ar := request
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.AssociateHaVip(ar)
})
if err != nil {
if IsExpectedErrors(err, []string{"TaskConflict", "IncorrectHaVipStatus", "InvalidVip.Status"}) {
return resource.RetryableError(fmt.Errorf("AssociateHaVip got an error: %#v", err))
}
return resource.NonRetryableError(fmt.Errorf("AssociateHaVip got an error: %#v", err))
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
return nil
}); err != nil {
return err
}
//check the havip attachment
if err := haVipService.WaitForHaVipAttachment(request.HaVipId, request.InstanceId, 5*DefaultTimeout); err != nil {
return fmt.Errorf("Wait for havip attachment got error: %#v", err)
}
d.SetId(request.HaVipId + COLON_SEPARATED + request.InstanceId)
return resourceAliyunHaVipAttachmentRead(d, meta)
}
func resourceAliyunHaVipAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
haVipService := HaVipService{client}
haVipId, instanceId, err := getHaVipIdAndInstanceId(d, meta)
err = haVipService.DescribeHaVipAttachment(haVipId, instanceId)
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error Describe HaVip Attribute: %#v", err)
}
d.Set("havip_id", haVipId)
d.Set("instance_id", instanceId)
return nil
}
func resourceAliyunHaVipAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
haVipService := HaVipService{client}
haVipId, instanceId, err := getHaVipIdAndInstanceId(d, meta)
if err != nil {
return err
}
request := vpc.CreateUnassociateHaVipRequest()
request.RegionId = client.RegionId
request.HaVipId = haVipId
request.InstanceId = instanceId
return resource.Retry(5*time.Minute, func() *resource.RetryError {
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.UnassociateHaVip(request)
})
//Waiting for unassociate the havip
if err != nil {
if IsExpectedErrors(err, []string{"TaskConflict"}) {
return resource.RetryableError(fmt.Errorf("Unassociate HaVip timeout and got an error:%#v.", err))
}
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
//Eusure the instance has been unassociated truly.
err = haVipService.DescribeHaVipAttachment(haVipId, instanceId)
if err != nil {
if NotFoundError(err) {
return nil
}
return resource.NonRetryableError(err)
}
return resource.RetryableError(fmt.Errorf("Unassociate HaVip timeout."))
})
}