Skip to content

Commit

Permalink
Config: Improves the invoking config api method and supports refreshi…
Browse files Browse the repository at this point in the history
…ng credential automatically
  • Loading branch information
xiaozhu36 committed Dec 26, 2024
1 parent 25f7256 commit ea4ca0c
Show file tree
Hide file tree
Showing 50 changed files with 533 additions and 690 deletions.
1 change: 1 addition & 0 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5194,6 +5194,7 @@ func (client *AliyunClient) NewSlsClient() (*openapi.Client, error) {
AccessKeyId: tea.String(client.config.AccessKey),
AccessKeySecret: tea.String(client.config.SecretKey),
SecurityToken: tea.String(client.config.SecurityToken),
UserAgent: tea.String(client.config.getUserAgent()),
}

endpoint := client.config.LogEndpoint
Expand Down
14 changes: 9 additions & 5 deletions alicloud/connectivity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (c *Config) setAuthCredentialByOidc() (err error) {
conf := &openapi.Config{
RegionId: tea.String(c.RegionId),
Endpoint: tea.String(c.StsEndpoint),
UserAgent: tea.String(fmt.Sprintf("%s/%s %s/%s %s/%s %s/%s", Terraform, c.TerraformVersion, Provider, providerVersion, Module, c.ConfigurationSource, TerraformTraceId, c.TerraformTraceId)),
UserAgent: tea.String(c.getUserAgent()),
// currently, sts endpoint only supports https
Protocol: tea.String("HTTPS"),
ReadTimeout: tea.Int(c.ClientReadTimeout),
Expand Down Expand Up @@ -465,7 +465,7 @@ func (c *Config) RefreshAuthCredential() error {

func (c *Config) getTeaDslSdkConfig(stsSupported bool) (config rpc.Config, err error) {
config.SetRegionId(c.RegionId)
config.SetUserAgent(fmt.Sprintf("%s/%s %s/%s %s/%s %s/%s", Terraform, c.TerraformVersion, Provider, providerVersion, Module, c.ConfigurationSource, TerraformTraceId, c.TerraformTraceId))
config.SetUserAgent(c.getUserAgent())
credential, err := credential.NewCredential(c.getCredentialConfig(stsSupported))
config.SetCredential(credential).
SetRegionId(c.RegionId).
Expand All @@ -484,7 +484,7 @@ func (c *Config) getTeaDslSdkConfig(stsSupported bool) (config rpc.Config, err e
}
func (c *Config) getTeaRoaDslSdkConfig(stsSupported bool) (config roa.Config, err error) {
config.SetRegionId(c.RegionId)
config.SetUserAgent(fmt.Sprintf("%s/%s %s/%s %s/%s %s/%s", Terraform, c.TerraformVersion, Provider, providerVersion, Module, c.ConfigurationSource, TerraformTraceId, c.TerraformTraceId))
config.SetUserAgent(c.getUserAgent())
credential, err := credential.NewCredential(c.getCredentialConfig(stsSupported))
config.SetCredential(credential).
SetRegionId(c.RegionId).
Expand All @@ -502,7 +502,7 @@ func (c *Config) getTeaRoaDslSdkConfig(stsSupported bool) (config roa.Config, er
}
func (c *Config) getTeaRpcOpenapiConfig(stsSupported bool) (config openapi.Config, err error) {
config.SetRegionId(c.RegionId)
config.SetUserAgent(fmt.Sprintf("%s/%s %s/%s %s/%s %s/%s", Terraform, c.TerraformVersion, Provider, providerVersion, Module, c.ConfigurationSource, TerraformTraceId, c.TerraformTraceId))
config.SetUserAgent(c.getUserAgent())
credential, err := credential.NewCredential(c.getCredentialConfig(stsSupported))
config.SetCredential(credential).
SetRegionId(c.RegionId).
Expand All @@ -527,7 +527,7 @@ func (c *Config) getTeaRpcOpenapiConfig(stsSupported bool) (config openapi.Confi
}
func (c *Config) getTeaRoaOpenapiConfig(stsSupported bool) (config openapi.Config, err error) {
config.SetRegionId(c.RegionId)
config.SetUserAgent(fmt.Sprintf("%s/%s %s/%s %s/%s %s/%s", Terraform, c.TerraformVersion, Provider, providerVersion, Module, c.ConfigurationSource, TerraformTraceId, c.TerraformTraceId))
config.SetUserAgent(c.getUserAgent())
credential, err := credential.NewCredential(c.getCredentialConfig(stsSupported))
config.SetCredential(credential).
SetRegionId(c.RegionId).
Expand Down Expand Up @@ -578,3 +578,7 @@ func (c *Config) getCredentialConfig(stsSupported bool) *credential.Config {
credentialConfig.Type = &credentialType
return credentialConfig
}

func (c *Config) getUserAgent() string {
return fmt.Sprintf("%s/%s %s/%s %s/%s %s/%s", Terraform, c.TerraformVersion, Provider, providerVersion, Module, c.ConfigurationSource, TerraformTraceId, c.TerraformTraceId)
}
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 @@ -164,16 +163,11 @@ func dataSourceAlicloudConfigAggregateCompliancePacksRead(d *schema.ResourceData
}
status, statusOk := d.GetOk("status")
var response map[string]interface{}
conn, err := client.NewConfigClient()
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("GET"), StringPointer("2020-09-07"), StringPointer("AK"), request, nil, &runtime)
response, err = client.RpcGet("Config", "2020-09-07", action, request, nil)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudConfigAggregateCompliancePacksDataSource(t *testing.T) {
func TestAccAliCloudConfigAggregateCompliancePacksDataSource(t *testing.T) {
rand := acctest.RandInt()
idsConf := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudConfigAggregateCompliancePacksDataSourceName(rand, map[string]string{
Expand Down Expand Up @@ -92,11 +92,22 @@ variable "name" {
default = "tf-testAccAggregateCompliancePack-%d"
}
data "alicloud_config_aggregators" "default" {}
data "alicloud_resource_manager_accounts" "default" {
status = "CreateSuccess"
}
resource "alicloud_config_aggregator" "default" {
aggregator_accounts {
account_id = data.alicloud_resource_manager_accounts.default.accounts.1.account_id
account_name = data.alicloud_resource_manager_accounts.default.accounts.1.display_name
account_type = "ResourceDirectory"
}
aggregator_name = var.name
description = var.name
}
resource "alicloud_config_aggregate_compliance_pack" "default" {
aggregate_compliance_pack_name = var.name
aggregator_id = data.alicloud_config_aggregators.default.ids.0
aggregator_id = alicloud_config_aggregator.default.id
compliance_pack_template_id = "ct-3d20ff4e06a30027f76e"
description = var.name
risk_level = "1"
Expand Down
11 changes: 3 additions & 8 deletions alicloud/data_source_alicloud_config_aggregate_config_rules.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 @@ -205,6 +204,7 @@ func dataSourceAlicloudConfigAggregateConfigRulesRead(d *schema.ResourceData, me
request["PageSize"] = PageSizeLarge
request["PageNumber"] = 1
var objects []map[string]interface{}
var err error
var aggregateConfigRuleNameRegex *regexp.Regexp
if v, ok := d.GetOk("name_regex"); ok {
r, err := regexp.Compile(v.(string))
Expand All @@ -224,16 +224,11 @@ func dataSourceAlicloudConfigAggregateConfigRulesRead(d *schema.ResourceData, me
}
}
var response map[string]interface{}
conn, err := client.NewConfigClient()
if err != nil {
return WrapError(err)
}

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("GET"), StringPointer("2020-09-07"), StringPointer("AK"), request, nil, &runtime)
response, err = client.RpcGet("Config", "2020-09-07", action, request, nil)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudConfigAggregateConfigRulesDataSource(t *testing.T) {
func TestAccAliCloudConfigAggregateConfigRulesDataSource(t *testing.T) {
rand := acctest.RandInt()
idsConf := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudConfigAggregateConfigRulesDataSourceName(rand, map[string]string{
Expand Down Expand Up @@ -136,12 +136,73 @@ data "alicloud_resource_manager_resource_groups" "default" {
status = "OK"
}
data "alicloud_instances" "default" {}
data "alicloud_zones" default {
available_resource_creation = "Instance"
}
data "alicloud_instance_types" "default" {
availability_zone = "${data.alicloud_zones.default.zones.0.id}"
instance_type_family = "ecs.sn1ne"
}
data "alicloud_images" "default" {
name_regex = "^ubuntu"
most_recent = true
owners = "system"
}
resource "alicloud_vpc" "foo" {
cidr_block = "172.16.0.0/12"
name = "${var.name}"
}
resource "alicloud_vswitch" "foo" {
vpc_id = "${alicloud_vpc.foo.id}"
cidr_block = "172.16.0.0/21"
availability_zone = "${data.alicloud_zones.default.zones.0.id}"
name = "${var.name}"
}
data "alicloud_config_aggregators" "default" {}
resource "alicloud_security_group" "tf_test_foo" {
name = "${var.name}"
description = "foo"
vpc_id = "${alicloud_vpc.foo.id}"
}
resource "alicloud_instance" "default" {
availability_zone = "${data.alicloud_zones.default.zones.0.id}"
vswitch_id = "${alicloud_vswitch.foo.id}"
image_id = "${data.alicloud_images.default.images.0.id}"
# series III
instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}"
system_disk_category = "cloud_efficiency"
internet_charge_type = "PayByTraffic"
internet_max_bandwidth_out = 5
security_groups = ["${alicloud_security_group.tf_test_foo.id}"]
instance_name = "${var.name}"
user_data = "echo 'net.ipv4.ip_forward=1'>> /etc/sysctl.conf"
}
data "alicloud_instances" "default" {
name_regex = "${alicloud_instance.default.instance_name}"
}
data "alicloud_resource_manager_accounts" "default" {
status = "CreateSuccess"
}
resource "alicloud_config_aggregator" "default" {
aggregator_accounts {
account_id = data.alicloud_resource_manager_accounts.default.accounts.1.account_id
account_name = data.alicloud_resource_manager_accounts.default.accounts.1.display_name
account_type = "ResourceDirectory"
}
aggregator_name = var.name
description = var.name
}
resource "alicloud_config_aggregate_config_rule" "default" {
aggregator_id = data.alicloud_config_aggregators.default.ids.0
aggregator_id = alicloud_config_aggregator.default.id
aggregate_config_rule_name = var.name
source_owner = "ALIYUN"
source_identifier = "ecs-cpu-min-count-limit"
Expand Down
11 changes: 3 additions & 8 deletions alicloud/data_source_alicloud_config_aggregate_deliveries.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 @@ -129,6 +128,7 @@ func dataSourceAlicloudConfigAggregateDeliveriesRead(d *schema.ResourceData, met
request := make(map[string]interface{})
request["AggregatorId"] = d.Get("aggregator_id")
var objects []map[string]interface{}
var err error
var channelNameRegex *regexp.Regexp
if v, ok := d.GetOk("name_regex"); ok {
r, err := regexp.Compile(v.(string))
Expand All @@ -148,15 +148,10 @@ func dataSourceAlicloudConfigAggregateDeliveriesRead(d *schema.ResourceData, met
}
status, statusOk := d.GetOkExists("status")
var response map[string]interface{}
conn, err := client.NewConfigClient()
if err != nil {
return WrapError(err)
}
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("2020-09-07"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Config", "2020-09-07", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudConfigAggregateDeliveriesDataSource(t *testing.T) {
func TestAccAliCloudConfigAggregateDeliveriesDataSource(t *testing.T) {
rand := acctest.RandInt()
checkoutSupportedRegions(t, true, connectivity.CloudConfigSupportedRegions)
nameRegexConf := dataSourceTestAccConfig{
Expand Down Expand Up @@ -135,7 +135,7 @@ resource "alicloud_config_aggregate_delivery" "default" {
}
data "alicloud_config_aggregate_deliveries" "default" {
aggregator_id = alicloud_config_aggregator.default.id
aggregator_id = alicloud_config_aggregate_delivery.default.aggregator_id
%s
}
`, rand, defaultRegionToTest, strings.Join(pairs, " \n "))
Expand Down
11 changes: 3 additions & 8 deletions alicloud/data_source_alicloud_config_aggregators.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 @@ -137,16 +136,12 @@ func dataSourceAlicloudConfigAggregatorsRead(d *schema.ResourceData, meta interf
}
status, statusOk := d.GetOk("status")
var response map[string]interface{}
conn, err := client.NewConfigClient()
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("GET"), StringPointer("2020-09-07"), StringPointer("AK"), request, nil, &runtime)
response, err = client.RpcGet("Config", "2020-09-07", action, request, nil)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
2 changes: 1 addition & 1 deletion alicloud/data_source_alicloud_config_aggregators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudConfigAggregatorsDataSource(t *testing.T) {
func TestAccAliCloudConfigAggregatorsDataSource(t *testing.T) {
rand := acctest.RandInt()
idsConf := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudConfigAggregatorsDataSourceName(rand, map[string]string{
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_config_compliance_packs.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 @@ -158,16 +157,11 @@ func dataSourceAlicloudConfigCompliancePacksRead(d *schema.ResourceData, meta in
}
status, statusOk := d.GetOk("status")
var response map[string]interface{}
conn, err := client.NewConfigClient()
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("GET"), StringPointer("2020-09-07"), StringPointer("AK"), request, nil, &runtime)
response, err = client.RpcGet("Config", "2020-09-07", action, request, nil)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestAccAlicloudConfigCompliancePacksDataSource(t *testing.T) {
func TestAccAliCloudConfigCompliancePacksDataSource(t *testing.T) {
rand := acctest.RandInt()
idsConf := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudConfigCompliancePacksDataSourceName(rand, map[string]string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strconv"
"time"

util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand Down Expand Up @@ -60,12 +59,7 @@ func dataSourceAlicloudConfigConfigurationRecordersRead(d *schema.ResourceData,

action := "DescribeConfigurationRecorder"
request := make(map[string]interface{})
var response map[string]interface{}
conn, err := client.NewConfigClient()
if err != nil {
return WrapError(err)
}
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("GET"), StringPointer("2019-01-08"), StringPointer("AK"), request, nil, &util.RuntimeOptions{})
response, err := client.RpcGet("Config", "2019-01-08", action, request, nil)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_config_configuration_recorders", action, AlibabaCloudSdkGoERROR)
}
Expand Down
Loading

0 comments on commit ea4ca0c

Please sign in to comment.