Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dbss): add a new datasource to get the list of RDS databases #5903

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions docs/data-sources/dbss_rds_databases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
subcategory: "Database Security Service (DBSS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_dbss_rds_databases"
description: |-
Use this data source to get the list of RDS databases.
---

# huaweicloud_dbss_rds_databases

Use this data source to get the list of RDS databases.

## Example Usage

```hcl
variable "type" {}

data "huaweicloud_dbss_rds_databases" "test" {
type = var.type
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) Specifies the region in which to query the resource.
If omitted, the provider-level region will be used.

* `type` - (Required, String) Specifies the RDS database type.
The valid values are as follows:
+ **MYSQL**
+ **POSTGRESQL**
+ **SQLSERVER**
+ **TAURUS**
+ **DWS**
+ **MARIADB**
+ **GAUSSDBOPENGAUSS**

## Attribute Reference

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

* `id` - The data source ID.

* `databases` - The RDS database list.

The [databases](#databases_struct) structure is documented below.

<a name="databases_struct"></a>
The `databases` block supports:

* `id` - The RDS instance ID.

* `name` - The RDS database name.

* `type` - The RDS database type.

* `status` - The RDS instance status.
The valid values are as follows:
+ **BUILD**: The instance is being created.
+ **ACTIVE**: The instance is normal.
+ **FAILED**: The instance is abnormal.
+ **FROZEN**: The instance is frozen.
+ **MODIFYING**: The instance is being scaled out.
+ **REBOOTING**: The instance is being restarted.
+ **RESTORING**: The instance is being restored.
+ **MODIFYING INSTANCE TYPE**: The instance is changing to the active/standby mode.
+ **SWITCHOVER**: The instance is performing an active/standby switchover.
+ **MIGRATING**: The instance is being migrated.
+ **BACKING UP**: The instance is being backed up.
+ **MODIFYING DATABASE PORT**: The database port of the instance is being changed.
+ **STORAGE FULL**: The instance disk is full.

* `version` - The RDS database version.

* `ip` - The RDS database IP address.

* `port` - The RDS database port.

* `is_supported` - Whether agent-free audit is supported.

* `instance_name` - The RDS instance name.

* `enterprise_project_id` - The enterprise project ID to which the RDS instance belongs.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ func Provider() *schema.Provider {
"huaweicloud_dbss_flavors": dbss.DataSourceDbssFlavors(),
"huaweicloud_dbss_instances": dbss.DataSourceDbssInstances(),
"huaweicloud_dbss_operation_logs": dbss.DataSourceOperationLogs(),
"huaweicloud_dbss_rds_databases": dbss.DataSourceDbssRdsDatabases(),

"huaweicloud_dc_connections": dc.DataSourceDcConnections(),
"huaweicloud_dc_quotas": dc.DataSourceDcQuotas(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package dbss

import (
"fmt"
"testing"

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

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

func TestAccDataSourceRdsDatabases_basic(t *testing.T) {
var (
dataSourceName = "data.huaweicloud_dbss_rds_databases.test"
name = acceptance.RandomAccResourceName()
dc = acceptance.InitDataSourceCheck(dataSourceName)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceRdsDatabases_basic(name),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.id"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.name"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.type"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.status"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.version"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.ip"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.port"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.is_supported"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.instance_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "databases.0.enterprise_project_id"),
),
},
},
})
}

func testDataSourceRdsDatabases_basic(name string) string {
return fmt.Sprintf(`
data "huaweicloud_availability_zones" "test" {}

data "huaweicloud_vpc" "test" {
name = "vpc-default"
}

data "huaweicloud_vpc_subnet" "test" {
name = "subnet-default"
}

data "huaweicloud_networking_secgroup" "test" {
name = "default"
}

data "huaweicloud_rds_flavors" "test" {
db_type = "MySQL"
db_version = "8.0"
instance_mode = "single"
group_type = "dedicated"
vcpus = 2
}

resource "huaweicloud_rds_instance" "test" {
name = "%s"
flavor = data.huaweicloud_rds_flavors.test.flavors[0].name
security_group_id = data.huaweicloud_networking_secgroup.test.id
subnet_id = data.huaweicloud_vpc_subnet.test.id
vpc_id = data.huaweicloud_vpc.test.id
availability_zone = [data.huaweicloud_availability_zones.test.names[0]]

db {
type = "MySQL"
version = "8.0"
}

volume {
type = "CLOUDSSD"
size = 100
}
}

data "huaweicloud_dbss_rds_databases" "test" {
type = "MYSQL"

depends_on = [huaweicloud_rds_instance.test]
}
`, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Generated by PMS #443
package dbss

import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func DataSourceDbssRdsDatabases() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDbssRdsDatabasesRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
},
"type": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the RDS database type.`,
},
"databases": {
Type: schema.TypeList,
Computed: true,
Description: `The RDS database list.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The RDS instance ID.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The RDS database name.`,
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: `The RDS database type.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `The RDS instance status.`,
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: `The RDS database version.`,
},
"ip": {
Type: schema.TypeString,
Computed: true,
Description: `The RDS database IP address.`,
},
"port": {
Type: schema.TypeString,
Computed: true,
Description: `The RDS database port.`,
},
"is_supported": {
Type: schema.TypeBool,
Computed: true,
Description: `Whether agent-free audit is supported.`,
},
"instance_name": {
Type: schema.TypeString,
Computed: true,
Description: `The RDS instance name.`,
},
"enterprise_project_id": {
Type: schema.TypeString,
Computed: true,
Description: `The enterprise project ID to which the RDS instance belongs.`,
},
},
},
},
},
}
}

type RdsDatabasesDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newRdsDatabasesDSWrapper(d *schema.ResourceData, meta interface{}) *RdsDatabasesDSWrapper {
return &RdsDatabasesDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceDbssRdsDatabasesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newRdsDatabasesDSWrapper(d, meta)
listRdsDatabasesRst, err := wrapper.ListRdsDatabases()
if err != nil {
return diag.FromErr(err)
}

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

err = wrapper.listRdsDatabasesToSchema(listRdsDatabasesRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API DBSS GET /v2/{project_id}/audit/databases/rds
func (w *RdsDatabasesDSWrapper) ListRdsDatabases() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "dbss")
if err != nil {
return nil, err
}

uri := "/v2/{project_id}/audit/databases/rds"
params := map[string]any{
"db_type": w.Get("type"),
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("GET").
URI(uri).
Query(params).
OffsetPager("databases", "offset", "limit", 0).
Request().
Result()
}

func (w *RdsDatabasesDSWrapper) listRdsDatabasesToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("databases", schemas.SliceToList(body.Get("databases"),
func(databases gjson.Result) any {
return map[string]any{
"id": databases.Get("id").Value(),
"name": databases.Get("db_name").Value(),
"type": databases.Get("type").Value(),
"status": databases.Get("status").Value(),
"version": databases.Get("version").Value(),
"ip": databases.Get("ip").Value(),
"port": databases.Get("port").Value(),
"is_supported": databases.Get("is_supported").Value(),
"instance_name": databases.Get("instance_name").Value(),
"enterprise_project_id": databases.Get("enterprise_id").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
Loading