Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support iec flavors data source and docs #779

Merged
merged 3 commits into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
53 changes: 53 additions & 0 deletions huaweicloud/data_source_huaweicloud_iec_flavors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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) {
rName := "c6.large.2"
resourceName := "data.huaweicloud_iec_flavors.flavors_test"

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the "HW_REGION_NAME" env instead of a specified region

),
},
},
})
}

func testAccCheckIECFlavorsDataSourceID(n, rName string) resource.TestCheckFunc {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the param rName is not used

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(val string) string {
return fmt.Sprintf(`
data "huaweicloud_iec_flavors" "flavors_test" {
region = "cn-north-4"
name = "%s"
}
`, val)
}
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