-
Notifications
You must be signed in to change notification settings - Fork 162
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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 | ||
} |
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIECFlavorsDataSourceID(n, rName string) resource.TestCheckFunc { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the param |
||
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) | ||
} |
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.
There was a problem hiding this comment.
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