diff --git a/docs/data-sources/iec_sites.md b/docs/data-sources/iec_sites.md new file mode 100644 index 0000000000..3f3f626842 --- /dev/null +++ b/docs/data-sources/iec_sites.md @@ -0,0 +1,44 @@ +# huaweicloud\_iec\_sites + +Use this data source to get the available of HuaweiCloud IEC sites. + +## Example Usage + +### Basic IEC Sites + +```hcl +data "huaweicloud_iec_sites" "iec_sites" {} +``` + +## Argument Reference + +The following arguments are supported: + +* `area` - (Optional, String) Specifies the area of the iec sites located. + +* `province` - (Optional, String) Specifies the province of the iec sites + located. + +* `city` - (Optional, String) Specifies the city of the iec sites located. + +* `operator` - (Optional, String) Specifies the operator supported of the iec + sites. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - Specifies a data source ID in UUID format. + +* `sites` - An array of one or more iec service sites. + The sites object structure is documented below. + +The `sites` block supports: + +* `id` - The id of the iec service site. +* `name` - The name of the iec service site. +* `area` - The area of the iec service site located. +* `province` - The province of the iec service site located. +* `city` - The city of the iec service site located. +* `operator` - The operator information of the iec service site. +* `status` - The current status of the iec service site. diff --git a/huaweicloud/data_source_huaweicloud_iec_sites.go b/huaweicloud/data_source_huaweicloud_iec_sites.go new file mode 100644 index 0000000000..65349aeb3e --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_iec_sites.go @@ -0,0 +1,132 @@ +package huaweicloud + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/huaweicloud/golangsdk/openstack/iec/v1/sites" +) + +func dataSourceIecSites() *schema.Resource { + return &schema.Resource{ + Read: dataSourceIecSitesV1Read, + + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "area": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "province": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "city": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "operator": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "sites": { + 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, + }, + "area": { + Type: schema.TypeString, + Computed: true, + }, + "province": { + Type: schema.TypeString, + Computed: true, + }, + "city": { + Type: schema.TypeString, + Computed: true, + }, + "operator": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceIecSitesV1Read(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 := sites.ListSiteOpts{ + Area: d.Get("area").(string), + Province: d.Get("province").(string), + City: d.Get("city").(string), + Operator: d.Get("operator").(string), + } + pages, err := sites.List(iecClient, listOpts).AllPages() + if err != nil { + return fmt.Errorf("Unable to retrieve iec sites: %s", err) + } + + allSites, err := sites.ExtractSites(pages) + if err != nil { + return fmt.Errorf("Unable to extract iec sites: %s", err) + } + total := len(allSites.Sites) + if total < 1 { + return fmt.Errorf("Your query returned no results of huaweicloud_iec_sites. " + + "Please change your search criteria and try again.") + } + + log.Printf("[INFO] Retrieved [%d] IEC sites using given filter", total) + iecSites := make([]map[string]interface{}, 0, total) + for _, item := range allSites.Sites { + val := map[string]interface{}{ + "id": item.ID, + "name": item.Name, + "area": item.Area, + "province": item.Province, + "city": item.City, + "operator": item.Operator.Name, + "status": item.Status, + } + iecSites = append(iecSites, val) + } + if err := d.Set("sites", iecSites); err != nil { + return fmt.Errorf("Error saving IEC sites: %s", err) + } + + site := allSites.Sites[0] + d.SetId(site.ID) + + return nil +} diff --git a/huaweicloud/data_source_huaweicloud_iec_sites_test.go b/huaweicloud/data_source_huaweicloud_iec_sites_test.go new file mode 100644 index 0000000000..0ba652e4ee --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_iec_sites_test.go @@ -0,0 +1,55 @@ +package huaweicloud + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccIECSitesDataSource_basic(t *testing.T) { + resourceName := "data.huaweicloud_iec_sites.sites_test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccIECSitesConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckIECSitesDataSourceID(resourceName), + resource.TestMatchResourceAttr(resourceName, "sites.#", regexp.MustCompile("[1-9]\\d*")), + resource.TestCheckResourceAttr(resourceName, "region", HW_REGION_NAME), + resource.TestCheckResourceAttr(resourceName, "area", "east"), + resource.TestCheckResourceAttr(resourceName, "city", "hangzhou"), + ), + }, + }, + }) +} + +func testAccCheckIECSitesDataSourceID(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 sites data source ID not set") + } + return nil + } +} + +func testAccIECSitesConfig() string { + return fmt.Sprintf(` +data "huaweicloud_iec_sites" "sites_test" { + region = "%s" + area = "east" + city = "hangzhou" +} + `, HW_REGION_NAME) +} diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 0f086bc729..c817f5a0c7 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -273,6 +273,7 @@ func Provider() terraform.ResourceProvider { "huaweicloud_identity_role": DataSourceIdentityRoleV3(), "huaweicloud_iec_flavors": dataSourceIecFlavors(), "huaweicloud_iec_images": dataSourceIecImages(), + "huaweicloud_iec_sites": dataSourceIecSites(), "huaweicloud_images_image": DataSourceImagesImageV2(), "huaweicloud_kms_key": dataSourceKmsKeyV1(), "huaweicloud_kms_data_key": dataSourceKmsDataKeyV1(), diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/requests.go b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/requests.go new file mode 100644 index 0000000000..104d158caf --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/requests.go @@ -0,0 +1,58 @@ +package sites + +import ( + "github.com/huaweicloud/golangsdk" + "github.com/huaweicloud/golangsdk/pagination" +) + +//ListSiteOptsBuilder list sites builder +type ListSiteOptsBuilder interface { + ToListSiteQuery() (string, error) +} + +// ListSiteOpts to list site +type ListSiteOpts struct { + //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"` +} + +// ToListSiteQuery converts ListSiteOpts structures to query string +func (opts ListSiteOpts) ToListSiteQuery() (string, error) { + q, err := golangsdk.BuildQueryString(opts) + return q.String(), err +} + +// List returns a Pager which allows you to iterate over a collection of +// sites. It accepts a ListOpts struct, which allows you to filter and sort +// the returned collection for greater efficiency. +func List(client *golangsdk.ServiceClient, opts ListSiteOptsBuilder) pagination.Pager { + url := ListURL(client) + if opts != nil { + query, err := opts.ToListSiteQuery() + if err != nil { + return pagination.Pager{Err: err} + } + url += query + } + return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { + return SitePage{pagination.LinkedPageBase{PageResult: r}} + }) +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/results.go b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/results.go new file mode 100644 index 0000000000..6bfea9fa93 --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/results.go @@ -0,0 +1,34 @@ +package sites + +import ( + "github.com/huaweicloud/golangsdk/openstack/iec/v1/common" + "github.com/huaweicloud/golangsdk/pagination" +) + +// Sites 站点集合 +type Sites struct { + // 数量 + Count int32 `json:"count"` + + Sites []common.Site `json:"sites"` +} + +// SitePage is the page returned by a pager when traversing over a collection +// of sites. +type SitePage struct { + pagination.LinkedPageBase +} + +// IsEmpty returns true if a page contains no site results. +func (r SitePage) IsEmpty() (bool, error) { + s, err := ExtractSites(r) + return s.Count == 0, err +} + +// ExtractSites interprets the results of a single page from a List() call, +// producing a slice of site entities. +func ExtractSites(r pagination.Page) (Sites, error) { + var s Sites + err := r.(SitePage).ExtractInto(&s) + return s, err +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/urls.go b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/urls.go new file mode 100644 index 0000000000..28677fb413 --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/urls.go @@ -0,0 +1,9 @@ +package sites + +import ( + "github.com/huaweicloud/golangsdk" +) + +func ListURL(sc *golangsdk.ServiceClient) string { + return sc.ServiceURL("sites") +} diff --git a/vendor/modules.txt b/vendor/modules.txt index caf30b6b0c..49aa50c07a 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -270,6 +270,7 @@ 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/images +github.com/huaweicloud/golangsdk/openstack/iec/v1/sites github.com/huaweicloud/golangsdk/openstack/iec/v1/vpcs github.com/huaweicloud/golangsdk/openstack/imageservice/v2/imagedata github.com/huaweicloud/golangsdk/openstack/imageservice/v2/images