Skip to content

Commit

Permalink
support iec sites data source and docs (#782)
Browse files Browse the repository at this point in the history
* support iec sites data source and docs
  • Loading branch information
Lance52259 authored Dec 28, 2020
1 parent 1d95bf8 commit 2477722
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/data-sources/iec_sites.md
Original file line number Diff line number Diff line change
@@ -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.
132 changes: 132 additions & 0 deletions huaweicloud/data_source_huaweicloud_iec_sites.go
Original file line number Diff line number Diff line change
@@ -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
}
55 changes: 55 additions & 0 deletions huaweicloud/data_source_huaweicloud_iec_sites_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
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 @@ -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
Expand Down

0 comments on commit 2477722

Please sign in to comment.