From f27c8ea7ea00c8b8c18dca964243ac3b5b37697a Mon Sep 17 00:00:00 2001 From: Krista LaFentres Date: Wed, 15 Nov 2023 09:48:22 -0600 Subject: [PATCH] type helpers: migrate to internal and replace usages in r/slo --- honeycombio/resource_slo.go | 5 ++-- honeycombio/type_helpers.go | 15 ----------- honeycombio/type_helpers_test.go | 43 -------------------------------- 3 files changed, 3 insertions(+), 60 deletions(-) diff --git a/honeycombio/resource_slo.go b/honeycombio/resource_slo.go index 0a7dc7d4..8530abac 100644 --- a/honeycombio/resource_slo.go +++ b/honeycombio/resource_slo.go @@ -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 { @@ -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 @@ -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 diff --git a/honeycombio/type_helpers.go b/honeycombio/type_helpers.go index fda56210..5fdf2a31 100644 --- a/honeycombio/type_helpers.go +++ b/honeycombio/type_helpers.go @@ -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(), diff --git a/honeycombio/type_helpers_test.go b/honeycombio/type_helpers_test.go index bea7a3c4..f8e43f17 100644 --- a/honeycombio/type_helpers_test.go +++ b/honeycombio/type_helpers_test.go @@ -1,7 +1,6 @@ package honeycombio import ( - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -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)) - }) - } -}