-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support iec flavors data source and docs (#779)
* 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
1 parent
0851a0c
commit cc036e6
Showing
8 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
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,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. |
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,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 | ||
} |
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,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) | ||
} |
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
63 changes: 63 additions & 0 deletions
63
vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/requests.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/results.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/urls.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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