/
service_alicloud_ocean_base_v2.go
139 lines (119 loc) · 4.16 KB
/
service_alicloud_ocean_base_v2.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
package alicloud
import (
"fmt"
"time"
"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/blues/jsonata-go"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)
type OceanBaseServiceV2 struct {
client *connectivity.AliyunClient
}
// DescribeOceanBaseInstance <<< Encapsulated get interface for OceanBase Instance.
func (s *OceanBaseServiceV2) DescribeOceanBaseInstance(id string) (object map[string]interface{}, err error) {
client := s.client
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
action := "DescribeInstance"
conn, err := client.NewOceanbaseClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = id
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-01"), StringPointer("AK"), query, request, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
if err != nil {
if IsExpectedErrors(err, []string{"IllegalOperation.Resource", "UnknownError"}) {
return object, WrapErrorf(Error(GetNotFoundMessage("Instance", id)), NotFoundMsg, response)
}
return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR)
}
v, err := jsonpath.Get("$.Instance", response)
if err != nil {
return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.Instance", response)
}
return v.(map[string]interface{}), nil
}
func (s *OceanBaseServiceV2) DescribeDescribeInstances(id string) (object map[string]interface{}, err error) {
client := s.client
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
action := "DescribeInstances"
conn, err := client.NewOceanbaseClient()
if err != nil {
return object, WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = id
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-09-01"), StringPointer("AK"), query, request, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
if err != nil {
return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR)
}
v, err := jsonpath.Get("$.Instances[*]", response)
if err != nil {
return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.Instances[*]", response)
}
if len(v.([]interface{})) == 0 {
return object, WrapErrorf(Error(GetNotFoundMessage("Instance", id)), NotFoundMsg, response)
}
return v.([]interface{})[0].(map[string]interface{}), nil
}
func (s *OceanBaseServiceV2) OceanBaseInstanceStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
object, err := s.DescribeOceanBaseInstance(id)
if err != nil {
if NotFoundError(err) {
return nil, "", nil
}
return nil, "", WrapError(err)
}
v, err := jsonpath.Get(field, object)
currentStatus := fmt.Sprint(v)
if field == "$.InstanceClass" {
e := jsonata.MustCompile("$.InstanceClass & 'B'")
v, _ = e.Eval(object)
currentStatus = fmt.Sprint(v)
}
for _, failState := range failStates {
if currentStatus == failState {
return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus))
}
}
return object, currentStatus, nil
}
}
// DescribeOceanBaseInstance >>> Encapsulated.