-
Notifications
You must be signed in to change notification settings - Fork 194
feat(cts): add new data source to query resource tags #7588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
github-ci-robot
merged 1 commit into
huaweicloud:master
from
Zippo-Wang:feat_cts_resource_tag
Aug 26, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
subcategory: "Cloud Trace Service (CTS)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_cts_resource_tags" | ||
description: |- | ||
Use this data source to get resource tag list of CTS service within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_cts_resource_tags | ||
|
||
Use this data source to get resource tag list of CTS service within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "tracker_id" {} | ||
|
||
data "huaweicloud_cts_resource_tags" "test" { | ||
resource_id = var.tracker_id | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String) Specifies the region in which to query the resource tags. | ||
If omitted, the provider-level region will be used. | ||
|
||
* `resource_type` - (Required, String) Specifies the resource type to be queried. | ||
The valid value is **cts-tracker**. | ||
|
||
* `resource_id` - (Required, String) Specifies the resource ID to be queried. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The resource ID. | ||
|
||
* `tags` - The list of tags that matched filter parameters. | ||
The [tags](#cts_resource_tags_attr) structure is documented below. | ||
|
||
<a name="cts_resource_tags_attr"></a> | ||
The `tags` block supports: | ||
|
||
* `key` - The tag key. | ||
|
||
* `value` - The tag value. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
huaweicloud/services/acceptance/cts/data_source_huaweicloud_cts_resource_tags_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cts | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccDataSourceResourceTags_basic(t *testing.T) { | ||
dataSourceName := "data.huaweicloud_cts_resource_tags.test" | ||
dc := acceptance.InitDataSourceCheck(dataSourceName) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceResourceTags_basic(), | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "region"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "tags.#"), | ||
resource.TestCheckOutput("is_tags_exist", "true"), | ||
resource.TestCheckOutput("is_key_set", "true"), | ||
resource.TestCheckOutput("is_values_set", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceResourceTags_base() string { | ||
return ` | ||
resource "huaweicloud_obs_bucket" "test" { | ||
bucket = "tf-test-bucket" | ||
acl = "private" | ||
force_destroy = true | ||
} | ||
|
||
resource "huaweicloud_cts_tracker" "test" { | ||
bucket_name = huaweicloud_obs_bucket.test.bucket | ||
file_prefix = "cts" | ||
|
||
tags = { | ||
foo1 = "bar1", | ||
foo2 = "bar2" | ||
} | ||
} | ||
` | ||
} | ||
func testAccDataSourceResourceTags_basic() string { | ||
return fmt.Sprintf(` | ||
%s | ||
|
||
data "huaweicloud_cts_resource_tags" "test" { | ||
resource_id = huaweicloud_cts_tracker.test.id | ||
resource_type = "cts-tracker" | ||
} | ||
|
||
locals { | ||
tags = data.huaweicloud_cts_resource_tags.test.tags | ||
} | ||
|
||
output "is_tags_exist" { | ||
value = length(local.tags) > 0 | ||
} | ||
|
||
output "is_key_set" { | ||
value = alltrue([for v in local.tags[*].key : v != ""]) | ||
} | ||
|
||
output "is_values_set" { | ||
value = alltrue([for v in local.tags[*].value : v != ""]) | ||
} | ||
`, testAccDataSourceResourceTags_base()) | ||
} |
123 changes: 123 additions & 0 deletions
123
huaweicloud/services/cts/data_source_huaweicloud_cts_resources_tags.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package cts | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/chnsz/golangsdk" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
// @API CTS GET /v3/{project_id}/{resource_type}/{resource_id}/tags | ||
func DataSourceCtsResourcesTags() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceCtsResourcesTagsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: `The region in which to query the resource tags.`, | ||
}, | ||
"resource_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `The resource type to be queried.`, | ||
}, | ||
"resource_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `The resource ID to be queried.`, | ||
}, | ||
"tags": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: `The list of tags that matched filter parameters.`, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The tag key.`, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The tag value.`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceCtsResourcesTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
cfg := meta.(*config.Config) | ||
region := cfg.GetRegion(d) | ||
client, err := cfg.NewServiceClient("cts", region) | ||
if err != nil { | ||
return diag.Errorf("error creating CTS client: %s", err) | ||
} | ||
|
||
resourceId := d.Get("resource_id").(string) | ||
httpUrl := "v3/{project_id}/{resource_type}/{resource_id}/tags" | ||
path := client.Endpoint + httpUrl | ||
path = strings.ReplaceAll(path, "{project_id}", client.ProjectID) | ||
path = strings.ReplaceAll(path, "{resource_type}", d.Get("resource_type").(string)) | ||
path = strings.ReplaceAll(path, "{resource_id}", resourceId) | ||
|
||
requestOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
MoreHeaders: map[string]string{ | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
|
||
response, err := client.Request("GET", path, &requestOpt) | ||
if err != nil { | ||
return diag.Errorf("error retrieving CTS resource tags: %s", err) | ||
} | ||
|
||
respBody, err := utils.FlattenResponse(response) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
randUUID, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return diag.Errorf("unable to generate ID: %s", err) | ||
} | ||
d.SetId(randUUID) | ||
|
||
mErr := multierror.Append(nil, | ||
d.Set("region", region), | ||
d.Set("tags", flattenResourceTags(utils.PathSearch("tags", respBody, make([]interface{}, 0)).([]interface{}))), | ||
) | ||
|
||
return diag.FromErr(mErr.ErrorOrNil()) | ||
} | ||
|
||
func flattenResourceTags(tags []interface{}) []interface{} { | ||
if len(tags) == 0 { | ||
return nil | ||
} | ||
|
||
result := make([]interface{}, 0, len(tags)) | ||
for _, tag := range tags { | ||
result = append(result, map[string]interface{}{ | ||
"key": utils.PathSearch("key", tag, nil), | ||
"value": utils.PathSearch("value", tag, nil), | ||
}) | ||
} | ||
|
||
return result | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
watch out, the right thing has been removed!