Skip to content

Commit

Permalink
feat: Adds support for region & customer_endpoint_dns_name in private…
Browse files Browse the repository at this point in the history
…link (#1982)
  • Loading branch information
EspenAlbert committed Mar 8, 2024
1 parent ab06421 commit f899b80
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 141 deletions.
Expand Up @@ -5,8 +5,10 @@ resource "mongodbatlas_privatelink_endpoint" "pe_east" {
}

resource "mongodbatlas_privatelink_endpoint_service_data_federation_online_archive" "test" {
project_id = var.project_id
endpoint_id = aws_vpc_endpoint.vpce_east.id
provider_name = "AWS"
comment = "Terraform Acceptance Test"
project_id = var.project_id
endpoint_id = aws_vpc_endpoint.vpce_east.id
provider_name = "AWS"
comment = "Terraform Example Comment"
region = "US_EAST_1"
customer_endpoint_dns_name = aws_vpc_endpoint.vpce_east.dns_entry[0].dns_name
}
25 changes: 25 additions & 0 deletions internal/common/validate/string_validator.go
@@ -0,0 +1,25 @@
package validate

import (
"fmt"
"strings"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func StringIsUppercase() schema.SchemaValidateDiagFunc {
return func(v any, p cty.Path) diag.Diagnostics {
value := v.(string)
var diags diag.Diagnostics
if value != strings.ToUpper(value) {
diagError := diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("The provided string '%q' must be uppercase.", value),
}
diags = append(diags, diagError)
}
return diags
}
}
40 changes: 40 additions & 0 deletions internal/common/validate/string_validator_test.go
@@ -0,0 +1,40 @@
package validate_test

import (
"testing"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
)

func TestStringIsUppercase(t *testing.T) {
testCases := []struct {
name string
expectedError bool
}{
{
name: "AWS",
expectedError: false,
},
{
name: "aws",
expectedError: true,
},
{
name: "",
expectedError: false,
},
{
name: "AwS",
expectedError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
diag := validate.StringIsUppercase()(tc.name, nil)
if diag.HasError() != tc.expectedError {
t.Errorf("Case %s: Received unexpected error: %v", tc.name, diag[0].Summary)
}
})
}
}
16 changes: 0 additions & 16 deletions internal/service/advancedcluster/model_advanced_cluster.go
Expand Up @@ -9,7 +9,6 @@ import (
"slices"
"strings"

"github.com/hashicorp/go-cty/cty"
"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"
Expand Down Expand Up @@ -247,21 +246,6 @@ func SchemaAdvancedConfig() *schema.Schema {
}
}

func StringIsUppercase() schema.SchemaValidateDiagFunc {
return func(v any, p cty.Path) diag.Diagnostics {
value := v.(string)
var diags diag.Diagnostics
if value != strings.ToUpper(value) {
diagError := diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("The provided string '%q' must be uppercase.", value),
}
diags = append(diags, diagError)
}
return diags
}
}

func HashFunctionForKeyValuePair(v any) int {
var buf bytes.Buffer
m := v.(map[string]any)
Expand Down
33 changes: 0 additions & 33 deletions internal/service/advancedcluster/model_advanced_cluster_test.go
Expand Up @@ -120,39 +120,6 @@ func TestUpgradeRefreshFunc(t *testing.T) {
}
}

func TestStringIsUppercase(t *testing.T) {
testCases := []struct {
name string
expectedError bool
}{
{
name: "AWS",
expectedError: false,
},
{
name: "aws",
expectedError: true,
},
{
name: "",
expectedError: false,
},
{
name: "AwS",
expectedError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
diag := advancedcluster.StringIsUppercase()(tc.name, nil)
if diag.HasError() != tc.expectedError {
t.Errorf("Case %s: Received unexpected error: %v", tc.name, diag[0].Summary)
}
})
}
}

