Skip to content

Commit aa9c640

Browse files
committed
BastionHost: Improves the invoking api method and supports refreshing credential automatically
1 parent a6d0394 commit aa9c640

File tree

28 files changed

+73
-371
lines changed

28 files changed

+73
-371
lines changed

alicloud/connectivity/client.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ import (
5959
"github.com/aliyun/alibaba-cloud-sdk-go/services/smartag"
6060
"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
6161
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
62-
"github.com/aliyun/alibaba-cloud-sdk-go/services/yundun_bastionhost"
6362
"github.com/aliyun/alibaba-cloud-sdk-go/services/yundun_dbaudit"
6463
"github.com/aliyun/aliyun-datahub-sdk-go/datahub"
6564
sls "github.com/aliyun/aliyun-log-go-sdk"
@@ -135,7 +134,6 @@ type AliyunClient struct {
135134
emrconn *emr.Client
136135
sagconn *smartag.Client
137136
dbauditconn *yundun_dbaudit.Client
138-
bastionhostconn *yundun_bastionhost.Client
139137
marketconn *market.Client
140138
hbaseconn *hbase.Client
141139
adbconn *adb.Client
@@ -1910,27 +1908,6 @@ func (client *AliyunClient) WithDbauditClient(do func(*yundun_dbaudit.Client) (i
19101908

19111909
return do(client.dbauditconn)
19121910
}
1913-
1914-
func (client *AliyunClient) WithBastionhostClient(do func(*yundun_bastionhost.Client) (interface{}, error)) (interface{}, error) {
1915-
if client.bastionhostconn == nil {
1916-
bastionhostconn, err := yundun_bastionhost.NewClientWithOptions(client.config.RegionId, client.getSdkConfig(), client.config.getAuthCredential(true))
1917-
if err != nil {
1918-
return nil, fmt.Errorf("unable to initialize the BASTIONHOST client: %#v", err)
1919-
}
1920-
bastionhostconn.SetReadTimeout(time.Duration(client.config.ClientReadTimeout) * time.Millisecond)
1921-
bastionhostconn.SetConnectTimeout(time.Duration(client.config.ClientConnectTimeout) * time.Millisecond)
1922-
bastionhostconn.SourceIp = client.config.SourceIp
1923-
bastionhostconn.SecureTransport = client.config.SecureTransport
1924-
bastionhostconn.AppendUserAgent(Terraform, client.config.TerraformVersion)
1925-
bastionhostconn.AppendUserAgent(Provider, providerVersion)
1926-
bastionhostconn.AppendUserAgent(Module, client.config.ConfigurationSource)
1927-
bastionhostconn.AppendUserAgent(TerraformTraceId, client.config.TerraformTraceId)
1928-
client.bastionhostconn = bastionhostconn
1929-
}
1930-
1931-
return do(client.bastionhostconn)
1932-
}
1933-
19341911
func (client *AliyunClient) WithMarketClient(do func(*market.Client) (interface{}, error)) (interface{}, error) {
19351912
// Initialize the Market API client if necessary
19361913
if client.marketconn == nil {
@@ -3348,31 +3325,6 @@ func (client *AliyunClient) NewSddpClient() (*rpc.Client, error) {
33483325
return conn, nil
33493326
}
33503327

3351-
func (client *AliyunClient) NewBastionhostClient() (*rpc.Client, error) {
3352-
productCode := "bastionhost"
3353-
endpoint := ""
3354-
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
3355-
if err := client.loadEndpoint(productCode); err != nil {
3356-
endpoint = fmt.Sprintf("bastionhost.%s.aliyuncs.com", client.config.RegionId)
3357-
client.config.Endpoints.Store(productCode, endpoint)
3358-
log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, endpoint)
3359-
}
3360-
}
3361-
if v, ok := client.config.Endpoints.Load(productCode); ok && v.(string) != "" {
3362-
endpoint = v.(string)
3363-
}
3364-
if endpoint == "" {
3365-
return nil, fmt.Errorf("[ERROR] missing the product %s endpoint.", productCode)
3366-
}
3367-
sdkConfig := client.teaSdkConfig
3368-
sdkConfig.SetEndpoint(endpoint)
3369-
conn, err := rpc.NewClient(&sdkConfig)
3370-
if err != nil {
3371-
return nil, fmt.Errorf("unable to initialize the %s client: %#v", productCode, err)
3372-
}
3373-
return conn, nil
3374-
}
3375-
33763328
func (client *AliyunClient) NewSasClient() (*rpc.Client, error) {
33773329
productCode := "sas"
33783330
endpoint := ""

alicloud/data_source_alicloud_bastionhost_host_accounts.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/PaesslerAG/jsonpath"
9-
util "github.com/alibabacloud-go/tea-utils/service"
109
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
1110
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1211
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -140,16 +139,11 @@ func dataSourceAlicloudBastionhostHostAccountsRead(d *schema.ResourceData, meta
140139
}
141140
}
142141
var response map[string]interface{}
143-
conn, err := client.NewBastionhostClient()
144-
if err != nil {
145-
return WrapError(err)
146-
}
142+
var err error
147143
for {
148-
runtime := util.RuntimeOptions{}
149-
runtime.SetAutoretry(true)
150144
wait := incrementalWait(3*time.Second, 3*time.Second)
151145
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
152-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
146+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
153147
if err != nil {
154148
if NeedRetry(err) {
155149
wait()

alicloud/data_source_alicloud_bastionhost_host_groups.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/PaesslerAG/jsonpath"
9-
util "github.com/alibabacloud-go/tea-utils/service"
109
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
1110
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1211
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -113,16 +112,11 @@ func dataSourceAlicloudBastionhostHostGroupsRead(d *schema.ResourceData, meta in
113112
}
114113
}
115114
var response map[string]interface{}
116-
conn, err := client.NewBastionhostClient()
117-
if err != nil {
118-
return WrapError(err)
119-
}
115+
var err error
120116
for {
121-
runtime := util.RuntimeOptions{}
122-
runtime.SetAutoretry(true)
123117
wait := incrementalWait(3*time.Second, 3*time.Second)
124118
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
125-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
119+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
126120
if err != nil {
127121
if NeedRetry(err) {
128122
wait()

alicloud/data_source_alicloud_bastionhost_host_share_keys.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/PaesslerAG/jsonpath"
9-
util "github.com/alibabacloud-go/tea-utils/service"
109
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
1110
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1211
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -109,16 +108,11 @@ func dataSourceAlicloudBastionhostHostShareKeysRead(d *schema.ResourceData, meta
109108
}
110109
}
111110
var response map[string]interface{}
112-
conn, err := client.NewBastionhostClient()
113-
if err != nil {
114-
return WrapError(err)
115-
}
111+
var err error
116112
for {
117-
runtime := util.RuntimeOptions{}
118-
runtime.SetAutoretry(true)
119113
wait := incrementalWait(3*time.Second, 3*time.Second)
120114
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
121-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
115+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
122116
if err != nil {
123117
if NeedRetry(err) {
124118
wait()

alicloud/data_source_alicloud_bastionhost_hosts.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/PaesslerAG/jsonpath"
9-
util "github.com/alibabacloud-go/tea-utils/service"
109
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
1110
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1211
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -204,16 +203,11 @@ func dataSourceAlicloudBastionhostHostsRead(d *schema.ResourceData, meta interfa
204203
}
205204
}
206205
var response map[string]interface{}
207-
conn, err := client.NewBastionhostClient()
208-
if err != nil {
209-
return WrapError(err)
210-
}
206+
var err error
211207
for {
212-
runtime := util.RuntimeOptions{}
213-
runtime.SetAutoretry(true)
214208
wait := incrementalWait(3*time.Second, 3*time.Second)
215209
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
216-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
210+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
217211
if err != nil {
218212
if NeedRetry(err) {
219213
wait()

alicloud/data_source_alicloud_bastionhost_user_groups.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/PaesslerAG/jsonpath"
9-
util "github.com/alibabacloud-go/tea-utils/service"
109
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
1110
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1211
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -113,16 +112,11 @@ func dataSourceAlicloudBastionhostUserGroupsRead(d *schema.ResourceData, meta in
113112
}
114113
}
115114
var response map[string]interface{}
116-
conn, err := client.NewBastionhostClient()
117-
if err != nil {
118-
return WrapError(err)
119-
}
115+
var err error
120116
for {
121-
runtime := util.RuntimeOptions{}
122-
runtime.SetAutoretry(true)
123117
wait := incrementalWait(3*time.Second, 3*time.Second)
124118
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
125-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
119+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
126120
if err != nil {
127121
if NeedRetry(err) {
128122
wait()

alicloud/data_source_alicloud_bastionhost_users.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/PaesslerAG/jsonpath"
9-
util "github.com/alibabacloud-go/tea-utils/service"
109
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
1110
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1211
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -183,16 +182,11 @@ func dataSourceAlicloudBastionhostUsersRead(d *schema.ResourceData, meta interfa
183182
}
184183
}
185184
var response map[string]interface{}
186-
conn, err := client.NewBastionhostClient()
187-
if err != nil {
188-
return WrapError(err)
189-
}
185+
var err error
190186
for {
191-
runtime := util.RuntimeOptions{}
192-
runtime.SetAutoretry(true)
193187
wait := incrementalWait(3*time.Second, 3*time.Second)
194188
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
195-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
189+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
196190
if err != nil {
197191
if NeedRetry(err) {
198192
wait()

alicloud/resource_alicloud_bastionhost_host.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66
"time"
77

8-
util "github.com/alibabacloud-go/tea-utils/service"
98
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
109
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1110
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -81,10 +80,7 @@ func resourceAlicloudBastionhostHostCreate(d *schema.ResourceData, meta interfac
8180
var response map[string]interface{}
8281
action := "CreateHost"
8382
request := make(map[string]interface{})
84-
conn, err := client.NewBastionhostClient()
85-
if err != nil {
86-
return WrapError(err)
87-
}
83+
var err error
8884
request["ActiveAddressType"] = d.Get("active_address_type")
8985
if v, ok := d.GetOk("comment"); ok {
9086
request["Comment"] = v
@@ -108,7 +104,7 @@ func resourceAlicloudBastionhostHostCreate(d *schema.ResourceData, meta interfac
108104
}
109105
wait := incrementalWait(3*time.Second, 3*time.Second)
110106
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
111-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
107+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
112108
if err != nil {
113109
if NeedRetry(err) {
114110
wait()
@@ -158,10 +154,7 @@ func resourceAlicloudBastionhostHostRead(d *schema.ResourceData, meta interface{
158154
func resourceAlicloudBastionhostHostUpdate(d *schema.ResourceData, meta interface{}) error {
159155
client := meta.(*connectivity.AliyunClient)
160156
var response map[string]interface{}
161-
conn, err := client.NewBastionhostClient()
162-
if err != nil {
163-
return WrapError(err)
164-
}
157+
var err error
165158
parts, err := ParseResourceId(d.Id(), 2)
166159
if err != nil {
167160
return WrapError(err)
@@ -182,7 +175,7 @@ func resourceAlicloudBastionhostHostUpdate(d *schema.ResourceData, meta interfac
182175
action := "ModifyHostsActiveAddressType"
183176
wait := incrementalWait(3*time.Second, 3*time.Second)
184177
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
185-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
178+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
186179
if err != nil {
187180
if NeedRetry(err) {
188181
wait()
@@ -234,7 +227,7 @@ func resourceAlicloudBastionhostHostUpdate(d *schema.ResourceData, meta interfac
234227
action := "ModifyHost"
235228
wait := incrementalWait(3*time.Second, 3*time.Second)
236229
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
237-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, modifyHostReq, &util.RuntimeOptions{})
230+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, modifyHostReq, false)
238231
if err != nil {
239232
if NeedRetry(err) {
240233
wait()
@@ -265,10 +258,6 @@ func resourceAlicloudBastionhostHostDelete(d *schema.ResourceData, meta interfac
265258
}
266259
action := "DeleteHost"
267260
var response map[string]interface{}
268-
conn, err := client.NewBastionhostClient()
269-
if err != nil {
270-
return WrapError(err)
271-
}
272261
request := map[string]interface{}{
273262
"HostId": parts[1],
274263
"InstanceId": parts[0],
@@ -277,7 +266,7 @@ func resourceAlicloudBastionhostHostDelete(d *schema.ResourceData, meta interfac
277266
request["RegionId"] = client.RegionId
278267
wait := incrementalWait(3*time.Second, 3*time.Second)
279268
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
280-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
269+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
281270
if err != nil {
282271
if NeedRetry(err) {
283272
wait()

alicloud/resource_alicloud_bastionhost_host_account.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66
"time"
77

8-
util "github.com/alibabacloud-go/tea-utils/service"
98
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
109
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1110
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -82,10 +81,7 @@ func resourceAlicloudBastionhostHostAccountCreate(d *schema.ResourceData, meta i
8281
var response map[string]interface{}
8382
action := "CreateHostAccount"
8483
request := make(map[string]interface{})
85-
conn, err := client.NewBastionhostClient()
86-
if err != nil {
87-
return WrapError(err)
88-
}
84+
var err error
8985
request["HostAccountName"] = d.Get("host_account_name")
9086
request["HostId"] = d.Get("host_id")
9187
request["InstanceId"] = d.Get("instance_id")
@@ -102,7 +98,7 @@ func resourceAlicloudBastionhostHostAccountCreate(d *schema.ResourceData, meta i
10298
request["RegionId"] = client.RegionId
10399
wait := incrementalWait(3*time.Second, 3*time.Second)
104100
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
105-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
101+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
106102
if err != nil {
107103
if NeedRetry(err) {
108104
wait()
@@ -147,10 +143,7 @@ func resourceAlicloudBastionhostHostAccountRead(d *schema.ResourceData, meta int
147143
func resourceAlicloudBastionhostHostAccountUpdate(d *schema.ResourceData, meta interface{}) error {
148144
client := meta.(*connectivity.AliyunClient)
149145
var response map[string]interface{}
150-
conn, err := client.NewBastionhostClient()
151-
if err != nil {
152-
return WrapError(err)
153-
}
146+
var err error
154147
parts, err := ParseResourceId(d.Id(), 2)
155148
if err != nil {
156149
return WrapError(err)
@@ -187,7 +180,7 @@ func resourceAlicloudBastionhostHostAccountUpdate(d *schema.ResourceData, meta i
187180
action := "ModifyHostAccount"
188181
wait := incrementalWait(3*time.Second, 3*time.Second)
189182
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
190-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
183+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
191184
if err != nil {
192185
if NeedRetry(err) {
193186
wait()
@@ -212,10 +205,6 @@ func resourceAlicloudBastionhostHostAccountDelete(d *schema.ResourceData, meta i
212205
}
213206
action := "DeleteHostAccount"
214207
var response map[string]interface{}
215-
conn, err := client.NewBastionhostClient()
216-
if err != nil {
217-
return WrapError(err)
218-
}
219208
request := map[string]interface{}{
220209
"HostAccountId": parts[1],
221210
"InstanceId": parts[0],
@@ -224,7 +213,7 @@ func resourceAlicloudBastionhostHostAccountDelete(d *schema.ResourceData, meta i
224213
request["RegionId"] = client.RegionId
225214
wait := incrementalWait(3*time.Second, 3*time.Second)
226215
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
227-
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
216+
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
228217
if err != nil {
229218
if NeedRetry(err) {
230219
wait()

0 commit comments

Comments
 (0)