Skip to content

Commit

Permalink
add gaussdb instance data resource
Browse files Browse the repository at this point in the history
  • Loading branch information
yangshuai committed Jul 3, 2020
1 parent cd94d57 commit 03c9df4
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
106 changes: 106 additions & 0 deletions huaweicloud/data_source_huaweicloud_gaussdb_mysql_flavors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package huaweicloud

import (
"fmt"

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

func dataSourceGaussdbMysqlFlavors() *schema.Resource {
return &schema.Resource{
Read: dataSourceGaussdbMysqlFlavorsRead,

Schema: map[string]*schema.Schema{
"engine": {
Type: schema.TypeString,
Optional: true,
Default: "gaussdb-mysql",
},
"version": {
Type: schema.TypeString,
Optional: true,
Default: "8.0",
},
"flavors": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"vcpus": {
Type: schema.TypeString,
Computed: true,
},
"memory": {
Type: schema.TypeString,
Computed: true,
},
"mode": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceGaussdbMysqlFlavorsRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

client, err := config.initServiceClient("gaussdb", GetRegion(d, config), "mysql/v3")
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud GaussDB client: %s", err)
}

link := fmt.Sprintf("flavors/%s?version_name=%s&availability_zone_mode=single", d.Get("engine").(string), d.Get("version").(string))
url := client.ServiceURL(link)

r, err := sendGaussdbMysqlFlavorsListRequest(client, url)
if err != nil {
return err
}

flavors := make([]interface{}, 0, len(r.([]interface{})))
for _, item := range r.([]interface{}) {
val := item.(map[string]interface{})

flavors = append(flavors, map[string]interface{}{
"vcpus": val["vcpus"],
"memory": val["ram"],
"name": val["spec_code"],
"mode": val["instance_mode"],
"version": val["version_name"],
})
}

d.SetId("flavors")
return d.Set("flavors", flavors)
}

func sendGaussdbMysqlFlavorsListRequest(client *golangsdk.ServiceClient, url string) (interface{}, error) {
r := golangsdk.Result{}
_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
MoreHeaders: map[string]string{
"Content-Type": "application/json",
"X-Language": "en-us",
}})
if r.Err != nil {
return nil, fmt.Errorf("Error fetching flavors for gaussdb mysql, error: %s", r.Err)
}

v, err := navigateValue(r.Body, []string{"flavors"}, nil)
if err != nil {
return nil, err
}
return v, nil
}
46 changes: 46 additions & 0 deletions huaweicloud/data_source_huaweicloud_gaussdb_mysql_flavors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package huaweicloud

import (
"fmt"
"testing"

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

func TestAccHuaweiCloudGaussdbMysqlFlavorsDataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccHuaweiCloudGaussdbMysqlFlavorsDataSource_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckGaussdbMysqlFlavorsDataSourceID("data.huaweicloud_gaussdb_mysql_flavors.flavor"),
),
},
},
})
}

func testAccCheckGaussdbMysqlFlavorsDataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find GaussDB mysql data source: %s ", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("GaussDB mysql data source ID not set ")
}

return nil
}
}

var testAccHuaweiCloudGaussdbMysqlFlavorsDataSource_basic = `
data "huaweicloud_gaussdb_mysql_flavors" "flavor" {
engine = "gaussdb-mysql"
version = "8.0"
}
`
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_s3_bucket_object": dataSourceS3BucketObject(),
"huaweicloud_kms_key_v1": dataSourceKmsKeyV1(),
"huaweicloud_kms_data_key_v1": dataSourceKmsDataKeyV1(),
"huaweicloud_gaussdb_mysql_flavors": dataSourceGaussdbMysqlFlavors(),
"huaweicloud_rds_flavors_v1": dataSourceRdsFlavorV1(),
"huaweicloud_rds_flavors_v3": dataSourceRdsFlavorV3(),
"huaweicloud_sfs_file_system_v2": dataSourceSFSFileSystemV2(),
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/gaussdb_mysql_flavors.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_mysql_flavors"
sidebar_current: "docs-huaweicloud-datasource-gaussdb-mysql-flavors"
description: |-
Get the flavor information on an HuaweiCloud gaussdb mysql.
---

# huaweicloud\_gaussdb\_mysql\_flavors

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

## Example Usage

```hcl
data "huaweicloud_gaussdb_mysql_flavors" "flavors" {
}
```

## Argument Reference

* `engine` - (Optional) Specifies the database engine. Only "gauss-mysql" is supported now.

* `version` - (Optional) Specifies the database version. Only "8.0" is supported now.

## Attributes Reference

In addition, the following attributes are exported:

* `flavors` -
Indicates the flavors information. Structure is documented below.

The `flavors` block contains:

* `name` - The name of the gaussdb mysql flavor.
* `vcpus` - Indicates the CPU size.
* `memory` - Indicates the memory size in GB.
* `mode` - Indicates the database mode.
* `version` - Indicates the database version.
8 changes: 8 additions & 0 deletions website/huaweicloud.erb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,14 @@
<li>
<a href="#">GaussDB</a>
<ul class="nav">
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">
<li>``
<a href="/docs/providers/huaweicloud/d/gaussdb_mysql_flavors.html">huaweicloud_gaussdb_mysql_flavors</a>
</li>
</ul>
</li>
<li>
<a href="#">Resources</a>
<ul class="nav nav-auto-expand">
Expand Down

0 comments on commit 03c9df4

Please sign in to comment.