-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will probably want to check There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?