-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangshuai
committed
Jul 3, 2020
1 parent
cd94d57
commit 03c9df4
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
huaweicloud/data_source_huaweicloud_gaussdb_mysql_flavors.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,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
46
huaweicloud/data_source_huaweicloud_gaussdb_mysql_flavors_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,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" | ||
} | ||
` |
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
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,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. |
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