Skip to content

Commit

Permalink
feat(GaussDB): add gaussdb opengauss recycling policy resource
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 committed Jan 6, 2025
1 parent f346c75 commit d0f8570
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/resources/gaussdb_opengauss_recycling_policy.md
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>
```
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,7 @@ func Provider() *schema.Provider {
"huaweicloud_gaussdb_opengauss_parameter_template": gaussdb.ResourceOpenGaussParameterTemplate(),
"huaweicloud_gaussdb_opengauss_parameter_template_apply": gaussdb.ResourceOpenGaussParameterTemplateApply(),
"huaweicloud_gaussdb_opengauss_parameter_template_compare": gaussdb.ResourceOpenGaussParameterTemplateCompare(),
"huaweicloud_gaussdb_opengauss_recycling_policy": gaussdb.ResourceOpenGaussRecyclingPolicy(),

"huaweicloud_ges_graph": ges.ResourceGesGraph(),
"huaweicloud_ges_metadata": ges.ResourceGesMetadata(),
Expand Down
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)
}
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,
},
}
}

0 comments on commit d0f8570

Please sign in to comment.