Skip to content

Commit

Permalink
switch autoscaling struct fields to pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinzeng committed Feb 14, 2024
1 parent 58c034f commit 52acfd3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
12 changes: 6 additions & 6 deletions clickhouse/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down
44 changes: 32 additions & 12 deletions clickhouse/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 52acfd3

Please sign in to comment.