Skip to content

Commit

Permalink
feat: Support isSchemaUpgrade to handle legacy to new schema
Browse files Browse the repository at this point in the history
  • Loading branch information
EspenAlbert committed Dec 16, 2024
1 parent 27ef1c0 commit 5831f92
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
12 changes: 12 additions & 0 deletions internal/common/conversion/error_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"

"github.com/hashicorp/terraform-plugin-framework/diag"

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

type ErrBody interface {
Expand Down Expand Up @@ -32,3 +34,13 @@ func AddJSONBodyErrorToDiagnostics(msgPrefix string, err error, diags *diag.Diag
errorJSON := string(errorBytes)
diags.AddError(msgPrefix, errorJSON)
}

func AddLegacyDiags(diags *diag.Diagnostics, legacyDiags legacyDiag.Diagnostics) {
for _, diag := range legacyDiags {
if diag.Severity == legacyDiag.Error {
diags.AddError(diag.Summary, diag.Detail)
} else {
diags.AddWarning(diag.Summary, diag.Detail)
}
}
}
20 changes: 16 additions & 4 deletions internal/service/advancedclustertpf/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,24 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou
if diags.HasError() {
return
}
if usingLegacySchema(ctx, plan.ReplicationSpecs, diags) && !usingLegacySchema(ctx, state.ReplicationSpecs, diags) {
stateUsingLegacy := usingLegacySchema(ctx, state.ReplicationSpecs, diags)
planUsingLegacy := usingLegacySchema(ctx, plan.ReplicationSpecs, diags)
if planUsingLegacy && !stateUsingLegacy {
diags.AddError("error operation not permitted, nums_shards from 1 -> > 1", fmt.Sprintf("cannot increase num_shards to > 1 under the current configuration. New shards can be defined by adding new replication spec objects; %s", DeprecationOldSchemaAction))
return
}
stateReq := normalizeFromTFModel(ctx, &state, diags, false)
planReq := normalizeFromTFModel(ctx, &plan, diags, false)
isSchemaUpgrade := stateUsingLegacy && !planUsingLegacy
stateReq := normalizeFromTFModel(ctx, &state, diags, isSchemaUpgrade)
planReq := normalizeFromTFModel(ctx, &plan, diags, isSchemaUpgrade)
if diags.HasError() {
return
}
patchOptions := update.PatchOptions{
IgnoreInStatePrefix: []string{"regionConfigs"},
IncludeInStateSuffix: []string{"diskIOPS"},
}
if findNumShardsUpdates(ctx, &state, &plan, diags) != nil {
if isSchemaUpgrade || findNumShardsUpdates(ctx, &state, &plan, diags) != nil {
// isSchemaUpgrade will have no changes by default after flattening; therefore, force update the replicationSpecs
// `num_shards` updates is only in the legacy ClusterDescription; therefore, force update the replicationSpecs
patchOptions.ForceUpdateAttr = append(patchOptions.ForceUpdateAttr, "replicationSpecs")
}
Expand All @@ -130,6 +134,14 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou
if upgradeRequest != nil {
clusterResp = r.applyTenantUpgrade(ctx, &plan, upgradeRequest, diags)
} else {
if isSchemaUpgrade {
specs, localDiags := populateIDValuesUsingNewAPI(ctx, plan.ProjectID.ValueString(), plan.Name.ValueString(), r.Client.AtlasV2.ClustersApi, patchReq.ReplicationSpecs)
conversion.AddLegacyDiags(diags, localDiags)
if diags.HasError() {
return
}
patchReq.ReplicationSpecs = specs
}
clusterResp = r.applyClusterChanges(ctx, diags, &state, &plan, patchReq)
}
if diags.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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/service/advancedcluster"
"github.com/spf13/cast"
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
"go.mongodb.org/atlas-sdk/v20241113003/admin"
Expand Down Expand Up @@ -168,3 +170,27 @@ func convertDedicatedHardwareSpecToOldSDK(spec *admin.DedicatedHardwareSpec20240
InstanceSize: spec.InstanceSize,
}
}

// copied from advancedcluster/resource_update_logic.go
func populateIDValuesUsingNewAPI(ctx context.Context, projectID, clusterName string, connV2ClusterAPI admin.ClustersApi, replicationSpecs *[]admin.ReplicationSpec20240805) (*[]admin.ReplicationSpec20240805, diag.Diagnostics) {
if replicationSpecs == nil || len(*replicationSpecs) == 0 {
return replicationSpecs, nil
}
cluster, _, err := connV2ClusterAPI.GetCluster(ctx, projectID, clusterName).Execute()
if err != nil {
return nil, diag.FromErr(fmt.Errorf(errorRead, clusterName, err))
}

zoneToReplicationSpecsIDs := groupIDsByZone(cluster.GetReplicationSpecs())
result := advancedcluster.AddIDsToReplicationSpecs(*replicationSpecs, zoneToReplicationSpecsIDs)
return &result, nil
}

// copied from advancedcluster/resource_update_logic.go
func groupIDsByZone(specs []admin.ReplicationSpec20240805) map[string][]string {
result := make(map[string][]string)
for _, spec := range specs {
result[spec.GetZoneName()] = append(result[spec.GetZoneName()], spec.GetId())
}
return result
}

0 comments on commit 5831f92

Please sign in to comment.