Skip to content

Commit

Permalink
chore: Upgrades privatelink_endpoint_service_serverless resource to…
Browse files Browse the repository at this point in the history
… auto-generated SDK (#1932)

* migrate resource

* migrate singular data source

* migrate plural data source to new SDK

* add migration test

* rename refresh func

* change to mig.precheckbasic

* refactor flattener

* check only resourceName in migration test

* deprecate page_num and items_per_page

* change deprecation message
  • Loading branch information
oarbusi authored Feb 12, 2024
1 parent 577ba61 commit d5c7ea8
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 119 deletions.
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))
}

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{
PageNum: d.Get("page_num").(int),
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

0 comments on commit d5c7ea8

Please sign in to comment.