Skip to content

Commit

Permalink
support iec flavors data source and docs (#779)
Browse files Browse the repository at this point in the history
* support iec flavors data source and docs

* Add iec_flavors.md to data source docs

* Update data_source_huaweicloud_iec_flavors_test.go

Change list: 
1. Remove useless function parameter rName.
2. Remove flavors name in config and remove parameter check.
3. Use HW_REGION_NAME to instead old type.
  • Loading branch information
Lance52259 authored Dec 26, 2020
1 parent 0851a0c commit cc036e6
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/data-sources/iec_flavors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
subcategory: "Intelligent EdgeCloud (IEC)"
---

# huaweicloud\_iec\_flavors

Use this data source to get the available of HuaweiCloud IEC flavors.

## Example Usage

```hcl
variable "flavor_name" {
default = "c6.large.2"
}
data "huaweicloud_iec_flavors" "iec_flavor_test" {
name = var.flavor_name
}
```

## Argument Reference

The following arguments are supported:

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

* `name` - (Optional, String) Specifies the flavor name, which can be queried
with a regular expression.

* `site_ids` - (Optional, String) Specifies the list of edge service site.

* `area` - (Optional, String) Specifies the province of the iec instance located.

* `province` - (Optional, String) Specifies the province of the iec instance located.

* `city` - (Optional, String) Specifies the province of the iec instance located.

* `operator` - (Optional, String) Specifies the operator supported of the iec instance.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `flavors` - An array of one or more flavors.
The flavors object structure is documented below.

The `flavors` block supports:

* `id` - The id of the iec flavor.
* `name` - The name of the iec flavor.
* `vcpus` - The vcpus of the iec flavor.
* `memory` - The memory of the iec flavor.
119 changes: 119 additions & 0 deletions huaweicloud/data_source_huaweicloud_iec_flavors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package huaweicloud

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors"
)

func dataSourceIecFlavors() *schema.Resource {
return &schema.Resource{
Read: dataSourceIecFlavorsV1Read,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"site_ids": {
Type: schema.TypeString,
Optional: true,
},
"area": {
Type: schema.TypeString,
Optional: true,
},
"province": {
Type: schema.TypeString,
Optional: true,
},
"city": {
Type: schema.TypeString,
Optional: true,
},
"operator": {
Type: schema.TypeString,
Optional: true,
},
"flavors": {
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,
},
"vcpus": {
Type: schema.TypeInt,
Computed: true,
},
"memory": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}

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

iecClient, err := config.IECV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud IEC client: %s", err)
}

listOpts := flavors.ListOpts{
Name: d.Get("name").(string),
SiteIDS: d.Get("site_ids").(string),
Area: d.Get("area").(string),
Province: d.Get("province").(string),
City: d.Get("city").(string),
Operator: d.Get("operator").(string),
}
allFlavors, err := flavors.List(iecClient, listOpts).Extract()
if err != nil {
return fmt.Errorf("Unable to extract iec flavors: %s", err)
}
total := len(allFlavors.Flavors)
if total < 1 {
return fmt.Errorf("Your query returned no results of huaweicloud_iec_flavors. " +
"Please change your search criteria and try again.")
}

log.Printf("[INFO] Retrieved [%d] IEC flavors using given filter", total)
iecFlavors := make([]map[string]interface{}, 0, total)
for _, item := range allFlavors.Flavors {
val := map[string]interface{}{
"id": item.ID,
"name": item.Name,
"memory": item.Ram,
}
if vcpus, err := strconv.Atoi(item.Vcpus); err == nil {
val["vcpus"] = vcpus
}
iecFlavors = append(iecFlavors, val)
}
if err := d.Set("flavors", iecFlavors); err != nil {
return fmt.Errorf("Error saving IEC flavors: %s", err)
}

d.SetId(allFlavors.Flavors[0].ID)
return nil
}
51 changes: 51 additions & 0 deletions huaweicloud/data_source_huaweicloud_iec_flavors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package huaweicloud

import (
"fmt"
"regexp"
"testing"

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

func TestAccIECFlavorsDataSource_basic(t *testing.T) {
resourceName := "data.huaweicloud_iec_flavors.flavors_test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccIECFlavorsConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckIECFlavorsDataSourceID(resourceName),
resource.TestMatchResourceAttr(resourceName, "flavors.#", regexp.MustCompile("[1-9]\\d*")),
resource.TestCheckResourceAttr(resourceName, "region", HW_REGION_NAME),
),
},
},
})
}

func testAccCheckIECFlavorsDataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Root module has no resource called %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("IEC flavors data source ID not set")
}
return nil
}
}

func testAccIECFlavorsConfig() string {
return fmt.Sprintf(`
data "huaweicloud_iec_flavors" "flavors_test" {
region = "%s"
}
`, HW_REGION_NAME)
}
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_gaussdb_mysql_instance": dataSourceGaussDBMysqlInstance(),
"huaweicloud_iam_role": dataSourceIAMRoleV3(),
"huaweicloud_identity_role": DataSourceIdentityRoleV3(),
"huaweicloud_iec_flavors": dataSourceIecFlavors(),
"huaweicloud_images_image": DataSourceImagesImageV2(),
"huaweicloud_kms_key": dataSourceKmsKeyV1(),
"huaweicloud_kms_data_key": dataSourceKmsDataKeyV1(),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ github.com/huaweicloud/golangsdk/openstack/identity/v3/roles
github.com/huaweicloud/golangsdk/openstack/identity/v3/tokens
github.com/huaweicloud/golangsdk/openstack/identity/v3/users
github.com/huaweicloud/golangsdk/openstack/iec/v1/common
github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors
github.com/huaweicloud/golangsdk/openstack/iec/v1/vpcs
github.com/huaweicloud/golangsdk/openstack/imageservice/v2/imagedata
github.com/huaweicloud/golangsdk/openstack/imageservice/v2/images
Expand Down

0 comments on commit cc036e6

Please sign in to comment.