From b4ba8f299864a57a6269c8fc16723874067e2576 Mon Sep 17 00:00:00 2001 From: Andrew Babichev Date: Thu, 28 May 2020 11:23:33 +0300 Subject: [PATCH 1/2] New data source: aws_workspaces_image --- aws/data_source_aws_workspaces_image.go | 69 +++++++++++ aws/data_source_aws_workspaces_image_test.go | 113 ++++++++++++++++++ aws/provider.go | 1 + website/docs/d/workspaces_image.html.markdown | 35 ++++++ 4 files changed, 218 insertions(+) create mode 100644 aws/data_source_aws_workspaces_image.go create mode 100644 aws/data_source_aws_workspaces_image_test.go create mode 100644 website/docs/d/workspaces_image.html.markdown diff --git a/aws/data_source_aws_workspaces_image.go b/aws/data_source_aws_workspaces_image.go new file mode 100644 index 00000000000..3d2a4b8228b --- /dev/null +++ b/aws/data_source_aws_workspaces_image.go @@ -0,0 +1,69 @@ +package aws + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/workspaces" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceAwsWorkspacesImage() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsWorkspacesImageRead, + + Schema: map[string]*schema.Schema{ + "image_id": { + Type: schema.TypeString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "operating_system_type": { + Type: schema.TypeString, + Computed: true, + }, + "required_tenancy": { + Type: schema.TypeString, + Computed: true, + }, + "state": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsWorkspacesImageRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).workspacesconn + + imageID := d.Get("image_id").(string) + input := &workspaces.DescribeWorkspaceImagesInput{ + ImageIds: []*string{aws.String(imageID)}, + } + + resp, err := conn.DescribeWorkspaceImages(input) + if err != nil { + return fmt.Errorf("Failed describe workspaces images: %w", err) + } + if len(resp.Images) == 0 { + return fmt.Errorf("Workspace image %s was not found", imageID) + } + + image := resp.Images[0] + d.SetId(imageID) + d.Set("name", image.Name) + d.Set("description", image.Description) + d.Set("operating_system_type", image.OperatingSystem.Type) + d.Set("required_tenancy", image.RequiredTenancy) + d.Set("state", image.State) + + return nil +} diff --git a/aws/data_source_aws_workspaces_image_test.go b/aws/data_source_aws_workspaces_image_test.go new file mode 100644 index 00000000000..f847a3c327c --- /dev/null +++ b/aws/data_source_aws_workspaces_image_test.go @@ -0,0 +1,113 @@ +package aws + +import ( + "fmt" + "os" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/workspaces" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccDataSourceAwsWorkspacesImage_basic(t *testing.T) { + var image workspaces.WorkspaceImage + imageID := os.Getenv("AWS_WORKSPACES_IMAGE_ID") + dataSourceName := "data.aws_workspaces_image.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccWorkspacesImagePreCheck(t) + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsWorkspacesImageConfig(imageID), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckWorkspacesImageExists(dataSourceName, &image), + testAccCheckWorkspacesImageAttributes(dataSourceName, &image), + ), + }, + }, + }) +} + +func testAccWorkspacesImagePreCheck(t *testing.T) { + if os.Getenv("AWS_WORKSPACES_IMAGE_ID") == "" { + t.Skip("AWS_WORKSPACES_IMAGE_ID env var must be set for AWS WorkSpaces image acceptance tests. This is required until AWS provides ubiquitous (Windows, Linux) import image API.") + } +} + +func testAccDataSourceAwsWorkspacesImageConfig(imageID string) string { + return fmt.Sprintf(` +# TODO: Create aws_workspaces_image resource when API will be provided + +data "aws_workspaces_image" "test" { + image_id = %q +} +`, imageID) +} + +func testAccCheckWorkspacesImageExists(n string, image *workspaces.WorkspaceImage) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + conn := testAccProvider.Meta().(*AWSClient).workspacesconn + resp, err := conn.DescribeWorkspaceImages(&workspaces.DescribeWorkspaceImagesInput{ + ImageIds: []*string{aws.String(rs.Primary.ID)}, + }) + if err != nil { + return fmt.Errorf("Failed describe workspaces images: %w", err) + } + if len(resp.Images) == 0 { + return fmt.Errorf("Workspace image %s was not found", rs.Primary.ID) + } + if *resp.Images[0].ImageId != rs.Primary.ID { + return fmt.Errorf("Workspace image ID mismatch - existing: %q, state: %q", *resp.Images[0].ImageId, rs.Primary.ID) + } + + *image = *resp.Images[0] + + return nil + } +} + +func testAccCheckWorkspacesImageAttributes(n string, image *workspaces.WorkspaceImage) resource.TestCheckFunc { + return func(s *terraform.State) error { + _, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if err := resource.TestCheckResourceAttr(n, "id", *image.ImageId)(s); err != nil { + return err + } + + if err := resource.TestCheckResourceAttr(n, "name", *image.Name)(s); err != nil { + return err + } + + if err := resource.TestCheckResourceAttr(n, "description", *image.Description)(s); err != nil { + return err + } + + if err := resource.TestCheckResourceAttr(n, "operating_system_type", *image.OperatingSystem.Type)(s); err != nil { + return err + } + + if err := resource.TestCheckResourceAttr(n, "required_tenancy", *image.RequiredTenancy)(s); err != nil { + return err + } + + if err := resource.TestCheckResourceAttr(n, "state", *image.State)(s); err != nil { + return err + } + + return nil + } +} diff --git a/aws/provider.go b/aws/provider.go index 34e5c292584..59f4092bc32 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -353,6 +353,7 @@ func Provider() *schema.Provider { "aws_wafv2_web_acl": dataSourceAwsWafv2WebACL(), "aws_workspaces_bundle": dataSourceAwsWorkspacesBundle(), "aws_workspaces_directory": dataSourceAwsWorkspacesDirectory(), + "aws_workspaces_image": dataSourceAwsWorkspacesImage(), // Adding the Aliases for the ALB -> LB Rename "aws_lb": dataSourceAwsLb(), diff --git a/website/docs/d/workspaces_image.html.markdown b/website/docs/d/workspaces_image.html.markdown new file mode 100644 index 00000000000..24bade7c92f --- /dev/null +++ b/website/docs/d/workspaces_image.html.markdown @@ -0,0 +1,35 @@ +--- +subcategory: "WorkSpaces" +layout: "aws" +page_title: "AWS: aws_workspaces_image" +description: |- + Get information about Workspaces image. +--- + +# Data Source: aws_workspaces_image + +Use this data source to get information about a Workspaces image. + +## Example Usage + +```hcl +data "aws_workspaces_image" "example" { + image_id = "wsi-ten5h0y19" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `image_id` – (Required) The ID of the image. + +## Attributes Reference + +The following attributes are exported: + +* `name` – The name of the image. +* `description` – The description of the image. +* `os` – The operating system that the image is running. +* `required_tenancy` – Specifies whether the image is running on dedicated hardware. When Bring Your Own License (BYOL) is enabled, this value is set to DEDICATED. For more information, see [Bring Your Own Windows Desktop Images](https://docs.aws.amazon.com/workspaces/latest/adminguide/byol-windows-images.html). +* `state` – The status of the image. From e4e2c520aa1bd81641648534e20d0eed1390e7e0 Mon Sep 17 00:00:00 2001 From: Andrew Babichev Date: Tue, 25 Aug 2020 13:24:37 +0300 Subject: [PATCH 2/2] Update to Terraform 0.12+ syntax --- aws/data_source_aws_workspaces_image_test.go | 6 +++--- website/docs/d/workspaces_image.html.markdown | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/data_source_aws_workspaces_image_test.go b/aws/data_source_aws_workspaces_image_test.go index f847a3c327c..a3678870d94 100644 --- a/aws/data_source_aws_workspaces_image_test.go +++ b/aws/data_source_aws_workspaces_image_test.go @@ -7,8 +7,8 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/workspaces" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccDataSourceAwsWorkspacesImage_basic(t *testing.T) { @@ -44,7 +44,7 @@ func testAccDataSourceAwsWorkspacesImageConfig(imageID string) string { return fmt.Sprintf(` # TODO: Create aws_workspaces_image resource when API will be provided -data "aws_workspaces_image" "test" { +data aws_workspaces_image test { image_id = %q } `, imageID) diff --git a/website/docs/d/workspaces_image.html.markdown b/website/docs/d/workspaces_image.html.markdown index 24bade7c92f..107a80eb463 100644 --- a/website/docs/d/workspaces_image.html.markdown +++ b/website/docs/d/workspaces_image.html.markdown @@ -13,7 +13,7 @@ Use this data source to get information about a Workspaces image. ## Example Usage ```hcl -data "aws_workspaces_image" "example" { +data aws_workspaces_image example { image_id = "wsi-ten5h0y19" } ```