Skip to content

Commit

Permalink
add comment update support
Browse files Browse the repository at this point in the history
  • Loading branch information
maastha committed Apr 10, 2024
1 parent ed92a85 commit e7ddaa6
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"strings"
"time"

"go.mongodb.org/atlas-sdk/v20231115008/admin"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
)

const (
Expand All @@ -28,6 +30,7 @@ func Resource() *schema.Resource {
CreateWithoutTimeout: resourceCreate,
ReadWithoutTimeout: resourceRead,
DeleteWithoutTimeout: resourceDelete,
UpdateWithoutTimeout: resourceUpdate,
Importer: &schema.ResourceImporter{
StateContext: resourceImport,
},
Expand All @@ -50,7 +53,6 @@ func Resource() *schema.Resource {
"comment": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"provider_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -146,6 +148,51 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
return resourceRead(ctx, d, meta)
}

func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
projectID := d.Get("project_id").(string)
instanceName := d.Get("instance_name").(string)
endpointID := d.Get("endpoint_id").(string)

_, _, err := connV2.ServerlessPrivateEndpointsApi.GetServerlessPrivateEndpoint(ctx, projectID, instanceName, endpointID).Execute()
if err != nil {
return diag.Errorf("error getting Serverless PrivateLink Endpoint Information: %s", err)
}

// only "comment" attribute update is supported, updating other attributes forces replacement of this resource
updateRequest := admin.ServerlessTenantEndpointUpdate{
Comment: conversion.StringPtr(d.Get("comment").(string)),
ProviderName: d.Get("provider_name").(string),
}

endPoint, _, err := connV2.ServerlessPrivateEndpointsApi.UpdateServerlessPrivateEndpoint(ctx, projectID, instanceName, endpointID, &updateRequest).Execute()
if err != nil {
return diag.Errorf(ErrorServerlessServiceEndpointAdd, endpointID, err)
}

stateConf := &retry.StateChangeConf{
Pending: []string{"RESERVATION_REQUESTED", "INITIATING", "DELETING"},
Target: []string{"RESERVED", "FAILED", "DELETED", "AVAILABLE"},
Refresh: resourceRefreshFunc(ctx, connV2, projectID, instanceName, endpointID),
Timeout: d.Timeout(schema.TimeoutCreate),
MinTimeout: 5 * time.Second,
Delay: 1 * time.Minute,
}

_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.FromErr(fmt.Errorf(ErrorServerlessServiceEndpointAdd, endpointID, err))
}

d.SetId(conversion.EncodeStateID(map[string]string{
"project_id": projectID,
"instance_name": instanceName,
"endpoint_id": endPoint.GetId(),
}))

return resourceRead(ctx, d, meta)
}

func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
ids := conversion.DecodeStateID(d.Id())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ func TestMigServerlessPrivateLinkEndpointService_noComment(t *testing.T) {
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acc.RandomProjectName()
instanceName = acc.RandomClusterName()
config = configBasicNoComment(orgID, projectName, instanceName)
config = configAWSEndpoint(orgID, projectName, instanceName, true, "test comment")
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckBasic(t) },
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
ExternalProviders: mig.ExternalProvidersWithAWS(),
Config: config,
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAccServerlessPrivateLinkEndpointService_basic(t *testing.T) {
projectName = acc.RandomProjectName()
instanceName = acc.RandomClusterName()
commentOrigin = "this is a comment for serverless private link endpoint"
commentUpdated = "this is updated comment for serverless private link endpoint"
)

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -41,6 +42,18 @@ func TestAccServerlessPrivateLinkEndpointService_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(datasourceEndpointsName, "instance_name"),
),
},
{
Config: configBasic(orgID, projectName, instanceName, commentUpdated),
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "provider_name", "AWS"),
resource.TestCheckResourceAttr(resourceName, "comment", commentUpdated),
resource.TestCheckResourceAttr(datasourceName, "comment", commentUpdated),
resource.TestCheckResourceAttrSet(datasourceEndpointsName, "project_id"),
resource.TestCheckResourceAttrSet(datasourceEndpointsName, "results.#"),
resource.TestCheckResourceAttrSet(datasourceEndpointsName, "instance_name"),
),
},
{
Config: configBasic(orgID, projectName, instanceName, commentOrigin),
ResourceName: resourceName,
Expand All @@ -52,22 +65,24 @@ func TestAccServerlessPrivateLinkEndpointService_basic(t *testing.T) {
})
}

