Skip to content

Commit

Permalink
fix(MongoDB): Update errors and doc typo (#421)
Browse files Browse the repository at this point in the history
* docs(mongodb): Fix mongodb_image_products docs typo

* fix(mongodb): Change the setting condition of arbiter_product_code

* fix(mongodb): Fix resource update errors

* review(mongodb): Refactoring function and comment
  • Loading branch information
youngmn committed Apr 10, 2024
1 parent e726989 commit c18318d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 28 deletions.
8 changes: 5 additions & 3 deletions docs/data-sources/mongodb_image_products.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ data "ncloud_mongodb_image_products" "example" {
Outputs:
```terraform
image_list = {
"MongoDB 4.4.18 Enterprise Edition": "SW.VMGDB.LNX64.CNTOS.0708.MNGDB.4418.EE.B050",
"MongoDB 4.4.18 Community Edition": "SW.VMGDB.LNX64.CNTOS.0708.MNGDB.4418.CE.B050",
"MongoDB 5.0.19 Enterprise Edition": "SW.VMGDB.LNX64.CNTOS.0708.MNGDB.5019.EE.B050",
"MongoDB 5.0.19 Community Edition": "SW.VMGDB.LNX64.CNTOS.0708.MNGDB.4418.CE.B050"
"MongoDB 4.4.18 Enterprise Edition": "SW.VMGDB.LNX64.CNTOS.0708.MNGDB.4418.EE.B050",
"MongoDB 4.4.25 Community Edition": "SW.VMGDB.LNX64.CNTOS.0708.MNGDB.4425.CE.B050",
"MongoDB 4.4.25 Enterprise Edition": "SW.VMGDB.LNX64.CNTOS.0708.MNGDB.4425.EE.B050",
"MongoDB 5.0.19 Community Edition": "SW.VMGDB.LNX64.CNTOS.0708.MNGDB.5019.CE.B050",
"MongoDB 5.0.19 Enterprise Edition": "SW.VMGDB.LNX64.CNTOS.0708.MNGDB.5019.EE.B050"
}
```

Expand Down
23 changes: 23 additions & 0 deletions internal/common/convert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"strings"

"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-ncloud/internal/conn"
Expand Down Expand Up @@ -237,9 +238,31 @@ func ExpandStringList(configured []interface{}) []*string {
return vs
}

// Int64ValueFromInt32 converts an int32 pointer to a Framework Int64 value.
// A nil int32 pointer is converted to a null Int64.
func Int64ValueFromInt32(value *int32) basetypes.Int64Value {
if value == nil {
return basetypes.NewInt64Null()
}
return basetypes.NewInt64Value(int64(*value))
}

// Int64FromInt32OrDefault converts an int32 pointer to a Framework Int64 value.
// A nil int32 pointer is converted to a zero Int64.
// Used when the optional and computed attribute have no response value
func Int64FromInt32OrDefault(value *int32) basetypes.Int64Value {
if value == nil {
return basetypes.NewInt64Value(0)
}
return basetypes.NewInt64Value(int64(*value))
}

// StringFrameworkOrDefault converts a Framework StringValue struct to a same Framework StringValue.
// A null or unknown state is converted to a default(not aloocated) string.
// Used when the optional and computed attribute have no response value
func StringFrameworkOrDefault(value types.String) basetypes.StringValue {
if value.IsNull() || value.IsUnknown() {
return types.StringValue("not allocated")
}
return value
}
110 changes: 85 additions & 25 deletions internal/service/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,33 @@ func (m *mongodbResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},
"member_product_code": schema.StringAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
stringplanmodifier.RequiresReplace(),
},
},
"arbiter_product_code": schema.StringAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.RequiresReplaceIfConfigured(),
},
},
"mongos_product_code": schema.StringAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
stringplanmodifier.RequiresReplaceIfConfigured(),
},
},
"config_product_code": schema.StringAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
stringplanmodifier.RequiresReplaceIfConfigured(),
},
},
"shard_count": schema.Int64Attribute{
Expand All @@ -177,24 +184,40 @@ func (m *mongodbResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},
"member_server_count": schema.Int64Attribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{
int64validator.Between(2, 7),
},
},
"arbiter_server_count": schema.Int64Attribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{
int64validator.Between(0, 1),
},
},
"mongos_server_count": schema.Int64Attribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{
int64validator.Between(2, 5),
},
},
"config_server_count": schema.Int64Attribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
Validators: []validator.Int64{
int64validator.Between(3, 7),
},
Expand Down Expand Up @@ -237,7 +260,7 @@ func (m *mongodbResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
int64planmodifier.RequiresReplace(),
int64planmodifier.RequiresReplaceIfConfigured(),
},
Validators: []validator.Int64{
int64validator.Any(
Expand All @@ -247,16 +270,13 @@ func (m *mongodbResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},
"arbiter_port": schema.Int64Attribute{
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
"mongos_port": schema.Int64Attribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
int64planmodifier.RequiresReplace(),
int64planmodifier.RequiresReplaceIfConfigured(),
},
Validators: []validator.Int64{
int64validator.Any(
Expand All @@ -269,7 +289,7 @@ func (m *mongodbResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
int64planmodifier.RequiresReplace(),
int64planmodifier.RequiresReplaceIfConfigured(),
},
Validators: []validator.Int64{
int64validator.Any(
Expand Down Expand Up @@ -418,10 +438,10 @@ func (m *mongodbResource) Create(ctx context.Context, req resource.CreateRequest
}

if !plan.ArbiterProductCode.IsNull() && !plan.ArbiterProductCode.IsUnknown() {
if *reqParams.ClusterTypeCode != "SHARDED_CLUSTER" {
if *reqParams.ClusterTypeCode == "STAND_ALONE" {
resp.Diagnostics.AddError(
"CREATING ERROR",
"`arbiter_product_code` invalid. Necessary only if the cluster_type_code is SHARDED_CLUSTER.",
"`arbiter_product_code` invalid. Necessary only if the cluster_type_code is SINGLE_REPLICA_SET or SHARDED_CLUSTER.",
)
return
}
Expand Down Expand Up @@ -636,7 +656,7 @@ func (m *mongodbResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

plan.refreshFromOutput(ctx, output)
state.refreshFromOutput(ctx, output)
}

if !plan.MongosServerCount.Equal(state.MongosServerCount) {
Expand Down Expand Up @@ -667,7 +687,7 @@ func (m *mongodbResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

plan.refreshFromOutput(ctx, output)
state.refreshFromOutput(ctx, output)
}

if !plan.MemberServerCount.Equal(state.MemberServerCount) ||
Expand Down Expand Up @@ -700,7 +720,7 @@ func (m *mongodbResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

plan.refreshFromOutput(ctx, output)
state.refreshFromOutput(ctx, output)
}

if !plan.ShardCount.Equal(state.ShardCount) {
Expand Down Expand Up @@ -731,10 +751,10 @@ func (m *mongodbResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

plan.refreshFromOutput(ctx, output)
state.refreshFromOutput(ctx, output)
}

resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}

func (m *mongodbResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
Expand Down Expand Up @@ -984,31 +1004,34 @@ func (m *mongodbResourceModel) refreshFromOutput(ctx context.Context, output *vm
m.ServiceName = types.StringPointerValue(output.CloudMongoDbServiceName)
m.VpcNo = types.StringPointerValue(output.CloudMongoDbServerInstanceList[0].VpcNo)
m.SubnetNo = types.StringPointerValue(output.CloudMongoDbServerInstanceList[0].SubnetNo)
m.ClusterTypeCode = types.StringPointerValue(output.ClusterType.Code)
m.ImageProductCode = types.StringPointerValue(output.CloudMongoDbImageProductCode)
m.ShardCount = common.Int64ValueFromInt32(output.ShardCount)
m.BackupFileRetentionPeriod = common.Int64ValueFromInt32(output.BackupFileRetentionPeriod)
m.BackupTime = types.StringPointerValue(output.BackupTime)
m.ArbiterPort = common.Int64ValueFromInt32(output.ArbiterPort)
m.MemberPort = common.Int64ValueFromInt32(output.MemberPort)
m.MongosPort = common.Int64ValueFromInt32(output.MongosPort)
m.ConfigPort = common.Int64ValueFromInt32(output.ConfigPort)
m.DataStorageType = types.StringPointerValue(output.CloudMongoDbServerInstanceList[0].DataStorageType.Code)
m.CompressCode = types.StringPointerValue(output.Compress.Code)
m.ArbiterPort = common.Int64FromInt32OrDefault(output.ArbiterPort)
m.MemberPort = common.Int64FromInt32OrDefault(output.MemberPort)
m.MongosPort = common.Int64FromInt32OrDefault(output.MongosPort)
m.ConfigPort = common.Int64FromInt32OrDefault(output.ConfigPort)
m.EngineVersion = types.StringPointerValue(output.EngineVersion)
m.RegionCode = types.StringPointerValue(output.CloudMongoDbServerInstanceList[0].RegionCode)
m.ZoneCode = types.StringPointerValue(output.CloudMongoDbServerInstanceList[0].ZoneCode)

if output.CloudMongoDbServerInstanceList[0].DataStorageType != nil {
m.DataStorageType = types.StringPointerValue(output.CloudMongoDbServerInstanceList[0].DataStorageType.Code)
}
if output.Compress != nil {
m.CompressCode = types.StringPointerValue(output.Compress.Code)
}

acgList, _ := types.ListValueFrom(ctx, types.StringType, output.AccessControlGroupNoList)
m.AccessControlGroupNoList = acgList

var memberCount, arbiterCount, mongosCount, configCount int64
var serverList []mongoServer
for _, server := range output.CloudMongoDbServerInstanceList {
mongoServerInstance := mongoServer{
ServerNo: types.StringPointerValue(server.CloudMongoDbServerInstanceNo),
ServerName: types.StringPointerValue(server.CloudMongoDbServerName),
ServerRole: types.StringPointerValue(server.CloudMongoDbServerRole.CodeName),
ClusterRole: types.StringPointerValue(server.ClusterRole.Code),
ProductCode: types.StringPointerValue(server.CloudMongoDbProductCode),
PrivateDomain: types.StringPointerValue(server.PrivateDomain),
PublicDomain: types.StringPointerValue(server.PublicDomain),
Expand All @@ -1019,9 +1042,46 @@ func (m *mongodbResourceModel) refreshFromOutput(ctx context.Context, output *vm
Uptime: types.StringPointerValue(server.Uptime),
CreateDate: types.StringPointerValue(server.CreateDate),
}

if server.CloudMongoDbServerRole != nil {
mongoServerInstance.ServerRole = types.StringPointerValue(server.CloudMongoDbServerRole.CodeName)
if *server.CloudMongoDbServerRole.Code == "A" || *server.CloudMongoDbServerRole.Code == "MB" {
m.MemberProductCode = types.StringPointerValue(server.CloudMongoDbProductCode)
memberCount++
} else if *server.CloudMongoDbServerRole.Code == "AB" {
m.ArbiterProductCode = types.StringPointerValue(server.CloudMongoDbProductCode)
arbiterCount++
} else if *server.CloudMongoDbServerRole.Code == "RT" {
m.MongosProductCode = types.StringPointerValue(server.CloudMongoDbProductCode)
mongosCount++
} else if *server.CloudMongoDbServerRole.Code == "C" {
m.ConfigProductCode = types.StringPointerValue(server.CloudMongoDbProductCode)
configCount++
}
}
if server.ClusterRole != nil {
mongoServerInstance.ClusterRole = types.StringPointerValue(server.ClusterRole.Code)
}
serverList = append(serverList, mongoServerInstance)
}

if output.ClusterType != nil {
m.ClusterTypeCode = types.StringPointerValue(output.ClusterType.Code)
if *output.ClusterType.Code == "SHARDED_CLUSTER" {
memberCount = memberCount / int64(*output.ShardCount)
if arbiterCount > 0 {
arbiterCount = arbiterCount / int64(*output.ShardCount)
}
}
}
m.MemberServerCount = types.Int64Value(memberCount)
m.ArbiterServerCount = types.Int64Value(arbiterCount)
m.MongosServerCount = types.Int64Value(mongosCount)
m.ConfigServerCount = types.Int64Value(configCount)
m.ArbiterProductCode = common.StringFrameworkOrDefault(m.ArbiterProductCode)
m.MongosProductCode = common.StringFrameworkOrDefault(m.MongosProductCode)
m.ConfigProductCode = common.StringFrameworkOrDefault(m.ConfigProductCode)

mongoServers, _ := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: mongoServer{}.attrTypes()}, serverList)

m.MongoDbServerList = mongoServers
Expand Down

0 comments on commit c18318d

Please sign in to comment.