From cdef7f9441827fb3bbf8e4b434c72b5fee241fd3 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Tue, 13 Feb 2024 19:04:32 -0500 Subject: [PATCH 1/3] remove omitempty from non-computed fields --- clickhouse/client.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clickhouse/client.go b/clickhouse/client.go index bea97133..cdb2b0d6 100644 --- a/clickhouse/client.go +++ b/clickhouse/client.go @@ -68,15 +68,15 @@ type ServicePrivateEndpointConfig struct { type Service struct { Id string `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Provider string `json:"provider,omitempty"` - Region string `json:"region,omitempty"` - Tier string `json:"tier,omitempty"` - IdleScaling bool `json:"idleScaling,omitempty"` + Name string `json:"name"` + Provider string `json:"provider"` + Region string `json:"region"` + Tier string `json:"tier"` + IdleScaling bool `json:"idleScaling"` IpAccessList []IpAccess `json:"ipAccessList"` - MinTotalMemoryGb int `json:"minTotalMemoryGb,omitempty"` - MaxTotalMemoryGb int `json:"maxTotalMemoryGb,omitempty"` - IdleTimeoutMinutes int `json:"idleTimeoutMinutes,omitempty"` + MinTotalMemoryGb int `json:"minTotalMemoryGb"` + MaxTotalMemoryGb int `json:"maxTotalMemoryGb"` + IdleTimeoutMinutes int `json:"idleTimeoutMinutes"` State string `json:"state,omitempty"` Endpoints []Endpoint `json:"endpoints,omitempty"` IAMRole string `json:"iamRole,omitempty"` From 58c034ff6ead1f9abdf1278d24eac0b570df5077 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Tue, 13 Feb 2024 20:56:17 -0500 Subject: [PATCH 2/3] allow min_total_memory_gb, max_total_memory_gb, idle_timeout_minutes to be null if idle_scaling is not enabled --- clickhouse/service.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/clickhouse/service.go b/clickhouse/service.go index c6b63588..2e02f29b 100644 --- a/clickhouse/service.go +++ b/clickhouse/service.go @@ -245,10 +245,10 @@ func (r *ServiceResource) Create(ctx context.Context, req resource.CreateRequest return } } else if service.Tier == "production" { - if plan.IdleScaling.IsNull() || plan.MinTotalMemoryGb.IsNull() || plan.MaxTotalMemoryGb.IsNull() || plan.IdleTimeoutMinutes.IsNull() { + if plan.IdleScaling.ValueBool() && (plan.IdleScaling.IsNull() || plan.MinTotalMemoryGb.IsNull() || plan.MaxTotalMemoryGb.IsNull() || plan.IdleTimeoutMinutes.IsNull()) { resp.Diagnostics.AddError( "Invalid Configuration", - "idle_scaling, min_total_memory_gb, max_total_memory_gb, and idle_timeout_minutes must be defined if the service tier is production", + "idle_scaling, min_total_memory_gb, max_total_memory_gb, and idle_timeout_minutes must be defined if the service tier is production and idle_scaling is enabled", ) return } @@ -382,9 +382,16 @@ func (r *ServiceResource) Create(ctx context.Context, req resource.CreateRequest if s.Tier == "production" { plan.IdleScaling = types.BoolValue(s.IdleScaling) - plan.MinTotalMemoryGb = types.Int64Value(int64(s.MinTotalMemoryGb)) - plan.MaxTotalMemoryGb = types.Int64Value(int64(s.MaxTotalMemoryGb)) - plan.IdleTimeoutMinutes = types.Int64Value(int64(s.IdleTimeoutMinutes)) + + if !plan.MinTotalMemoryGb.IsNull() { + plan.MinTotalMemoryGb = types.Int64Value(int64(s.MinTotalMemoryGb)) + } + if !plan.MaxTotalMemoryGb.IsNull() { + plan.MaxTotalMemoryGb = types.Int64Value(int64(s.MaxTotalMemoryGb)) + } + if !plan.IdleTimeoutMinutes.IsNull() { + plan.IdleTimeoutMinutes = types.Int64Value(int64(s.IdleTimeoutMinutes)) + } } for ipAccessIndex, ipAccess := range s.IpAccessList { @@ -598,10 +605,10 @@ func (r *ServiceResource) Update(ctx context.Context, req resource.UpdateRequest return } } else if config.Tier.ValueString() == "production" { - if plan.IdleScaling.IsNull() || plan.MinTotalMemoryGb.IsNull() || plan.MaxTotalMemoryGb.IsNull() || plan.IdleTimeoutMinutes.IsNull() { + if plan.IdleScaling.ValueBool() && (plan.IdleScaling.IsNull() || plan.MinTotalMemoryGb.IsNull() || plan.MaxTotalMemoryGb.IsNull() || plan.IdleTimeoutMinutes.IsNull()) { resp.Diagnostics.AddError( "Invalid Configuration", - "idle_scaling, min_total_memory_gb, max_total_memory_gb, and idle_timeout_minutes must be defined if the service tier is production", + "idle_scaling, min_total_memory_gb, max_total_memory_gb, and idle_timeout_minutes must be defined if the service tier is production and idle_scaling is enabled", ) return } @@ -785,9 +792,15 @@ func (r *ServiceResource) Update(ctx context.Context, req resource.UpdateRequest if s.Tier == "production" { plan.IdleScaling = types.BoolValue(s.IdleScaling) - plan.MinTotalMemoryGb = types.Int64Value(int64(s.MinTotalMemoryGb)) - plan.MaxTotalMemoryGb = types.Int64Value(int64(s.MaxTotalMemoryGb)) - plan.IdleTimeoutMinutes = types.Int64Value(int64(s.IdleTimeoutMinutes)) + if !plan.MinTotalMemoryGb.IsNull() { + plan.MinTotalMemoryGb = types.Int64Value(int64(s.MinTotalMemoryGb)) + } + if !plan.MaxTotalMemoryGb.IsNull() { + plan.MaxTotalMemoryGb = types.Int64Value(int64(s.MaxTotalMemoryGb)) + } + if !plan.IdleTimeoutMinutes.IsNull() { + plan.IdleTimeoutMinutes = types.Int64Value(int64(s.IdleTimeoutMinutes)) + } } for ipAccessIndex, ipAccess := range s.IpAccessList { From 52acfd3244b6779e0edb12ba18f1d5cf9e322072 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Wed, 14 Feb 2024 15:42:00 -0500 Subject: [PATCH 3/3] switch autoscaling struct fields to pointers --- clickhouse/client.go | 12 ++++++------ clickhouse/service.go | 44 +++++++++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/clickhouse/client.go b/clickhouse/client.go index cdb2b0d6..a4767451 100644 --- a/clickhouse/client.go +++ b/clickhouse/client.go @@ -74,9 +74,9 @@ type Service struct { Tier string `json:"tier"` IdleScaling bool `json:"idleScaling"` IpAccessList []IpAccess `json:"ipAccessList"` - MinTotalMemoryGb int `json:"minTotalMemoryGb"` - MaxTotalMemoryGb int `json:"maxTotalMemoryGb"` - IdleTimeoutMinutes int `json:"idleTimeoutMinutes"` + MinTotalMemoryGb *int `json:"minTotalMemoryGb,omitempty"` + MaxTotalMemoryGb *int `json:"maxTotalMemoryGb,omitempty"` + IdleTimeoutMinutes *int `json:"idleTimeoutMinutes,omitempty"` State string `json:"state,omitempty"` Endpoints []Endpoint `json:"endpoints,omitempty"` IAMRole string `json:"iamRole,omitempty"` @@ -92,9 +92,9 @@ type ServiceUpdate struct { type ServiceScalingUpdate struct { IdleScaling *bool `json:"idleScaling,omitempty"` // bool pointer so that `false`` is not omitted - MinTotalMemoryGb int `json:"minTotalMemoryGb,omitempty"` - MaxTotalMemoryGb int `json:"maxTotalMemoryGb,omitempty"` - IdleTimeoutMinutes int `json:"idleTimeoutMinutes,omitempty"` + MinTotalMemoryGb *int `json:"minTotalMemoryGb,omitempty"` + MaxTotalMemoryGb *int `json:"maxTotalMemoryGb,omitempty"` + IdleTimeoutMinutes *int `json:"idleTimeoutMinutes,omitempty"` } type ServicePasswordUpdate struct { diff --git a/clickhouse/service.go b/clickhouse/service.go index 2e02f29b..c10fbae9 100644 --- a/clickhouse/service.go +++ b/clickhouse/service.go @@ -253,10 +253,21 @@ func (r *ServiceResource) Create(ctx context.Context, req resource.CreateRequest return } + service.IdleScaling = bool(plan.IdleScaling.ValueBool()) - service.MinTotalMemoryGb = int(plan.MinTotalMemoryGb.ValueInt64()) - service.MaxTotalMemoryGb = int(plan.MaxTotalMemoryGb.ValueInt64()) - service.IdleTimeoutMinutes = int(plan.IdleTimeoutMinutes.ValueInt64()) + + if !plan.MinTotalMemoryGb.IsNull() { + minTotalMemoryGb := int(plan.MinTotalMemoryGb.ValueInt64()) + service.MinTotalMemoryGb = &minTotalMemoryGb + } + if !plan.MaxTotalMemoryGb.IsNull() { + maxTotalMemoryGb := int(plan.MaxTotalMemoryGb.ValueInt64()) + service.MaxTotalMemoryGb = &maxTotalMemoryGb + } + if !plan.IdleTimeoutMinutes.IsNull() { + idleTimeoutMinutes := int(plan.IdleTimeoutMinutes.ValueInt64()) + service.IdleTimeoutMinutes = &idleTimeoutMinutes + } } if !plan.Password.IsNull() && !plan.PasswordHash.IsNull() { @@ -384,13 +395,13 @@ func (r *ServiceResource) Create(ctx context.Context, req resource.CreateRequest plan.IdleScaling = types.BoolValue(s.IdleScaling) if !plan.MinTotalMemoryGb.IsNull() { - plan.MinTotalMemoryGb = types.Int64Value(int64(s.MinTotalMemoryGb)) + plan.MinTotalMemoryGb = types.Int64Value(int64(*s.MinTotalMemoryGb)) } if !plan.MaxTotalMemoryGb.IsNull() { - plan.MaxTotalMemoryGb = types.Int64Value(int64(s.MaxTotalMemoryGb)) + plan.MaxTotalMemoryGb = types.Int64Value(int64(*s.MaxTotalMemoryGb)) } if !plan.IdleTimeoutMinutes.IsNull() { - plan.IdleTimeoutMinutes = types.Int64Value(int64(s.IdleTimeoutMinutes)) + plan.IdleTimeoutMinutes = types.Int64Value(int64(*s.IdleTimeoutMinutes)) } } @@ -713,15 +724,24 @@ func (r *ServiceResource) Update(ctx context.Context, req resource.UpdateRequest } if plan.MinTotalMemoryGb != state.MinTotalMemoryGb { scalingChange = true - serviceScaling.MinTotalMemoryGb = int(plan.MinTotalMemoryGb.ValueInt64()) + if !plan.MinTotalMemoryGb.IsNull() { + minTotalMemoryGb := int(plan.MinTotalMemoryGb.ValueInt64()) + serviceScaling.MinTotalMemoryGb = &minTotalMemoryGb + } } if plan.MaxTotalMemoryGb != state.MaxTotalMemoryGb { scalingChange = true - serviceScaling.MaxTotalMemoryGb = int(plan.MaxTotalMemoryGb.ValueInt64()) + if !plan.MaxTotalMemoryGb.IsNull() { + maxTotalMemoryGb := int(plan.MaxTotalMemoryGb.ValueInt64()) + serviceScaling.MaxTotalMemoryGb = &maxTotalMemoryGb + } } if plan.IdleTimeoutMinutes != state.IdleTimeoutMinutes { scalingChange = true - serviceScaling.IdleTimeoutMinutes = int(plan.IdleTimeoutMinutes.ValueInt64()) + if !plan.IdleTimeoutMinutes.IsNull() { + idleTimeoutMinutes := int(plan.IdleTimeoutMinutes.ValueInt64()) + serviceScaling.IdleTimeoutMinutes = &idleTimeoutMinutes + } } if scalingChange { @@ -793,13 +813,13 @@ func (r *ServiceResource) Update(ctx context.Context, req resource.UpdateRequest if s.Tier == "production" { plan.IdleScaling = types.BoolValue(s.IdleScaling) if !plan.MinTotalMemoryGb.IsNull() { - plan.MinTotalMemoryGb = types.Int64Value(int64(s.MinTotalMemoryGb)) + plan.MinTotalMemoryGb = types.Int64Value(int64(*s.MinTotalMemoryGb)) } if !plan.MaxTotalMemoryGb.IsNull() { - plan.MaxTotalMemoryGb = types.Int64Value(int64(s.MaxTotalMemoryGb)) + plan.MaxTotalMemoryGb = types.Int64Value(int64(*s.MaxTotalMemoryGb)) } if !plan.IdleTimeoutMinutes.IsNull() { - plan.IdleTimeoutMinutes = types.Int64Value(int64(s.IdleTimeoutMinutes)) + plan.IdleTimeoutMinutes = types.Int64Value(int64(*s.IdleTimeoutMinutes)) } }