diff --git a/docs/data-sources/iec_images.md b/docs/data-sources/iec_images.md new file mode 100644 index 0000000000..401ec820b8 --- /dev/null +++ b/docs/data-sources/iec_images.md @@ -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. diff --git a/huaweicloud/data_source_huaweicloud_iec_images.go b/huaweicloud/data_source_huaweicloud_iec_images.go new file mode 100644 index 0000000000..06f66c7c59 --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_iec_images.go @@ -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 +} diff --git a/huaweicloud/data_source_huaweicloud_iec_images_test.go b/huaweicloud/data_source_huaweicloud_iec_images_test.go new file mode 100644 index 0000000000..9bae532529 --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_iec_images_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 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) +} diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 27766f69ab..650c5a054e 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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(), diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/images/requests.go b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/images/requests.go new file mode 100644 index 0000000000..7de948f57e --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/images/requests.go @@ -0,0 +1,101 @@ +package images + +import ( + "github.com/huaweicloud/golangsdk" + "github.com/huaweicloud/golangsdk/pagination" +) + +// ListOptsBuilder allows extensions to add additional parameters to the +// List request. +type ListOptsBuilder interface { + ToImageListQuery() (string, error) +} + +// ListOpts allows the filtering and sorting of paginated collections through +// the API. Filtering is achieved by passing in struct field values that map to +// the server attributes you want to see returned. Marker and Limit are used +// for pagination. +// +// http://developer.openstack.org/api-ref-image-v2.html +type ListOpts struct { + //Image type + ImageType string `q:"__imagetype"` + + Protected string `q:"protected"` + + // Visibility filters on the visibility of the image. + Visibility string `q:"visibility"` + + // Status filters on the status of the image. + Status string `q:"status"` + + // Name filters on the name of the image. + Name string `q:"name"` + + //Indicates the image OS type. The value can be Linux, Windows, or Other. + OsType string `q:"__os_type"` + + // + VirtualEnvType string `q:"virtual_env_type"` + + // Specifies whether the image is available. + IsRegistered string `q:"__isregistered"` + + // Integer value for the limit of values to return. + Limit int `q:"limit"` + + Offset string `q:"offset"` + + // SortKey will sort the results based on a specified image property. + SortKey string `q:"sort_key"` + + // SortDir will sort the list results either ascending or decending. + SortDir string `q:"sort_dir"` + + // Owner filters on the project ID of the image. + Owner string `q:"owner"` + + //Image ID + ID string `q:"id"` + + //Specifies whether the image supports KVM. + //If yes, the value is true. Otherwise, this attribute is not required. + SupportKvm string `q:"__support_kvm"` + + //If the image supports the GPU type on the KVM virtualization platform, + //the value is V100_vGPU or RTX5000. Otherwise, this attribute is unavailable. + SupportKvmGpuType string `q:"__support_kvm_gpu_type"` + + //Specifies whether the image supports AI acceleration. + SupportKvmAscend310 string `q:"__support_kvm_ascend_310"` + + //Specifies whether the image supports Computing enhancement + SupportKvmHi1822Hiovs string `q:"__support_kvm_hi1822_hiovs"` + + //Specifies whether the image supports ARM architecture + SupportArm string `q:"__support_arm"` + + //HwFirmwareType firmware type + HwFirmwareType string `q:"hw_firmware_type"` +} + +// ToImageListQuery formats a ListOpts into a query string. +func (opts ListOpts) ToImageListQuery() (string, error) { + q, err := golangsdk.BuildQueryString(opts) + return q.String(), err +} + +// List implements image list request. +func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { + url := ListURL(c) + if opts != nil { + query, err := opts.ToImageListQuery() + if err != nil { + return pagination.Pager{Err: err} + } + url += query + } + return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { + return ImagePage{pagination.LinkedPageBase{PageResult: r}} + }) +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/images/results.go b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/images/results.go new file mode 100644 index 0000000000..081cb03f34 --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/images/results.go @@ -0,0 +1,30 @@ +package images + +import ( + "github.com/huaweicloud/golangsdk/openstack/iec/v1/common" + "github.com/huaweicloud/golangsdk/pagination" +) + +// EdgeImages 边缘镜像列表信息 +type EdgeImages struct { + Total int `json:"total"` + Images []common.EdgeImageInfo `json:"images"` +} + +// ImagePage represents the results of a List request. +type ImagePage struct { + pagination.LinkedPageBase +} + +// IsEmpty returns true if a page contains no image results. +func (r ImagePage) IsEmpty() (bool, error) { + s, err := ExtractImages(r) + return s.Total == 0, err +} + +// ExtractImages 输出边缘镜像列表 +func ExtractImages(r pagination.Page) (EdgeImages, error) { + var s EdgeImages + err := (r.(ImagePage)).ExtractInto(&s) + return s, err +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/images/urls.go b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/images/urls.go new file mode 100644 index 0000000000..c49bb8b4b1 --- /dev/null +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/iec/v1/images/urls.go @@ -0,0 +1,8 @@ +package images + +import "github.com/huaweicloud/golangsdk" + +// ListURL list iec image url +func ListURL(sc *golangsdk.ServiceClient) string { + return sc.ServiceURL("images") +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 04438c6ca2..37802ad971 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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