-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11428 from Tensho/d-workspaces-image
New Data Source: aws_workspaces_image
- Loading branch information
Showing
4 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |