Skip to content

INTMDB-364 Add support for serverless private endpoints (AWS + Azure) #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package mongodbatlas

import (
"context"
"fmt"

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

func dataSourceMongoDBAtlasPrivateLinkEndpointServerless() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasPrivateEndpointServiceServerlessLinkRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"endpoint_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"comment": {
Type: schema.TypeString,
Computed: true,
},
"endpoint_service_name": {
Type: schema.TypeString,
Computed: true,
},
"cloud_provider_endpoint_id": {
Type: schema.TypeString,
Computed: true,
},
"private_link_service_resource_id": {
Type: schema.TypeString,
Computed: true,
},
"private_endpoint_ip_address": {
Type: schema.TypeString,
Computed: true,
},
"error_message": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceMongoDBAtlasPrivateEndpointServiceServerlessLinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get client connection.
conn := meta.(*MongoDBClient).Atlas

projectID := d.Get("project_id").(string)
instanceName := d.Get("instance_name").(string)
endpointID := d.Get("endpoint_id").(string)

serviceEndpoint, _, err := conn.ServerlessPrivateEndpoints.Get(ctx, projectID, instanceName, endpointID)
if err != nil {
return diag.FromErr(fmt.Errorf(errorServiceEndpointRead, endpointID, err))
}

if err := d.Set("error_message", serviceEndpoint.ErrorMessage); err != nil {
return diag.FromErr(fmt.Errorf(errorEndpointSetting, "error_message", endpointID, err))
}

if err := d.Set("status", serviceEndpoint.Status); err != nil {
return diag.FromErr(fmt.Errorf(errorEndpointSetting, "status", endpointID, err))
}

if err := d.Set("comment", serviceEndpoint.Comment); err != nil {
return diag.FromErr(fmt.Errorf(errorEndpointSetting, "comment", endpointID, err))
}

if err := d.Set("error_message", serviceEndpoint.ErrorMessage); err != nil {
return diag.FromErr(fmt.Errorf(errorEndpointSetting, "error_message", endpointID, err))
}

if err := d.Set("endpoint_service_name", serviceEndpoint.EndpointServiceName); err != nil {
return diag.FromErr(fmt.Errorf(errorEndpointSetting, "endpoint_service_name", endpointID, err))
}

if err := d.Set("cloud_provider_endpoint_id", serviceEndpoint.CloudProviderEndpointID); err != nil {
return diag.FromErr(fmt.Errorf(errorEndpointSetting, "cloud_provider_endpoint_id", endpointID, err))
}

if err := d.Set("private_link_service_resource_id", serviceEndpoint.PrivateLinkServiceResourceID); err != nil {
return diag.FromErr(fmt.Errorf(errorEndpointSetting, "private_link_service_resource_id", endpointID, err))
}

if err := d.Set("private_endpoint_ip_address", serviceEndpoint.PrivateEndpointIPAddress); err != nil {
return diag.FromErr(fmt.Errorf(errorEndpointSetting, "private_endpoint_ip_address", endpointID, err))
}

d.SetId(encodeStateID(map[string]string{
"project_id": projectID,
"instance_name": instanceName,
"endpoint_id": endpointID,
}))

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package mongodbatlas

import (
"context"
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDataSourceMongoDBAtlasPrivateLinkEndpointServiceServerless_basic(t *testing.T) {
var (
resourceName = "data.mongodbatlas_privatelink_endpoint_service_serverless.test"
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
instanceName = "dsserverless"
commentOrigin = "create"
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccDSCheckMongoDBAtlasPrivateLinkEndpointServiceServerlessDestroy,
Steps: []resource.TestStep{
{
Config: testAccDSMongoDBAtlasPrivateLinkEndpointServiceServerlessConfig(projectID, instanceName, commentOrigin),
Check: resource.ComposeTestCheckFunc(
testAccDSCheckMongoDBAtlasPrivateLinkEndpointServiceServerlessExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "comment", commentOrigin),
),
},
},
})
}

func testAccDSCheckMongoDBAtlasPrivateLinkEndpointServiceServerlessDestroy(state *terraform.State) error {
conn := testAccProvider.Meta().(*MongoDBClient).Atlas

for _, rs := range state.RootModule().Resources {
if rs.Type != "data.mongodbatlas_privatelink_endpoint_service_serverless" {
continue
}

ids := decodeStateID(rs.Primary.ID)

privateLink, _, err := conn.ServerlessPrivateEndpoints.Get(context.Background(), ids["project_id"], ids["instance_name"], ids["endpoint_id"])
if err == nil && privateLink != nil {
return fmt.Errorf("endpoint_id (%s) still exists", ids["endpoint_id"])
}
}

return nil
}

