Skip to content

Commit

Permalink
Merge pull request #11428 from Tensho/d-workspaces-image
Browse files Browse the repository at this point in the history
New Data Source: aws_workspaces_image
  • Loading branch information
gdavison committed Sep 24, 2020
2 parents 8183002 + 507726b commit c7d40f2
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
69 changes: 69 additions & 0 deletions aws/data_source_aws_workspaces_image.go
Original file line number Diff line number Diff line change
@@ -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
}
113 changes: 113 additions & 0 deletions aws/data_source_aws_workspaces_image_test.go
Original file line number Diff line number Diff line change
@@ -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/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/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
}
}
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ 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(),
"aws_alb": dataSourceAwsLb(),
Expand Down
35 changes: 35 additions & 0 deletions website/docs/d/workspaces_image.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit c7d40f2

Please sign in to comment.