Skip to content

Commit

Permalink
ADB: Improves the invoking adb api method and supports refreshing cre…
Browse files Browse the repository at this point in the history
…dential automatically
  • Loading branch information
xiaozhu36 committed Dec 27, 2024
1 parent 75419fa commit 18dc490
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 421 deletions.
53 changes: 0 additions & 53 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2021,35 +2021,6 @@ func (client *AliyunClient) WithHbaseClient(do func(*hbase.Client) (interface{},
return do(client.hbaseconn)
}

func (client *AliyunClient) NewAdbClient() (*rpc.Client, error) {
productCode := "ads"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if v := client.config.AdbEndpoint; v != "" {
client.config.Endpoints.Store(productCode, v)
}
if err := client.loadEndpoint(productCode); err != nil {
return nil, err
}
}
if v, exist := client.config.Endpoints.Load(productCode); exist && 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) WithAdbClient(do func(*adb.Client) (interface{}, error)) (interface{}, error) {
// Initialize the adb client if necessary
if client.adbconn == nil {
Expand Down Expand Up @@ -3001,30 +2972,6 @@ func (client *AliyunClient) NewHcsSgwClient() (*rpc.Client, error) {
}
return conn, nil
}

func (client *AliyunClient) NewAdsClient() (*rpc.Client, error) {
productCode := "ads"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
return nil, err
}
}
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) NewDdoscooClient() (*rpc.Client, error) {
productCode := "ddoscoo"
endpoint := ""
Expand Down
11 changes: 2 additions & 9 deletions alicloud/data_source_alicloud_adb_db_cluster_lake_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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 @@ -182,28 +181,22 @@ func dataSourceAlicloudAdbDbClusterLakeVersionsRead(d *schema.ResourceData, meta
}
}

conn, err := client.NewAdsClient()
if err != nil {
return WrapError(err)
}
var objects []interface{}
var response map[string]interface{}
var err error

for {
action := "DescribeDBClusters"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, err := conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2021-12-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("adb", "2021-12-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
response = resp
addDebug(action, response, request)
return nil
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,16 @@ variable "name" {
data "alicloud_resource_manager_resource_groups" "default" {}
data "alicloud_zones" "default" {
available_resource_creation = "VSwitch"
data "alicloud_adb_zones" "default" {
}
data "alicloud_vpcs" "default" {
name_regex = "^default-NODELETING$"
name_regex = "^default-NODELETING$"
}
data "alicloud_vswitches" "default" {
vpc_id = data.alicloud_vpcs.default.ids.0
zone_id = data.alicloud_zones.default.ids[length(data.alicloud_zones.default.ids) - 1]
vpc_id = data.alicloud_vpcs.default.ids.0
zone_id = data.alicloud_adb_zones.default.ids.0
}
resource "alicloud_adb_db_cluster_lake_version" "default" {
Expand All @@ -129,7 +128,7 @@ resource "alicloud_adb_db_cluster_lake_version" "default" {
storage_resource = "0ACU"
vswitch_id = data.alicloud_vswitches.default.ids.0
vpc_id = data.alicloud_vpcs.default.ids.0
zone_id = data.alicloud_zones.default.ids[length(data.alicloud_zones.default.ids) - 1]
zone_id = data.alicloud_adb_zones.default.ids.0
}
data "alicloud_adb_db_cluster_lake_versions" "default" {
Expand Down
52 changes: 44 additions & 8 deletions alicloud/data_source_alicloud_adb_db_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"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/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -242,6 +241,30 @@ func dataSourceAlicloudAdbDbClusters() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"kernel_version": {
Type: schema.TypeString,
Computed: true,
},
"available_kernel_versions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kernel_version": {
Type: schema.TypeString,
Computed: true,
},
"release_date": {
Type: schema.TypeString,
Computed: true,
},
"expire_date": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -313,14 +336,9 @@ func dataSourceAlicloudAdbDbClustersRead(d *schema.ResourceData, meta interface{
}
}
var response map[string]interface{}
conn, err := client.NewAdsClient()
if err != nil {
return WrapError(err)
}
var err error
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-03-15"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("adb", "2019-03-15", action, nil, request, true)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_adb_db_clusters", action, AlibabaCloudSdkGoERROR)
}
Expand Down Expand Up @@ -440,6 +458,24 @@ func dataSourceAlicloudAdbDbClustersRead(d *schema.ResourceData, meta interface{
}
mapping["engine_version"] = getResp2["EngineVersion"]
mapping["maintain_time"] = getResp2["MaintainTime"]
getResp3, err := adbService.DescribeAdbDbClusterKernelVersion(id)
if err != nil {
return WrapError(err)
}
mapping["kernel_version"] = getResp3["KernelVersion"]
availableKernelVersion := getResp3["AvailableKernelVersion"]
availKernels := make([]map[string]interface{}, 0)
if availableKernelVersion != nil {
for _, v := range availableKernelVersion.([]interface{}) {
object := v.(map[string]interface{})
availKernels = append(availKernels, map[string]interface{}{
"kernel_version": object["KernelVersion"],
"release_date": object["ReleaseDate"],
"expire_date": object["ExpireDate"],
})
}
mapping["available_kernel_versions"] = availKernels
}

ids = append(ids, fmt.Sprint(object["DBClusterId"]))
s = append(s, mapping)
Expand Down
1 change: 1 addition & 0 deletions alicloud/data_source_alicloud_adb_db_clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestAccAlicloudADBDbClustersDataSource(t *testing.T) {
"clusters.0.maintain_time": "23:00Z-00:00Z",
"clusters.0.security_ips.#": "2",
"clusters.0.mode": "flexible",
"clusters.0.kernel_version": CHECKSET,
}
}

Expand Down
11 changes: 2 additions & 9 deletions alicloud/data_source_alicloud_adb_resource_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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 @@ -99,26 +98,20 @@ func dataSourceAlicloudAdbResourceGroupsRead(d *schema.ResourceData, meta interf
}
}

conn, err := client.NewAdsClient()
if err != nil {
return WrapError(err)
}
var objects []interface{}
var response map[string]interface{}
var err error
action := "DescribeDBResourceGroup"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, err := conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-03-15"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("adb", "2019-03-15", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
response = resp
addDebug(action, response, request)
return nil
})
Expand Down
Loading

0 comments on commit 18dc490

Please sign in to comment.