-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support iec sites data source and docs (#782)
* support iec sites data source and docs
- Loading branch information
1 parent
1d95bf8
commit 2477722
Showing
8 changed files
with
334 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,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. |
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,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 | ||
} |
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,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) | ||
} |
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
58 changes: 58 additions & 0 deletions
58
vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/requests.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/sites/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/sites/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