Skip to content

Commit

Permalink
BastionHost: Improves the invoking api method and supports refreshing…
Browse files Browse the repository at this point in the history
… credential automatically
  • Loading branch information
xiaozhu36 committed Feb 1, 2025
1 parent a6d0394 commit aa9c640
Show file tree
Hide file tree
Showing 28 changed files with 73 additions and 371 deletions.
48 changes: 0 additions & 48 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/services/smartag"
"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/aliyun/alibaba-cloud-sdk-go/services/yundun_bastionhost"
"github.com/aliyun/alibaba-cloud-sdk-go/services/yundun_dbaudit"
"github.com/aliyun/aliyun-datahub-sdk-go/datahub"
sls "github.com/aliyun/aliyun-log-go-sdk"
Expand Down Expand Up @@ -135,7 +134,6 @@ type AliyunClient struct {
emrconn *emr.Client
sagconn *smartag.Client
dbauditconn *yundun_dbaudit.Client
bastionhostconn *yundun_bastionhost.Client
marketconn *market.Client
hbaseconn *hbase.Client
adbconn *adb.Client
Expand Down Expand Up @@ -1910,27 +1908,6 @@ func (client *AliyunClient) WithDbauditClient(do func(*yundun_dbaudit.Client) (i

return do(client.dbauditconn)
}

func (client *AliyunClient) WithBastionhostClient(do func(*yundun_bastionhost.Client) (interface{}, error)) (interface{}, error) {
if client.bastionhostconn == nil {
bastionhostconn, err := yundun_bastionhost.NewClientWithOptions(client.config.RegionId, client.getSdkConfig(), client.config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the BASTIONHOST client: %#v", err)
}
bastionhostconn.SetReadTimeout(time.Duration(client.config.ClientReadTimeout) * time.Millisecond)
bastionhostconn.SetConnectTimeout(time.Duration(client.config.ClientConnectTimeout) * time.Millisecond)
bastionhostconn.SourceIp = client.config.SourceIp
bastionhostconn.SecureTransport = client.config.SecureTransport
bastionhostconn.AppendUserAgent(Terraform, client.config.TerraformVersion)
bastionhostconn.AppendUserAgent(Provider, providerVersion)
bastionhostconn.AppendUserAgent(Module, client.config.ConfigurationSource)
bastionhostconn.AppendUserAgent(TerraformTraceId, client.config.TerraformTraceId)
client.bastionhostconn = bastionhostconn
}

return do(client.bastionhostconn)
}

func (client *AliyunClient) WithMarketClient(do func(*market.Client) (interface{}, error)) (interface{}, error) {
// Initialize the Market API client if necessary
if client.marketconn == nil {
Expand Down Expand Up @@ -3348,31 +3325,6 @@ func (client *AliyunClient) NewSddpClient() (*rpc.Client, error) {
return conn, nil
}

func (client *AliyunClient) NewBastionhostClient() (*rpc.Client, error) {
productCode := "bastionhost"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
endpoint = fmt.Sprintf("bastionhost.%s.aliyuncs.com", client.config.RegionId)
client.config.Endpoints.Store(productCode, endpoint)
log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, endpoint)
}
}
if v, ok := client.config.Endpoints.Load(productCode); ok && v.(string) != "" {
endpoint = v.(string)
}
if endpoint == "" {
return nil, fmt.Errorf("[ERROR] missing the product %s endpoint.", productCode)
}
sdkConfig := client.teaSdkConfig
sdkConfig.SetEndpoint(endpoint)
conn, err := rpc.NewClient(&sdkConfig)
if err != nil {
return nil, fmt.Errorf("unable to initialize the %s client: %#v", productCode, err)
}
return conn, nil
}

func (client *AliyunClient) NewSasClient() (*rpc.Client, error) {
productCode := "sas"
endpoint := ""
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_bastionhost_host_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -140,16 +139,11 @@ func dataSourceAlicloudBastionhostHostAccountsRead(d *schema.ResourceData, meta
}
}
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_bastionhost_host_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -113,16 +112,11 @@ func dataSourceAlicloudBastionhostHostGroupsRead(d *schema.ResourceData, meta in
}
}
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_bastionhost_host_share_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -109,16 +108,11 @@ func dataSourceAlicloudBastionhostHostShareKeysRead(d *schema.ResourceData, meta
}
}
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_bastionhost_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -204,16 +203,11 @@ func dataSourceAlicloudBastionhostHostsRead(d *schema.ResourceData, meta interfa
}
}
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_bastionhost_user_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -113,16 +112,11 @@ func dataSourceAlicloudBastionhostUserGroupsRead(d *schema.ResourceData, meta in
}
}
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_bastionhost_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -183,16 +182,11 @@ func dataSourceAlicloudBastionhostUsersRead(d *schema.ResourceData, meta interfa
}
}
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
23 changes: 6 additions & 17 deletions alicloud/resource_alicloud_bastionhost_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"time"

