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_workspace #14135

Merged
merged 7 commits into from
Nov 5, 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
169 changes: 169 additions & 0 deletions aws/data_source_aws_workspaces_workspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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"

"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsWorkspacesWorkspace() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsWorkspacesWorkspaceRead,

Schema: map[string]*schema.Schema{
"bundle_id": {
Type: schema.TypeString,
Computed: true,
},
"directory_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
RequiredWith: []string{"user_name"},
ConflictsWith: []string{"workspace_id"},
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"computer_name": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"root_volume_encryption_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"user_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
RequiredWith: []string{"directory_id"},
ConflictsWith: []string{"workspace_id"},
},
"user_volume_encryption_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"volume_encryption_key": {
Type: schema.TypeString,
Computed: true,
},
"workspace_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ConflictsWith: []string{"directory_id", "user_name"},
},
"workspace_properties": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"compute_type_name": {
Type: schema.TypeString,
Computed: true,
},
"root_volume_size_gib": {
Type: schema.TypeInt,
Computed: true,
},
"running_mode": {
Type: schema.TypeString,
Computed: true,
},
"running_mode_auto_stop_timeout_in_minutes": {
Type: schema.TypeInt,
Computed: true,
},
"user_volume_size_gib": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsWorkspacesWorkspaceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).workspacesconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

var workspace *workspaces.Workspace

if workspaceID, ok := d.GetOk("workspace_id"); ok {
resp, err := conn.DescribeWorkspaces(&workspaces.DescribeWorkspacesInput{
WorkspaceIds: aws.StringSlice([]string{workspaceID.(string)}),
})
if err != nil {
return err
}

if len(resp.Workspaces) != 1 {
return fmt.Errorf("expected 1 result for WorkSpace %q, found %d", workspaceID, len(resp.Workspaces))
}

workspace = resp.Workspaces[0]

if workspace == nil {
return fmt.Errorf("no WorkSpace with ID %q found", workspaceID)
}
}

if directoryID, ok := d.GetOk("directory_id"); ok {
userName := d.Get("user_name").(string)
resp, err := conn.DescribeWorkspaces(&workspaces.DescribeWorkspacesInput{
DirectoryId: aws.String(directoryID.(string)),
UserName: aws.String(userName),
})
if err != nil {
return err
}

if len(resp.Workspaces) != 1 {
return fmt.Errorf("expected 1 result for %q WorkSpace in %q directory, found %d", userName, directoryID, len(resp.Workspaces))
}

workspace = resp.Workspaces[0]

if workspace == nil {
return fmt.Errorf("no %q WorkSpace in %q directory found", userName, directoryID)
}
}

d.SetId(aws.StringValue(workspace.WorkspaceId))
d.Set("bundle_id", aws.StringValue(workspace.BundleId))
d.Set("directory_id", aws.StringValue(workspace.DirectoryId))
d.Set("ip_address", aws.StringValue(workspace.IpAddress))
d.Set("computer_name", aws.StringValue(workspace.ComputerName))
d.Set("state", aws.StringValue(workspace.State))
d.Set("root_volume_encryption_enabled", aws.BoolValue(workspace.RootVolumeEncryptionEnabled))
d.Set("user_name", aws.StringValue(workspace.UserName))
d.Set("user_volume_encryption_enabled", aws.BoolValue(workspace.UserVolumeEncryptionEnabled))
d.Set("volume_encryption_key", aws.StringValue(workspace.VolumeEncryptionKey))
if err := d.Set("workspace_properties", flattenWorkspaceProperties(workspace.WorkspaceProperties)); err != nil {
return fmt.Errorf("error setting workspace properties: %s", err)
}

tags, err := keyvaluetags.WorkspacesListTags(conn, d.Id())
if err != nil {
return fmt.Errorf("error listing tags: %s", err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}
157 changes: 157 additions & 0 deletions aws/data_source_aws_workspaces_workspace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceAwsWorkspacesWorkspace_byWorkspaceID(t *testing.T) {
rName := acctest.RandString(8)
dataSourceName := "data.aws_workspaces_workspace.test"
resourceName := "aws_workspaces_workspace.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasIAMRole(t, "workspaces_DefaultRole") },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceWorkspacesWorkspaceConfig_byWorkspaceID(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "directory_id", resourceName, "directory_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "bundle_id", resourceName, "bundle_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "ip_address", resourceName, "ip_address"),
resource.TestCheckResourceAttrPair(dataSourceName, "state", resourceName, "state"),
resource.TestCheckResourceAttrPair(dataSourceName, "root_volume_encryption_enabled", resourceName, "root_volume_encryption_enabled"),
resource.TestCheckResourceAttrPair(dataSourceName, "user_name", resourceName, "user_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "volume_encryption_key", resourceName, "volume_encryption_key"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.#", resourceName, "workspace_properties.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.compute_type_name", resourceName, "workspace_properties.0.compute_type_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.root_volume_size_gib", resourceName, "workspace_properties.0.root_volume_size_gib"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.running_mode", resourceName, "workspace_properties.0.running_mode"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.running_mode_auto_stop_timeout_in_minutes", resourceName, "workspace_properties.0.running_mode_auto_stop_timeout_in_minutes"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.user_volume_size_gib", resourceName, "workspace_properties.0.user_volume_size_gib"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"),
),
},
},
})
}

