Skip to content

Commit

Permalink
feat(dws): add new datasource to query schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhuanhong committed Sep 14, 2024
1 parent c8e6dfb commit 2fd7a84
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 1 deletion.
69 changes: 69 additions & 0 deletions docs/data-sources/dws_schema_space_managements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
subcategory: "GaussDB(DWS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_dws_schema_space_managements"
description: |-
Use this data source to get the list of schema space management information of the DWS Cluster within HuaweiCloud.
---

# huaweicloud_dws_schema_space_managements

Use this data source to get the list of schema space management information of the DWS Cluster within HuaweiCloud.

## Example Usage

```hcl
variable "dws_cluster_id" {}
variable "database_name" {}
data "huaweicloud_dws_schema_space_managements" "test" {
cluster_id = var.dws_cluster_id
database_name = var.database_name
}
```

## 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.

* `cluster_id` - (Required, String) Specifies the DWS cluster ID.

* `database_name` - (Required, String) Specifies the database name to which the schema space management belongs.

* `schema_name` - (Optional, String) Specifies the name of the schema. Fuzzy search is supported.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `schemas` - All schemas that match the filter parameters.

The [schemas](#schemas_struct) structure is documented below.

<a name="schemas_struct"></a>
The `schemas` block supports:

* `database_name` - The database name corresponding to the schema.

* `schema_name` - The name of the schema.

* `used` - The number of schema spaces used, in bytes.

* `space_limit` - The number of available spaces, in bytes.

* `skew_percent` - The skew rate of the schema.

* `min_value` - The number of used spaces by the DN with the minimum usage, in bytes.

* `max_value` - The number of used spaces by the DN with the maximum usage, in bytes.

* `dn_num` - The number of DNs.

* `min_dn` - The DN that uses the least space.

* `max_dn` - The DN that uses the most space.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ func Provider() *schema.Provider {
"huaweicloud_dws_logical_clusters": dws.DataSourceDwsLogicalClusters(),
"huaweicloud_dws_om_account_configuration": dws.DataSourceOmAccountConfiguration(),
"huaweicloud_dws_quotas": dws.DataSourceDwsQuotas(),
"huaweicloud_dws_schema_space_managements": dws.DataSourceDwsSchemaSpaceManagements(),
"huaweicloud_dws_snapshot_policies": dws.DataSourceDwsSnapshotPolicies(),
"huaweicloud_dws_snapshots": dws.DataSourceDwsSnapshots(),
"huaweicloud_dws_workload_plans": dws.DataSourceDwsWorkloadPlans(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package dws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataSourceSchemaSpaceManagements_basic(t *testing.T) {
var (
notFoundDatabase = "data.huaweicloud_dws_schema_space_managements.test"
dcNotFoundDatabase = acceptance.InitDataSourceCheck(notFoundDatabase)
dataSource = "data.huaweicloud_dws_schema_space_managements.test"
dc = acceptance.InitDataSourceCheck(dataSource)
bySchemaName = "data.huaweicloud_dws_schema_space_managements.filter_by_schema_name"
dcBySchemaName = acceptance.InitDataSourceCheck(bySchemaName)
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckDwsClusterId(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceSchemaSpaceManagements_clusterIdNotExist(),
ExpectError: regexp.MustCompile("Cluster does not exist or has been deleted"),
},
{
Config: testDataSourceSchemaSpaceManagements_basic(),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
dcNotFoundDatabase.CheckResourceExists(),
resource.TestCheckOutput("not_found_database", "true"),
resource.TestMatchResourceAttr(dataSource, "schemas.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
resource.TestCheckOutput("assert_space_limit", "true"),
dcBySchemaName.CheckResourceExists(),
resource.TestCheckResourceAttr(bySchemaName, "schemas.0.database_name", "gaussdb"),
resource.TestCheckResourceAttrSet(bySchemaName, "schemas.0.schema_name"),
resource.TestCheckResourceAttrSet(bySchemaName, "schemas.0.used"),
resource.TestCheckResourceAttrSet(bySchemaName, "schemas.0.space_limit"),
resource.TestCheckResourceAttrSet(bySchemaName, "schemas.0.skew_percent"),
resource.TestCheckResourceAttrSet(bySchemaName, "schemas.0.dn_num"),
),
},
},
})
}

func testDataSourceSchemaSpaceManagements_clusterIdNotExist() string {
clusterId, _ := uuid.GenerateUUID()
return fmt.Sprintf(`
data "huaweicloud_dws_schema_space_managements" "test" {
cluster_id = "%s"
database_name = "gaussdb"
}
`, clusterId)
}

func testDataSourceSchemaSpaceManagements_basic() string {
return fmt.Sprintf(`
data "huaweicloud_dws_schema_space_managements" "not_found_database" {
cluster_id = "%[1]s"
database_name = "not_found_database"
}
output "not_found_database" {
value = length(data.huaweicloud_dws_schema_space_managements.not_found_database.schemas) == 0
}
data "huaweicloud_dws_schema_space_managements" "test" {
depends_on = [
huaweicloud_dws_schema_space_management.test
]
cluster_id = "%[1]s"
database_name = huaweicloud_dws_schema_space_management.test.database_name
}
# Modify space quota for scheduler to 2MB (2048 Byte).
resource "huaweicloud_dws_schema_space_management" "test" {
cluster_id = "%[1]s"
database_name = "gaussdb"
schema_name = "scheduler"
space_limit = "2048"
}
# Filter by schema name.
data "huaweicloud_dws_schema_space_managements" "filter_by_schema_name" {
depends_on = [
huaweicloud_dws_schema_space_management.test
]
cluster_id = "%[1]s"
database_name = huaweicloud_dws_schema_space_management.test.database_name
schema_name = local.schema_name
}
locals {
schema_name = huaweicloud_dws_schema_space_management.test.schema_name
# Convert the obtained value from Byte to MB.
space_limit = try([for v in data.huaweicloud_dws_schema_space_managements.filter_by_schema_name.schemas : ceil(v.space_limit / 1024 / 1024)
if v.schema_name == local.schema_name][0], null)
}
output "assert_space_limit" {
value = local.space_limit == 2
}
`, acceptance.HW_DWS_CLUSTER_ID)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Generated by PMS #336
package dws

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/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"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func DataSourceDwsSchemaSpaceManagements() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDwsSchemaSpaceManagementsRead,

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.`,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the DWS cluster ID.`,
},
"database_name": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the database name to which the schema space management belongs.`,
},
"schema_name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the name of the schema. Fuzzy search is supported.`,
},
"schemas": {
Type: schema.TypeList,
Computed: true,
Description: `All schemas that match the filter parameters.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"database_name": {
Type: schema.TypeString,
Computed: true,
Description: `The database name corresponding to the schema.`,
},
"schema_name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the schema.`,
},
"used": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of schema spaces used, in bytes.`,
},
"space_limit": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of available spaces, in bytes.`,
},
"skew_percent": {
Type: schema.TypeFloat,
Computed: true,
Description: `The skew rate of the schema.`,
},
"min_value": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of used spaces by the DN with the minimum usage, in bytes.`,
},
"max_value": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of used spaces by the DN with the maximum usage, in bytes.`,
},
"dn_num": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of DNs.`,
},
"min_dn": {
Type: schema.TypeString,
Computed: true,
Description: `The DN that uses the least space.`,
},
"max_dn": {
Type: schema.TypeString,
Computed: true,
Description: `The DN that uses the most space.`,
},
},
},
},
},
}
}

type SchemaSpaceManagementsDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newSchemaSpaceManagementsDSWrapper(d *schema.ResourceData, meta interface{}) *SchemaSpaceManagementsDSWrapper {
return &SchemaSpaceManagementsDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceDwsSchemaSpaceManagementsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newSchemaSpaceManagementsDSWrapper(d, meta)
listSchemasRst, err := wrapper.ListSchemas()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.listSchemasToSchema(listSchemasRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API DWS GET /v2/{project_id}/clusters/{cluster_id}/databases/{database_name}/schemas
func (w *SchemaSpaceManagementsDSWrapper) ListSchemas() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "dws")
if err != nil {
return nil, err
}

uri := "/v2/{project_id}/clusters/{cluster_id}/databases/{database_name}/schemas"
uri = strings.ReplaceAll(uri, "{cluster_id}", w.Get("cluster_id").(string))
uri = strings.ReplaceAll(uri, "{database_name}", w.Get("database_name").(string))
params := map[string]any{
"keywords": w.Get("schema_name"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
OffsetPager("schemas", "offset", "limit", 100).
Request().
Result()
}

func (w *SchemaSpaceManagementsDSWrapper) listSchemasToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("schemas", schemas.SliceToList(body.Get("schemas"),
func(schemas gjson.Result) any {
return map[string]any{
"database_name": schemas.Get("database_name").Value(),
"schema_name": schemas.Get("schema_name").Value(),
"used": schemas.Get("total_value").Value(),
"space_limit": schemas.Get("perm_space").Value(),
"skew_percent": schemas.Get("skew_percent").Value(),
"min_value": schemas.Get("min_value").Value(),
"max_value": schemas.Get("max_value").Value(),
"dn_num": schemas.Get("dn_num").Value(),
"min_dn": schemas.Get("min_dn").Value(),
"max_dn": schemas.Get("max_dn").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func resourceSchemaSpaceManagementCreate(ctx context.Context, d *schema.Resource
)
client, err := cfg.NewServiceClient("dws", cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating DWS Client: %s", err)
return diag.Errorf("error creating DWS client: %s", err)
}

path := client.Endpoint + httpUrl
Expand Down

0 comments on commit 2fd7a84

Please sign in to comment.