-
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(GaussDB): add gaussdb opengauss tags (#6225)
- Loading branch information
Showing
7 changed files
with
448 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: "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. |
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: "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. |
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
35 changes: 35 additions & 0 deletions
35
...ices/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_predefined_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,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" {}` | ||
} |
109 changes: 109 additions & 0 deletions
109
...icloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_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,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)) | ||
} |
110 changes: 110 additions & 0 deletions
110
huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_predefined_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,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 | ||
} |
Oops, something went wrong.