Skip to content

Commit

Permalink
type helpers: migrate to internal and replace usages in r/slo
Browse files Browse the repository at this point in the history
  • Loading branch information
lafentres committed Nov 15, 2023
1 parent bccb59c commit f27c8ea
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 60 deletions.
5 changes: 3 additions & 2 deletions honeycombio/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client"
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper"
)

func newSLO() *schema.Resource {
Expand Down Expand Up @@ -117,7 +118,7 @@ func resourceSLORead(ctx context.Context, d *schema.ResourceData, meta interface
d.Set("name", s.Name)
d.Set("description", s.Description)
d.Set("sli", s.SLI.Alias)
d.Set("target_percentage", tpmToFloat(s.TargetPerMillion))
d.Set("target_percentage", helper.PPMToFloat(s.TargetPerMillion))
d.Set("time_period", s.TimePeriodDays)

return nil
Expand Down Expand Up @@ -159,7 +160,7 @@ func expandSLO(d *schema.ResourceData) (*honeycombio.SLO, error) {
Name: d.Get("name").(string),
Description: d.Get("description").(string),
TimePeriodDays: d.Get("time_period").(int),
TargetPerMillion: floatToTPM(d.Get("target_percentage").(float64)),
TargetPerMillion: helper.FloatToPPM(d.Get("target_percentage").(float64)),
SLI: honeycombio.SLIRef{Alias: d.Get("sli").(string)},
}
return s, nil
Expand Down
15 changes: 0 additions & 15 deletions honeycombio/type_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@ func coerceValueToType(i string) interface{} {
return i
}

// The SLO API uses 'Target Per Million' to avoid the problems with floats.
// In the name of offering a nicer UX with percentages, we handle the conversion
// back and fourth to allow things like 99.98 to be provided in the HCL and
// handle the conversion to and from 999800

// converts a floating point percentage to a 'Target Per Million' SLO value
func floatToTPM(f float64) int {
return int(f * 10000)
}

// converts a SLO 'Target Per Million' value to a floating point percentage
func tpmToFloat(t int) float64 {
return float64(t) / 10000
}

func expandRecipient(t honeycombio.RecipientType, d *schema.ResourceData) (*honeycombio.Recipient, error) {
r := &honeycombio.Recipient{
ID: d.Id(),
Expand Down
43 changes: 0 additions & 43 deletions honeycombio/type_helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package honeycombio

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -55,45 +54,3 @@ func Test_coerceValueToType(t *testing.T) {
})
}
}

func Test_floatToTPM(t *testing.T) {
testCases := []struct {
input float64
expected int
}{
{input: 100, expected: 1000000},
{input: 99, expected: 990000},
{input: 9.9999, expected: 99999},
{input: 9, expected: 90000},
{input: 0.1, expected: 1000},
{input: 0.01, expected: 100},
{input: 0.001, expected: 10},
{input: 0.0001, expected: 1},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("returns correct value for input %g", testCase.input), func(t *testing.T) {
assert.Equal(t, testCase.expected, floatToTPM(testCase.input))
})
}
}

func Test_tpmToFloat(t *testing.T) {
testCases := []struct {
input int
expected float64
}{
{input: 1000000, expected: 100},
{input: 990000, expected: 99},
{input: 99999, expected: 9.9999},
{input: 90000, expected: 9},
{input: 1000, expected: 0.1},
{input: 100, expected: 0.01},
{input: 10, expected: 0.001},
{input: 1, expected: 0.0001},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("returns correct value for input %d", testCase.input), func(t *testing.T) {
assert.Equal(t, testCase.expected, tpmToFloat(testCase.input))
})
}
}

0 comments on commit f27c8ea

Please sign in to comment.