diff --git a/docs/data-sources/iec_flavors.md b/docs/data-sources/iec_flavors.md new file mode 100644 index 0000000000..344747f9d0 --- /dev/null +++ b/docs/data-sources/iec_flavors.md @@ -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. diff --git a/huaweicloud/data_source_huaweicloud_iec_flavors.go b/huaweicloud/data_source_huaweicloud_iec_flavors.go new file mode 100644 index 0000000000..e660a6c9db --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_iec_flavors.go @@ -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 +} diff --git a/huaweicloud/data_source_huaweicloud_iec_flavors_test.go b/huaweicloud/data_source_huaweicloud_iec_flavors_test.go new file mode 100644 index 0000000000..4d27075254 --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_iec_flavors_test.go @@ -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) +} diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index ba6f8d1c81..27766f69ab 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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(), diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/requests.go b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/requests.go new file mode 100644 index 0000000000..f72348012c --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/requests.go @@ -0,0 +1,63 @@ +package flavors + +import ( + "net/http" + + "github.com/huaweicloud/golangsdk" +) + +//ListOptsBuilder list builder +type ListOptsBuilder interface { + ToListQuery() (string, error) +} + +// ListOpts to list site +type ListOpts struct { + //SiteIDS query by site ids + SiteIDS string `json:"site_ids"` + + //Name query by name + Name string `json:"name"` + + //Limit query limit + Limit string `q:"limit"` + + //Offset query begin index + Offset string `q:"offset"` + + //ID query by id + ID string `q:"id"` + + //Area query by area + Area string `q:"area"` + + //Province query by province + Province string `q:"province"` + + //City query by city + City string `q:"city"` + + //Operator query by operator + Operator string `q:"operator"` +} + +// ToListQuery converts ListOpts structures to query string +func (opts ListOpts) ToListQuery() (string, error) { + q, err := golangsdk.BuildQueryString(opts) + return q.String(), err +} + +func List(client *golangsdk.ServiceClient, listOpts ListOptsBuilder) (r GetResult) { + url := ListURL(client) + if listOpts != nil { + query, err := listOpts.ToListQuery() + if err != nil { + //return pagination.Pager{Err: err} + } + url += query + } + _, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{http.StatusOK}, + }) + return +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/results.go b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/results.go new file mode 100644 index 0000000000..6274053325 --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/results.go @@ -0,0 +1,25 @@ +package flavors + +import ( + "github.com/huaweicloud/golangsdk" + "github.com/huaweicloud/golangsdk/openstack/iec/v1/common" +) + +type commonResult struct { + golangsdk.Result +} + +type GetResult struct { + commonResult +} + +type Flavors struct { + Count int `json:"count"` + Flavors []common.Flavor `json:"flavors"` +} + +func (r GetResult) Extract() (*Flavors, error) { + var entity Flavors + err := r.ExtractIntoStructPtr(&entity, "") + return &entity, err +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/urls.go b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/urls.go new file mode 100644 index 0000000000..e7fdeeb24e --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/flavors/urls.go @@ -0,0 +1,9 @@ +package flavors + +import ( + "github.com/huaweicloud/golangsdk" +) + +func ListURL(c *golangsdk.ServiceClient) string { + return c.ServiceURL("cloudservers", "flavors") +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 02db8f3cbf..04438c6ca2 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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