diff --git a/.changelog/3377.txt b/.changelog/3377.txt new file mode 100644 index 0000000000..a39a9c3812 --- /dev/null +++ b/.changelog/3377.txt @@ -0,0 +1,7 @@ +```release-note:enhancement +resource/tencentcloud_as_scaling_config: support `tags` +``` + +```release-note:enhancement +resource/tencentcloud_as_scaling_group: fix error while creating multi tags +``` \ No newline at end of file diff --git a/tencentcloud/services/as/resource_tc_as_scaling_config.go b/tencentcloud/services/as/resource_tc_as_scaling_config.go index e5a5dc8709..76544f0bf9 100644 --- a/tencentcloud/services/as/resource_tc_as_scaling_config.go +++ b/tencentcloud/services/as/resource_tc_as_scaling_config.go @@ -5,6 +5,8 @@ import ( "fmt" "log" + svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag" + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" svccvm "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cvm" @@ -278,6 +280,11 @@ func ResourceTencentCloudAsScalingConfig() *schema.Resource { Optional: true, Description: "Dedicated Cluster ID.", }, + "tags": { + Type: schema.TypeMap, + Optional: true, + Description: "Tags of launch configuration.", + }, // Computed values "status": { Type: schema.TypeString, @@ -514,6 +521,18 @@ func resourceTencentCloudAsScalingConfigCreate(d *schema.ResourceData, meta inte request.DedicatedClusterId = helper.String(v.(string)) } + if tags := helper.GetTags(d, "tags"); len(tags) > 0 { + for tagKey, tagValue := range tags { + tag := as.Tag{ + ResourceType: helper.String("launch-configuration"), + Key: helper.String(tagKey), + Value: helper.String(tagValue), + } + + request.Tags = append(request.Tags, &tag) + } + } + var launchConfigurationId string err := resource.Retry(4*tccommon.WriteRetryTimeout, func() *resource.RetryError { response, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().CreateLaunchConfiguration(request) @@ -648,6 +667,10 @@ func resourceTencentCloudAsScalingConfigRead(d *schema.ResourceData, meta interf _ = d.Set("dedicated_cluster_id", config.DedicatedClusterId) } + if config.Tags != nil && len(config.Tags) > 0 { + _ = d.Set("tags", flattenTagsMapping(config.Tags)) + } + return nil }) if err != nil { @@ -922,6 +945,23 @@ func resourceTencentCloudAsScalingConfigUpdate(d *schema.ResourceData, meta inte } } + if d.HasChange("tags") { + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + + client := meta.(tccommon.ProviderMeta).GetAPIV3Conn() + tagService := svctag.NewTagService(client) + region := client.Region + + oldValue, newValue := d.GetChange("tags") + replaceTags, deleteTags := svctag.DiffTags(oldValue.(map[string]interface{}), newValue.(map[string]interface{})) + + resourceName := tccommon.BuildTagResourceName("as", "launch-configuration", region, d.Id()) + err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags) + if err != nil { + return err + } + } + err := resource.Retry(4*tccommon.WriteRetryTimeout, func() *resource.RetryError { response, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().ModifyLaunchConfigurationAttributes(request) if e != nil { diff --git a/tencentcloud/services/as/resource_tc_as_scaling_config.md b/tencentcloud/services/as/resource_tc_as_scaling_config.md index 6c79ff809c..7e960dfedd 100644 --- a/tencentcloud/services/as/resource_tc_as_scaling_config.md +++ b/tencentcloud/services/as/resource_tc_as_scaling_config.md @@ -42,6 +42,11 @@ resource "tencentcloud_as_scaling_config" "example" { instance_tags = { tag = "example" } + + tags = { + "createdBy" = "Terraform" + "owner" = "tf" + } } ``` @@ -60,6 +65,11 @@ resource "tencentcloud_as_scaling_config" "example" { instance_charge_type = "SPOTPAID" spot_instance_type = "one-time" spot_max_price = "1000" + + tags = { + "createdBy" = "Terraform" + "owner" = "tf" + } } ``` @@ -159,6 +169,11 @@ resource "tencentcloud_as_scaling_config" "example" { instance_tags = { tag = "example" } + + tags = { + "createdBy" = "Terraform" + "owner" = "tf" + } } ``` diff --git a/tencentcloud/services/as/resource_tc_as_scaling_group.go b/tencentcloud/services/as/resource_tc_as_scaling_group.go index 992e19d281..f3da195fd6 100644 --- a/tencentcloud/services/as/resource_tc_as_scaling_group.go +++ b/tencentcloud/services/as/resource_tc_as_scaling_group.go @@ -352,12 +352,14 @@ func resourceTencentCloudAsScalingGroupCreate(d *schema.ResourceData, meta inter } if tags := helper.GetTags(d, "tags"); len(tags) > 0 { - for k, v := range tags { - request.Tags = append(request.Tags, &as.Tag{ + for tagKey, tagValue := range tags { + tag := as.Tag{ ResourceType: helper.String("auto-scaling-group"), - Key: &k, - Value: &v, - }) + Key: helper.String(tagKey), + Value: helper.String(tagValue), + } + + request.Tags = append(request.Tags, &tag) } } diff --git a/tencentcloud/services/as/service_tencentcloud_as.go b/tencentcloud/services/as/service_tencentcloud_as.go index edc4126d5a..0e62dddf93 100644 --- a/tencentcloud/services/as/service_tencentcloud_as.go +++ b/tencentcloud/services/as/service_tencentcloud_as.go @@ -648,6 +648,14 @@ func flattenInstanceTagsMapping(list []*as.InstanceTag) map[string]interface{} { return result } +func flattenTagsMapping(list []*as.Tag) map[string]interface{} { + result := make(map[string]interface{}, len(list)) + for _, v := range list { + result[*v.Key] = *v.Value + } + return result +} + func (me *AsService) DescribeAsAdvices(ctx context.Context, param map[string]interface{}) (advices []*as.AutoScalingAdvice, errRet error) { var ( logId = tccommon.GetLogId(ctx) diff --git a/website/docs/r/as_scaling_config.html.markdown b/website/docs/r/as_scaling_config.html.markdown index a34256cce3..a0d22333a4 100644 --- a/website/docs/r/as_scaling_config.html.markdown +++ b/website/docs/r/as_scaling_config.html.markdown @@ -53,6 +53,11 @@ resource "tencentcloud_as_scaling_config" "example" { instance_tags = { tag = "example" } + + tags = { + "createdBy" = "Terraform" + "owner" = "tf" + } } ``` @@ -71,6 +76,11 @@ resource "tencentcloud_as_scaling_config" "example" { instance_charge_type = "SPOTPAID" spot_instance_type = "one-time" spot_max_price = "1000" + + tags = { + "createdBy" = "Terraform" + "owner" = "tf" + } } ``` @@ -169,6 +179,11 @@ resource "tencentcloud_as_scaling_config" "example" { instance_tags = { tag = "example" } + + tags = { + "createdBy" = "Terraform" + "owner" = "tf" + } } ``` @@ -206,6 +221,7 @@ The following arguments are supported: * `spot_max_price` - (Optional, String) Max price of a spot instance, is the format of decimal string, for example "0.50". Note: it only works when instance_charge_type is set to `SPOTPAID`. * `system_disk_size` - (Optional, Int) Volume of system disk in GB. Default is `50`. * `system_disk_type` - (Optional, String) Type of a CVM disk. Valid values: `CLOUD_PREMIUM` and `CLOUD_SSD`. Default is `CLOUD_PREMIUM`. valid when disk_type_policy is ORIGINAL. +* `tags` - (Optional, Map) Tags of launch configuration. * `user_data` - (Optional, String) ase64-encoded User Data text, the length limit is 16KB. The `data_disk` object supports the following: