-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dbss): add a new datasource to get the list of RDS databases
- Loading branch information
1 parent
c7bd6e0
commit fd4a2da
Showing
4 changed files
with
355 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,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. |
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
95 changes: 95 additions & 0 deletions
95
huaweicloud/services/acceptance/dbss/data_source_huaweicloud_dbss_rds_databases_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,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) | ||
} |
174 changes: 174 additions & 0 deletions
174
huaweicloud/services/dbss/data_source_huaweicloud_dbss_rds_databases.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,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() | ||
} |