func TestResourceListAdvancedRefreshFunc(t *testing.T) {
testCases := []struct {
mockCluster *admin.PaginatedAdvancedClusterDescription
Expand Down
5 changes: 3 additions & 2 deletions internal/service/advancedcluster/resource_advanced_cluster.go
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"github.com/spf13/cast"
"go.mongodb.org/atlas-sdk/v20231115007/admin"
Expand Down Expand Up @@ -264,13 +265,13 @@ func Resource() *schema.Resource {
"provider_name": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: StringIsUppercase(),
ValidateDiagFunc: validate.StringIsUppercase(),
},
"read_only_specs": schemaSpecs(),
"region_name": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: StringIsUppercase(),
ValidateDiagFunc: validate.StringIsUppercase(),
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions internal/service/cluster/resource_cluster.go
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
"github.com/spf13/cast"
Expand Down Expand Up @@ -153,7 +154,7 @@ func Resource() *schema.Resource {
"provider_name": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: advancedcluster.StringIsUppercase(),
ValidateDiagFunc: validate.StringIsUppercase(),
},
"pit_enabled": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -236,7 +237,7 @@ func Resource() *schema.Resource {
"region_name": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: advancedcluster.StringIsUppercase(),
ValidateDiagFunc: validate.StringIsUppercase(),
},
"electable_nodes": {
Type: schema.TypeInt,
Expand Down
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)

Expand Down Expand Up @@ -33,36 +32,30 @@ func DataSource() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
"customer_endpoint_dns_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

privateEndpoint, _, err := connV2.DataFederationApi.GetDataFederationPrivateEndpoint(ctx, projectID, endopointID).Execute()
privateEndpoint, _, err := connV2.DataFederationApi.GetDataFederationPrivateEndpoint(ctx, projectID, endpointID).Execute()
if err != nil {
return diag.Errorf(errorPrivateEndpointServiceDataFederationOnlineArchiveRead, endopointID, projectID, err)
}

if err := d.Set("comment", privateEndpoint.GetComment()); err != nil {
return diag.Errorf(errorPrivateEndpointServiceDataFederationOnlineArchiveRead, endopointID, projectID, err)
}

if err := d.Set("provider_name", privateEndpoint.GetProvider()); err != nil {
return diag.Errorf(errorPrivateEndpointServiceDataFederationOnlineArchiveRead, endopointID, projectID, err)
return diag.Errorf(errorPrivateEndpointServiceDataFederationOnlineArchiveRead, endpointID, projectID, err)
}

if err := d.Set("type", privateEndpoint.GetType()); err != nil {
return diag.Errorf(errorPrivateEndpointServiceDataFederationOnlineArchiveRead, endopointID, projectID, err)
err = populateResourceData(d, privateEndpoint, projectID, endpointID)
if err != nil {
return diag.Errorf(errorPrivateEndpointServiceDataFederationOnlineArchiveRead, endpointID, projectID, err)
}

d.SetId(conversion.EncodeStateID(map[string]string{
"project_id": projectID,
"endpoint_id": endopointID,
}))

return nil
}
Expand Up @@ -26,7 +26,9 @@ func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchiveDS_basic
checkExists(resourceName),
resource.TestCheckResourceAttr(dataSourcePrivatelinkEndpointServiceDataFederetionDataArchive, "project_id", projectID),
resource.TestCheckResourceAttr(dataSourcePrivatelinkEndpointServiceDataFederetionDataArchive, "endpoint_id", endpointID),
resource.TestCheckResourceAttrSet(dataSourcePrivatelinkEndpointServiceDataFederetionDataArchive, "comment"),
resource.TestCheckResourceAttr(dataSourcePrivatelinkEndpointServiceDataFederetionDataArchive, "comment", comment),
resource.TestCheckResourceAttr(dataSourcePrivatelinkEndpointServiceDataFederetionDataArchive, "region", atlasRegion),
resource.TestCheckResourceAttr(dataSourcePrivatelinkEndpointServiceDataFederetionDataArchive, "customer_endpoint_dns_name", customerEndpointDNSName),
resource.TestCheckResourceAttrSet(dataSourcePrivatelinkEndpointServiceDataFederetionDataArchive, "type"),
resource.TestCheckResourceAttrSet(dataSourcePrivatelinkEndpointServiceDataFederetionDataArchive, "provider_name"),
),
Expand All @@ -38,15 +40,17 @@ func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchiveDS_basic
func dataSourcesConfigBasic(projectID, endpointID string) string {
return fmt.Sprintf(`
resource "mongodbatlas_privatelink_endpoint_service_data_federation_online_archive" "test" {
project_id = %[1]q
endpoint_id = %[2]q
provider_name = "AWS"
comment = "Terraform Acceptance Test"
project_id = %[1]q
endpoint_id = %[2]q
provider_name = "AWS"
comment = "Terraform Acceptance Test"
region = %[3]q
customer_endpoint_dns_name = %[4]q
}
data "mongodbatlas_privatelink_endpoint_service_data_federation_online_archive" "test" {
project_id = mongodbatlas_privatelink_endpoint_service_data_federation_online_archive.test.project_id
endpoint_id = mongodbatlas_privatelink_endpoint_service_data_federation_online_archive.test.endpoint_id
}
`, projectID, endpointID)
`, projectID, endpointID, atlasRegion, customerEndpointDNSName)
}
Expand Up @@ -43,6 +43,14 @@ func PluralDataSource() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
"customer_endpoint_dns_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -77,10 +85,12 @@ func flattenPrivateLinkEndpointDataLakeResponse(atlasPrivateLinkEndpointDataLake

for i, atlasPrivateLinkEndpointDataLake := range atlasPrivateLinkEndpointDataLakes {
results[i] = map[string]any{
"endpoint_id": atlasPrivateLinkEndpointDataLake.GetEndpointId(),
"provider_name": atlasPrivateLinkEndpointDataLake.GetProvider(),
"comment": atlasPrivateLinkEndpointDataLake.GetComment(),
"type": atlasPrivateLinkEndpointDataLake.GetType(),
"endpoint_id": atlasPrivateLinkEndpointDataLake.GetEndpointId(),
"provider_name": atlasPrivateLinkEndpointDataLake.GetProvider(),
"comment": atlasPrivateLinkEndpointDataLake.GetComment(),
"type": atlasPrivateLinkEndpointDataLake.GetType(),
"region": atlasPrivateLinkEndpointDataLake.GetRegion(),
"customer_endpoint_dns_name": atlasPrivateLinkEndpointDataLake.GetCustomerEndpointDNSName(),
}
}

Expand Down

0 comments on commit f899b80

Please sign in to comment.