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

New Data Source: aws_workspaces_image #11428

Merged
merged 3 commits into from
Sep 24, 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
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,
},
Comment on lines +36 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the state attribute, since it is more of a run-time property, not a configuration property.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is a data source, not a resource. Don't we need to get info about any possible attribute there?

},
}
}

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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will probably want to check len(resp.Images) > 0 here.
See #11837.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will add

if len(resp.Images) == 0 {
Tensho marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -359,6 +359,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.