Skip to content

Commit

Permalink
feat(dws): add new resource supports schema space management
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhuanhong committed Sep 14, 2024
1 parent 98fb8ba commit c8e6dfb
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/resources/dws_schema_space_management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
subcategory: "GaussDB(DWS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_dws_schema_space_management"
description: |-
Use this resource to modify schema space limit under specified DWS cluster within HuaweiCloud.
---
# huaweicloud_dws_schema_space_management

Use this resource to modify schema space limit under specified DWS cluster within HuaweiCloud.

-> 1. This resource is supported only in `8.1.1` or later.
<br>2. The space quota limit only common users but not database administrators.
<br>3.This resource is only a one-time action resource for modifying schema space limit. Deleting this resource will
not clear the corresponding request record, but will only remove the resource information from the tfstate file.

## Example Usage

```hcl
variable "dws_cluster_id" {}
variable "database_name" {}
variable "schema_name" {}
resource "huaweicloud_dws_schema_space_management" "test" {
cluster_id = var.dws_cluster_id
database_name = var.database_name
schema_name = var.schema_name
space_limit = "1024"
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
If omitted, the provider-level region will be used.
Changing this creates a new resource.

* `cluster_id` - (Required, String, ForceNew) Specifies the DWS cluster ID.
Changing this creates a new resource.

* `database_name` - (Required, String, ForceNew) Specifies the database name to which the schema space management belongs.
Changing this creates a new resource.

* `schema_name` - (Required, String, ForceNew) Specifies the name of the schema.
Changing this creates a new resource.

* `space_limit` - (Required, String, ForceNew) Specifies space limit of the schema, in KB.
The valid value ranges from `-1` to `9,007,199,254,740,992`, `-1` and `0` means unlimited.

Changing this creates a new resource.

## Attribute Reference

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

* `id` - The resource ID.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ func Provider() *schema.Provider {
"huaweicloud_dws_logical_cluster": dws.ResourceLogicalCluster(),
"huaweicloud_dws_om_account_action": dws.ResourceOmAccountAction(),
"huaweicloud_dws_public_domain_associate": dws.ResourcePublicDomainAssociate(),
"huaweicloud_dws_schema_space_management": dws.ResourceSchemaSpaceManagement(),
"huaweicloud_dws_snapshot_policy": dws.ResourceDwsSnapshotPolicy(),
"huaweicloud_dws_snapshot": dws.ResourceDwsSnapshot(),
"huaweicloud_dws_workload_configuration": dws.ResourceWorkLoadConfiguration(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package dws

import (
"context"
"strings"

"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"
)

// @API APIG PUT /v2/{project_id}/clusters/{cluster_id}/databases/{database_name}/schemas
func ResourceSchemaSpaceManagement() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSchemaSpaceManagementCreate,
ReadContext: resourceSchemaSpaceManagementRead,
DeleteContext: resourceSchemaSpaceManagementDelete,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Specifies the DWS cluster ID.",
},
"database_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Specifies the database name to which the schema space management belongs.",
},
"schema_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Specifies the name of the schema.",
},
"space_limit": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Specifies space limit of the schema.",
},
},
}
}

func resourceSchemaSpaceManagementCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
httpUrl = "v2/{project_id}/clusters/{cluster_id}/databases/{database_name}/schemas"
clusterId = d.Get("cluster_id").(string)
databaseName = d.Get("database_name").(string)
)
client, err := cfg.NewServiceClient("dws", cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating DWS Client: %s", err)
}

path := client.Endpoint + httpUrl
path = strings.ReplaceAll(path, "{project_id}", client.ProjectID)
path = strings.ReplaceAll(path, "{cluster_id}", clusterId)
path = strings.ReplaceAll(path, "{database_name}", databaseName)
createOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
JSONBody: map[string]interface{}{
"schema_name": d.Get("schema_name"),
"perm_space": d.Get("space_limit"),
},
}

_, err = client.Request("PUT", path, &createOpt)
if err != nil {
return diag.Errorf("error modifying schema space limit for database (%s): %s", databaseName, err)
}

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

return resourceSchemaSpaceManagementRead(ctx, d, meta)
}

func resourceSchemaSpaceManagementRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
}

func resourceSchemaSpaceManagementDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
errorMsg := `This resource is only a one-time action resource for modifying schema space limit. Deleting this resource will
not clear the corresponding request record, but will only remove the resource information from the tfstate file.`
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: errorMsg,
},
}
}

0 comments on commit c8e6dfb

Please sign in to comment.