|
| 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 | +} |
0 commit comments