Skip to content

Commit

Permalink
feat(dbss): add new resource manages self built database bind DBSS in…
Browse files Browse the repository at this point in the history
…stance (#5899)
  • Loading branch information
ruwenqiang123 authored Nov 22, 2024
1 parent 3917011 commit d9bb121
Show file tree
Hide file tree
Showing 4 changed files with 620 additions and 0 deletions.
151 changes: 151 additions & 0 deletions docs/resources/dbss_ecs_database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
---
subcategory: "Database Security Service (DBSS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_dbss_ecs_database"
description: |-
Manage the resource of adding self built database to DBSS instance within HuaweiCloud.
---

# huaweicloud_dbss_ecs_database

Manage the resource of adding self built database to DBSS instance within HuaweiCloud.

-> Before adding the self built database to the DBSS instance, the DBSS instance `status` must be **ACTIVE**.

## Example Usage

```hcl
variable "instance_id" {}
variable "name" {}
variable "type" {}
variable "version" {}
variable "ip" {}
variable "port" {}
variable "os" {}
resource "huaweicloud_dbss_ecs_database" "test" {
instance_id = var.instance_id
name = var.name
type = var.type
version = var.version
ip = var.ip
port = var.port
os = var.os
}
```

## 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 DBSS instance ID.
Changing this parameter will create a new resource.

* `name` - (Required, String, ForceNew) Specifies the self built database name.
Changing this parameter will create a new resource.

* `type` - (Required, String, ForceNew) Specifies the self built database type.
The valid values are as follows:
+ **MYSQL**
+ **ORACLE**
+ **POSTGRESQL**
+ **SQLSERVER**
+ **DAMENG**
+ **TAURUS**
+ **DWS**
+ **KINGBASE**
+ **GAUSSDBOPENGAUSS**
+ **GREENPLUM**
+ **HIGHGO**
+ **SHENTONG**
+ **GBASE8A**
+ **GBASE8S**
+ **GBASEXDM**
+ **MONGODB**
+ **DDS**

Changing this parameter will create a new resource.

* `version` - (Required, String, ForceNew) Specifies the self built database version.
Changing this parameter will create a new resource.

* `ip` - (Required, String, ForceNew) Specifies the self built database IP address.
Changing this parameter will create a new resource.

* `port` - (Required, String, ForceNew) Specifies the self built database port.
Changing this parameter will create a new resource.

* `os` - (Required, String, ForceNew) Specifies the self built database operation system.
The valid values are as follows:
+ **LINUX64**
+ **WINDOWS64**
+ **UNIX**

Changing this parameter will create a new resource.

* `charset` - (Optional, String, ForceNew) Specifies the self built database character set.
The value can be **GBK** or **UTF8**. Defaults to **UTF8**

Changing this parameter will create a new resource.

* `instance_name` - (Optional, String, ForceNew) Specifies the self built database instance name.
Changing this parameter will create a new resource.

* `status` - (Optional, String) Specifies the audit status of the self built database.
The valid values are as follows:
+ **ON**
+ **OFF**

After a self built database is associated with the DBSS instance, the audit status is **OFF** by default.

* `lts_audit_switch` - (Optional, Int) Specifies whether to disable LTS audit.
The valid values are as follows:
+ `1`: Indicates disable.
+ `0`: Remain unchanged. (In this case, the value can also be an integer other than `1`).

-> This parameter is used in the DWS database scenario. If you do not need to close it,
there is no need to set this field.

## Attribute Reference

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

* `id` - The resource ID.

* `audit_status` - The database running status.
The value can be **ACTIVE**, **SHUTOFF** or **ERROR**.

* `agent_url` - The unique ID of the agent.

* `db_classification` - The classification of the database.

## Import

The resource can be imported using the related `instance_id` and their `id`, separated by a slash (/), e.g.

```bash
$ terraform import huaweicloud_dbss_ecs_database.test <instance_id>/<id>
```

Note that the imported state may not be identical to your resource definition, due to some attributes missing from the
API response.
The missing attributes include: `lts_audit_switch`.
It is generally recommended running `terraform plan` after importing the resource.
You can then decide if changes should be applied to the instance, or the resource definition should be updated to align
with the instance. Also, you can ignore changes as below.

```hcl
resource "huaweicloud_dbss_ecs_database" "test" {
...
lifecycle {
ignore_changes = [
lts_audit_switch,
]
}
}
```
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ func Provider() *schema.Provider {
"huaweicloud_css_logstash_custom_template": css.ResourceLogstashCustomTemplate(),

"huaweicloud_dbss_audit_risk_rule_action": dbss.ResourceRiskRuleAction(),
"huaweicloud_dbss_ecs_database": dbss.ResourceAddEcsDatabase(),
"huaweicloud_dbss_instance": dbss.ResourceInstance(),
"huaweicloud_dbss_rds_database": dbss.ResourceAddRdsDatabase(),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package dbss

import (
"fmt"
"testing"

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

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

func getAddEcsDatabaseFunc(conf *config.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := conf.NewServiceClient("dbss", acceptance.HW_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating DBSS client: %s", err)
}
return dbss.GetDatabases(client, state.Primary.Attributes["instance_id"], state.Primary.ID)
}

func TestAccAddEcsDatabase_basic(t *testing.T) {
var (
addEcsDatabase interface{}
rName = "huaweicloud_dbss_ecs_database.test"
name = acceptance.RandomAccResourceName()
)

rc := acceptance.InitResourceCheck(
rName,
&addEcsDatabase,
getAddEcsDatabaseFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccAddEcsDatabase_basic(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttrPair(rName, "instance_id", "huaweicloud_dbss_instance.test", "instance_id"),
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "type", "MYSQL"),
resource.TestCheckResourceAttr(rName, "version", "8"),
resource.TestCheckResourceAttr(rName, "ip", "192.168.0.88"),
resource.TestCheckResourceAttr(rName, "port", "3306"),
resource.TestCheckResourceAttr(rName, "os", "LINUX64"),
resource.TestCheckResourceAttr(rName, "charset", "UTF8"),
resource.TestCheckResourceAttr(rName, "instance_name", name),
resource.TestCheckResourceAttr(rName, "status", "ON"),
resource.TestCheckResourceAttrSet(rName, "db_classification"),
),
},
{
Config: testAccAddEcsDatabase_update(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "status", "OFF"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"lts_audit_switch",
},
ImportStateIdFunc: testAccAddEcsDatabaseImportState(rName),
},
},
})
}