func testAccDSMongoDBAtlasPrivateLinkEndpointServiceServerlessConfig(projectID, instanceName, comment string) string {
return fmt.Sprintf(`

data "mongodbatlas_privatelink_endpoint_service_serverless" "test" {
project_id = "%[1]s"
instance_name = mongodbatlas_serverless_instance.test.name
endpoint_id = mongodbatlas_privatelink_endpoint_serverless.test.endpoint_id
}

resource "mongodbatlas_privatelink_endpoint_serverless" "test" {
project_id = "%[1]s"
instance_name = mongodbatlas_serverless_instance.test.name
provider_name = "AWS"
}


resource "mongodbatlas_privatelink_endpoint_service_serverless" "test" {
project_id = "%[1]s"
instance_name = "%[2]s"
endpoint_id = mongodbatlas_privatelink_endpoint_serverless.test.endpoint_id
provider_name = "AWS"
comment = "%[3]s"
}

resource "mongodbatlas_serverless_instance" "test" {
project_id = "%[1]s"
name = "%[2]s"
provider_settings_backing_provider_name = "AWS"
provider_settings_provider_name = "SERVERLESS"
provider_settings_region_name = "US_EAST_1"
continuous_backup_enabled = true
}

`, projectID, instanceName, comment)
}

func testAccDSCheckMongoDBAtlasPrivateLinkEndpointServiceServerlessExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*MongoDBClient).Atlas

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}

ids := decodeStateID(rs.Primary.ID)

_, _, err := conn.ServerlessPrivateEndpoints.Get(context.Background(), ids["project_id"], ids["instance_name"], ids["endpoint_id"])
if err == nil {
return nil
}

return fmt.Errorf("endpoint_id (%s) does not exist", ids["endpoint_id"])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package mongodbatlas

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

func dataSourceMongoDBAtlasPrivateLinkEndpointsServiceServerless() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasPrivateLinkEndpointsServiceServerlessRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
},
"instance_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"page_num": {
Type: schema.TypeInt,
Optional: true,
},
"items_per_page": {
Type: schema.TypeInt,
Optional: true,
},
"results": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cloud_provider_endpoint_id": {
Type: schema.TypeString,
Computed: true,
},
"comment": {
Type: schema.TypeString,
Computed: true,
},
"endpoint_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"endpoint_service_name": {
Type: schema.TypeString,
Computed: true,
},
"private_link_service_resource_id": {
Type: schema.TypeString,
Computed: true,
},
"private_endpoint_ip_address": {
Type: schema.TypeString,
Computed: true,
},
"error_message": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceMongoDBAtlasPrivateLinkEndpointsServiceServerlessRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get client connection.
conn := meta.(*MongoDBClient).Atlas
projectID := d.Get("project_id").(string)
instanceName := d.Get("instance_name").(string)

options := &matlas.ListOptions{
PageNum: d.Get("page_num").(int),
ItemsPerPage: d.Get("items_per_page").(int),
}

privateLinkEndpoints, _, err := conn.ServerlessPrivateEndpoints.List(ctx, projectID, instanceName, options)
if err != nil {
return diag.Errorf("error getting Serverless PrivateLink Endpoints Information: %s", err)
}

if err := d.Set("results", flattenServerlessPrivateLinkEndpoints(privateLinkEndpoints)); err != nil {
return diag.Errorf("error setting `results`: %s", err)
}

d.SetId(resource.UniqueId())

return nil
}

func flattenServerlessPrivateLinkEndpoints(privateLinks []matlas.ServerlessPrivateEndpointConnection) []map[string]interface{} {
var results []map[string]interface{}

if len(privateLinks) == 0 {
return results
}

results = make([]map[string]interface{}, len(privateLinks))

for k := range privateLinks {
results[k] = map[string]interface{}{
"endpoint_id": privateLinks[k].ID,
"endpoint_service_name": privateLinks[k].EndpointServiceName,
"cloud_provider_endpoint_id": privateLinks[k].CloudProviderEndpointID,
"private_link_service_resource_id": privateLinks[k].PrivateLinkServiceResourceID,
"private_endpoint_ip_address": privateLinks[k].PrivateEndpointIPAddress,
"comment": privateLinks[k].Comment,
"error_message": privateLinks[k].ErrorMessage,
"status": privateLinks[k].Status,
}
}

return results
}
Loading