diff --git a/aws/data_source_aws_workspaces_bundle.go b/aws/data_source_aws_workspaces_bundle.go index e312bd0e8ac..685b136ca2f 100644 --- a/aws/data_source_aws_workspaces_bundle.go +++ b/aws/data_source_aws_workspaces_bundle.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) -func dataSourceAwsWorkspaceBundle() *schema.Resource { +func dataSourceAwsWorkspacesBundle() *schema.Resource { return &schema.Resource{ Read: dataSourceAwsWorkspaceBundleRead, diff --git a/aws/data_source_aws_workspaces_directory.go b/aws/data_source_aws_workspaces_directory.go new file mode 100644 index 00000000000..1e4025df366 --- /dev/null +++ b/aws/data_source_aws_workspaces_directory.go @@ -0,0 +1,148 @@ +package aws + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/service/workspaces" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/workspaces/waiter" +) + +func dataSourceAwsWorkspacesDirectory() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsWorkspacesDirectoryRead, + + Schema: map[string]*schema.Schema{ + "directory_id": { + Type: schema.TypeString, + Required: true, + }, + "self_service_permissions": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "change_compute_type": { + Type: schema.TypeBool, + Computed: true, + }, + "increase_volume_size": { + Type: schema.TypeBool, + Computed: true, + }, + "rebuild_workspace": { + Type: schema.TypeBool, + Computed: true, + }, + "restart_workspace": { + Type: schema.TypeBool, + Computed: true, + }, + "switch_running_mode": { + Type: schema.TypeBool, + Computed: true, + }, + }, + }, + }, + "subnet_ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "workspace_security_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "iam_role_id": { + Type: schema.TypeString, + Computed: true, + }, + "registration_code": { + Type: schema.TypeString, + Computed: true, + }, + "directory_name": { + Type: schema.TypeString, + Computed: true, + }, + "directory_type": { + Type: schema.TypeString, + Computed: true, + }, + "customer_user_name": { + Type: schema.TypeString, + Computed: true, + }, + "alias": { + Type: schema.TypeString, + Computed: true, + }, + "ip_group_ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "dns_ip_addresses": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "tags": tagsSchema(), + }, + } +} + +func dataSourceAwsWorkspacesDirectoryRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).workspacesconn + ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig + + directoryID := d.Get("directory_id").(string) + + rawOutput, state, err := waiter.DirectoryState(conn, directoryID)() + if err != nil { + return fmt.Errorf("error getting WorkSpaces Directory (%s): %s", directoryID, err) + } + if state == workspaces.WorkspaceDirectoryStateDeregistered { + return fmt.Errorf("WorkSpaces directory %s was not found", directoryID) + } + + d.SetId(directoryID) + + directory := rawOutput.(*workspaces.WorkspaceDirectory) + d.Set("directory_id", directory.DirectoryId) + d.Set("workspace_security_group_id", directory.WorkspaceSecurityGroupId) + d.Set("iam_role_id", directory.IamRoleId) + d.Set("registration_code", directory.RegistrationCode) + d.Set("directory_name", directory.DirectoryName) + d.Set("directory_type", directory.DirectoryType) + d.Set("alias", directory.Alias) + + if err := d.Set("subnet_ids", flattenStringSet(directory.SubnetIds)); err != nil { + return fmt.Errorf("error setting subnet_ids: %s", err) + } + + if err := d.Set("self_service_permissions", flattenSelfServicePermissions(directory.SelfservicePermissions)); err != nil { + return fmt.Errorf("error setting self_service_permissions: %s", err) + } + + if err := d.Set("ip_group_ids", flattenStringSet(directory.IpGroupIds)); err != nil { + return fmt.Errorf("error setting ip_group_ids: %s", err) + } + + if err := d.Set("dns_ip_addresses", flattenStringSet(directory.DnsIpAddresses)); err != nil { + return fmt.Errorf("error setting dns_ip_addresses: %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 +} diff --git a/aws/data_source_aws_workspaces_directory_test.go b/aws/data_source_aws_workspaces_directory_test.go new file mode 100644 index 00000000000..4dfab4efbbf --- /dev/null +++ b/aws/data_source_aws_workspaces_directory_test.go @@ -0,0 +1,68 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccDataSourceAwsWorkspacesDirectory_basic(t *testing.T) { + rName := acctest.RandString(8) + + resourceName := "aws_workspaces_directory.test" + dataSourceName := "data.aws_workspaces_directory.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPreCheckHasIAMRole(t, "workspaces_DefaultRole") }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsWorkspacesDirectoryConfig(rName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair(dataSourceName, "subnet_ids.#", resourceName, "subnet_ids.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "self_service_permissions.#", resourceName, "self_service_permissions.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "self_service_permissions.0.change_compute_type", resourceName, "self_service_permissions.0.change_compute_type"), + resource.TestCheckResourceAttrPair(dataSourceName, "self_service_permissions.0.increase_volume_size", resourceName, "self_service_permissions.0.increase_volume_size"), + resource.TestCheckResourceAttrPair(dataSourceName, "self_service_permissions.0.rebuild_workspace", resourceName, "self_service_permissions.0.rebuild_workspace"), + resource.TestCheckResourceAttrPair(dataSourceName, "self_service_permissions.0.restart_workspace", resourceName, "self_service_permissions.0.restart_workspace"), + resource.TestCheckResourceAttrPair(dataSourceName, "self_service_permissions.0.switch_running_mode", resourceName, "self_service_permissions.0.switch_running_mode"), + resource.TestCheckResourceAttrPair(dataSourceName, "dns_ip_addresses.#", resourceName, "dns_ip_addresses.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "directory_type", resourceName, "directory_type"), + resource.TestCheckResourceAttrPair(dataSourceName, "directory_name", resourceName, "directory_name"), + resource.TestCheckResourceAttrPair(dataSourceName, "alias", resourceName, "alias"), + resource.TestCheckResourceAttrPair(dataSourceName, "directory_id", resourceName, "directory_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "iam_role_id", resourceName, "iam_role_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "workspace_security_group_id", resourceName, "workspace_security_group_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "registration_code", resourceName, "registration_code"), + resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsWorkspacesDirectoryConfig(rName string) string { + return testAccAwsWorkspacesDirectoryConfig_Prerequisites(rName) + fmt.Sprintf(` +resource "aws_workspaces_directory" "test" { + directory_id = "${aws_directory_service_directory.main.id}" + + self_service_permissions { + change_compute_type = false + increase_volume_size = true + rebuild_workspace = true + restart_workspace = false + switch_running_mode = true + } +} + +data "aws_workspaces_directory" "test" { + directory_id = "${aws_workspaces_directory.test.id}" +} + +data "aws_iam_role" "workspaces-default" { + name = "workspaces_DefaultRole" +} +`) +} diff --git a/aws/provider.go b/aws/provider.go index 73e47737a9e..fefa623748c 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -352,7 +352,8 @@ func Provider() terraform.ResourceProvider { "aws_wafv2_regex_pattern_set": dataSourceAwsWafv2RegexPatternSet(), "aws_wafv2_rule_group": dataSourceAwsWafv2RuleGroup(), "aws_wafv2_web_acl": dataSourceAwsWafv2WebACL(), - "aws_workspaces_bundle": dataSourceAwsWorkspaceBundle(), + "aws_workspaces_bundle": dataSourceAwsWorkspacesBundle(), + "aws_workspaces_directory": dataSourceAwsWorkspacesDirectory(), // Adding the Aliases for the ALB -> LB Rename "aws_lb": dataSourceAwsLb(), diff --git a/website/aws.erb b/website/aws.erb index c9d41959b9b..909722049b0 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -3694,6 +3694,9 @@