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
/
service_alicloud_ons.go
189 lines (157 loc) · 5.2 KB
/
service_alicloud_ons.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
package alicloud
import (
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ons"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
type OnsService struct {
client *connectivity.AliyunClient
}
func (s *OnsService) DescribeOnsInstance(id string) (*ons.OnsInstanceBaseInfoResponse, error) {
response := &ons.OnsInstanceBaseInfoResponse{}
request := ons.CreateOnsInstanceBaseInfoRequest()
request.RegionId = s.client.RegionId
request.InstanceId = id
raw, err := s.client.WithOnsClient(func(onsClient *ons.Client) (interface{}, error) {
return onsClient.OnsInstanceBaseInfo(request)
})
if err != nil {
if IsExpectedErrors(err, []string{"InvalidDomainName.NoExist"}) {
return response, WrapErrorf(err, NotFoundMsg, AlibabaCloudSdkGoERROR)
}
return response, WrapErrorf(err, DefaultErrorMsg, id, request.GetActionName(), AlibabaCloudSdkGoERROR)
}
response, _ = raw.(*ons.OnsInstanceBaseInfoResponse)
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
return response, nil
}
func (s *OnsService) DescribeOnsTopic(id string) (*ons.PublishInfoDo, error) {
onsTopic := &ons.PublishInfoDo{}
parts, err := ParseResourceId(id, 2)
if err != nil {
return onsTopic, WrapError(err)
}
instanceId := parts[0]
topic := parts[1]
request := ons.CreateOnsTopicListRequest()
request.RegionId = s.client.RegionId
request.InstanceId = instanceId
raw, err := s.client.WithOnsClient(func(onsClient *ons.Client) (interface{}, error) {
return onsClient.OnsTopicList(request)
})
if err != nil {
if IsExpectedErrors(err, []string{"AUTH_RESOURCE_OWNER_ERROR", "INSTANCE_NOT_FOUND"}) {
return onsTopic, WrapErrorf(err, NotFoundMsg, AlibabaCloudSdkGoERROR)
}
return onsTopic, WrapErrorf(err, DefaultErrorMsg, id, request.GetActionName(), AlibabaCloudSdkGoERROR)
}
topicListResp, _ := raw.(*ons.OnsTopicListResponse)
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
for _, v := range topicListResp.Data.PublishInfoDo {
if v.Topic == topic {
return &v, nil
}
}
return onsTopic, WrapErrorf(err, NotFoundMsg, AlibabaCloudSdkGoERROR)
}
func (s *OnsService) DescribeOnsGroup(id string) (*ons.SubscribeInfoDo, error) {
onsGroup := &ons.SubscribeInfoDo{}
parts, err := ParseResourceId(id, 2)
if err != nil {
return onsGroup, WrapError(err)
}
instanceId := parts[0]
groupId := parts[1]
request := ons.CreateOnsGroupListRequest()
request.RegionId = s.client.RegionId
request.InstanceId = instanceId
raw, err := s.client.WithOnsClient(func(onsClient *ons.Client) (interface{}, error) {
return onsClient.OnsGroupList(request)
})
if err != nil {
if IsExpectedErrors(err, []string{"AUTH_RESOURCE_OWNER_ERROR", "INSTANCE_NOT_FOUND"}) {
return onsGroup, WrapErrorf(err, NotFoundMsg, AlibabaCloudSdkGoERROR)
}
return onsGroup, WrapErrorf(err, DefaultErrorMsg, id, request.GetActionName(), AlibabaCloudSdkGoERROR)
}
groupListResp, _ := raw.(*ons.OnsGroupListResponse)
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
for _, v := range groupListResp.Data.SubscribeInfoDo {
if v.GroupId == groupId {
return &v, nil
}
}
return onsGroup, WrapErrorf(err, NotFoundMsg, AlibabaCloudSdkGoERROR)
}
func (s *OnsService) WaitForOnsInstance(id string, status Status, timeout int) error {
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
for {
response, err := s.DescribeOnsInstance(id)
if err != nil {
if NotFoundError(err) {
if status == Deleted {
return nil
}
} else {
return WrapError(err)
}
}
if response.InstanceBaseInfo.InstanceId == id && status != Deleted {
return nil
}
if time.Now().After(deadline) {
return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, response.InstanceBaseInfo.InstanceId, id, ProviderERROR)
}
time.Sleep(DefaultIntervalShort * time.Second)
}
}
func (s *OnsService) WaitForOnsTopic(id string, status Status, timeout int) error {
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
parts, err := ParseResourceId(id, 2)
if err != nil {
return WrapError(err)
}
instanceId := parts[0]
topic := parts[1]
for {
response, err := s.DescribeOnsTopic(id)
if err != nil {
if NotFoundError(err) {
if status == Deleted {
return nil
}
} else {
return WrapError(err)
}
}
if response.InstanceId+":"+response.Topic == id && status != Deleted {
return nil
}
if time.Now().After(deadline) {
return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, instanceId+":"+topic, id, ProviderERROR)
}
time.Sleep(DefaultIntervalShort * time.Second)
}
}
func (s *OnsService) WaitForOnsGroup(id string, status Status, timeout int) error {
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
for {
response, err := s.DescribeOnsGroup(id)
if err != nil {
if NotFoundError(err) {
if status == Deleted {
return nil
}
} else {
return WrapError(err)
}
}
if response.InstanceId+":"+response.GroupId == id && status != Deleted {
return nil
}
if time.Now().After(deadline) {
return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, response.InstanceId+":"+response.GroupId, id, ProviderERROR)
}
time.Sleep(DefaultIntervalShort * time.Second)
}
}