-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32276 from joshjluo/f-aws_opensearchserverless_vp…
…c_endpoint-data-source Add aws_opensearchserverless_vpc_endpoint data source
- Loading branch information
Showing
16 changed files
with
253 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
aws_opensearchserverless_vpc_endpoint | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
internal/service/opensearchserverless/vpc_endpoint_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
"vpc_endpoint_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("vpc_endpoint_id").(string) | ||
vpcEndpoint, err := findVPCEndpointByID(ctx, conn, id) | ||
|
||
if err != nil { | ||
return sdkdiag.AppendErrorf(diags, "reading OpenSearch Serverless 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
107
internal/service/opensearchserverless/vpc_endpoint_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" { | ||
vpc_endpoint_id = aws_opensearchserverless_vpc_endpoint.test.id | ||
} | ||
`, rName)) | ||
} |
Oops, something went wrong.