Skip to content

Commit

Permalink
Add gaussdb_cassandra_instance data source (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuzhenguo authored Nov 25, 2020
1 parent 4ae4afc commit 38fd9fb
Show file tree
Hide file tree
Showing 9 changed files with 482 additions and 15 deletions.
81 changes: 81 additions & 0 deletions docs/data-sources/gaussdb_cassandra_instance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
subcategory: "GaussDB"
---

# huaweicloud\_gaussdb\_cassandra\_instance

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

## Example Usage

```hcl
data "huaweicloud_gaussdb_cassandra_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.

* `status` - Indicates the DB instance status.

* `mode` - Indicates the instance mode.

* `flavor` - Indicates the instance specifications.

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

* `enterprise_project_id` - Indicates the enterprise project id.

* `db_user_name` - Indicates the default username.

* `availability_zone` - Indicates the instance availability zone.

* `port` - Indicates the database port.

* `node_num` - Indicates the count of the nodes.

* `volume_size` - Indicates the size of the volume.

* `private_ips` - Indicates the list of private IP address of the nodes.

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

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

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

* `tags` - Indicates the key/value tags of the instance.


The `datastore` block supports:

* `engine` - Indicates the database engine.
* `storage_engine` - Indicates the database storage 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.
- `private_ip` - Indicates the private IP address of a node.
- `status` - Indicates the node status.
- `support_reduce` - Indicates whether the node support reduce.
- `availability_zone` - Indicates the availability zone where the node resides.
290 changes: 290 additions & 0 deletions huaweicloud/data_source_huaweicloud_gaussdb_cassandra_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
package huaweicloud

import (
"fmt"
"log"
"sort"
"strconv"
"strings"

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

"github.com/huaweicloud/golangsdk/openstack/common/tags"
"github.com/huaweicloud/golangsdk/openstack/geminidb/v3/instances"
)

func dataSourceGeminiDBInstance() *schema.Resource {
return &schema.Resource{
Read: dataSourceGeminiDBInstanceRead,

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,
},
"enterprise_project_id": {
Type: schema.TypeString,
Computed: true,
},
"db_user_name": {
Type: schema.TypeString,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeInt,
Computed: true,
},
"datastore": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"engine": {
Type: schema.TypeString,
Computed: true,
},
"storage_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,
},
},
},
},
"node_num": {
Type: schema.TypeInt,
Computed: true,
},
"volume_size": {
Type: schema.TypeInt,
Computed: true,
},
"private_ips": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"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,
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"support_reduce": {
Type: schema.TypeBool,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"tags": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

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

listOpts := instances.ListGeminiDBInstanceOpts{
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.ExtractGeminiDBInstances(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")
}

instance := allInstances.Instances[0]

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

d.Set("name", instance.Name)
d.Set("region", instance.Region)
d.Set("status", instance.Status)
d.Set("vpc_id", instance.VpcId)
d.Set("subnet_id", instance.SubnetId)
d.Set("security_group_id", instance.SecurityGroupId)
d.Set("enterprise_project_id", instance.EnterpriseProjectId)
d.Set("mode", instance.Mode)
d.Set("db_user_name", instance.DbUserName)

if dbPort, err := strconv.Atoi(instance.Port); err == nil {
d.Set("port", dbPort)
}

dbList := make([]map[string]interface{}, 0, 1)
db := map[string]interface{}{
"engine": instance.DataStore.Type,
"version": instance.DataStore.Version,
"storage_engine": instance.Engine,
}
dbList = append(dbList, db)
d.Set("datastore", dbList)

specCode := ""
wrongFlavor := "Inconsistent Flavor"
ipsList := []string{}
azList := []string{}
nodesList := make([]map[string]interface{}, 0, 1)
for _, group := range instance.Groups {
for _, Node := range group.Nodes {
node := map[string]interface{}{
"id": Node.Id,
"name": Node.Name,
"status": Node.Status,
"private_ip": Node.PrivateIp,
"support_reduce": Node.SupportReduce,
"availability_zone": Node.AvailabilityZone,
}
if specCode == "" {
specCode = Node.SpecCode
} else if specCode != Node.SpecCode && specCode != wrongFlavor {
specCode = wrongFlavor
}
nodesList = append(nodesList, node)
azList = append(azList, Node.AvailabilityZone)
// Only return Node private ips which doesn't support reduce
if !Node.SupportReduce {
ipsList = append(ipsList, Node.PrivateIp)
}
}
if volSize, err := strconv.Atoi(group.Volume.Size); err == nil {
d.Set("volume_size", volSize)
}
if specCode != "" {
log.Printf("[DEBUG] Node SpecCode: %s", specCode)
d.Set("flavor", specCode)
}
}
d.Set("nodes", nodesList)
d.Set("private_ips", ipsList)

//remove duplicate az
azList = removeDuplicateElem(azList)
sort.Strings(azList)
d.Set("availability_zone", strings.Join(azList, ","))
d.Set("node_num", len(nodesList))

backupStrategyList := make([]map[string]interface{}, 0, 1)
backupStrategy := map[string]interface{}{
"start_time": instance.BackupStrategy.StartTime,
"keep_days": instance.BackupStrategy.KeepDays,
}
backupStrategyList = append(backupStrategyList, backupStrategy)
d.Set("backup_strategy", backupStrategyList)

//save geminidb tags
resourceTags, err := tags.Get(client, "instances", d.Id()).Extract()
if err != nil {
return fmt.Errorf("Error fetching HuaweiCloud geminidb tags: %s", err)
}

tagmap := tagsToMap(resourceTags.Tags)
if err := d.Set("tags", tagmap); err != nil {
return fmt.Errorf("Error saving tags for HuaweiCloud geminidb (%s): %s", d.Id(), err)
}

return nil
}
Loading

0 comments on commit 38fd9fb

Please sign in to comment.