Skip to content

Commit a3bfaee

Browse files
feat(as): add a new datasource to get list of AS group tags (#6548)
1 parent 345ae81 commit a3bfaee

File tree

4 files changed

+253
-1
lines changed

4 files changed

+253
-1
lines changed

docs/data-sources/as_group_tags.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
subcategory: "Auto Scaling"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_as_group_tags"
5+
description: |-
6+
Use this data source to get the list of all AS groups tags under the specified project.
7+
---
8+
9+
# huaweicloud_as_group_tags
10+
11+
Use this data source to get the list of all AS groups tags under the specified project.
12+
13+
## Example Usage
14+
15+
```hcl
16+
variable "resource_type" {}
17+
18+
data "huaweicloud_as_group_tags" "test" {
19+
resource_type = var.resource_type
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `region` - (Optional, String) Specifies the region in which to query the resource.
28+
If omitted, the provider-level region will be used.
29+
30+
* `resource_type` - (Required, String) Specifies the resource type.
31+
The valid values are as follows:
32+
+ **scaling_group_tag**: Indicates the resource type is AS group.
33+
34+
## Attribute Reference
35+
36+
In addition to all arguments above, the following attributes are exported:
37+
38+
* `id` - The data source ID.
39+
40+
* `tags` - The list of the tags.
41+
42+
The [tags](#tags_struct) structure is documented below.
43+
44+
<a name="tags_struct"></a>
45+
The `tags` block supports:
46+
47+
* `key` - The key of the tag.
48+
49+
* `values` - The list of the tag values.

huaweicloud/provider.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ func Provider() *schema.Provider {
490490

491491
"huaweicloud_as_activity_logs": as.DataSourceActivityLogs(),
492492
"huaweicloud_as_configurations": as.DataSourceASConfigurations(),
493+
"huaweicloud_as_group_quotas": as.DataSourceAsGroupQuotas(),
494+
"huaweicloud_as_group_tags": as.DataSourceAsGroupTags(),
493495
"huaweicloud_as_groups": as.DataSourceASGroups(),
494496
"huaweicloud_as_hook_instances": as.DataSourceAsHookInstances(),
495497
"huaweicloud_as_instances": as.DataSourceASInstances(),
@@ -499,7 +501,6 @@ func Provider() *schema.Provider {
499501
"huaweicloud_as_policies": as.DataSourceASPolicies(),
500502
"huaweicloud_as_policy_execute_logs": as.DataSourcePolicyExecuteLogs(),
501503
"huaweicloud_as_quotas": as.DataSourceAsQuotas(),
502-
"huaweicloud_as_group_quotas": as.DataSourceAsGroupQuotas(),
503504

504505
"huaweicloud_asm_meshes": asm.DataSourceAsmMeshes(),
505506

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package as
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataSourceGroupTags_basic(t *testing.T) {
13+
var (
14+
dataSourceName = "data.huaweicloud_as_group_tags.test"
15+
name = acceptance.RandomAccResourceName()
16+
dc = acceptance.InitDataSourceCheck(dataSourceName)
17+
)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() {
21+
acceptance.TestAccPreCheck(t)
22+
},
23+
ProviderFactories: acceptance.TestAccProviderFactories,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testDataSourceGroupTags_basic(name),
27+
Check: resource.ComposeTestCheckFunc(
28+
dc.CheckResourceExists(),
29+
resource.TestCheckResourceAttrSet(dataSourceName, "tags.#"),
30+
resource.TestCheckResourceAttrSet(dataSourceName, "tags.0.key"),
31+
resource.TestCheckResourceAttrSet(dataSourceName, "tags.0.values.#"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
func testDataSourceGroupTags_basic(name string) string {
39+
return fmt.Sprintf(`
40+
%[1]s
41+
42+
resource "huaweicloud_as_configuration" "test"{
43+
scaling_configuration_name = "%[2]s"
44+
instance_config {
45+
image = data.huaweicloud_images_image.test.id
46+
flavor = data.huaweicloud_compute_flavors.test.ids[0]
47+
key_name = huaweicloud_kps_keypair.acc_key.id
48+
security_group_ids = [huaweicloud_networking_secgroup.test.id]
49+
50+
disk {
51+
size = 40
52+
volume_type = "SSD"
53+
disk_type = "SYS"
54+
}
55+
}
56+
}
57+
58+
resource "huaweicloud_as_group" "test"{
59+
scaling_group_name = "%[2]s"
60+
scaling_configuration_id = huaweicloud_as_configuration.test.id
61+
vpc_id = huaweicloud_vpc.test.id
62+
delete_publicip = true
63+
delete_volume = true
64+
65+
networks {
66+
id = huaweicloud_vpc_subnet.test.id
67+
}
68+
69+
tags = {
70+
foo = "bar"
71+
key = "value"
72+
}
73+
}
74+
75+
data "huaweicloud_as_group_tags" "test" {
76+
resource_type = "scaling_group_tag"
77+
78+
depends_on = [huaweicloud_as_group.test]
79+
}`, testAccASConfiguration_base(name), name)
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Generated by PMS #592
2+
package as
3+
4+
import (
5+
"context"
6+
"strings"
7+
8+
"github.com/hashicorp/go-multierror"
9+
"github.com/hashicorp/go-uuid"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/tidwall/gjson"
13+
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
16+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
17+
)
18+
19+
func DataSourceAsGroupTags() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceAsGroupTagsRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"region": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Computed: true,
28+
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
29+
},
30+
"resource_type": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
Description: `Specifies the resource type.`,
34+
},
35+
"tags": {
36+
Type: schema.TypeList,
37+
Computed: true,
38+
Description: `The list of the tags.`,
39+
Elem: &schema.Resource{
40+
Schema: map[string]*schema.Schema{
41+
"key": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
Description: `The key of the tag.`,
45+
},
46+
"values": {
47+
Type: schema.TypeList,
48+
Computed: true,
49+
Elem: &schema.Schema{Type: schema.TypeString},
50+
Description: `The list of the tag values.`,
51+
},
52+
},
53+
},
54+
},
55+
},
56+
}
57+
}
58+
59+
type GroupTagsDSWrapper struct {
60+
*schemas.ResourceDataWrapper
61+
Config *config.Config
62+
}
63+
64+
func newGroupTagsDSWrapper(d *schema.ResourceData, meta interface{}) *GroupTagsDSWrapper {
65+
return &GroupTagsDSWrapper{
66+
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
67+
Config: meta.(*config.Config),
68+
}
69+
}
70+
71+
func dataSourceAsGroupTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
72+
wrapper := newGroupTagsDSWrapper(d, meta)
73+
lstibtir, err := wrapper.ListScalingTagInfosByTenantId()
74+
if err != nil {
75+
return diag.FromErr(err)
76+
}
77+
78+
id, err := uuid.GenerateUUID()
79+
if err != nil {
80+
return diag.FromErr(err)
81+
}
82+
d.SetId(id)
83+
84+
err = wrapper.listScalingTagInfosByTenantIdToSchema(lstibtir)
85+
if err != nil {
86+
return diag.FromErr(err)
87+
}
88+
89+
return nil
90+
}
91+
92+
// @API AS GET /autoscaling-api/v1/{project_id}/{resource_type}/tags
93+
func (w *GroupTagsDSWrapper) ListScalingTagInfosByTenantId() (*gjson.Result, error) {
94+
client, err := w.NewClient(w.Config, "autoscaling")
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
uri := "/autoscaling-api/v1/{project_id}/{resource_type}/tags"
100+
uri = strings.ReplaceAll(uri, "{resource_type}", w.Get("resource_type").(string))
101+
return httphelper.New(client).
102+
Method("GET").
103+
URI(uri).
104+
Request().
105+
Result()
106+
}
107+
108+
func (w *GroupTagsDSWrapper) listScalingTagInfosByTenantIdToSchema(body *gjson.Result) error {
109+
d := w.ResourceData
110+
mErr := multierror.Append(nil,
111+
d.Set("region", w.Config.GetRegion(w.ResourceData)),
112+
d.Set("tags", schemas.SliceToList(body.Get("tags"),
113+
func(tags gjson.Result) any {
114+
return map[string]any{
115+
"key": tags.Get("key").Value(),
116+
"values": schemas.SliceToStrList(tags.Get("values")),
117+
}
118+
},
119+
)),
120+
)
121+
return mErr.ErrorOrNil()
122+
}

0 commit comments

Comments
 (0)