forked from aliyun/terraform-provider-alicloud
/
service_alicloud_havip.go
115 lines (103 loc) · 2.82 KB
/
service_alicloud_havip.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
package alicloud
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
type HaVipService struct {
client *connectivity.AliyunClient
}
func (s *HaVipService) DescribeHaVip(haVipId string) (v vpc.HaVip, err error) {
request := vpc.CreateDescribeHaVipsRequest()
values := []string{haVipId}
filter := []vpc.DescribeHaVipsFilter{{
Key: "HaVipId",
Value: &values,
},
}
request.Filter = &filter
invoker := NewInvoker()
err = invoker.Run(func() error {
raw, err := s.client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.DescribeHaVips(request)
})
if err != nil {
return err
}
resp, _ := raw.(*vpc.DescribeHaVipsResponse)
if resp == nil || len(resp.HaVips.HaVip) <= 0 ||
resp.HaVips.HaVip[0].HaVipId != haVipId {
return GetNotFoundErrorFromString(GetNotFoundMessage("HaVip", haVipId))
}
v = resp.HaVips.HaVip[0]
return nil
})
return
}
func (s *HaVipService) WaitForHaVip(haVipId string, status Status, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
for {
//wait the order effective
havip, err := s.DescribeHaVip(haVipId)
if err != nil {
return err
}
if strings.ToLower(havip.Status) == strings.ToLower(string(status)) {
break
}
timeout = timeout - DefaultIntervalShort
if timeout <= 0 {
return GetTimeErrorFromString(GetTimeoutMessage("HaVip", string(status)))
}
time.Sleep(DefaultIntervalShort * time.Second)
}
return nil
}
func (s *HaVipService) DescribeHaVipAttachment(haVipId string, instanceId string) (err error) {
invoker := NewInvoker()
return invoker.Run(func() error {
haVip, err := s.DescribeHaVip(haVipId)
if err != nil {
return err
}
for _, id := range haVip.AssociatedInstances.AssociatedInstance {
if id == instanceId {
return nil
}
}
return GetNotFoundErrorFromString(GetNotFoundMessage("HaVipAttachment", haVipId+COLON_SEPARATED+instanceId))
})
}
func (s *HaVipService) WaitForHaVipAttachment(haVipId string, instanceId string, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
for {
err := s.DescribeHaVipAttachment(haVipId, instanceId)
if err != nil {
if !NotFoundError(err) {
return err
}
} else {
break
}
timeout = timeout - DefaultIntervalShort
if timeout <= 0 {
return GetTimeErrorFromString(GetTimeoutMessage("HaVip Attachment", string("Unavailable")))
}
time.Sleep(DefaultIntervalShort * time.Second)
}
return nil
}
func getHaVipIdAndInstanceId(d *schema.ResourceData, meta interface{}) (string, string, error) {
parts := strings.Split(d.Id(), COLON_SEPARATED)
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid resource id")
}
return parts[0], parts[1], nil
}