func TestAccServerlessPrivateLinkEndpointService_noComment(t *testing.T) {
func TestAccServerlessPrivateLinkEndpointService_AWSEndpointCommentUpdate(t *testing.T) {
var (
resourceName = "mongodbatlas_privatelink_endpoint_service_serverless.test"
datasourceEndpointsName = "data.mongodbatlas_privatelink_endpoints_service_serverless.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acc.RandomProjectName()
instanceName = acc.RandomClusterName()
commentUpdated = "this is updated comment for serverless private link endpoint"
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ExternalProviders: acc.ExternalProvidersOnlyAWS(),
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configBasicNoComment(orgID, projectName, instanceName),
Config: configAWSEndpoint(orgID, projectName, instanceName, false, ""),
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "provider_name", "AWS"),
Expand All @@ -77,7 +92,18 @@ func TestAccServerlessPrivateLinkEndpointService_noComment(t *testing.T) {
),
},
{
Config: configBasicNoComment(orgID, projectName, instanceName),
Config: configAWSEndpoint(orgID, projectName, instanceName, true, commentUpdated),
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "provider_name", "AWS"),
resource.TestCheckResourceAttr(resourceName, "comment", commentUpdated),
resource.TestCheckResourceAttrSet(datasourceEndpointsName, "project_id"),
resource.TestCheckResourceAttrSet(datasourceEndpointsName, "results.#"),
resource.TestCheckResourceAttrSet(datasourceEndpointsName, "instance_name"),
),
},
{
Config: configAWSEndpoint(orgID, projectName, instanceName, true, commentUpdated),
ResourceName: resourceName,
ImportStateIdFunc: importStateIDFunc(resourceName),
ImportState: true,
Expand Down Expand Up @@ -156,47 +182,120 @@ func configBasic(orgID, projectName, instanceName, comment string) string {
`, orgID, projectName, instanceName, comment)
}

func configBasicNoComment(orgID, projectName, instanceName string) string {
return fmt.Sprintf(`
func configAWSVPCEndpoint() string {
return `
resource "mongodbatlas_project" "test" {
name = %[2]q
org_id = %[1]q
}
# Create Primary VPC
resource "aws_vpc" "primary" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
}
resource "mongodbatlas_privatelink_endpoint_serverless" "test" {
project_id = mongodbatlas_project.test.id
instance_name = mongodbatlas_serverless_instance.test.name
provider_name = "AWS"
# Create IGW
resource "aws_internet_gateway" "primary" {
vpc_id = aws_vpc.primary.id
}
# Route Table
resource "aws_route" "primary-internet_access" {
route_table_id = aws_vpc.primary.main_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.primary.id
}
# Subnet-A
resource "aws_subnet" "primary-az1" {
vpc_id = aws_vpc.primary.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "us-east-1a"
}
# Subnet-B
resource "aws_subnet" "primary-az2" {
vpc_id = aws_vpc.primary.id
cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = false
availability_zone = "us-east-1b"
}
resource "aws_security_group" "primary_default" {
name_prefix = "default-"
description = "Default security group for all instances in ${aws_vpc.primary.id}"
vpc_id = aws_vpc.primary.id
ingress {
from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0",
]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}`
}

func configAWSEndpoint(orgID, projectName, instanceName string, updateComment bool, comment string) string {
peServiceServerless := `resource "mongodbatlas_privatelink_endpoint_service_serverless" "test" {
project_id = mongodbatlas_privatelink_endpoint_serverless.test.project_id
instance_name = mongodbatlas_serverless_instance.test.name
endpoint_id = mongodbatlas_privatelink_endpoint_serverless.test.endpoint_id
cloud_provider_endpoint_id = aws_vpc_endpoint.test.id
provider_name = "AWS"
}`
if updateComment {
peServiceServerless = fmt.Sprintf(`resource "mongodbatlas_privatelink_endpoint_service_serverless" "test" {
project_id = mongodbatlas_privatelink_endpoint_serverless.test.project_id
instance_name = mongodbatlas_serverless_instance.test.name
endpoint_id = mongodbatlas_privatelink_endpoint_serverless.test.endpoint_id
cloud_provider_endpoint_id = aws_vpc_endpoint.test.id
provider_name = "AWS"
comment = %[1]q
}`, comment)
}

return fmt.Sprintf(`
resource "mongodbatlas_privatelink_endpoint_service_serverless" "test" {
project_id = mongodbatlas_privatelink_endpoint_serverless.test.project_id
instance_name = mongodbatlas_privatelink_endpoint_serverless.test.instance_name
endpoint_id = mongodbatlas_privatelink_endpoint_serverless.test.endpoint_id
provider_name = "AWS"
resource "mongodbatlas_project" "test" {
name = %[2]q
org_id = %[1]q
}
resource "mongodbatlas_serverless_instance" "test" {
project_id = mongodbatlas_project.test.id
name = %[3]q
provider_settings_backing_provider_name = "AWS"
provider_settings_provider_name = "SERVERLESS"
provider_settings_region_name = "US_EAST_1"
continuous_backup_enabled = true
provider_settings_provider_name = "SERVERLESS"
provider_settings_region_name = "US_EAST_1"
continuous_backup_enabled = true
}
lifecycle {
ignore_changes = [connection_strings_private_endpoint_srv]
}
}
resource "mongodbatlas_privatelink_endpoint_serverless" "test" {
project_id = mongodbatlas_project.test.id
provider_name = "AWS"
instance_name = mongodbatlas_serverless_instance.test.name
}
data "mongodbatlas_serverless_instance" "test" {
project_id = mongodbatlas_privatelink_endpoint_service_serverless.test.project_id
name = mongodbatlas_serverless_instance.test.name
}
# aws_vpc config
%[4]s
resource "aws_vpc_endpoint" "test" {
vpc_id = aws_vpc.primary.id
service_name = mongodbatlas_privatelink_endpoint_serverless.test.endpoint_service_name
vpc_endpoint_type = "Interface"
subnet_ids = [aws_subnet.primary-az1.id, aws_subnet.primary-az2.id]
security_group_ids = [aws_security_group.primary_default.id]
}
%[5]s
data "mongodbatlas_privatelink_endpoints_service_serverless" "test" {
data "mongodbatlas_privatelink_endpoints_service_serverless" "test" {
project_id = mongodbatlas_privatelink_endpoint_service_serverless.test.project_id
instance_name = mongodbatlas_serverless_instance.test.name
}
Expand All @@ -207,7 +306,7 @@ func configBasicNoComment(orgID, projectName, instanceName string) string {
endpoint_id = mongodbatlas_privatelink_endpoint_service_serverless.test.endpoint_id
}
`, orgID, projectName, instanceName)
`, orgID, projectName, instanceName, configAWSVPCEndpoint(), peServiceServerless)
}

func checkExists(resourceName string) resource.TestCheckFunc {
Expand Down

0 comments on commit e7ddaa6

Please sign in to comment.