-
Notifications
You must be signed in to change notification settings - Fork 165
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 schemas data source (#6252)
- Loading branch information
Showing
4 changed files
with
316 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,48 @@ | ||
--- | ||
subcategory: "GaussDB" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_schemas" | ||
description: |- | ||
Use this data source to get the database schemas of a specified GaussDB OpenGauss instance. | ||
--- | ||
|
||
# huaweicloud_gaussdb_opengauss_schemas | ||
|
||
Use this data source to get the database schemas of a specified GaussDB OpenGauss instance. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "instance_id" {} | ||
data "huaweicloud_gaussdb_opengauss_schemas" "this" { | ||
instance_id = var.instance_id | ||
db_name = "test_db_name" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `region` - (Optional, String) The region in which to obtain the instance. If omitted, the provider-level region will | ||
be used. | ||
|
||
* `instance_id` - (Required, String) Specifies the ID of the GaussDB OpenGauss instance. | ||
|
||
* `db_name` - (Required, String) Specifies the database name. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - Indicates the ID of the data source. | ||
|
||
* `database_schemas` - Indicates the list of the database schemas. | ||
|
||
The [database_schemas](#database_schemas_struct) structure is documented below. | ||
|
||
<a name="database_schemas_struct"></a> | ||
The `database_schemas` block supports: | ||
|
||
* `schema_name` - Indicates the schema name. | ||
|
||
* `owner` - Indicates the owner of the schema. |
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
129 changes: 129 additions & 0 deletions
129
...oud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_schemas_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,129 @@ | ||
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 TestAccOpenGaussSchemasDataSource_basic(t *testing.T) { | ||
dataSource := "data.huaweicloud_gaussdb_opengauss_schemas.test" | ||
rName := acceptance.RandomAccResourceName() | ||
dc := acceptance.InitDataSourceCheck(dataSource) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckEpsID(t) | ||
acceptance.TestAccPreCheckHighCostAllow(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccOpenGaussSchemasDataSource_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrSet(dataSource, "database_schemas.#"), | ||
resource.TestCheckResourceAttrSet(dataSource, "database_schemas.0.schema_name"), | ||
resource.TestCheckResourceAttrSet(dataSource, "database_schemas.0.owner"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccOpenGaussSchemasDataSource_base(rName 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 | ||
} | ||
} | ||
resource "huaweicloud_gaussdb_opengauss_database" "test" { | ||
instance_id = huaweicloud_gaussdb_opengauss_instance.test.id | ||
name = "%[2]s" | ||
character_set = "UTF8" | ||
owner = "root" | ||
template = "template0" | ||
lc_collate = "C" | ||
lc_ctype = "C" | ||
} | ||
resource "huaweicloud_gaussdb_opengauss_schema" "test" { | ||
depends_on = [huaweicloud_gaussdb_opengauss_database.test] | ||
instance_id = huaweicloud_gaussdb_opengauss_instance.test.id | ||
db_name = huaweicloud_gaussdb_opengauss_database.test.name | ||
name = "%[2]s" | ||
owner = "root" | ||
} | ||
`, common.TestBaseNetwork(rName), rName, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST) | ||
} | ||
|
||
func testAccOpenGaussSchemasDataSource_basic(rName string) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
data "huaweicloud_gaussdb_opengauss_schemas" "test" { | ||
depends_on = [huaweicloud_gaussdb_opengauss_schema.test] | ||
instance_id = huaweicloud_gaussdb_opengauss_instance.test.id | ||
db_name = huaweicloud_gaussdb_opengauss_database.test.name | ||
} | ||
`, testAccOpenGaussSchemasDataSource_base(rName)) | ||
} |
138 changes: 138 additions & 0 deletions
138
huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_schemas.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,138 @@ | ||
package gaussdb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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}/instances/{instance_id}/schemas | ||
func DataSourceOpenGaussSchemas() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceOpenGaussSchemasRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"db_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"database_schemas": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"schema_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"owner": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceOpenGaussSchemasRead(_ 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}/instances/{instance_id}/schemas" | ||
product = "opengauss" | ||
) | ||
client, err := cfg.NewServiceClient(product, region) | ||
if err != nil { | ||
return diag.Errorf("error creating GaussDB client: %s", err) | ||
} | ||
|
||
listBasePath := client.Endpoint + httpUrl | ||
listBasePath = strings.ReplaceAll(listBasePath, "{project_id}", client.ProjectID) | ||
listBasePath = strings.ReplaceAll(listBasePath, "{instance_id}", d.Get("instance_id").(string)) | ||
|
||
listOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
} | ||
|
||
offset := 0 | ||
var res []interface{} | ||
dnName := d.Get("db_name").(string) | ||
|
||
for { | ||
listPath := listBasePath + buildOpenGaussSchemasQueryParams(dnName, offset) | ||
listResp, err := client.Request("GET", listPath, &listOpt) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
listRespBody, err := utils.FlattenResponse(listResp) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
schemas := flattenListOpenGaussSchemasResponseBody(listRespBody) | ||
res = append(res, schemas...) | ||
totalCount := utils.PathSearch("total_count", listRespBody, float64(0)).(float64) | ||
if int(totalCount) <= (offset+1)*100 { | ||
break | ||
} | ||
offset++ | ||
} | ||
|
||
dataSourceId, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(dataSourceId) | ||
|
||
mErr = multierror.Append( | ||
mErr, | ||
d.Set("region", region), | ||
d.Set("database_schemas", res), | ||
) | ||
|
||
return diag.FromErr(mErr.ErrorOrNil()) | ||
} | ||
|
||
func buildOpenGaussSchemasQueryParams(dbName string, page int) string { | ||
return fmt.Sprintf("?db_name=%s&limit=100&offset=%v", dbName, page) | ||
} | ||
|
||
func flattenListOpenGaussSchemasResponseBody(resp interface{}) []interface{} { | ||
if resp == nil { | ||
return nil | ||
} | ||
curJson := utils.PathSearch("database_schemas", resp, make([]interface{}, 0)) | ||
curArray := curJson.([]interface{}) | ||
rst := make([]interface{}, 0, len(curArray)) | ||
for _, v := range curArray { | ||
rst = append(rst, map[string]interface{}{ | ||
"schema_name": utils.PathSearch("schema_name", v, nil), | ||
"owner": utils.PathSearch("owner", v, nil), | ||
}) | ||
} | ||
return rst | ||
} |