diff --git a/.changelog/29208.txt b/.changelog/29208.txt new file mode 100644 index 000000000000..03f11e9f6ac3 --- /dev/null +++ b/.changelog/29208.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_redshiftserverless_workgroup +``` diff --git a/internal/service/redshiftserverless/service_package_gen.go b/internal/service/redshiftserverless/service_package_gen.go index aa1f50e8702c..503c34a1a601 100644 --- a/internal/service/redshiftserverless/service_package_gen.go +++ b/internal/service/redshiftserverless/service_package_gen.go @@ -25,6 +25,10 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Factory: DataSourceCredentials, TypeName: "aws_redshiftserverless_credentials", }, + { + Factory: DataSourceWorkgroup, + TypeName: "aws_redshiftserverless_workgroup", + }, } } diff --git a/internal/service/redshiftserverless/workgroup_data_source.go b/internal/service/redshiftserverless/workgroup_data_source.go new file mode 100644 index 000000000000..31fe743bc2e1 --- /dev/null +++ b/internal/service/redshiftserverless/workgroup_data_source.go @@ -0,0 +1,141 @@ +package redshiftserverless + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" +) + +// @SDKDataSource("aws_redshiftserverless_workgroup") +func DataSourceWorkgroup() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataSourceWorkgroupRead, + + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "endpoint": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "address": { + Type: schema.TypeString, + Computed: true, + }, + "port": { + Type: schema.TypeInt, + Computed: true, + }, + "vpc_endpoint": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "network_interface": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "availability_zone": { + Type: schema.TypeString, + Computed: true, + }, + "network_interface_id": { + Type: schema.TypeString, + Computed: true, + }, + "private_ip_address": { + Type: schema.TypeString, + Computed: true, + }, + "subnet_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "vpc_endpoint_id": { + Type: schema.TypeString, + Computed: true, + }, + "vpc_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + }, + }, + "enhanced_vpc_routing": { + Type: schema.TypeBool, + Computed: true, + }, + "namespace_name": { + Type: schema.TypeString, + Computed: true, + }, + "publicly_accessible": { + Type: schema.TypeBool, + Computed: true, + }, + "security_group_ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "subnet_ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "workgroup_id": { + Type: schema.TypeString, + Computed: true, + }, + "workgroup_name": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func dataSourceWorkgroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).RedshiftServerlessConn() + + workgroupName := d.Get("workgroup_name").(string) + + resource, err := FindWorkgroupByName(ctx, conn, workgroupName) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Redshift Serverless Workgroup (%s): %s", workgroupName, err) + } + + d.SetId(workgroupName) + d.Set("arn", resource.WorkgroupArn) + if err := d.Set("endpoint", []interface{}{flattenEndpoint(resource.Endpoint)}); err != nil { + return sdkdiag.AppendErrorf(diags, "setting endpoint: %s", err) + } + d.Set("enhanced_vpc_routing", resource.EnhancedVpcRouting) + d.Set("namespace_name", resource.NamespaceName) + d.Set("publicly_accessible", resource.PubliclyAccessible) + d.Set("security_group_ids", resource.SecurityGroupIds) + d.Set("subnet_ids", resource.SubnetIds) + d.Set("workgroup_id", resource.WorkgroupId) + + return diags +} diff --git a/internal/service/redshiftserverless/workgroup_data_source_test.go b/internal/service/redshiftserverless/workgroup_data_source_test.go new file mode 100644 index 000000000000..566c07709e81 --- /dev/null +++ b/internal/service/redshiftserverless/workgroup_data_source_test.go @@ -0,0 +1,70 @@ +package redshiftserverless_test + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/service/redshiftserverless" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" +) + +func TestAccRedshiftServerlessWorkgroupDataSource_basic(t *testing.T) { + ctx := acctest.Context(t) + dataSourceName := "data.aws_redshiftserverless_workgroup.test" + resourceName := "aws_redshiftserverless_workgroup.test" + + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, redshiftserverless.EndpointsID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccWorkgroupDataSourceConfig_basic(rName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.#", resourceName, "endpoint.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.address", resourceName, "endpoint.0.address"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.port", resourceName, "endpoint.0.port"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.port", resourceName, "endpoint.0.port"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.vpc_endpoint.#", resourceName, "endpoint.0.vpc_endpoint.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.vpc_endpoint.0.vpc_endpoint_id", resourceName, "endpoint.0.vpc_endpoint.0.vpc_endpoint_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.vpc_endpoint.0.vpc_id", resourceName, "endpoint.0.vpc_endpoint.0.vpc_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.vpc_endpoint.0.network_interface.#", resourceName, "endpoint.0.vpc_endpoint.0.network_interface.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.vpc_endpoint.0.network_interface.availability_zone", resourceName, "endpoint.0.vpc_endpoint.0.network_interface.availability_zone"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.vpc_endpoint.0.network_interface.network_interface_id", resourceName, "endpoint.0.vpc_endpoint.0.network_interface.network_interface_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.vpc_endpoint.0.network_interface.private_ip_address", resourceName, "endpoint.0.vpc_endpoint.0.network_interface.private_ip_address"), + resource.TestCheckResourceAttrPair(dataSourceName, "endpoint.0.vpc_endpoint.0.network_interface.subnet_id", resourceName, "endpoint.0.vpc_endpoint.0.network_interface.subnet_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "enhanced_vpc_routing", resourceName, "enhanced_vpc_routing"), + resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"), + resource.TestCheckResourceAttrPair(dataSourceName, "namespace_name", resourceName, "namespace_name"), + resource.TestCheckResourceAttrPair(dataSourceName, "publicly_accessible", resourceName, "publicly_accessible"), + resource.TestCheckResourceAttrPair(dataSourceName, "security_group_ids.#", resourceName, "security_group_ids.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "subnet_ids.#", resourceName, "subnet_ids.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "workgroup_id", resourceName, "workgroup_id"), + resource.TestCheckResourceAttrPair(dataSourceName, "workgroup_name", resourceName, "workgroup_name"), + ), + }, + }, + }) +} + +func testAccWorkgroupDataSourceConfig_basic(rName string) string { + return fmt.Sprintf(` +resource "aws_redshiftserverless_namespace" "test" { + namespace_name = %[1]q +} + +resource "aws_redshiftserverless_workgroup" "test" { + namespace_name = aws_redshiftserverless_namespace.test.namespace_name + workgroup_name = %[1]q +} + +data "aws_redshiftserverless_workgroup" "test" { + workgroup_name = aws_redshiftserverless_workgroup.test.workgroup_name +} +`, rName) +} diff --git a/website/docs/d/redshiftserverless_workgroup.html.markdown b/website/docs/d/redshiftserverless_workgroup.html.markdown new file mode 100644 index 000000000000..c323f40e4dfa --- /dev/null +++ b/website/docs/d/redshiftserverless_workgroup.html.markdown @@ -0,0 +1,59 @@ +--- +subcategory: "Redshift Serverless" +layout: "aws" +page_title: "AWS: aws_redshiftserverless_workgroup" +description: |- + Terraform data source for managing an AWS Redshift Serverless Workgroup. +--- + +# Data Source: aws_redshiftserverless_workgroup + +Terraform data source for managing an AWS Redshift Serverless Workgroup. + +## Example Usage + +### Basic Usage + +```terraform +data "aws_redshiftserverless_workgroup" "example" { + workgroup_name = aws_redshiftserverless_workgroup.example.workgroup_name +} +``` + +## Argument Reference + +The following arguments are required: + +* `workgroup_name` - (Required) The name of the workgroup associated with the database. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `arn` - Amazon Resource Name (ARN) of the Redshift Serverless Workgroup. +* `id` - The Redshift Workgroup Name. +* `endpoint` - The endpoint that is created from the workgroup. See `Endpoint` below. +* `enhanced_vpc_routing` - The value that specifies whether to turn on enhanced virtual private cloud (VPC) routing, which forces Amazon Redshift Serverless to route traffic through your VPC instead of over the internet. +* `publicly_accessible` - A value that specifies whether the workgroup can be accessed from a public network. +* `security_group_ids` - An array of security group IDs to associate with the workgroup. +* `subnet_ids` - An array of VPC subnet IDs to associate with the workgroup. When set, must contain at least three subnets spanning three Availability Zones. A minimum number of IP addresses is required and scales with the Base Capacity. For more information, see the following [AWS document](https://docs.aws.amazon.com/redshift/latest/mgmt/serverless-known-issues.html). +* `workgroup_id` - The Redshift Workgroup ID. + +### Endpoint + +* `address` - The DNS address of the VPC endpoint. +* `port` - The port that Amazon Redshift Serverless listens on. +* `vpc_endpoint` - The VPC endpoint or the Redshift Serverless workgroup. See `VPC Endpoint` below. + +#### VPC Endpoint + +* `vpc_endpoint_id` - The DNS address of the VPC endpoint. +* `vpc_id` - The port that Amazon Redshift Serverless listens on. +* `network_interface` - The network interfaces of the endpoint.. See `Network Interface` below. + +##### Network Interface + +* `availability_zone` - The availability Zone. +* `network_interface_id` - The unique identifier of the network interface. +* `private_ip_address` - The IPv4 address of the network interface within the subnet. +* `subnet_id` - The unique identifier of the subnet.