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: Migrates privatelink_endpoint_regional_mode to the new auto-generated SDK #2246

Merged
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
Expand Up @@ -11,7 +11,7 @@ import (

func DataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasPrivateEndpointRegionalModeRead,
ReadContext: dataSourceRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Expand All @@ -25,15 +25,12 @@ func DataSource() *schema.Resource {
}
}

func dataSourceMongoDBAtlasPrivateEndpointRegionalModeRead(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 {
conn := meta.(*config.MongoDBClient).AtlasV2
projectID := d.Get("project_id").(string)

setting, _, err := conn.PrivateEndpoints.GetRegionalizedPrivateEndpointSetting(ctx, projectID)
setting, _, err := conn.PrivateEndpointServicesApi.GetRegionalizedPrivateEndpointSetting(ctx, projectID).Execute()
if err != nil {
// case 404
// deleted in the backend case
if strings.Contains(err.Error(), "404") {
d.SetId("")
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
"go.mongodb.org/atlas-sdk/v20231115013/admin"
)

type permCtxKey string
Expand All @@ -26,12 +27,12 @@ var regionalModeTimeoutCtxKey permCtxKey = "regionalModeTimeout"

func Resource() *schema.Resource {
return &schema.Resource{
CreateContext: resourceMongoDBAtlasPrivateEndpointRegionalModeCreate,
ReadContext: resourceMongoDBAtlasPrivateEndpointRegionalModeRead,
UpdateContext: resourceMongoDBAtlasPrivateEndpointRegionalModeUpdate,
DeleteContext: resourceMongoDBAtlasPrivateEndpointRegionalModeDelete,
CreateContext: resourceCreate,
ReadContext: resourceRead,
UpdateContext: resourceUpdate,
DeleteContext: resourceDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceMongoDBAtlasPrivateEndpointRegionalModeImportState,
StateContext: resourceImportState,
},
Schema: map[string]*schema.Schema{
"project_id": {
Expand All @@ -53,23 +54,23 @@ func Resource() *schema.Resource {
}
}

func resourceMongoDBAtlasPrivateEndpointRegionalModeCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
d.SetId(d.Get("project_id").(string))
err := resourceMongoDBAtlasPrivateEndpointRegionalModeUpdate(context.WithValue(ctx, regionalModeTimeoutCtxKey, schema.TimeoutCreate), d, meta)
err := resourceUpdate(context.WithValue(ctx, regionalModeTimeoutCtxKey, schema.TimeoutCreate), d, meta)

if err != nil {
return err
}

return resourceMongoDBAtlasPrivateEndpointRegionalModeRead(ctx, d, meta)
return resourceRead(ctx, d, meta)
}

func resourceMongoDBAtlasPrivateEndpointRegionalModeRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).AtlasV2

projectID := d.Id()

setting, resp, err := conn.PrivateEndpoints.GetRegionalizedPrivateEndpointSetting(context.Background(), projectID)
setting, resp, err := conn.PrivateEndpointServicesApi.GetRegionalizedPrivateEndpointSetting(ctx, projectID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
Expand All @@ -86,9 +87,8 @@ func resourceMongoDBAtlasPrivateEndpointRegionalModeRead(ctx context.Context, d
return nil
}

func resourceMongoDBAtlasPrivateEndpointRegionalModeUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
connV2 := meta.(*config.MongoDBClient).AtlasV2
func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).AtlasV2

projectID := d.Id()
enabled := d.Get("enabled").(bool)
Expand All @@ -97,10 +97,12 @@ func resourceMongoDBAtlasPrivateEndpointRegionalModeUpdate(ctx context.Context,
if timeoutKey == nil {
timeoutKey = schema.TimeoutUpdate
}

_, resp, err := conn.PrivateEndpoints.UpdateRegionalizedPrivateEndpointSetting(ctx, projectID, enabled)
settingParam := admin.ProjectSettingItem{
Enabled: enabled,
}
_, resp, err := conn.PrivateEndpointServicesApi.ToggleRegionalizedPrivateEndpointSetting(ctx, projectID, &settingParam).Execute()
if err != nil {
if resp != nil && resp.Response.StatusCode == 404 {
if resp != nil && resp.StatusCode == 404 {
return nil
}

Expand All @@ -112,12 +114,11 @@ func resourceMongoDBAtlasPrivateEndpointRegionalModeUpdate(ctx context.Context,
stateConf := &retry.StateChangeConf{
Pending: []string{"REPEATING", "PENDING"},
Target: []string{"IDLE", "DELETED"},
Refresh: advancedcluster.ResourceClusterListAdvancedRefreshFunc(ctx, projectID, connV2.ClustersApi),
Refresh: advancedcluster.ResourceClusterListAdvancedRefreshFunc(ctx, projectID, conn.ClustersApi),
Timeout: d.Timeout(timeoutKey.(string)),
MinTimeout: 5 * time.Second,
Delay: 3 * time.Second,
}
// Wait, catching any errors
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf(errorPrivateEndpointRegionalModeUpdate, projectID, err)
Expand All @@ -126,9 +127,9 @@ func resourceMongoDBAtlasPrivateEndpointRegionalModeUpdate(ctx context.Context,
return nil
}

func resourceMongoDBAtlasPrivateEndpointRegionalModeDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
if err := d.Set("enabled", false); err == nil {
resourceMongoDBAtlasPrivateEndpointRegionalModeUpdate(context.WithValue(ctx, regionalModeTimeoutCtxKey, schema.TimeoutDelete), d, meta)
resourceUpdate(context.WithValue(ctx, regionalModeTimeoutCtxKey, schema.TimeoutDelete), d, meta)
} else {
log.Printf(errorPrivateEndpointRegionalModeSetting, "enabled", d.Id(), err)
}
Expand All @@ -138,11 +139,11 @@ func resourceMongoDBAtlasPrivateEndpointRegionalModeDelete(ctx context.Context,
return nil
}

func resourceMongoDBAtlasPrivateEndpointRegionalModeImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
conn := meta.(*config.MongoDBClient).Atlas
func resourceImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
conn := meta.(*config.MongoDBClient).AtlasV2
projectID := d.Id()

setting, _, err := conn.PrivateEndpoints.GetRegionalizedPrivateEndpointSetting(ctx, projectID)
setting, _, err := conn.PrivateEndpointServicesApi.GetRegionalizedPrivateEndpointSetting(ctx, projectID).Execute()
if err != nil {
return nil, fmt.Errorf("couldn't import Private Endpoint Regional Mode for project %s error: %s", projectID, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAccPrivateEndpointRegionalMode_conn(t *testing.T) {
clusterResourceName = "test"
clusterResource = acc.ConfigClusterGlobal(orgID, projectName, clusterName)
clusterDataSource = modeClusterData(clusterResourceName, resourceSuffix, endpointResourceSuffix)
endpointResources = testAccMongoDBAtlasPrivateLinkEndpointServiceConfigUnmanagedAWS(
endpointResources = testConfigUnmanagedAWS(
awsAccessKey, awsSecretKey, projectID, providerName, region, endpointResourceSuffix,
)
dependencies = []string{clusterResource, clusterDataSource, endpointResources}
Expand Down Expand Up @@ -218,7 +218,7 @@ func checkDestroy(s *terraform.State) error {
return nil
}

func testAccMongoDBAtlasPrivateLinkEndpointServiceConfigUnmanagedAWS(awsAccessKey, awsSecretKey, projectID, providerName, region, serviceResourceName string) string {
func testConfigUnmanagedAWS(awsAccessKey, awsSecretKey, projectID, providerName, region, serviceResourceName string) string {
return fmt.Sprintf(`
provider "aws" {
region = "%[5]s"
Expand Down