-
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 recycling policy resource
- Loading branch information
Showing
4 changed files
with
283 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,42 @@ | ||
--- | ||
subcategory: "GaussDB" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_recycling_policy" | ||
description: |- | ||
Manage a GaussDB OpenGauss recycling policy resource within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_gaussdb_opengauss_recycling_policy | ||
|
||
Manage a GaussDB OpenGauss recycling policy resource within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "huaweicloud_gaussdb_opengauss_recycling_policy" "test" { | ||
retention_period_in_days = "5" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String, ForceNew) The region in which to create the recycling policy resource. If omitted, the | ||
provider-level region will be used. Changing this creates a new GaussDB OpenGauss instance resource. | ||
|
||
* `retention_period_in_days` - (Required, String) Specifies the retention period, in days. Value ranges: **1** to **7**. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - Indicates the resource ID. The value is the project ID of the region. | ||
|
||
## Import | ||
|
||
The GaussDB OpenGauss recycling policy can be imported using the `id`, e.g. | ||
|
||
```bash | ||
$ terraform import huaweicloud_gaussdb_opengauss_recycling_policy.test <id> | ||
``` |
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
99 changes: 99 additions & 0 deletions
99
...rvices/acceptance/gaussdb/resource_huaweicloud_gaussdb_opengauss_recycling_policy_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,99 @@ | ||
package gaussdb | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
||
"github.com/chnsz/golangsdk" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
func getOpenGaussRecyclingPolicyResourceFunc(cfg *config.Config, _ *terraform.ResourceState) (interface{}, error) { | ||
region := acceptance.HW_REGION_NAME | ||
var ( | ||
httpUrl = "v3/{project_id}/recycle-policy" | ||
product = "opengauss" | ||
) | ||
client, err := cfg.NewServiceClient(product, region) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating GaussDB client: %s", err) | ||
} | ||
|
||
getPath := client.Endpoint + httpUrl | ||
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID) | ||
|
||
getOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
MoreHeaders: map[string]string{"Content-Type": "application/json;charset=UTF-8"}, | ||
} | ||
getResp, err := client.Request("GET", getPath, &getOpt) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("error retrieving GaussDB OpenGauss recycling policy: %s", err) | ||
} | ||
|
||
getRespBody, err := utils.FlattenResponse(getResp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
retentionPeriod := utils.PathSearch("retention_period_in_days", getRespBody, nil) | ||
if retentionPeriod == nil { | ||
return nil, fmt.Errorf("error retrieving GaussDB OpenGauss recycling policy, retention_period_in_days " + | ||
"is not found") | ||
} | ||
return getRespBody, nil | ||
} | ||
|
||
func TestAccOpenGaussRecyclingPolicy_basic(t *testing.T) { | ||
var obj interface{} | ||
|
||
rName := "huaweicloud_gaussdb_opengauss_recycling_policy.test" | ||
|
||
rc := acceptance.InitResourceCheck( | ||
rName, | ||
&obj, | ||
getOpenGaussRecyclingPolicyResourceFunc, | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.TestAccPreCheck(t) }, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testOpenGaussRecyclingPolicy_basic("3"), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
resource.TestCheckResourceAttr(rName, "retention_period_in_days", "3"), | ||
), | ||
}, | ||
{ | ||
Config: testOpenGaussRecyclingPolicy_basic("6"), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
resource.TestCheckResourceAttr(rName, "retention_period_in_days", "6"), | ||
), | ||
}, | ||
{ | ||
ResourceName: rName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testOpenGaussRecyclingPolicy_basic(retentionPeriodInDays string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_gaussdb_opengauss_recycling_policy" "test" { | ||
retention_period_in_days = "%s" | ||
} | ||
`, retentionPeriodInDays) | ||
} |
141 changes: 141 additions & 0 deletions
141
huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_opengauss_recycling_policy.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,141 @@ | ||
package gaussdb | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"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/common" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
// @API GaussDB PUT /v3/{project_id}/recycle-policy | ||
// @API GaussDB GET /v3/{project_id}/recycle-policy | ||
func ResourceOpenGaussRecyclingPolicy() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceOpenGaussRecyclingPolicyCreateOrUpdate, | ||
UpdateContext: resourceOpenGaussRecyclingPolicyCreateOrUpdate, | ||
ReadContext: resourceOpenGaussRecyclingPolicyRead, | ||
DeleteContext: resourceOpenGaussRecyclingPolicyDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
"retention_period_in_days": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceOpenGaussRecyclingPolicyCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
cfg := meta.(*config.Config) | ||
region := cfg.GetRegion(d) | ||
|
||
var ( | ||
httpUrl = "v3/{project_id}/recycle-policy" | ||
product = "opengauss" | ||
) | ||
client, err := cfg.NewServiceClient(product, region) | ||
if err != nil { | ||
return diag.Errorf("error creating GaussDB client: %s", err) | ||
} | ||
|
||
createPath := client.Endpoint + httpUrl | ||
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID) | ||
|
||
createOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
} | ||
createOpt.JSONBody = buildCreateOpenGaussRecyclingPolicyBodyParams(d) | ||
|
||
_, err = client.Request("PUT", createPath, &createOpt) | ||
if err != nil { | ||
return diag.Errorf("error creating GaussDB OpenGauss recycling policy: %s", err) | ||
} | ||
|
||
d.SetId(client.ProjectID) | ||
|
||
return resourceOpenGaussRecyclingPolicyRead(ctx, d, meta) | ||
} | ||
|
||
func buildCreateOpenGaussRecyclingPolicyBodyParams(d *schema.ResourceData) map[string]interface{} { | ||
param := make(map[string]interface{}) | ||
if v, ok := d.GetOk("retention_period_in_days"); ok { | ||
param["retention_period_in_days"] = v | ||
} | ||
bodyParams := map[string]interface{}{ | ||
"recycle_policy": param, | ||
} | ||
return bodyParams | ||
} | ||
|
||
func resourceOpenGaussRecyclingPolicyRead(_ 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}/recycle-policy" | ||
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, | ||
MoreHeaders: map[string]string{"Content-Type": "application/json;charset=UTF-8"}, | ||
} | ||
getResp, err := client.Request("GET", getPath, &getOpt) | ||
if err != nil { | ||
return common.CheckDeletedDiag(d, err, "error retrieving GaussDB OpenGauss recycling policy") | ||
} | ||
|
||
getRespBody, err := utils.FlattenResponse(getResp) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
retentionPeriod := utils.PathSearch("retention_period_in_days", getRespBody, nil) | ||
if retentionPeriod == nil { | ||
return diag.Errorf("error retrieving GaussDB OpenGauss recycling policy, retention_period_in_days is " + | ||
"not found") | ||
} | ||
|
||
mErr = multierror.Append( | ||
mErr, | ||
d.Set("region", region), | ||
d.Set("retention_period_in_days", retentionPeriod), | ||
) | ||
return diag.FromErr(mErr.ErrorOrNil()) | ||
} | ||
|
||
func resourceOpenGaussRecyclingPolicyDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { | ||
errorMsg := "Deleting recycling policy is not supported. The recycling policy is only removed from the state," + | ||
" but it remains in the cloud. And the recycling policy doesn't return to the original state." | ||
return diag.Diagnostics{ | ||
diag.Diagnostic{ | ||
Severity: diag.Warning, | ||
Summary: errorMsg, | ||
}, | ||
} | ||
} |