diff --git a/docs/data-sources/gaussdb_opengauss_predefined_tags.md b/docs/data-sources/gaussdb_opengauss_predefined_tags.md new file mode 100644 index 0000000000..264a7fa6dc --- /dev/null +++ b/docs/data-sources/gaussdb_opengauss_predefined_tags.md @@ -0,0 +1,41 @@ +--- +subcategory: "GaussDB" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_predefined_tags" +description: |- + Use this data source to get the list of predefined tags. +--- + +# huaweicloud_gaussdb_opengauss_predefined_tags + +Use this data source to get the list of predefined tags. + +## Example Usage + +```hcl +data "huaweicloud_gaussdb_opengauss_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 tags. + + The [tags](#tags_struct) structure is documented below. + + +The `tags` block supports: + +* `key` - Indicates the tag key. + +* `values` - Indicates the tag values. diff --git a/docs/data-sources/gaussdb_opengauss_tags.md b/docs/data-sources/gaussdb_opengauss_tags.md new file mode 100644 index 0000000000..8d06f4970d --- /dev/null +++ b/docs/data-sources/gaussdb_opengauss_tags.md @@ -0,0 +1,41 @@ +--- +subcategory: "GaussDB" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_tags" +description: |- + Use this data source to get the all user tags in a project. +--- + +# huaweicloud_gaussdb_opengauss_tags + +Use this data source to get the all user tags in a project. + +## Example Usage + +```hcl +data "huaweicloud_gaussdb_opengauss_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` - Indicates the tags of the project. + + The [tags](#tags_struct) structure is documented below. + + +The `tags` block supports: + +* `key` - Indicates the tag key. + +* `values` - Indicates the tag values. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index f311817760..161702e3b9 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -855,6 +855,8 @@ func Provider() *schema.Provider { "huaweicloud_gaussdb_opengauss_tasks": gaussdb.DataSourceOpenGaussTasks(), "huaweicloud_gaussdb_opengauss_quotas": gaussdb.DataSourceGaussdbOpengaussQuotas(), "huaweicloud_gaussdb_opengauss_upgrade_versions": gaussdb.DataSourceGaussdbOpengaussUpgradeVersions(), + "huaweicloud_gaussdb_opengauss_tags": gaussdb.DataSourceOpenGaussTags(), + "huaweicloud_gaussdb_opengauss_predefined_tags": gaussdb.DataSourceOpenGaussPredefinedTags(), "huaweicloud_gaussdb_mysql_engine_versions": taurusdb.DataSourceGaussdbMysqlEngineVersions(), "huaweicloud_gaussdb_mysql_configuration": taurusdb.DataSourceGaussdbMysqlConfiguration(), diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_predefined_tags_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_predefined_tags_test.go new file mode 100644 index 0000000000..bbcb4e9b4d --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_predefined_tags_test.go @@ -0,0 +1,35 @@ +package gaussdb + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDataSourceOpenGaussPredefinedTags_basic(t *testing.T) { + dataSource := "data.huaweicloud_gaussdb_opengauss_predefined_tags.test" + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceOpenGaussPredefined_basic(), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "tags.0.key"), + resource.TestCheckResourceAttrSet(dataSource, "tags.0.values.#"), + ), + }, + }, + }) +} + +func testDataSourceOpenGaussPredefined_basic() string { + return `data "huaweicloud_gaussdb_opengauss_predefined_tags" "test" {}` +} diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_tags_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_tags_test.go new file mode 100644 index 0000000000..d1f1aa0688 --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_tags_test.go @@ -0,0 +1,109 @@ +package gaussdb + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common" +) + +func TestAccDataSourceOpenGaussTags_basic(t *testing.T) { + dataSource := "data.huaweicloud_gaussdb_opengauss_tags.test" + rName := acceptance.RandomAccResourceName() + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceOpenGaussTags_basic(rName), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "tags.0.key"), + resource.TestCheckResourceAttrSet(dataSource, "tags.0.values.#"), + ), + }, + }, + }) +} + +func testDataSourceOpenGaussTags_base(name string) string { + return fmt.Sprintf(` +%[1]s + +data "huaweicloud_availability_zones" "test" {} + +data "huaweicloud_gaussdb_opengauss_flavors" "test" { + version = "8.201" + ha_mode = "centralization_standard" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "ingress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss_egress" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "egress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_gaussdb_opengauss_instance" "test" { + depends_on = [ + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss, + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss_egress + ] + + vpc_id = huaweicloud_vpc.test.id + subnet_id = huaweicloud_vpc_subnet.test.id + security_group_id = huaweicloud_networking_secgroup.test.id + flavor = data.huaweicloud_gaussdb_opengauss_flavors.test.flavors[0].spec_code + name = "%[2]s" + password = "Huangwei!120521" + enterprise_project_id = "%[3]s" + + availability_zone = join(",", [data.huaweicloud_availability_zones.test.names[0], + data.huaweicloud_availability_zones.test.names[1], + data.huaweicloud_availability_zones.test.names[2]]) + + ha { + mode = "centralization_standard" + replication_mode = "sync" + consistency = "eventual" + instance_mode = "basic" + } + + volume { + type = "ULTRAHIGH" + size = 40 + } + + tags = { + key = "value" + owner = "terraform" + } +} +`, common.TestBaseNetwork(name), name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST) +} + +func testDataSourceOpenGaussTags_basic(name string) string { + return fmt.Sprintf(` +%s + +data "huaweicloud_gaussdb_opengauss_tags" "test" { + depends_on = [huaweicloud_gaussdb_opengauss_instance.test] +} +`, testDataSourceOpenGaussTags_base(name)) +} diff --git a/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_predefined_tags.go b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_predefined_tags.go new file mode 100644 index 0000000000..52b496999d --- /dev/null +++ b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_predefined_tags.go @@ -0,0 +1,110 @@ +package gaussdb + +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 GaussDB GET /v3/{project_id}/predefined-tags +func DataSourceOpenGaussPredefinedTags() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceOpenGaussPredefinedTagsRead, + + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "tags": { + Type: schema.TypeList, + Computed: true, + Description: `Indicates the list of tags.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "key": { + Type: schema.TypeString, + Computed: true, + }, + "values": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }, + } +} + +func dataSourceOpenGaussPredefinedTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + cfg := meta.(*config.Config) + region := cfg.GetRegion(d) + + var mErr *multierror.Error + + var ( + httpUrl = "v3/{project_id}/predefined-tags" + product = "opengauss" + ) + client, err := cfg.NewServiceClient(product, region) + if err != nil { + return diag.Errorf("error creating GaussDB Client: %s", err) + } + + getPath := client.Endpoint + httpUrl + getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID) + + getOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + } + getResp, err := client.Request("GET", getPath, &getOpt) + if err != nil { + return diag.Errorf("error retrieving GaussDB OpenGauss predefined tags: %s", err) + } + + getRespBody, err := utils.FlattenResponse(getResp) + if err != nil { + return diag.FromErr(err) + } + + dataSourceId, err := uuid.GenerateUUID() + if err != nil { + return diag.Errorf("unable to generate ID: %s", err) + } + d.SetId(dataSourceId) + + mErr = multierror.Append( + mErr, + d.Set("region", region), + d.Set("tags", flattenOpenGaussPredefinedTagsResponseBody(getRespBody)), + ) + return diag.FromErr(mErr.ErrorOrNil()) +} + +func flattenOpenGaussPredefinedTagsResponseBody(resp interface{}) []interface{} { + if resp == nil { + return nil + } + curJson := utils.PathSearch("tags", resp, make([]interface{}, 0)) + curArray := curJson.([]interface{}) + rst := make([]interface{}, 0, len(curArray)) + for _, v := range curArray { + rst = append(rst, map[string]interface{}{ + "key": utils.PathSearch("key", v, nil), + "values": utils.PathSearch("values", v, nil), + }) + } + return rst +} diff --git a/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_tags.go b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_tags.go new file mode 100644 index 0000000000..87ec968d63 --- /dev/null +++ b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_tags.go @@ -0,0 +1,110 @@ +package gaussdb + +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 GaussDB GET /v3/{project_id}/tags +func DataSourceOpenGaussTags() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceOpenGaussTagsRead, + + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "tags": { + Type: schema.TypeList, + Computed: true, + Description: `Indicates the list of tags.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "key": { + Type: schema.TypeString, + Computed: true, + }, + "values": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }, + } +} + +func dataSourceOpenGaussTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + cfg := meta.(*config.Config) + region := cfg.GetRegion(d) + + var mErr *multierror.Error + + var ( + httpUrl = "v3/{project_id}/tags" + product = "opengauss" + ) + client, err := cfg.NewServiceClient(product, region) + if err != nil { + return diag.Errorf("error creating GaussDB Client: %s", err) + } + + getPath := client.Endpoint + httpUrl + getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID) + + getOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + } + getResp, err := client.Request("GET", getPath, &getOpt) + if err != nil { + return diag.Errorf("error retrieving GaussDB OpenGauss tags: %s", err) + } + + getRespBody, err := utils.FlattenResponse(getResp) + if err != nil { + return diag.FromErr(err) + } + + dataSourceId, err := uuid.GenerateUUID() + if err != nil { + return diag.Errorf("unable to generate ID: %s", err) + } + d.SetId(dataSourceId) + + mErr = multierror.Append( + mErr, + d.Set("region", region), + d.Set("tags", flattenOpenGaussTagsResponseBody(getRespBody)), + ) + return diag.FromErr(mErr.ErrorOrNil()) +} + +func flattenOpenGaussTagsResponseBody(resp interface{}) []interface{} { + if resp == nil { + return nil + } + curJson := utils.PathSearch("tags", resp, make([]interface{}, 0)) + curArray := curJson.([]interface{}) + rst := make([]interface{}, 0, len(curArray)) + for _, v := range curArray { + rst = append(rst, map[string]interface{}{ + "key": utils.PathSearch("key", v, nil), + "values": utils.PathSearch("values", v, nil), + }) + } + return rst +}