Skip to content

Commit

Permalink
refactor: rename flattenScaleDown
Browse files Browse the repository at this point in the history
  • Loading branch information
infezek committed Nov 25, 2024
1 parent 84c9fea commit 2d8a2bb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/provider/resource_tsuru_app_autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func resourceTsuruApplicationAutoscaleRead(ctx context.Context, d *schema.Resour

d.Set("schedule", flattenSchedules(autoscale.Schedules))
d.Set("prometheus", flattenPrometheus(autoscale.Prometheus, d))
d.Set("scale_down", newFlattenScaleDown(autoscale.Behavior.ScaleDown, proposed).execute())
d.Set("scale_down", flattenScaleDown(autoscale.Behavior.ScaleDown, proposed))
return nil
}

Expand Down
19 changes: 10 additions & 9 deletions internal/provider/resource_tsuru_app_autoscale_flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
tsuru_client "github.com/tsuru/go-tsuruclient/pkg/tsuru"
)

type flattenScaleDown struct {
type flattenScaleDownBehavior struct {
PERCENTAGE_VALUE int32
PERCENTAGE_LABEL string
STABILIZATION_WINDOW_VALUE int32
Expand All @@ -22,8 +22,8 @@ type flattenScaleDown struct {
Proposed interface{}
}

func newFlattenScaleDown(scaleDownRead tsuru_client.AutoScaleSpecBehaviorScaleDown, proposed interface{}) *flattenScaleDown {
return &flattenScaleDown{
func flattenScaleDown(scaleDownRead tsuru_client.AutoScaleSpecBehaviorScaleDown, proposed interface{}) interface{} {
fsd := &flattenScaleDownBehavior{
PERCENTAGE_VALUE: 10,
PERCENTAGE_LABEL: "percentage",
STABILIZATION_WINDOW_VALUE: 300,
Expand All @@ -33,9 +33,10 @@ func newFlattenScaleDown(scaleDownRead tsuru_client.AutoScaleSpecBehaviorScaleDo
ScaleDownRead: scaleDownRead,
Proposed: proposed,
}
return fsd.execute()
}

func (fsd *flattenScaleDown) execute() interface{} {
func (fsd *flattenScaleDownBehavior) execute() interface{} {
if fsd.ScaleDownRead == (tsuru_client.AutoScaleSpecBehaviorScaleDown{}) {
return nil
}
Expand All @@ -53,7 +54,7 @@ func (fsd *flattenScaleDown) execute() interface{} {
return fsd.withInputParameters(proposedList)
}

func (fsd *flattenScaleDown) withInputParameters(proposedList []map[string]interface{}) (value []map[string]interface{}) {
func (fsd *flattenScaleDownBehavior) withInputParameters(proposedList []map[string]interface{}) (value []map[string]interface{}) {
scaleDownCurrent := []map[string]interface{}{{}}
percentage, ok := fsd.findScaleDownInProposedList(proposedList, fsd.PERCENTAGE_LABEL)
if ok && percentage != 0 || fsd.ScaleDownRead.PercentagePolicyValue != int32(fsd.PERCENTAGE_VALUE) {
Expand All @@ -70,7 +71,7 @@ func (fsd *flattenScaleDown) withInputParameters(proposedList []map[string]inter
return scaleDownCurrent
}

func (fsd *flattenScaleDown) noInputParameters(proposedList []map[string]interface{}) (value interface{}, ok bool) {
func (fsd *flattenScaleDownBehavior) noInputParameters(proposedList []map[string]interface{}) (value interface{}, ok bool) {
if len(proposedList) != 0 {
return nil, false
}
Expand All @@ -90,7 +91,7 @@ func (fsd *flattenScaleDown) noInputParameters(proposedList []map[string]interfa
return scaleDownCurrent, true
}

func (fsd *flattenScaleDown) findScaleDownInProposedList(proposedList []map[string]interface{}, key string) (value int, ok bool) {
func (fsd *flattenScaleDownBehavior) findScaleDownInProposedList(proposedList []map[string]interface{}, key string) (value int, ok bool) {
for _, item := range proposedList {
if v, ok := item[key]; ok {
return v.(int), true
Expand All @@ -99,7 +100,7 @@ func (fsd *flattenScaleDown) findScaleDownInProposedList(proposedList []map[stri
return 0, false
}

func (fsd *flattenScaleDown) convertToMapSlice(input interface{}) ([]map[string]interface{}, error) {
func (fsd *flattenScaleDownBehavior) convertToMapSlice(input interface{}) ([]map[string]interface{}, error) {
var result []map[string]interface{}
if reflect.TypeOf(input).Kind() != reflect.Slice {
return nil, fmt.Errorf("scale down: invalid input type, slice expected")
Expand All @@ -114,7 +115,7 @@ func (fsd *flattenScaleDown) convertToMapSlice(input interface{}) ([]map[string]
return result, nil
}

func (fsd *flattenScaleDown) isScaleDownEmpty(param []map[string]interface{}) bool {
func (fsd *flattenScaleDownBehavior) isScaleDownEmpty(param []map[string]interface{}) bool {
if len(param) != 1 {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestFluentDown(t *testing.T) {
},
}
for _, test := range tests {
readToDiff := newFlattenScaleDown(test.scaleDownRead, test.scaleDownInput).execute()
readToDiff := flattenScaleDown(test.scaleDownRead, test.scaleDownInput)
assert.Equal(test.expected, readToDiff)
}
}

0 comments on commit 2d8a2bb

Please sign in to comment.