Skip to content

Commit

Permalink
feat(GaussDB): add gaussdb opengauss tags (#6225)
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 authored Jan 20, 2025
1 parent 2ce9154 commit 963992b
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/data-sources/gaussdb_opengauss_predefined_tags.md
Original file line number Diff line number Diff line change
@@ -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.

<a name="tags_struct"></a>
The `tags` block supports:

* `key` - Indicates the tag key.

* `values` - Indicates the tag values.
41 changes: 41 additions & 0 deletions docs/data-sources/gaussdb_opengauss_tags.md
Original file line number Diff line number Diff line change
@@ -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.

<a name="tags_struct"></a>
The `tags` block supports:

* `key` - Indicates the tag key.

* `values` - Indicates the tag values.
2 changes: 2 additions & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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" {}`
}
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 963992b

Please sign in to comment.