func testAccAddEcsDatabase_basic(name string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_dbss_ecs_database" "test" {
instance_id = huaweicloud_dbss_instance.test.instance_id
name = "%[2]s"
type = "MYSQL"
version = "8"
ip = "192.168.0.88"
port = "3306"
os = "LINUX64"
charset = "UTF8"
instance_name = "%[2]s"
status = "ON"
}
`, testInstance_basic(name), name)
}

func testAccAddEcsDatabase_update(name string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_dbss_ecs_database" "test" {
instance_id = huaweicloud_dbss_instance.test.instance_id
name = "%[2]s"
type = "MYSQL"
version = "8"
ip = "192.168.0.88"
port = "3306"
os = "LINUX64"
charset = "UTF8"
instance_name = "%[2]s"
status = "OFF"
}
`, testInstance_basic(name), name)
}

func testAccAddEcsDatabaseImportState(rName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
var instanceId, databaseId string
rs, ok := s.RootModule().Resources[rName]
if !ok {
return "", fmt.Errorf("resource (%s) not found", rName)
}

instanceId = rs.Primary.Attributes["instance_id"]
databaseId = rs.Primary.ID
if instanceId == "" || databaseId == "" {
return "", fmt.Errorf("invalid format specified for import ID, want '<instance_id>/<id>', but got '%s/%s'",
instanceId, databaseId)
}
return fmt.Sprintf("%s/%s", instanceId, databaseId), nil
}
}
Loading

0 comments on commit d9bb121

Please sign in to comment.