Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support iec images data source and docs #780

Merged
merged 2 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/data-sources/iec_images.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subcategory: "Intelligent EdgeCloud (IEC)"
---

# huaweicloud\_iec\_images

Use this data source to get the available of HuaweiCloud IEC images.

## Example Usage

```hcl
data "huaweicloud_iec_images" "iec_image" {
os_type = "Linux"
}
```

## Argument Reference

The following arguments are supported:
* `region` - (Optional, String) The region in which to obtain the images.
If omitted, the provider-level region will be used.

* `name` - (Optional, String) Specifies the image Name, which can be queried
with a regular expression.

* `os_type` - (Optional, String) Specifies the os type of the iec image.
"Linux", "Windows" and "Other" are supported.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `images` - An array of one or more image.
The images object structure is documented below.

The `images` block supports:

* `id` - The id of the iec images.
* `name` - The name of the iec images.
* `status` - The status of the iec images.
* `os_type` - The os_type of the iec images.
107 changes: 107 additions & 0 deletions huaweicloud/data_source_huaweicloud_iec_images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package huaweicloud

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/huaweicloud/golangsdk/openstack/iec/v1/images"
)

func dataSourceIecImages() *schema.Resource {
return &schema.Resource{
Read: dataSourceIecImagesV1Read,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"os_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"Linux", "Windows", "Other",
}, false),
},
"images": {
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,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"os_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceIecImagesV1Read(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 := images.ListOpts{
Name: d.Get("name").(string),
OsType: d.Get("os_type").(string),
Status: "active",
SortKey: "name",
}
pages, err := images.List(iecClient, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to retrieve iec images: %s", err)
}

allImages, err := images.ExtractImages(pages)
if err != nil {
return fmt.Errorf("Unable to extract iec images: %s", err)
}
total := len(allImages.Images)
if total < 1 {
return fmt.Errorf("Your query returned no results of huaweicloud_iec_images. " +
"Please change your search criteria and try again.")
}

log.Printf("[INFO] Retrieved [%d] IEC images using given filter", total)
edgeImages := make([]map[string]interface{}, 0, total)
for _, item := range allImages.Images {
val := map[string]interface{}{
"id": item.ID,
"name": item.Name,
"status": item.Status,
"os_type": item.OsType,
}
edgeImages = append(edgeImages, val)
}
if err := d.Set("images", edgeImages); err != nil {
return fmt.Errorf("Error saving IEC iamges: %s", err)
}

d.SetId(allImages.Images[0].ID)
return nil
}
51 changes: 51 additions & 0 deletions huaweicloud/data_source_huaweicloud_iec_images_test.go
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 TestAccIECImagesDataSource_basic(t *testing.T) {
resourceName := "data.huaweicloud_iec_images.images_test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccIECImagesConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckIECImagesDataSourceID(resourceName),
resource.TestMatchResourceAttr(resourceName, "images.#", regexp.MustCompile("[1-9]\\d*")),
resource.TestCheckResourceAttr(resourceName, "region", HW_REGION_NAME),
),
},
},
})
}

func testAccCheckIECImagesDataSourceID(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 images data source ID not set")
}
return nil
}
}

func testAccIECImagesConfig() string {
return fmt.Sprintf(`
data "huaweicloud_iec_images" "images_test" {
region = "%s"
}
`, HW_REGION_NAME)
}
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_iam_role": dataSourceIAMRoleV3(),
"huaweicloud_identity_role": DataSourceIdentityRoleV3(),
"huaweicloud_iec_flavors": dataSourceIecFlavors(),
"huaweicloud_iec_images": dataSourceIecImages(),
"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 @@ -269,6 +269,7 @@ 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/images
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