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: Moves bi_connector_config and advanced_configuration in TPF to blocks #2909

Merged
merged 13 commits into from
Dec 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,26 @@ func NewTFModel(ctx context.Context, input *admin.ClusterDescription20240805, ti
}
}

func NewBiConnectorConfigObjType(ctx context.Context, input *admin.BiConnector, diags *diag.Diagnostics) types.Object {
func restrictResourceModel(modelOut, modelIn *TFModel) {
EspenAlbert marked this conversation as resolved.
Show resolved Hide resolved
if modelIn.AdvancedConfiguration.IsNull() {
modelOut.AdvancedConfiguration = modelIn.AdvancedConfiguration
}
if modelIn.BiConnectorConfig.IsNull() {
modelOut.BiConnectorConfig = modelIn.BiConnectorConfig
}
}

func NewBiConnectorConfigObjType(ctx context.Context, input *admin.BiConnector, diags *diag.Diagnostics) types.List {
if input == nil {
return types.ObjectNull(BiConnectorConfigObjType.AttrTypes)
return types.ListNull(BiConnectorConfigObjType)
}
tfModel := TFBiConnectorModel{
Enabled: types.BoolValue(conversion.SafeValue(input.Enabled)),
ReadPreference: types.StringValue(conversion.SafeValue(input.ReadPreference)),
}
objType, diagsLocal := types.ObjectValueFrom(ctx, BiConnectorConfigObjType.AttrTypes, tfModel)
listType, diagsLocal := types.ListValueFrom(ctx, BiConnectorConfigObjType, []TFBiConnectorModel{tfModel})
diags.Append(diagsLocal...)
return objType
return listType
}

func NewConnectionStringsObjType(ctx context.Context, input *admin.ClusterConnectionStrings, diags *diag.Diagnostics) types.Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func AddAdvancedConfig(ctx context.Context, tfModel *TFModel, input *admin.Clust
TransactionLifetimeLimitSeconds: types.Int64Value(conversion.SafeValue(input.TransactionLifetimeLimitSeconds)),
}
}
objType, diagsLocal := types.ObjectValueFrom(ctx, AdvancedConfigurationObjType.AttrTypes, advancedConfig)
listType, diagsLocal := types.ListValueFrom(ctx, AdvancedConfigurationObjType, []TFAdvancedConfigurationModel{advancedConfig})
diags.Append(diagsLocal...)
tfModel.AdvancedConfiguration = objType
tfModel.AdvancedConfiguration = listType
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,24 @@ func NewAtlasReq(ctx context.Context, input *TFModel, diags *diag.Diagnostics) *
VersionReleaseSystem: conversion.NilForUnknown(input.VersionReleaseSystem, input.VersionReleaseSystem.ValueStringPointer()),
}
}
func newBiConnector(ctx context.Context, input types.Object, diags *diag.Diagnostics) *admin.BiConnector {

func newBiConnector(ctx context.Context, input types.List, diags *diag.Diagnostics) *admin.BiConnector {
var resp *admin.BiConnector
if input.IsUnknown() || input.IsNull() {
if input.IsUnknown() || input.IsNull() || len(input.Elements()) == 0 {
return resp
}
item := &TFBiConnectorModel{}
if localDiags := input.As(ctx, item, basetypes.ObjectAsOptions{}); len(localDiags) > 0 {
diags.Append(localDiags...)
return resp
elements := make([]TFBiConnectorModel, len(input.Elements()))
diags.Append(input.ElementsAs(ctx, &elements, false)...)
if diags.HasError() {
return nil
}
item := elements[0]
return &admin.BiConnector{
Enabled: conversion.NilForUnknown(item.Enabled, item.Enabled.ValueBoolPointer()),
ReadPreference: conversion.NilForUnknown(item.ReadPreference, item.ReadPreference.ValueStringPointer()),
}
}

func newComponentLabel(ctx context.Context, input types.Set, diags *diag.Diagnostics) *[]admin.ComponentLabel {
if input.IsUnknown() {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ import (

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"go.mongodb.org/atlas-sdk/v20241113003/admin"
)

func NewAtlasReqAdvancedConfiguration(ctx context.Context, objInput *types.Object, diags *diag.Diagnostics) *admin.ClusterDescriptionProcessArgs20240805 {
func NewAtlasReqAdvancedConfiguration(ctx context.Context, input *types.List, diags *diag.Diagnostics) *admin.ClusterDescriptionProcessArgs20240805 {
var resp *admin.ClusterDescriptionProcessArgs20240805
if objInput == nil || objInput.IsUnknown() || objInput.IsNull() {
if input == nil || input.IsUnknown() || input.IsNull() || len(input.Elements()) == 0 {
return resp
}
input := &TFAdvancedConfigurationModel{}
if localDiags := objInput.As(ctx, input, basetypes.ObjectAsOptions{}); len(localDiags) > 0 {
diags.Append(localDiags...)
return resp
elements := make([]TFAdvancedConfigurationModel, len(input.Elements()))
diags.Append(input.ElementsAs(ctx, &elements, false)...)
if diags.HasError() {
return nil
}
item := elements[0]
return &admin.ClusterDescriptionProcessArgs20240805{
ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds: conversion.NilForUnknown(input.ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds, conversion.Int64PtrToIntPtr(input.ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds.ValueInt64Pointer())),
DefaultWriteConcern: conversion.NilForUnknown(input.DefaultWriteConcern, input.DefaultWriteConcern.ValueStringPointer()),
JavascriptEnabled: conversion.NilForUnknown(input.JavascriptEnabled, input.JavascriptEnabled.ValueBoolPointer()),
MinimumEnabledTlsProtocol: conversion.NilForUnknown(input.MinimumEnabledTlsProtocol, input.MinimumEnabledTlsProtocol.ValueStringPointer()),
NoTableScan: conversion.NilForUnknown(input.NoTableScan, input.NoTableScan.ValueBoolPointer()),
OplogMinRetentionHours: conversion.NilForUnknown(input.OplogMinRetentionHours, input.OplogMinRetentionHours.ValueFloat64Pointer()),
OplogSizeMB: conversion.NilForUnknown(input.OplogSizeMb, conversion.Int64PtrToIntPtr(input.OplogSizeMb.ValueInt64Pointer())),
SampleRefreshIntervalBIConnector: conversion.NilForUnknown(input.SampleRefreshIntervalBiconnector, conversion.Int64PtrToIntPtr(input.SampleRefreshIntervalBiconnector.ValueInt64Pointer())),
SampleSizeBIConnector: conversion.NilForUnknown(input.SampleSizeBiconnector, conversion.Int64PtrToIntPtr(input.SampleSizeBiconnector.ValueInt64Pointer())),
TransactionLifetimeLimitSeconds: conversion.NilForUnknown(input.TransactionLifetimeLimitSeconds, input.TransactionLifetimeLimitSeconds.ValueInt64Pointer()),
ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds: conversion.NilForUnknown(item.ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds, conversion.Int64PtrToIntPtr(item.ChangeStreamOptionsPreAndPostImagesExpireAfterSeconds.ValueInt64Pointer())),
DefaultWriteConcern: conversion.NilForUnknown(item.DefaultWriteConcern, item.DefaultWriteConcern.ValueStringPointer()),
JavascriptEnabled: conversion.NilForUnknown(item.JavascriptEnabled, item.JavascriptEnabled.ValueBoolPointer()),
MinimumEnabledTlsProtocol: conversion.NilForUnknown(item.MinimumEnabledTlsProtocol, item.MinimumEnabledTlsProtocol.ValueStringPointer()),
NoTableScan: conversion.NilForUnknown(item.NoTableScan, item.NoTableScan.ValueBoolPointer()),
OplogMinRetentionHours: conversion.NilForUnknown(item.OplogMinRetentionHours, item.OplogMinRetentionHours.ValueFloat64Pointer()),
OplogSizeMB: conversion.NilForUnknown(item.OplogSizeMb, conversion.Int64PtrToIntPtr(item.OplogSizeMb.ValueInt64Pointer())),
SampleRefreshIntervalBIConnector: conversion.NilForUnknown(item.SampleRefreshIntervalBiconnector, conversion.Int64PtrToIntPtr(item.SampleRefreshIntervalBiconnector.ValueInt64Pointer())),
SampleSizeBIConnector: conversion.NilForUnknown(item.SampleSizeBiconnector, conversion.Int64PtrToIntPtr(item.SampleSizeBiconnector.ValueInt64Pointer())),
TransactionLifetimeLimitSeconds: conversion.NilForUnknown(item.TransactionLifetimeLimitSeconds, item.TransactionLifetimeLimitSeconds.ValueInt64Pointer()),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ import (

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
)

func NewAtlasReqAdvancedConfigurationLegacy(ctx context.Context, objInput *types.Object, diags *diag.Diagnostics) *admin20240530.ClusterDescriptionProcessArgs {
func NewAtlasReqAdvancedConfigurationLegacy(ctx context.Context, input *types.List, diags *diag.Diagnostics) *admin20240530.ClusterDescriptionProcessArgs {
var resp *admin20240530.ClusterDescriptionProcessArgs
if objInput == nil || objInput.IsUnknown() || objInput.IsNull() {
if input == nil || input.IsUnknown() || input.IsNull() || len(input.Elements()) == 0 {
EspenAlbert marked this conversation as resolved.
Show resolved Hide resolved
return resp
}
input := &TFAdvancedConfigurationModel{}
if localDiags := objInput.As(ctx, input, basetypes.ObjectAsOptions{}); len(localDiags) > 0 {
diags.Append(localDiags...)
return resp
elements := make([]TFAdvancedConfigurationModel, len(input.Elements()))
diags.Append(input.ElementsAs(ctx, &elements, false)...)
if diags.HasError() {
return nil
}
item := elements[0]

// Choosing to only handle legacy fields in the old API
return &admin20240530.ClusterDescriptionProcessArgs{
DefaultReadConcern: conversion.NilForUnknown(input.DefaultReadConcern, input.DefaultReadConcern.ValueStringPointer()),
FailIndexKeyTooLong: conversion.NilForUnknown(input.FailIndexKeyTooLong, input.FailIndexKeyTooLong.ValueBoolPointer()),
DefaultReadConcern: conversion.NilForUnknown(item.DefaultReadConcern, item.DefaultReadConcern.ValueStringPointer()),
FailIndexKeyTooLong: conversion.NilForUnknown(item.FailIndexKeyTooLong, item.FailIndexKeyTooLong.ValueBoolPointer()),
}
}
3 changes: 3 additions & 0 deletions internal/service/advancedclustertpf/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou
}
model := r.createCluster(ctx, &plan, diags)
if model != nil {
restrictResourceModel(model, &plan)
diags.Append(resp.State.Set(ctx, model)...)
}
}
Expand All @@ -90,6 +91,7 @@ func (r *rs) Read(ctx context.Context, req resource.ReadRequest, resp *resource.
}
model := r.readCluster(ctx, diags, &state, &resp.State)
if model != nil {
restrictResourceModel(model, &state)
diags.Append(resp.State.Set(ctx, model)...)
}
}
Expand Down Expand Up @@ -167,6 +169,7 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou
} else {
modelOut.AdvancedConfiguration = state.AdvancedConfiguration
}
restrictResourceModel(modelOut, &plan)
diags.Append(resp.State.Set(ctx, modelOut)...)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,13 @@ func checksTenantUpgraded(projectID, name string) resource.TestCheckFunc {

func replicasetAdvConfigUpdate(t *testing.T) *resource.TestCase {
t.Helper()
t.Skip() // TODO: Import failing
var (
projectID = acc.ProjectIDExecution(t)
clusterName = acc.RandomClusterName()
fullUpdate = `
backup_enabled = true
bi_connector_config = {
bi_connector_config {
enabled = true
}
# config_server_management_mode = "ATLAS_MANAGED" UNSTABLE: After applying this test step, the non-refresh plan was not empty
Expand All @@ -354,7 +355,7 @@ func replicasetAdvConfigUpdate(t *testing.T) *resource.TestCase {
# termination_protection_enabled = true # must be reset to false to enable delete
version_release_system = "CONTINUOUS"

advanced_configuration = {
advanced_configuration {
change_stream_options_pre_and_post_images_expire_after_seconds = 100
default_read_concern = "available"
default_write_concern = "majority"
Expand Down Expand Up @@ -385,7 +386,7 @@ func replicasetAdvConfigUpdate(t *testing.T) *resource.TestCase {
Config: configBasic(projectID, clusterName, fullUpdate),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "mongo_db_major_version", "8.0"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.change_stream_options_pre_and_post_images_expire_after_seconds", "100"),
resource.TestCheckResourceAttr(resourceName, "advanced_configuration.0.change_stream_options_pre_and_post_images_expire_after_seconds", "100"),
),
},
acc.TestStepImportCluster(resourceName),
Expand Down
Loading
Loading