func TestAccDataSourceAwsWorkspacesWorkspace_byDirectoryID_userName(t *testing.T) {
rName := acctest.RandString(8)
dataSourceName := "data.aws_workspaces_workspace.test"
resourceName := "aws_workspaces_workspace.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasIAMRole(t, "workspaces_DefaultRole") },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceWorkspacesWorkspaceConfig_byDirectoryID_userName(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "directory_id", resourceName, "directory_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "bundle_id", resourceName, "bundle_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "ip_address", resourceName, "ip_address"),
resource.TestCheckResourceAttrPair(dataSourceName, "state", resourceName, "state"),
resource.TestCheckResourceAttrPair(dataSourceName, "root_volume_encryption_enabled", resourceName, "root_volume_encryption_enabled"),
resource.TestCheckResourceAttrPair(dataSourceName, "user_name", resourceName, "user_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "volume_encryption_key", resourceName, "volume_encryption_key"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.#", resourceName, "workspace_properties.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.compute_type_name", resourceName, "workspace_properties.0.compute_type_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.root_volume_size_gib", resourceName, "workspace_properties.0.root_volume_size_gib"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.running_mode", resourceName, "workspace_properties.0.running_mode"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.running_mode_auto_stop_timeout_in_minutes", resourceName, "workspace_properties.0.running_mode_auto_stop_timeout_in_minutes"),
resource.TestCheckResourceAttrPair(dataSourceName, "workspace_properties.0.user_volume_size_gib", resourceName, "workspace_properties.0.user_volume_size_gib"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"),
),
ExpectNonEmptyPlan: true, // Hack to overcome data source with depends_on refresh
},
},
})
}

func TestAccDataSourceAwsWorkspacesWorkspace_workspaceIDAndDirectoryIDConflict(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasIAMRole(t, "workspaces_DefaultRole") },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsWorkspacesWorkspaceConfig_workspaceIDAndDirectoryIDConflict(),
ExpectError: regexp.MustCompile("\"workspace_id\": conflicts with directory_id"),
},
},
})
}

func testAccDataSourceWorkspacesWorkspaceConfig_byWorkspaceID(rName string) string {
return composeConfig(
testAccAwsWorkspacesWorkspaceConfig_Prerequisites(rName),
fmt.Sprintf(`
resource "aws_workspaces_workspace" "test" {
bundle_id = data.aws_workspaces_bundle.test.id
directory_id = aws_workspaces_directory.test.id

# NOTE: WorkSpaces API doesn't allow creating users in the directory.
# However, "AWS_WorkSpaces" user is always present in a bare directory.
user_name = "AWS_WorkSpaces"

workspace_properties {
root_volume_size_gib = 80
user_volume_size_gib = 10
}

tags = {
TerraformProviderAwsTest = true
}
}

data "aws_workspaces_workspace" "test" {
workspace_id = aws_workspaces_workspace.test.id
}
`))
}

func testAccDataSourceWorkspacesWorkspaceConfig_byDirectoryID_userName(rName string) string {
return composeConfig(
testAccAwsWorkspacesWorkspaceConfig_Prerequisites(rName),
fmt.Sprintf(`
resource "aws_workspaces_workspace" "test" {
bundle_id = data.aws_workspaces_bundle.test.id
directory_id = aws_workspaces_directory.test.id

# NOTE: WorkSpaces API doesn't allow creating users in the directory.
# However, "Administrator" user is always present in a bare directory.
user_name = "Administrator"

workspace_properties {
root_volume_size_gib = 80
user_volume_size_gib = 10
}

tags = {
TerraformProviderAwsTest = true
}
}

data "aws_workspaces_workspace" "test" {
directory_id = aws_workspaces_directory.test.id
user_name = "Administrator"

depends_on = [aws_workspaces_workspace.test]
}
`))
}

func testAccDataSourceAwsWorkspacesWorkspaceConfig_workspaceIDAndDirectoryIDConflict() string {
return fmt.Sprintf(`
data "aws_workspaces_workspace" "test" {
workspace_id = "ws-cj5xcxsz5"
directory_id = "d-9967252f57"
user_name = "Administrator"
}
`)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ func Provider() *schema.Provider {
"aws_workspaces_bundle": dataSourceAwsWorkspacesBundle(),
"aws_workspaces_directory": dataSourceAwsWorkspacesDirectory(),
"aws_workspaces_image": dataSourceAwsWorkspacesImage(),
"aws_workspaces_workspace": dataSourceAwsWorkspacesWorkspace(),

// Adding the Aliases for the ALB -> LB Rename
"aws_lb": dataSourceAwsLb(),
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_workspaces_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func expandWorkspaceProperties(properties []interface{}) *workspaces.WorkspacePr
UserVolumeSizeGib: aws.Int64(int64(p["user_volume_size_gib"].(int))),
}

if p["running_mode"] == workspaces.RunningModeAutoStop {
if p["running_mode"].(string) == workspaces.RunningModeAutoStop {
workspaceProperties.RunningModeAutoStopTimeoutInMinutes = aws.Int64(int64(p["running_mode_auto_stop_timeout_in_minutes"].(int)))
}

Expand Down
Loading