Skip to content

Commit b4ba8f2

Browse files
committed
New data source: aws_workspaces_image
1 parent 9b92541 commit b4ba8f2

4 files changed

Lines changed: 218 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aws/aws-sdk-go/aws"
7+
"github.com/aws/aws-sdk-go/service/workspaces"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceAwsWorkspacesImage() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceAwsWorkspacesImageRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"image_id": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
},
20+
"name": {
21+
Type: schema.TypeString,
22+
Computed: true,
23+
},
24+
"description": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
},
28+
"operating_system_type": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
},
32+
"required_tenancy": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"state": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
},
40+
},
41+
}
42+
}
43+
44+
func dataSourceAwsWorkspacesImageRead(d *schema.ResourceData, meta interface{}) error {
45+
conn := meta.(*AWSClient).workspacesconn
46+
47+
imageID := d.Get("image_id").(string)
48+
input := &workspaces.DescribeWorkspaceImagesInput{
49+
ImageIds: []*string{aws.String(imageID)},
50+
}
51+
52+
resp, err := conn.DescribeWorkspaceImages(input)
53+
if err != nil {
54+
return fmt.Errorf("Failed describe workspaces images: %w", err)
55+
}
56+
if len(resp.Images) == 0 {
57+
return fmt.Errorf("Workspace image %s was not found", imageID)
58+
}
59+
60+
image := resp.Images[0]
61+
d.SetId(imageID)
62+
d.Set("name", image.Name)
63+
d.Set("description", image.Description)
64+
d.Set("operating_system_type", image.OperatingSystem.Type)
65+
d.Set("required_tenancy", image.RequiredTenancy)
66+
d.Set("state", image.State)
67+
68+
return nil
69+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/service/workspaces"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
12+
)
13+
14+
func TestAccDataSourceAwsWorkspacesImage_basic(t *testing.T) {
15+
var image workspaces.WorkspaceImage
16+
imageID := os.Getenv("AWS_WORKSPACES_IMAGE_ID")
17+
dataSourceName := "data.aws_workspaces_image.test"
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() {
21+
testAccPreCheck(t)
22+
testAccWorkspacesImagePreCheck(t)
23+
},
24+
Providers: testAccProviders,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccDataSourceAwsWorkspacesImageConfig(imageID),
28+
Check: resource.ComposeAggregateTestCheckFunc(
29+
testAccCheckWorkspacesImageExists(dataSourceName, &image),
30+
testAccCheckWorkspacesImageAttributes(dataSourceName, &image),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccWorkspacesImagePreCheck(t *testing.T) {
38+
if os.Getenv("AWS_WORKSPACES_IMAGE_ID") == "" {
39+
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.")
40+
}
41+
}
42+
43+
func testAccDataSourceAwsWorkspacesImageConfig(imageID string) string {
44+
return fmt.Sprintf(`
45+
# TODO: Create aws_workspaces_image resource when API will be provided
46+
47+
data "aws_workspaces_image" "test" {
48+
image_id = %q
49+
}
50+
`, imageID)
51+
}
52+
53+
func testAccCheckWorkspacesImageExists(n string, image *workspaces.WorkspaceImage) resource.TestCheckFunc {
54+
return func(s *terraform.State) error {
55+
rs, ok := s.RootModule().Resources[n]
56+
if !ok {
57+
return fmt.Errorf("Not found: %s", n)
58+
}
59+
60+
conn := testAccProvider.Meta().(*AWSClient).workspacesconn
61+
resp, err := conn.DescribeWorkspaceImages(&workspaces.DescribeWorkspaceImagesInput{
62+
ImageIds: []*string{aws.String(rs.Primary.ID)},
63+
})
64+
if err != nil {
65+
return fmt.Errorf("Failed describe workspaces images: %w", err)
66+
}
67+
if len(resp.Images) == 0 {
68+
return fmt.Errorf("Workspace image %s was not found", rs.Primary.ID)
69+
}
70+
if *resp.Images[0].ImageId != rs.Primary.ID {
71+
return fmt.Errorf("Workspace image ID mismatch - existing: %q, state: %q", *resp.Images[0].ImageId, rs.Primary.ID)
72+
}
73+
74+
*image = *resp.Images[0]
75+
76+
return nil
77+
}
78+
}
79+
80+
func testAccCheckWorkspacesImageAttributes(n string, image *workspaces.WorkspaceImage) resource.TestCheckFunc {
81+
return func(s *terraform.State) error {
82+
_, ok := s.RootModule().Resources[n]
83+
if !ok {
84+
return fmt.Errorf("Not found: %s", n)
85+
}
86+
87+
if err := resource.TestCheckResourceAttr(n, "id", *image.ImageId)(s); err != nil {
88+
return err
89+
}
90+
91+
if err := resource.TestCheckResourceAttr(n, "name", *image.Name)(s); err != nil {
92+
return err
93+
}
94+
95+
if err := resource.TestCheckResourceAttr(n, "description", *image.Description)(s); err != nil {
96+
return err
97+
}
98+
99+
if err := resource.TestCheckResourceAttr(n, "operating_system_type", *image.OperatingSystem.Type)(s); err != nil {
100+
return err
101+
}
102+
103+
if err := resource.TestCheckResourceAttr(n, "required_tenancy", *image.RequiredTenancy)(s); err != nil {
104+
return err
105+
}
106+
107+
if err := resource.TestCheckResourceAttr(n, "state", *image.State)(s); err != nil {
108+
return err
109+
}
110+
111+
return nil
112+
}
113+
}

aws/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ func Provider() *schema.Provider {
353353
"aws_wafv2_web_acl": dataSourceAwsWafv2WebACL(),
354354
"aws_workspaces_bundle": dataSourceAwsWorkspacesBundle(),
355355
"aws_workspaces_directory": dataSourceAwsWorkspacesDirectory(),
356+
"aws_workspaces_image": dataSourceAwsWorkspacesImage(),
356357

357358
// Adding the Aliases for the ALB -> LB Rename
358359
"aws_lb": dataSourceAwsLb(),
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
subcategory: "WorkSpaces"
3+
layout: "aws"
4+
page_title: "AWS: aws_workspaces_image"
5+
description: |-
6+
Get information about Workspaces image.
7+
---
8+
9+
# Data Source: aws_workspaces_image
10+
11+
Use this data source to get information about a Workspaces image.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "aws_workspaces_image" "example" {
17+
image_id = "wsi-ten5h0y19"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
* `image_id` – (Required) The ID of the image.
26+
27+
## Attributes Reference
28+
29+
The following attributes are exported:
30+
31+
* `name` – The name of the image.
32+
* `description` – The description of the image.
33+
* `os` – The operating system that the image is running.
34+
* `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).
35+
* `state` – The status of the image.

0 commit comments

Comments
 (0)