Skip to content

Commit 871b216

Browse files
authored
feat(sdrs): new datasource to query instance tags (#7340)
1 parent 60d7de7 commit 871b216

6 files changed

+195
-2
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
subcategory: "Storage Disaster Recovery Service (SDRS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_sdrs_protected_instance_tags"
5+
description: |-
6+
Use this data source to query SDRS protected instance tags within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_sdrs_protected_instance_tags
10+
11+
Use this data source to query SDRS protected instance tags within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_sdrs_protected_instance_tags" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region in which to query the resource.
24+
If omitted, the provider-level region will be used.
25+
26+
## Attribute Reference
27+
28+
In addition to all arguments above, the following attributes are exported:
29+
30+
* `id` - The data source ID.
31+
32+
* `tags` - The tag list.
33+
34+
The [tags](#tags_struct) structure is documented below.
35+
36+
<a name="tags_struct"></a>
37+
The `tags` block supports:
38+
39+
* `key` - The tag key.
40+
41+
* `values` - The tag values.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,7 @@ func Provider() *schema.Provider {
13671367
"huaweicloud_sdrs_drills": sdrs.DataSourceSdrsDrills(),
13681368
"huaweicloud_sdrs_protected_instances": sdrs.DataSourceSdrsProtectedInstances(),
13691369
"huaweicloud_sdrs_protected_instances_by_tags": sdrs.DataSourceSdrsProtectedInstancesByTags(),
1370+
"huaweicloud_sdrs_protected_instance_tags": sdrs.DataSourceSdrsProtectedInstanceTags(),
13701371

13711372
"huaweicloud_secmaster_workflows": secmaster.DataSourceSecmasterWorkflows(),
13721373
"huaweicloud_secmaster_workspaces": secmaster.DataSourceSecmasterWorkspaces(),

huaweicloud/services/acceptance/acceptance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ func TestAccPreCheckSDRSDeleteNic(t *testing.T) {
814814
}
815815

816816
// lintignore:AT003
817-
func TestAccPreCheckSDRSInstanceResize(t *testing.T) {
817+
func TestAccPreCheckSDRSInstanceID(t *testing.T) {
818818
if HW_SDRS_PROTECTION_INSTANCE_ID == "" {
819819
t.Skip("HW_SDRS_PROTECTION_INSTANCE_ID must be set for this acceptance test")
820820
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sdrs
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
9+
)
10+
11+
func TestAccDataSourceSdrsProtectedInstanceTags_basic(t *testing.T) {
12+
dataSource := "data.huaweicloud_sdrs_protected_instance_tags.test"
13+
dc := acceptance.InitDataSourceCheck(dataSource)
14+
15+
resource.ParallelTest(t, resource.TestCase{
16+
PreCheck: func() {
17+
acceptance.TestAccPreCheck(t)
18+
// Please prepare an SDRS instance with tags before running this test case.
19+
acceptance.TestAccPreCheckSDRSInstanceID(t)
20+
},
21+
ProviderFactories: acceptance.TestAccProviderFactories,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testDataSourceSdrsProtectedInstanceTags_basic,
25+
Check: resource.ComposeTestCheckFunc(
26+
dc.CheckResourceExists(),
27+
resource.TestCheckResourceAttrSet(dataSource, "tags.#"),
28+
resource.TestCheckResourceAttrSet(dataSource, "tags.0.key"),
29+
resource.TestCheckResourceAttrSet(dataSource, "tags.0.values.#"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
const testDataSourceSdrsProtectedInstanceTags_basic = `data "huaweicloud_sdrs_protected_instance_tags" "test" {}`

huaweicloud/services/acceptance/sdrs/resource_huaweicloud_sdrs_protected_instance_resize_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestAccProtectedInstanceResize_basic(t *testing.T) {
1414
resource.ParallelTest(t, resource.TestCase{
1515
PreCheck: func() {
1616
acceptance.TestAccPreCheck(t)
17-
acceptance.TestAccPreCheckSDRSInstanceResize(t)
17+
acceptance.TestAccPreCheckSDRSInstanceID(t)
1818
acceptance.TestAccPreCheckSDRSInstanceResizeFlavor(t)
1919
},
2020
ProviderFactories: acceptance.TestAccProviderFactories,
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Generated by PMS #689
2+
package sdrs
3+
4+
import (
5+
"context"
6+
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/hashicorp/go-uuid"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/tidwall/gjson"
12+
13+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
16+
)
17+
18+
func DataSourceSdrsProtectedInstanceTags() *schema.Resource {
19+
return &schema.Resource{
20+
ReadContext: dataSourceSdrsProtectedInstanceTagsRead,
21+
22+
Schema: map[string]*schema.Schema{
23+
"region": {
24+
Type: schema.TypeString,
25+
Optional: true,
26+
Computed: true,
27+
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
28+
},
29+
"tags": {
30+
Type: schema.TypeList,
31+
Computed: true,
32+
Description: `The tag list.`,
33+
Elem: &schema.Resource{
34+
Schema: map[string]*schema.Schema{
35+
"key": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
Description: `The tag key.`,
39+
},
40+
"values": {
41+
Type: schema.TypeList,
42+
Computed: true,
43+
Elem: &schema.Schema{Type: schema.TypeString},
44+
Description: `The tag values.`,
45+
},
46+
},
47+
},
48+
},
49+
},
50+
}
51+
}
52+
53+
type ProtectedInstanceTagsDSWrapper struct {
54+
*schemas.ResourceDataWrapper
55+
Config *config.Config
56+
}
57+
58+
func newProtectedInstanceTagsDSWrapper(d *schema.ResourceData, meta interface{}) *ProtectedInstanceTagsDSWrapper {
59+
return &ProtectedInstanceTagsDSWrapper{
60+
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
61+
Config: meta.(*config.Config),
62+
}
63+
}
64+
65+
func dataSourceSdrsProtectedInstanceTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
66+
wrapper := newProtectedInstanceTagsDSWrapper(d, meta)
67+
lisProInsProTagRst, err := wrapper.ListProtectedInstancesProjectTags()
68+
if err != nil {
69+
return diag.FromErr(err)
70+
}
71+
72+
id, err := uuid.GenerateUUID()
73+
if err != nil {
74+
return diag.FromErr(err)
75+
}
76+
d.SetId(id)
77+
78+
err = wrapper.listProtectedInstancesProjectTagsToSchema(lisProInsProTagRst)
79+
if err != nil {
80+
return diag.FromErr(err)
81+
}
82+
83+
return nil
84+
}
85+
86+
// @API SDRS GET /v1/{project_id}/protected-instances/tags
87+
func (w *ProtectedInstanceTagsDSWrapper) ListProtectedInstancesProjectTags() (*gjson.Result, error) {
88+
client, err := w.NewClient(w.Config, "sdrs")
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
uri := "/v1/{project_id}/protected-instances/tags"
94+
return httphelper.New(client).
95+
Method("GET").
96+
URI(uri).
97+
Request().
98+
Result()
99+
}
100+
101+
func (w *ProtectedInstanceTagsDSWrapper) listProtectedInstancesProjectTagsToSchema(body *gjson.Result) error {
102+
d := w.ResourceData
103+
mErr := multierror.Append(nil,
104+
d.Set("region", w.Config.GetRegion(w.ResourceData)),
105+
d.Set("tags", schemas.SliceToList(body.Get("tags"),
106+
func(tags gjson.Result) any {
107+
return map[string]any{
108+
"key": tags.Get("key").Value(),
109+
"values": schemas.SliceToStrList(tags.Get("values")),
110+
}
111+
},
112+
)),
113+
)
114+
return mErr.ErrorOrNil()
115+
}

0 commit comments

Comments
 (0)