Skip to content
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

chore: Upgrades privatelink_endpoint_service_serverless resource to auto-generated SDK #1932

Merged
merged 10 commits into from
Feb 12, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func DataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasPrivateEndpointServiceServerlessLinkRead,
ReadContext: dataSourceRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -62,48 +62,43 @@ func DataSource() *schema.Resource {
}
}

func dataSourceMongoDBAtlasPrivateEndpointServiceServerlessLinkRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
// Get client connection.
conn := meta.(*config.MongoDBClient).Atlas
func dataSourceRead(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)

serviceEndpoint, _, err := conn.ServerlessPrivateEndpoints.Get(ctx, projectID, instanceName, endpointID)
serviceEndpoint, _, err := connV2.ServerlessPrivateEndpointsApi.GetServerlessPrivateEndpoint(ctx, projectID, instanceName, endpointID).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf(privatelinkendpointservice.ErrorServiceEndpointRead, endpointID, err))
}

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

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

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

oarbusi marked this conversation as resolved.
Show resolved Hide resolved
if err := d.Set("error_message", serviceEndpoint.ErrorMessage); err != nil {
return diag.FromErr(fmt.Errorf(privatelinkendpointservice.ErrorEndpointSetting, "error_message", endpointID, err))
}

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package privatelinkendpointserviceserverless

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
matlas "go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/atlas-sdk/v20231115006/admin"
)

func PluralDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasPrivateLinkEndpointsServiceServerlessRead,
ReadContext: dataSourcePluralRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Expand All @@ -24,12 +26,14 @@ func PluralDataSource() *schema.Resource {
ForceNew: true,
},
"page_num": {
Type: schema.TypeInt,
Optional: true,
Deprecated: fmt.Sprintf(constant.DeprecationParamByVersion, "1.17.0"),
Type: schema.TypeInt,
Optional: true,
},
"items_per_page": {
Type: schema.TypeInt,
Optional: true,
Deprecated: fmt.Sprintf(constant.DeprecationParamByVersion, "1.17.0"),
Type: schema.TypeInt,
Optional: true,
},
"results": {
Type: schema.TypeList,
Expand Down Expand Up @@ -76,18 +80,12 @@ func PluralDataSource() *schema.Resource {
}
}

func dataSourceMongoDBAtlasPrivateLinkEndpointsServiceServerlessRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
// Get client connection.
conn := meta.(*config.MongoDBClient).Atlas
func dataSourcePluralRead(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)

options := &matlas.ListOptions{
Copy link
Collaborator Author

@oarbusi oarbusi Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no more need of pagination options, endpoint called by new SDK returns all serverless private endpoints

PageNum: d.Get("page_num").(int),
oarbusi marked this conversation as resolved.
Show resolved Hide resolved
ItemsPerPage: d.Get("items_per_page").(int),
}

privateLinkEndpoints, _, err := conn.ServerlessPrivateEndpoints.List(ctx, projectID, instanceName, options)
privateLinkEndpoints, _, err := connV2.ServerlessPrivateEndpointsApi.ListServerlessPrivateEndpoints(ctx, projectID, instanceName).Execute()
if err != nil {
return diag.Errorf("error getting Serverless PrivateLink Endpoints Information: %s", err)
}
Expand All @@ -101,25 +99,23 @@ func dataSourceMongoDBAtlasPrivateLinkEndpointsServiceServerlessRead(ctx context
return nil
}

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

func flattenServerlessPrivateLinkEndpoints(privateLinks []admin.ServerlessTenantEndpoint) []map[string]any {
if len(privateLinks) == 0 {
return results
return nil
}

results = make([]map[string]any, len(privateLinks))
results := make([]map[string]any, len(privateLinks))

for k := range privateLinks {
results[k] = map[string]any{
"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,
"endpoint_id": privateLinks[k].GetId(),
"endpoint_service_name": privateLinks[k].GetEndpointServiceName(),
"cloud_provider_endpoint_id": privateLinks[k].GetCloudProviderEndpointId(),
"private_link_service_resource_id": privateLinks[k].GetPrivateLinkServiceResourceId(),
"private_endpoint_ip_address": privateLinks[k].GetPrivateEndpointIpAddress(),
"comment": privateLinks[k].GetComment(),
"error_message": privateLinks[k].GetErrorMessage(),
"status": privateLinks[k].GetStatus(),
}
}

Expand Down
Loading
Loading