Skip to content

Commit

Permalink
Add aws_opensearchserverless_vpc_endpoint data source
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Luo committed Jun 30, 2023
1 parent 595e62b commit 10adf75
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/32276.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_opensearchserverless_vpc_endpoint
```
4 changes: 4 additions & 0 deletions internal/service/opensearchserverless/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions internal/service/opensearchserverless/vpc_endpoint_data_source.go
Original file line number Diff line number Diff line change
@@ -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
}
107 changes: 107 additions & 0 deletions internal/service/opensearchserverless/vpc_endpoint_data_source_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
35 changes: 35 additions & 0 deletions website/docs/d/opensearchserverless_vpc_endpoint.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 10adf75

Please sign in to comment.