diff --git a/.changelog/32276.txt b/.changelog/32276.txt new file mode 100644 index 00000000000..8cb046fbcca --- /dev/null +++ b/.changelog/32276.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_opensearchserverless_vpc_endpoint +``` \ No newline at end of file diff --git a/internal/service/opensearchserverless/service_package_gen.go b/internal/service/opensearchserverless/service_package_gen.go index 96f8ffa50a3..db6b44f5964 100644 --- a/internal/service/opensearchserverless/service_package_gen.go +++ b/internal/service/opensearchserverless/service_package_gen.go @@ -61,6 +61,10 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Factory: DataSourceSecurityPolicy, TypeName: "aws_opensearchserverless_security_policy", }, + { + Factory: DataSourceVPCEndpoint, + TypeName: "aws_opensearchserverless_vpc_endpoint", + }, } } diff --git a/internal/service/opensearchserverless/vpc_endpoint_data_source.go b/internal/service/opensearchserverless/vpc_endpoint_data_source.go new file mode 100644 index 00000000000..0a2075c2adc --- /dev/null +++ b/internal/service/opensearchserverless/vpc_endpoint_data_source.go @@ -0,0 +1,78 @@ +package opensearchserverless + +import ( + "context" + "regexp" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" +) + +// @SDKDataSource("aws_opensearchserverless_vpc_endpoint") +func DataSourceVPCEndpoint() *schema.Resource { + return &schema.Resource{ + ReadWithoutTimeout: dataSourceVPCEndpointRead, + + Schema: map[string]*schema.Schema{ + "created_date": { + Type: schema.TypeString, + Computed: true, + }, + "id": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.All( + validation.StringLenBetween(1, 255), + validation.StringMatch(regexp.MustCompile(`^vpce-[0-9a-z]*$`), `must start with "vpce-" and can include any lower case letter or number`), + ), + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "security_group_ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "subnet_ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "vpc_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceVPCEndpointRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).OpenSearchServerlessClient(ctx) + + id := d.Get("id").(string) + vpcEndpoint, err := FindVPCEndpointByID(ctx, conn, id) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading VPC Endpoint with id (%s): %s", id, err) + } + + d.SetId(aws.ToString(vpcEndpoint.Id)) + + createdDate := time.UnixMilli(aws.ToInt64(vpcEndpoint.CreatedDate)) + d.Set("created_date", createdDate.Format(time.RFC3339)) + + d.Set("name", vpcEndpoint.Name) + d.Set("security_group_ids", vpcEndpoint.SecurityGroupIds) + d.Set("subnet_ids", vpcEndpoint.SubnetIds) + d.Set("vpc_id", vpcEndpoint.VpcId) + + return diags +} diff --git a/internal/service/opensearchserverless/vpc_endpoint_data_source_test.go b/internal/service/opensearchserverless/vpc_endpoint_data_source_test.go new file mode 100644 index 00000000000..0df04b7e811 --- /dev/null +++ b/internal/service/opensearchserverless/vpc_endpoint_data_source_test.go @@ -0,0 +1,107 @@ +package opensearchserverless_test + +import ( + "fmt" + "testing" + + sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/names" +) + +func TestAccOpenSearchServerlessVPCEndpointDataSource_basic(t *testing.T) { + ctx := acctest.Context(t) + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_opensearchserverless_vpc_endpoint.test" + dataSourceName := "data.aws_opensearchserverless_vpc_endpoint.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartitionHasService(t, names.OpenSearchServerlessEndpointID) + }, + ErrorCheck: acctest.ErrorCheck(t, names.OpenSearchServerlessEndpointID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckVPCEndpointDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccVPCEndpointDataSourceConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dataSourceName, "created_date"), + resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"), + resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"), + resource.TestCheckResourceAttrPair(dataSourceName, "security_group_ids.#", resourceName, "security_group_ids.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "subnet_ids.#", resourceName, "subnet_ids.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "vpc_id", resourceName, "vpc_id"), + ), + }, + }, + }) +} + +func testAccVPCEndpointDataSourceConfig_networkingBase(rName string, subnetCount int) string { + return acctest.ConfigCompose( + acctest.ConfigAvailableAZsNoOptInDefaultExclude(), + fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + enable_dns_hostnames = true + + tags = { + Name = %[1]q + } +} + +resource "aws_subnet" "test" { + count = %[2]d + + vpc_id = aws_vpc.test.id + availability_zone = data.aws_availability_zones.available.names[count.index] + cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 8, count.index) + + tags = { + Name = %[1]q + } +} +`, rName, subnetCount), + ) +} + +func testAccVPCEndpointDataSourceConfig_securityGroupBase(rName string, sgCount int) string { + return acctest.ConfigCompose( + fmt.Sprintf(` +resource "aws_security_group" "test" { + count = %[2]d + name = "%[1]s-${count.index}" + vpc_id = aws_vpc.test.id + + tags = { + Name = %[1]q + } +} +`, rName, sgCount), + ) +} + +func testAccVPCEndpointDataSourceConfig_basic(rName string) string { + return acctest.ConfigCompose( + testAccVPCEndpointDataSourceConfig_networkingBase(rName, 2), + testAccVPCEndpointDataSourceConfig_securityGroupBase(rName, 2), + fmt.Sprintf(` +resource "aws_opensearchserverless_vpc_endpoint" "test" { + name = %[1]q + security_group_ids = aws_security_group.test[*].id + subnet_ids = aws_subnet.test[*].id + vpc_id = aws_vpc.test.id +} + +data "aws_opensearchserverless_vpc_endpoint" "test" { + id = aws_opensearchserverless_vpc_endpoint.test.id +} +`, rName)) +} diff --git a/website/docs/d/opensearchserverless_vpc_endpoint.html.markdown b/website/docs/d/opensearchserverless_vpc_endpoint.html.markdown new file mode 100644 index 00000000000..43f529d0bee --- /dev/null +++ b/website/docs/d/opensearchserverless_vpc_endpoint.html.markdown @@ -0,0 +1,35 @@ +--- +subcategory: "OpenSearch Serverless" +layout: "aws" +page_title: "AWS: aws_opensearchserverless_vpc_endpoint" +description: |- + Terraform data source for managing an AWS OpenSearch Serverless VPC Endpoint. +--- + +# Data Source: aws_opensearchserverless_vpc_endpoint + +Terraform data source for managing an AWS OpenSearch Serverless VPC Endpoint. + +## Example Usage + +```terraform +data "aws_opensearchserverless_vpc_endpoint" "example" { + id = "vpce-829a4487959e2a839" +} +``` + +## Argument Reference + +The following arguments are required: + +* `id` - (Required) The unique identifier of the endpoint. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `created_date` - The date the endpoint was created. +* `name` - The name of the endpoint. +* `security_group_ids` - The IDs of the security groups that define the ports, protocols, and sources for inbound traffic that you are authorizing into your endpoint. +* `subnet_ids` - The IDs of the subnets from which you access OpenSearch Serverless. +* `vpc_id` - The ID of the VPC from which you access OpenSearch Serverless.