util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -81,10 +80,7 @@ func resourceAlicloudBastionhostHostCreate(d *schema.ResourceData, meta interfac
var response map[string]interface{}
action := "CreateHost"
request := make(map[string]interface{})
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
request["ActiveAddressType"] = d.Get("active_address_type")
if v, ok := d.GetOk("comment"); ok {
request["Comment"] = v
Expand All @@ -108,7 +104,7 @@ func resourceAlicloudBastionhostHostCreate(d *schema.ResourceData, meta interfac
}
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -158,10 +154,7 @@ func resourceAlicloudBastionhostHostRead(d *schema.ResourceData, meta interface{
func resourceAlicloudBastionhostHostUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
Expand All @@ -182,7 +175,7 @@ func resourceAlicloudBastionhostHostUpdate(d *schema.ResourceData, meta interfac
action := "ModifyHostsActiveAddressType"
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -234,7 +227,7 @@ func resourceAlicloudBastionhostHostUpdate(d *schema.ResourceData, meta interfac
action := "ModifyHost"
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, modifyHostReq, &util.RuntimeOptions{})
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, modifyHostReq, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -265,10 +258,6 @@ func resourceAlicloudBastionhostHostDelete(d *schema.ResourceData, meta interfac
}
action := "DeleteHost"
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"HostId": parts[1],
"InstanceId": parts[0],
Expand All @@ -277,7 +266,7 @@ func resourceAlicloudBastionhostHostDelete(d *schema.ResourceData, meta interfac
request["RegionId"] = client.RegionId
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
21 changes: 5 additions & 16 deletions alicloud/resource_alicloud_bastionhost_host_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"time"

util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -82,10 +81,7 @@ func resourceAlicloudBastionhostHostAccountCreate(d *schema.ResourceData, meta i
var response map[string]interface{}
action := "CreateHostAccount"
request := make(map[string]interface{})
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
request["HostAccountName"] = d.Get("host_account_name")
request["HostId"] = d.Get("host_id")
request["InstanceId"] = d.Get("instance_id")
Expand All @@ -102,7 +98,7 @@ func resourceAlicloudBastionhostHostAccountCreate(d *schema.ResourceData, meta i
request["RegionId"] = client.RegionId
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -147,10 +143,7 @@ func resourceAlicloudBastionhostHostAccountRead(d *schema.ResourceData, meta int
func resourceAlicloudBastionhostHostAccountUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
var err error
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
Expand Down Expand Up @@ -187,7 +180,7 @@ func resourceAlicloudBastionhostHostAccountUpdate(d *schema.ResourceData, meta i
action := "ModifyHostAccount"
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -212,10 +205,6 @@ func resourceAlicloudBastionhostHostAccountDelete(d *schema.ResourceData, meta i
}
action := "DeleteHostAccount"
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"HostAccountId": parts[1],
"InstanceId": parts[0],
Expand All @@ -224,7 +213,7 @@ func resourceAlicloudBastionhostHostAccountDelete(d *schema.ResourceData, meta i
request["RegionId"] = client.RegionId
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Yundun-bastionhost", "2019-12-09", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Loading

0 comments on commit aa9c640

Please sign in to comment.