Skip to content

Commit

Permalink
feat(GaussDB): add gaussdb opengauss instance restart resource (#6044)
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 authored Dec 18, 2024
1 parent 5a304c2 commit fe52479
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/resources/gaussdb_opengauss_instance_restart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
subcategory: "GaussDB"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_instance_restart"
description: |-
Manages a GaussDB OpenGauss instance restart resource within HuaweiCloud.
---

# huaweicloud_gaussdb_opengauss_instance_restart

Manages a GaussDB OpenGauss instance restart resource within HuaweiCloud.

## Example Usage

```hcl
variable "instance_id" {}
resource "huaweicloud_gaussdb_opengauss_instance_restart" "test" {
instance_id = var.instance_id
}
```

## 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 parameter will create a new resource.

* `instance_id` - (Required, String, ForceNew) Specifies the ID of the GaussDB OpenGauss instance. Changing this parameter
will create a new resource.

## Attribute Reference

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

* `id` - The resource ID which equals to the `instance_id`.

## Timeouts

This resource provides the following timeouts configuration options:

* `create` - Default is 30 minutes.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,7 @@ func Provider() *schema.Provider {
"huaweicloud_gaussdb_mysql_instant_task_delete": taurusdb.ResourceGaussDBInstantTaskDelete(),

"huaweicloud_gaussdb_opengauss_instance": gaussdb.ResourceOpenGaussInstance(),
"huaweicloud_gaussdb_opengauss_instance_restart": gaussdb.ResourceOpenGaussInstanceRestart(),
"huaweicloud_gaussdb_opengauss_database": gaussdb.ResourceOpenGaussDatabase(),
"huaweicloud_gaussdb_opengauss_backup": gaussdb.ResourceGaussDBOpenGaussBackup(),
"huaweicloud_gaussdb_opengauss_backup_stop": gaussdb.ResourceOpenGaussBackupStop(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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 TestAccOpenGaussInstanceRestart_basic(t *testing.T) {
var obj interface{}
rName := acceptance.RandomAccResourceNameWithDash()
resourceName := "huaweicloud_gaussdb_opengauss_instance_restart.test"

rc := acceptance.InitResourceCheck(
resourceName,
&obj,
getOpenGaussInstanceFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccOpenGaussInstanceRestart_basic(rName),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
),
},
},
})
}

func testAccOpenGaussInstanceRestart_base(rName string) string {
return fmt.Sprintf(`
%[1]s
data "huaweicloud_availability_zones" "test" {}
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 = "gaussdb.bs.s6.xlarge.x864.ha"
name = "%[2]s"
password = "Huangwei!120521"
replica_num = 3
availability_zone = join(",", [data.huaweicloud_availability_zones.test.names[0],
data.huaweicloud_availability_zones.test.names[1],
data.huaweicloud_availability_zones.test.names[2]])
enterprise_project_id = "%[3]s"
ha {
mode = "centralization_standard"
replication_mode = "sync"
consistency = "eventual"
instance_mode = "basic"
}
volume {
type = "ULTRAHIGH"
size = 40
}
}
resource "huaweicloud_vpc_eip" "test" {
publicip {
type = "5_bgp"
}
bandwidth {
name = "%[2]s"
size = 8
share_type = "PER"
charge_mode = "traffic"
}
}
`, common.TestBaseNetwork(rName), rName, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST)
}

func testAccOpenGaussInstanceRestart_basic(rName string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_gaussdb_opengauss_instance_restart" "test" {
instance_id = huaweicloud_gaussdb_opengauss_instance.test.id
}`, testAccOpenGaussInstanceRestart_base(rName))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package gaussdb

import (
"context"
"net/http"
"strings"
"time"

"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 POST /v3/{project_id}/instances/{instance_id}/restart
// @API GaussDB POST /v3/{project_id}/instances/{instance_id}/nodes/{node_id}/restart
// @API GaussDB GET /v3/{project_id}/jobs
func ResourceOpenGaussInstanceRestart() *schema.Resource {
return &schema.Resource{
CreateContext: resourceOpenGaussInstanceRestartCreate,
ReadContext: resourceOpenGaussInstanceRestartRead,
DeleteContext: resourceOpenGaussInstanceRestartDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceOpenGaussInstanceRestartCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)

var (
httpUrl = "v3/{project_id}/instances/{instance_id}/restart"
product = "opengauss"
)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating GaussDB client: %s", err)
}

instanceId := d.Get("instance_id").(string)
createPath := client.Endpoint + httpUrl
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)
createPath = strings.ReplaceAll(createPath, "{instance_id}", instanceId)

createOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}

retryFunc := func() (interface{}, bool, error) {
res, err := client.Request("POST", createPath, &createOpt)
retry, err := handleMultiOperationsError(err)
return res, retry, err
}
r, err := common.RetryContextWithWaitForState(&common.RetryContextWithWaitForStateParam{
Ctx: ctx,
RetryFunc: retryFunc,
WaitFunc: instanceStateRefreshFunc(client, instanceId),
WaitTarget: []string{"ACTIVE"},
Timeout: d.Timeout(schema.TimeoutCreate),
DelayTimeout: 10 * time.Second,
PollInterval: 10 * time.Second,
})
if err != nil {
return diag.Errorf("error restarting GaussDB OpenGauss instance(%s): %s", instanceId, err)
}

d.SetId(instanceId)

createRespBody, err := utils.FlattenResponse(r.(*http.Response))
if err != nil {
return diag.FromErr(err)
}

jobId := utils.PathSearch("job_id", createRespBody, nil)
if jobId == nil {
return diag.Errorf("error restarting GaussDB OpenGauss instance(%s), job_id is not found in the response",
instanceId)
}
err = checkGaussDBOpenGaussJobFinish(ctx, client, jobId.(string), 2, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}

return nil
}

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

func resourceOpenGaussInstanceRestartDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
errorMsg := "Deleting GaussDB OpenGauss restart resource is not supported. The restart resource is only removed " +
"from the state, the GaussDB OpenGauss instance remains in the cloud."
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: errorMsg,
},
}
}

0 comments on commit fe52479

Please sign in to comment.