Skip to content

Commit

Permalink
Add gaussdb_mysql_instance data source (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuzhenguo committed Nov 24, 2020
1 parent db7a61b commit f693f3b
Show file tree
Hide file tree
Showing 5 changed files with 428 additions and 2 deletions.
82 changes: 82 additions & 0 deletions docs/data-sources/gaussdb_mysql_instance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
subcategory: "GaussDB"
---

# huaweicloud\_gaussdb\_mysql\_instance

Use this data source to get available HuaweiCloud gaussdb mysql instance.

## Example Usage

```hcl
data "huaweicloud_gaussdb_mysql_instance" "this" {
name = "gaussdb-instance"
}
```

## Argument Reference

* `region` - (Optional) The region in which to obtain the instance. If omitted, the provider-level region will be used.

* `name` - (Optional) Specifies the name of the instance.

* `vpc_id` - (Optional) Specifies the VPC ID.

* `subnet_id` - (Optional) Specifies the network ID of a subnet.


## Attributes Reference

* `id` - Indicates the ID of the instance.

* `flavor` - Indicates the instance specifications.

* `security_group_id` - Indicates the security group ID. Required if the selected subnet doesn't enable network ACL.

* `configuration_id` - Indicates the configuration ID.

* `enterprise_project_id` - Indicates the enterprise project id.

* `read_replicas` - Indicates the count of read replicas.

* `time_zone` - Indicates the time zone.

* `availability_zone_mode` - Indicates the availability zone mode: "single" or "multi".

* `master_availability_zone` - Indicates the availability zone where the master node resides.

* `datastore` - Indicates the database information. Structure is documented below.

* `backup_strategy` - Indicates the advanced backup policy. Structure is documented below.

* `status` - Indicates the DB instance status.

* `port` - Indicates the database port.

* `mode` - Indicates the instance mode.

* `db_user_name` - Indicates the default username.

* `private_write_ip` - Indicates the private IP address of the DB instance.

* `nodes` - Indicates the instance nodes information. Structure is documented below.


The `datastore` block supports:

* `engine` - Indicates the database engine.
* `version` - Indicates the database version.

The `backup_strategy` block supports:

* `start_time` - Indicates the backup time window.
* `keep_days` - Indicates the number of days to retain the generated

The `nodes` block contains:

- `id` - Indicates the node ID.
- `name` - Indicates the node name.
- `type` - Indicates the node type: master or slave.
- `status` - Indicates the node status.
- `private_read_ip` - Indicates the private IP address of a node.
- `availability_zone` - Indicates the availability zone where the node resides.
275 changes: 275 additions & 0 deletions huaweicloud/data_source_huaweicloud_gaussdb_mysql_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
package huaweicloud

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/huaweicloud/golangsdk/openstack/taurusdb/v3/instances"
)

func dataSourceGaussDBMysqlInstance() *schema.Resource {
return &schema.Resource{
Read: dataSourceGaussDBMysqlInstanceRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
},
"subnet_id": {
Type: schema.TypeString,
Optional: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"mode": {
Type: schema.TypeString,
Computed: true,
},
"security_group_id": {
Type: schema.TypeString,
Computed: true,
},
"configuration_id": {
Type: schema.TypeString,
Computed: true,
},
"enterprise_project_id": {
Type: schema.TypeString,
Computed: true,
},
"db_user_name": {
Type: schema.TypeString,
Computed: true,
},
"time_zone": {
Type: schema.TypeString,
Computed: true,
},
"availability_zone_mode": {
Type: schema.TypeString,
Computed: true,
},
"master_availability_zone": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Computed: true,
},
"private_write_ip": {
Type: schema.TypeString,
Computed: true,
},
"datastore": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"engine": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"backup_strategy": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_time": {
Type: schema.TypeString,
Computed: true,
},
"keep_days": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"read_replicas": {
Type: schema.TypeInt,
Computed: true,
},
"flavor": {
Type: schema.TypeString,
Computed: true,
},
"nodes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"private_read_ip": {
Type: schema.TypeString,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceGaussDBMysqlInstanceRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region := GetRegion(d, config)
client, err := config.gaussdbV3Client(region)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud GaussDB client: %s", err)
}

listOpts := instances.ListTaurusDBInstanceOpts{
Name: d.Get("name").(string),
VpcId: d.Get("vpc_id").(string),
SubnetId: d.Get("subnet_id").(string),
}

pages, err := instances.List(client, listOpts).AllPages()
if err != nil {
return err
}

allInstances, err := instances.ExtractTaurusDBInstances(pages)
if err != nil {
return fmt.Errorf("Unable to retrieve instances: %s", err)
}

if allInstances.TotalCount < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}

if allInstances.TotalCount > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}

instanceID := allInstances.Instances[0].Id
instance, err := instances.Get(client, instanceID).Extract()
if err != nil {
return err
}

log.Printf("[DEBUG] Retrieved Instance %s: %+v", instance.Id, instance)
d.SetId(instance.Id)

d.Set("region", region)
d.Set("name", instance.Name)
d.Set("status", instance.Status)
d.Set("mode", instance.Type)
d.Set("vpc_id", instance.VpcId)
d.Set("subnet_id", instance.SubnetId)
d.Set("security_group_id", instance.SecurityGroupId)
d.Set("configuration_id", instance.ConfigurationId)
d.Set("enterprise_project_id", instance.EnterpriseProjectId)
d.Set("db_user_name", instance.DbUserName)
d.Set("time_zone", instance.TimeZone)
d.Set("availability_zone_mode", instance.AZMode)
d.Set("master_availability_zone", instance.MasterAZ)

if dbPort, err := strconv.Atoi(instance.Port); err == nil {
d.Set("port", dbPort)
}
if len(instance.PrivateIps) > 0 {
d.Set("private_write_ip", instance.PrivateIps[0])
}

// set data store
dbList := make([]map[string]interface{}, 1)
db := map[string]interface{}{
"version": instance.DataStore.Version,
}
// normalize engine
engine := instance.DataStore.Type
if engine == "GaussDB(for MySQL)" {
engine = "gaussdb-mysql"
}
db["engine"] = engine
dbList[0] = db
d.Set("datastore", dbList)

// set nodes
flavor := ""
slave_count := 0
nodesList := make([]map[string]interface{}, 0, 1)
for _, raw := range instance.Nodes {
node := map[string]interface{}{
"id": raw.Id,
"name": raw.Name,
"status": raw.Status,
"type": raw.Type,
"availability_zone": raw.AvailabilityZone,
}
if len(raw.PrivateIps) > 0 {
node["private_read_ip"] = raw.PrivateIps[0]
}
nodesList = append(nodesList, node)
if raw.Type == "slave" && raw.Status == "ACTIVE" {
slave_count += 1
}
if flavor == "" {
flavor = raw.Flavor
}
}
d.Set("nodes", nodesList)
d.Set("read_replicas", slave_count)
if flavor != "" {
log.Printf("[DEBUG] Node Flavor: %s", flavor)
d.Set("flavor", flavor)
}

// set backup_strategy
backupStrategyList := make([]map[string]interface{}, 1)
backupStrategy := map[string]interface{}{
"start_time": instance.BackupStrategy.StartTime,
}
if days, err := strconv.Atoi(instance.BackupStrategy.KeepDays); err == nil {
backupStrategy["keep_days"] = days
}
backupStrategyList[0] = backupStrategy
d.Set("backup_strategy", backupStrategyList)

return nil
}
Loading

0 comments on commit f693f3b

Please sign in to comment.