-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cfw): add this data source to get the list of CFW tags
- Loading branch information
1 parent
41dba37
commit c48be09
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,41 @@ | ||
--- | ||
subcategory: "Cloud Firewall (CFW)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_cfw_tags" | ||
description: |- | ||
Use this data source to get the list of CFW tags. | ||
--- | ||
|
||
# huaweicloud_cfw_tags | ||
|
||
Use this data source to get the list of CFW tags. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "huaweicloud_cfw_tags" "test" {} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String) Specifies the region in which to query the resource. | ||
If omitted, the provider-level region will be used. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The data source ID. | ||
|
||
* `tags` - The tag List. | ||
|
||
The [tags](#tags_struct) structure is documented below. | ||
|
||
<a name="tags_struct"></a> | ||
The `tags` block supports: | ||
|
||
* `key` - The tag key. | ||
|
||
* `values` - The tag values. |
This file contains 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
43 changes: 43 additions & 0 deletions
43
huaweicloud/services/acceptance/cfw/data_source_huaweicloud_cfw_tags_test.go
This file contains 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,43 @@ | ||
package cfw | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccDataSourceCfwTags_basic(t *testing.T) { | ||
dataSource := "data.huaweicloud_cfw_tags.test" | ||
dc := acceptance.InitDataSourceCheck(dataSource) | ||
name := acceptance.RandomAccResourceName() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testDataSourceDataSourceCfwTags_basic(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrSet(dataSource, "tags.0.key"), | ||
resource.TestCheckResourceAttrSet(dataSource, "tags.0.values.#"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testDataSourceDataSourceCfwTags_basic(name string) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
data "huaweicloud_cfw_tags" "test" { | ||
depends_on = [huaweicloud_cfw_firewall.test] | ||
} | ||
`, testFirewall_basic(name)) | ||
} |
116 changes: 116 additions & 0 deletions
116
huaweicloud/services/cfw/data_source_huaweicloud_cfw_tags.go
This file contains 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,116 @@ | ||
// Generated by PMS #514 | ||
package cfw | ||
|
||
import ( | ||
"context" | ||
|
||
"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/tidwall/gjson" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" | ||
) | ||
|
||
func DataSourceCfwTags() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceCfwTagsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, | ||
}, | ||
"tags": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: `The tag List.`, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The tag key.`, | ||
}, | ||
"values": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Description: `The tag values.`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type TagsDSWrapper struct { | ||
*schemas.ResourceDataWrapper | ||
Config *config.Config | ||
} | ||
|
||
func newTagsDSWrapper(d *schema.ResourceData, meta interface{}) *TagsDSWrapper { | ||
return &TagsDSWrapper{ | ||
ResourceDataWrapper: schemas.NewSchemaWrapper(d), | ||
Config: meta.(*config.Config), | ||
} | ||
} | ||
|
||
func dataSourceCfwTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
wrapper := newTagsDSWrapper(d, meta) | ||
listProjectTagsRst, err := wrapper.ListProjectTags() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
id, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(id) | ||
|
||
err = wrapper.listProjectTagsToSchema(listProjectTagsRst) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// @API CFW GET /v2/{project_id}/cfw-cfw/tags | ||
func (w *TagsDSWrapper) ListProjectTags() (*gjson.Result, error) { | ||
client, err := w.NewClient(w.Config, "cfw") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
uri := "/v2/{project_id}/cfw-cfw/tags" | ||
return httphelper.New(client). | ||
Method("GET"). | ||
URI(uri). | ||
OffsetPager("tags", "offset", "limit", 1000). | ||
Request(). | ||
Result() | ||
} | ||
|
||
func (w *TagsDSWrapper) listProjectTagsToSchema(body *gjson.Result) error { | ||
d := w.ResourceData | ||
mErr := multierror.Append(nil, | ||
d.Set("region", w.Config.GetRegion(w.ResourceData)), | ||
d.Set("tags", schemas.SliceToList(body.Get("tags"), | ||
func(tags gjson.Result) any { | ||
return map[string]any{ | ||
"key": tags.Get("key").Value(), | ||
"values": schemas.SliceToStrList(tags.Get("values")), | ||
} | ||
}, | ||
)), | ||
) | ||
return mErr.ErrorOrNil() | ||
} |