From bb53d3d40131fb741c64a3c979c9a2cacf3c7aba Mon Sep 17 00:00:00 2001 From: Jason Harley Date: Wed, 8 Nov 2023 10:19:33 -0500 Subject: [PATCH] maint: prune dead code and tidy up (#387) With the refactors and migrations going on there was some dead code left behind, some code that could be easily be replaced by generics, and some test code that was doing things differently than the more recent stuff. There's likely more to do in this vein, but this was a bit of a time-boxed effort. --- client/column.go | 7 +- client/query_spec.go | 9 +- client/type_helpers_test.go | 6 +- honeycombio/data_source_column_test.go | 8 +- honeycombio/data_source_columns_test.go | 7 +- .../data_source_query_specification.go | 14 +- honeycombio/data_source_recipient_test.go | 8 +- honeycombio/data_source_recipients_test.go | 8 +- honeycombio/data_source_trigger_recipient.go | 4 +- .../data_source_trigger_recipient_test.go | 36 ++- honeycombio/helpers_test.go | 52 ----- honeycombio/resource_board.go | 6 +- honeycombio/resource_column.go | 4 +- honeycombio/resource_query_annotation_test.go | 23 +- honeycombio/resource_recipients_test.go | 20 ++ honeycombio/resource_slo_test.go | 33 ++- honeycombio/type_helpers.go | 214 +----------------- honeycombio/type_helpers_test.go | 7 +- internal/provider/slos_data_source_test.go | 11 +- internal/provider/trigger_resource_test.go | 6 +- 20 files changed, 142 insertions(+), 341 deletions(-) diff --git a/client/column.go b/client/column.go index 52537e3c..6b1b8293 100644 --- a/client/column.go +++ b/client/column.go @@ -76,7 +76,12 @@ const ( // ColumnTypes returns an exhaustive list of column types. func ColumnTypes() []ColumnType { - return []ColumnType{ColumnTypeString, ColumnTypeFloat, ColumnTypeInteger, ColumnTypeBoolean} + return []ColumnType{ + ColumnTypeString, + ColumnTypeFloat, + ColumnTypeInteger, + ColumnTypeBoolean, + } } func (s *columns) List(ctx context.Context, dataset string) ([]Column, error) { diff --git a/client/query_spec.go b/client/query_spec.go index 47c384a2..643cd1c5 100644 --- a/client/query_spec.go +++ b/client/query_spec.go @@ -167,8 +167,14 @@ func (c CalculationOp) IsUnaryOp() bool { return c == CalculationOpCount || c == CalculationOpConcurrency } -// CalculationOps returns an exhaustive list of calculation operators. +// CalculationOps returns an exhaustive list of Calculation Operators. func CalculationOps() []CalculationOp { + return append(HavingCalculationOps(), CalculationOpHeatmap) +} + +// HavingCalculationOps returns an exhaustive list of calculation operators +// supported by Havings. Havings does not support Heatmap. +func HavingCalculationOps() []CalculationOp { return []CalculationOp{ CalculationOpCount, CalculationOpConcurrency, @@ -188,7 +194,6 @@ func CalculationOps() []CalculationOp { CalculationOpP95, CalculationOpP99, CalculationOpP999, - CalculationOpHeatmap, CalculationOpRateAvg, CalculationOpRateSum, CalculationOpRateMax, diff --git a/client/type_helpers_test.go b/client/type_helpers_test.go index b76fb3e6..1c70caa0 100644 --- a/client/type_helpers_test.go +++ b/client/type_helpers_test.go @@ -2,6 +2,8 @@ package client import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestEquivalent(t *testing.T) { @@ -24,9 +26,7 @@ func TestEquivalent(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := Equivalent(tt.a, tt.b); got != tt.want { - t.Errorf("Equivalent() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, Equivalent(tt.a, tt.b)) }) } } diff --git a/honeycombio/data_source_column_test.go b/honeycombio/data_source_column_test.go index 689e7b08..68bae3de 100644 --- a/honeycombio/data_source_column_test.go +++ b/honeycombio/data_source_column_test.go @@ -7,6 +7,8 @@ import ( "strings" "testing" + "github.com/stretchr/testify/require" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -28,12 +30,12 @@ func TestAccDataSourceHoneycombioColumn_basic(t *testing.T) { for i, column := range testColumns { col, err := c.Columns.Create(ctx, dataset, &column) + require.NoError(t, err) // update ID for removal later testColumns[i].ID = col.ID - if err != nil { - t.Error(err) - } + } + //nolint:errcheck t.Cleanup(func() { // remove Columns at the of the test run for _, col := range testColumns { diff --git a/honeycombio/data_source_columns_test.go b/honeycombio/data_source_columns_test.go index d863ed4f..f1b3cf07 100644 --- a/honeycombio/data_source_columns_test.go +++ b/honeycombio/data_source_columns_test.go @@ -6,6 +6,8 @@ import ( "strings" "testing" + "github.com/stretchr/testify/require" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -32,12 +34,11 @@ func TestAccDataSourceHoneycombioColumns_basic(t *testing.T) { for i, column := range testColumns { col, err := c.Columns.Create(ctx, dataset, &column) + require.NoError(t, err) // update ID for removal later testColumns[i].ID = col.ID - if err != nil { - t.Error(err) - } } + //nolint:errcheck t.Cleanup(func() { // remove Columns at the of the test run for _, col := range testColumns { diff --git a/honeycombio/data_source_query_specification.go b/honeycombio/data_source_query_specification.go index a6c94ed1..12e95c9d 100644 --- a/honeycombio/data_source_query_specification.go +++ b/honeycombio/data_source_query_specification.go @@ -10,7 +10,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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" "github.com/honeycombio/terraform-provider-honeycombio/internal/helper/hashcode" ) @@ -27,7 +29,7 @@ func dataSourceHoneycombioQuerySpec() *schema.Resource { "op": { Type: schema.TypeString, Required: true, - ValidateFunc: validation.StringInSlice(calculationOpStrings(), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.CalculationOps()), false), }, "column": { Type: schema.TypeString, @@ -48,7 +50,7 @@ func dataSourceHoneycombioQuerySpec() *schema.Resource { "op": { Type: schema.TypeString, Required: true, - ValidateFunc: validation.StringInSlice(filterOpStrings(), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.FilterOps()), false), }, "value": { Type: schema.TypeString, @@ -90,7 +92,7 @@ func dataSourceHoneycombioQuerySpec() *schema.Resource { "calculate_op": { Type: schema.TypeString, Required: true, - ValidateFunc: validation.StringInSlice(havingCalculateOpStrings(), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.HavingCalculationOps()), false), }, "column": { Type: schema.TypeString, @@ -100,7 +102,7 @@ func dataSourceHoneycombioQuerySpec() *schema.Resource { "op": { Type: schema.TypeString, Required: true, - ValidateFunc: validation.StringInSlice(havingOpStrings(), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.HavingOps()), false), }, "value": { // API currently assumes this is a number @@ -131,7 +133,7 @@ func dataSourceHoneycombioQuerySpec() *schema.Resource { "op": { Type: schema.TypeString, Optional: true, - ValidateFunc: validation.StringInSlice(calculationOpStrings(), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.CalculationOps()), false), }, "column": { Type: schema.TypeString, @@ -140,7 +142,7 @@ func dataSourceHoneycombioQuerySpec() *schema.Resource { "order": { Type: schema.TypeString, Optional: true, - ValidateFunc: validation.StringInSlice(sortOrderStrings(), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.SortOrders()), false), }, }, }, diff --git a/honeycombio/data_source_recipient_test.go b/honeycombio/data_source_recipient_test.go index 7b287ee7..dce3d8b1 100644 --- a/honeycombio/data_source_recipient_test.go +++ b/honeycombio/data_source_recipient_test.go @@ -6,7 +6,10 @@ import ( "regexp" "testing" + "github.com/stretchr/testify/require" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client" ) @@ -72,12 +75,11 @@ func TestAccDataSourceHoneycombioRecipient_basic(t *testing.T) { for i, r := range testRecipients { rcpt, err := c.Recipients.Create(ctx, &r) + require.NoError(t, err) // update ID for removal later testRecipients[i].ID = rcpt.ID - if err != nil { - t.Error(err) - } } + //nolint:errcheck t.Cleanup(func() { // remove Recipients at the of the test run for _, r := range testRecipients { diff --git a/honeycombio/data_source_recipients_test.go b/honeycombio/data_source_recipients_test.go index ebb5e71d..3d3367e9 100644 --- a/honeycombio/data_source_recipients_test.go +++ b/honeycombio/data_source_recipients_test.go @@ -5,7 +5,10 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/require" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client" ) @@ -42,12 +45,11 @@ func TestAccDataSourceHoneycombioRecipients_basic(t *testing.T) { for i, r := range testRecipients { rcpt, err := c.Recipients.Create(ctx, &r) + require.NoError(t, err) // update ID for removal later testRecipients[i].ID = rcpt.ID - if err != nil { - t.Error(err) - } } + //nolint:errcheck t.Cleanup(func() { // remove Recipients at the of the test run for _, r := range testRecipients { diff --git a/honeycombio/data_source_trigger_recipient.go b/honeycombio/data_source_trigger_recipient.go index 52b2b7f1..e06eeecb 100644 --- a/honeycombio/data_source_trigger_recipient.go +++ b/honeycombio/data_source_trigger_recipient.go @@ -6,7 +6,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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 dataSourceHoneycombioSlackRecipient() *schema.Resource { @@ -21,7 +23,7 @@ func dataSourceHoneycombioSlackRecipient() *schema.Resource { "type": { Type: schema.TypeString, Required: true, - ValidateFunc: validation.StringInSlice(recipientTypeStrings(honeycombio.TriggerRecipientTypes()), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.TriggerRecipientTypes()), false), }, "target": { Type: schema.TypeString, diff --git a/honeycombio/data_source_trigger_recipient_test.go b/honeycombio/data_source_trigger_recipient_test.go index cb505358..c039903d 100644 --- a/honeycombio/data_source_trigger_recipient_test.go +++ b/honeycombio/data_source_trigger_recipient_test.go @@ -1,22 +1,46 @@ package honeycombio import ( + "context" "fmt" "regexp" "testing" + "github.com/stretchr/testify/require" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client" ) func TestAccDataSourceHoneycombioTriggerRecipient_basic(t *testing.T) { + ctx := context.Background() dataset := testAccDataset() + c := testAccClient(t) - _, deleteFn := createTriggerWithRecipient(t, dataset, honeycombio.NotificationRecipient{ - Type: honeycombio.RecipientTypeEmail, - Target: "acctest@example.com", + trigger, err := c.Triggers.Create(ctx, dataset, &honeycombio.Trigger{ + Name: "test trigger", + Query: &honeycombio.QuerySpec{ + Calculations: []honeycombio.CalculationSpec{ + {Op: honeycombio.CalculationOpCount}, + }, + }, + Threshold: &honeycombio.TriggerThreshold{ + Op: honeycombio.TriggerThresholdOpGreaterThan, + Value: 100, + }, + Recipients: []honeycombio.NotificationRecipient{ + { + Type: honeycombio.RecipientTypeEmail, + Target: "acctest@example.com", + }, + }, + }) + require.NoError(t, err) + //nolint:errcheck + t.Cleanup(func() { + c.Triggers.Delete(ctx, dataset, trigger.ID) }) - defer deleteFn() resource.Test(t, resource.TestCase{ PreCheck: testAccPreCheck(t), @@ -41,7 +65,7 @@ func testAccTriggerRecipient(dataset, recipientType, target string) string { return fmt.Sprintf(` data "honeycombio_trigger_recipient" "test" { dataset = "%s" - type = "%s" - target = "%s" + type = "%s" + target = "%s" }`, dataset, recipientType, target) } diff --git a/honeycombio/helpers_test.go b/honeycombio/helpers_test.go index 1768703a..36a40400 100644 --- a/honeycombio/helpers_test.go +++ b/honeycombio/helpers_test.go @@ -1,13 +1,10 @@ package honeycombio import ( - "context" "fmt" - "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client" ) // testCheckOutputContains checks an output in the Terraform configuration @@ -54,52 +51,3 @@ func testCheckOutputDoesNotContain(name, contains string) resource.TestCheckFunc return nil } } - -func createTriggerWithRecipient(t *testing.T, dataset string, recipient honeycombio.NotificationRecipient) (trigger *honeycombio.Trigger, deleteFn func()) { - ctx := context.Background() - c := testAccClient(t) - - trigger = &honeycombio.Trigger{ - Name: "Terraform provider - acc test trigger recipient", - Query: &honeycombio.QuerySpec{ - Calculations: []honeycombio.CalculationSpec{ - { - Op: honeycombio.CalculationOpCount, - }, - }, - }, - Threshold: &honeycombio.TriggerThreshold{ - Op: honeycombio.TriggerThresholdOpGreaterThan, - Value: 100, - }, - Recipients: []honeycombio.NotificationRecipient{recipient}, - } - trigger, err := c.Triggers.Create(ctx, dataset, trigger) - if err != nil { - t.Error(err) - } - - return trigger, func() { - err := c.Triggers.Delete(ctx, dataset, trigger.ID) - if err != nil { - t.Error(err) - } - } -} - -func testAccCheckRecipientExists(t *testing.T, resourceName string) resource.TestCheckFunc { - return func(s *terraform.State) error { - resourceState, ok := s.RootModule().Resources[resourceName] - if !ok { - return fmt.Errorf("not found: %s", resourceName) - } - - client := testAccClient(t) - _, err := client.Recipients.Get(context.Background(), resourceState.Primary.ID) - if err != nil { - return fmt.Errorf("could not find created Recipient: %w", err) - } - - return nil - } -} diff --git a/honeycombio/resource_board.go b/honeycombio/resource_board.go index 4b938598..940bcf27 100644 --- a/honeycombio/resource_board.go +++ b/honeycombio/resource_board.go @@ -7,7 +7,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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 newBoard() *schema.Resource { @@ -46,7 +48,7 @@ func newBoard() *schema.Resource { Default: "visual", Description: "How the Board should be displayed in the UI.", Deprecated: "All Boards now are displayed visually in the UI. Setting this value will have no effect.", - ValidateFunc: validation.StringInSlice(boardStyleStrings(), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.BoardStyles()), false), }, "board_url": { Type: schema.TypeString, @@ -72,7 +74,7 @@ func newBoard() *schema.Resource { Optional: true, Computed: true, Description: "How the Query should be displayed within the Board.", - ValidateFunc: validation.StringInSlice(boardQueryStyleStrings(), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.BoardQueryStyles()), false), }, "graph_settings": { Type: schema.TypeList, diff --git a/honeycombio/resource_column.go b/honeycombio/resource_column.go index dad92540..69ae620e 100644 --- a/honeycombio/resource_column.go +++ b/honeycombio/resource_column.go @@ -10,7 +10,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "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 newColumn() *schema.Resource { @@ -55,7 +57,7 @@ func newColumn() *schema.Resource { Type: schema.TypeString, Optional: true, Default: "string", - ValidateFunc: validation.StringInSlice(columnTypeStrings(), false), + ValidateFunc: validation.StringInSlice(helper.AsStringSlice(honeycombio.ColumnTypes()), false), }, "dataset": { Type: schema.TypeString, diff --git a/honeycombio/resource_query_annotation_test.go b/honeycombio/resource_query_annotation_test.go index 15739882..3b9b41db 100644 --- a/honeycombio/resource_query_annotation_test.go +++ b/honeycombio/resource_query_annotation_test.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" - "github.com/stretchr/testify/assert" ) func TestAccHoneycombioQueryAnnotation_update(t *testing.T) { @@ -22,13 +21,19 @@ func TestAccHoneycombioQueryAnnotation_update(t *testing.T) { { Config: testAccResourceQueryAnnotationConfig(dataset, firstName), Check: resource.ComposeTestCheckFunc( - testAccCheckQueryAnnotationExists(t, dataset, "honeycombio_query_annotation.test", firstName), + testAccCheckQueryAnnotationExists(t, dataset, "honeycombio_query_annotation.test"), + resource.TestCheckResourceAttr("honeycombio_query_annotation.test", "name", firstName), + resource.TestCheckResourceAttr("honeycombio_query_annotation.test", "description", "Test query annotation description"), + resource.TestCheckResourceAttrPair("honeycombio_query_annotation.test", "query_id", "honeycombio_query.test", "id"), ), }, { Config: testAccResourceQueryAnnotationConfig(dataset, secondName), Check: resource.ComposeTestCheckFunc( - testAccCheckQueryAnnotationExists(t, dataset, "honeycombio_query_annotation.test", secondName), + testAccCheckQueryAnnotationExists(t, dataset, "honeycombio_query_annotation.test"), + resource.TestCheckResourceAttr("honeycombio_query_annotation.test", "name", secondName), + resource.TestCheckResourceAttr("honeycombio_query_annotation.test", "description", "Test query annotation description"), + resource.TestCheckResourceAttrPair("honeycombio_query_annotation.test", "query_id", "honeycombio_query.test", "id"), ), }, }, @@ -64,23 +69,19 @@ resource "honeycombio_query_annotation" "test" { `, dataset, dataset, name) } -func testAccCheckQueryAnnotationExists(t *testing.T, dataset string, resourceName string, name string) resource.TestCheckFunc { +func testAccCheckQueryAnnotationExists(t *testing.T, dataset string, name string) resource.TestCheckFunc { return func(s *terraform.State) error { - resourceState, ok := s.RootModule().Resources[resourceName] + resourceState, ok := s.RootModule().Resources[name] if !ok { - return fmt.Errorf("not found: %s", resourceName) + return fmt.Errorf("not found: %s", name) } client := testAccClient(t) - createdQueryAnnotation, err := client.QueryAnnotations.Get(context.Background(), dataset, resourceState.Primary.ID) + _, err := client.QueryAnnotations.Get(context.Background(), dataset, resourceState.Primary.ID) if err != nil { return fmt.Errorf("could not find created query: %w", err) } - assert.Equal(t, resourceState.Primary.ID, createdQueryAnnotation.ID) - assert.Equal(t, name, createdQueryAnnotation.Name) - assert.Equal(t, resourceState.Primary.Attributes["query_id"], createdQueryAnnotation.QueryID) - return nil } } diff --git a/honeycombio/resource_recipients_test.go b/honeycombio/resource_recipients_test.go index e4d147ca..b94fb1ae 100644 --- a/honeycombio/resource_recipients_test.go +++ b/honeycombio/resource_recipients_test.go @@ -1,9 +1,12 @@ package honeycombio import ( + "context" + "fmt" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccHoneycombioRecipients_basic(t *testing.T) { @@ -77,3 +80,20 @@ resource "honeycombio_webhook_recipient" "test" { }, }) } + +func testAccCheckRecipientExists(t *testing.T, resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + resourceState, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("not found: %s", resourceName) + } + + client := testAccClient(t) + _, err := client.Recipients.Get(context.Background(), resourceState.Primary.ID) + if err != nil { + return fmt.Errorf("could not find created Recipient: %w", err) + } + + return nil + } +} diff --git a/honeycombio/resource_slo_test.go b/honeycombio/resource_slo_test.go index 258ecd09..1e73de3d 100644 --- a/honeycombio/resource_slo_test.go +++ b/honeycombio/resource_slo_test.go @@ -7,8 +7,10 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + + "github.com/stretchr/testify/require" + honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client" - "github.com/stretchr/testify/assert" ) func TestAccHoneycombioSLO_basic(t *testing.T) { @@ -20,11 +22,10 @@ func TestAccHoneycombioSLO_basic(t *testing.T) { Alias: "sli.acc_slo_test", Expression: "LT($duration_ms, 1000)", }) - if err != nil { - t.Error(err) - } - // remove SLI DC at end of test run + require.NoError(t, err) + //nolint:errcheck t.Cleanup(func() { + // remove SLI DC at end of test run c.DerivedColumns.Delete(ctx, dataset, sli.ID) }) @@ -44,33 +45,31 @@ resource "honeycombio_slo" "test" { } `, dataset, sli.Alias), Check: resource.ComposeTestCheckFunc( - testAccCheckSLOExists(t, dataset, "honeycombio_slo.test", "TestAcc SLO"), + testAccCheckSLOExists(t, dataset, "honeycombio_slo.test"), + resource.TestCheckResourceAttr("honeycombio_slo.test", "name", "TestAcc SLO"), + resource.TestCheckResourceAttr("honeycombio_slo.test", "description", "integration test SLO"), + resource.TestCheckResourceAttr("honeycombio_slo.test", "sli", sli.Alias), + resource.TestCheckResourceAttr("honeycombio_slo.test", "target_percentage", "99.95"), + resource.TestCheckResourceAttr("honeycombio_slo.test", "time_period", "30"), ), }, }, }) } -func testAccCheckSLOExists(t *testing.T, dataset string, resourceName string, name string) resource.TestCheckFunc { +func testAccCheckSLOExists(t *testing.T, dataset string, name string) resource.TestCheckFunc { return func(s *terraform.State) error { - resourceState, ok := s.RootModule().Resources[resourceName] + resourceState, ok := s.RootModule().Resources[name] if !ok { - return fmt.Errorf("not found: %s", resourceName) + return fmt.Errorf("not found: %s", name) } client := testAccClient(t) - createdSLO, err := client.SLOs.Get(context.Background(), dataset, resourceState.Primary.ID) + _, err := client.SLOs.Get(context.Background(), dataset, resourceState.Primary.ID) if err != nil { return fmt.Errorf("could not find created SLO: %w", err) } - assert.Equal(t, resourceState.Primary.ID, createdSLO.ID) - assert.Equal(t, name, createdSLO.Name) - assert.Equal(t, resourceState.Primary.Attributes["description"], createdSLO.Description) - assert.Equal(t, resourceState.Primary.Attributes["sli"], createdSLO.SLI.Alias) - assert.Equal(t, resourceState.Primary.Attributes["target_percentage"], fmt.Sprintf("%v", tpmToFloat(createdSLO.TargetPerMillion))) - assert.Equal(t, resourceState.Primary.Attributes["time_period"], fmt.Sprintf("%v", createdSLO.TimePeriodDays)) - return nil } } diff --git a/honeycombio/type_helpers.go b/honeycombio/type_helpers.go index f7f2052e..fda56210 100644 --- a/honeycombio/type_helpers.go +++ b/honeycombio/type_helpers.go @@ -9,122 +9,10 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client" ) -func boardStyleStrings() []string { - in := honeycombio.BoardStyles() - out := make([]string, len(in)) - - for i := range in { - out[i] = string(in[i]) - } - - return out -} - -func boardQueryStyleStrings() []string { - in := honeycombio.BoardQueryStyles() - out := make([]string, len(in)) - - for i := range in { - out[i] = string(in[i]) - } - - return out -} - -func calculationOpStrings() []string { - in := honeycombio.CalculationOps() - out := make([]string, len(in)) - - for i := range in { - out[i] = string(in[i]) - } - - return out -} - -func columnTypeStrings() []string { - in := honeycombio.ColumnTypes() - out := make([]string, len(in)) - - for i := range in { - out[i] = string(in[i]) - } - - return out -} - -func filterOpStrings() []string { - in := honeycombio.FilterOps() - out := make([]string, len(in)) - - for i := range in { - out[i] = string(in[i]) - } - - return out -} - -func havingOpStrings() []string { - in := honeycombio.HavingOps() - out := make([]string, len(in)) - - for i := range in { - out[i] = string(in[i]) - } - - return out -} - -func havingCalculateOpStrings() []string { - in := honeycombio.CalculationOps() - out := make([]string, len(in)) - - for i := range in { - // havings cannot use HEATMAP - if in[i] != honeycombio.CalculationOpHeatmap { - out[i] = string(in[i]) - } - } - - return out -} - -func sortOrderStrings() []string { - in := honeycombio.SortOrders() - out := make([]string, len(in)) - - for i := range in { - out[i] = string(in[i]) - } - - return out -} - -func recipientTypeStrings(recipientTypes []honeycombio.RecipientType) []string { - in := recipientTypes - out := make([]string, len(in)) - - for i := range in { - out[i] = string(in[i]) - } - - return out -} - -func triggerThresholdOpStrings() []string { - in := honeycombio.TriggerThresholdOps() - out := make([]string, len(in)) - - for i := range in { - out[i] = string(in[i]) - } - - return out -} - func coerceValueToType(i string) interface{} { // HCL really has three base types: bool, string, and number // The Plugin SDK allows typing a schema field to Int or Float @@ -156,106 +44,6 @@ func tpmToFloat(t int) float64 { return float64(t) / 10000 } -func flattenNotificationRecipients(rs []honeycombio.NotificationRecipient) []map[string]interface{} { - result := make([]map[string]interface{}, len(rs)) - - for i, r := range rs { - rcpt := map[string]interface{}{ - "id": r.ID, - "type": string(r.Type), - "target": r.Target, - } - if r.Details != nil { - // notification details have been provided - details := make([]map[string]interface{}, 1) - details[0] = map[string]interface{}{} - if r.Details.PDSeverity != "" { - details[0]["pagerduty_severity"] = string(r.Details.PDSeverity) - } - rcpt["notification_details"] = details - } - result[i] = rcpt - } - - return result -} - -func expandNotificationRecipients(s []interface{}) []honeycombio.NotificationRecipient { - recipients := make([]honeycombio.NotificationRecipient, len(s)) - - for i, r := range s { - rMap := r.(map[string]interface{}) - - rcpt := honeycombio.NotificationRecipient{ - ID: rMap["id"].(string), - Type: honeycombio.RecipientType(rMap["type"].(string)), - Target: rMap["target"].(string), - } - if v, ok := rMap["notification_details"].([]interface{}); ok && len(v) > 0 { - // notification details have been provided - details := v[0].(map[string]interface{}) - if s, ok := details["pagerduty_severity"]; ok { - rcpt.Details = &honeycombio.NotificationRecipientDetails{ - PDSeverity: honeycombio.PagerDutySeverity(s.(string)), - } - } - } - recipients[i] = rcpt - } - - return recipients -} - -// Matches read recipients against those declared in HCL and returns -// the Trigger recipients in a stable order grouped by recipient type. -// -// This cannot currently be handled efficiently by a DiffSuppressFunc. -// See: https://github.com/hashicorp/terraform-plugin-sdk/issues/477 -func matchNotificationRecipientsWithSchema(readRecipients []honeycombio.NotificationRecipient, declaredRecipients []interface{}) []honeycombio.NotificationRecipient { - result := make([]honeycombio.NotificationRecipient, 0) - - rMap := make(map[string]honeycombio.NotificationRecipient, len(readRecipients)) - for _, recipient := range readRecipients { - rMap[recipient.ID] = recipient - } - - // Build up result, with each readRecipient in the same position as it - // appears in declaredRecipients, by looking at each declaredRecipient and - // finding its matching readRecipient (via rMap). - // - // If the declaredRecipient has an ID, this is easy: just look it up and - // put it in it's place. Otherwise, try to match it to a readRecipient with - // the same type and target. If we can't find it at all, it must be new, so - // put it at the end. - for _, declaredRcpt := range declaredRecipients { - declaredRcpt := declaredRcpt.(map[string]interface{}) - - if declaredRcpt["id"] != "" { - if v, ok := rMap[declaredRcpt["id"].(string)]; ok { - // matched recipient declared by ID - result = append(result, v) - delete(rMap, v.ID) - } - } else { - // group result recipients by type - for key, rcpt := range rMap { - if string(rcpt.Type) == declaredRcpt["type"] && rcpt.Target == declaredRcpt["target"] { - result = append(result, rcpt) - delete(rMap, key) - break - } - } - } - } - - // append unmatched read recipients to the result - for _, rcpt := range rMap { - result = append(result, rcpt) - } - - return result -} - 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 87d5ee17..f8e43f17 100644 --- a/honeycombio/type_helpers_test.go +++ b/honeycombio/type_helpers_test.go @@ -1,8 +1,9 @@ package honeycombio import ( - "reflect" "testing" + + "github.com/stretchr/testify/assert" ) func Test_coerceValueToType(t *testing.T) { @@ -49,9 +50,7 @@ func Test_coerceValueToType(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := coerceValueToType(tt.input); !reflect.DeepEqual(got, tt.expected) { - t.Errorf("coerceInputToType() = %v<%T>, want %v<%T>", got, got, tt.expected, tt.expected) - } + assert.Equal(t, tt.expected, coerceValueToType(tt.input)) }) } } diff --git a/internal/provider/slos_data_source_test.go b/internal/provider/slos_data_source_test.go index d92261e5..9acb1f9f 100644 --- a/internal/provider/slos_data_source_test.go +++ b/internal/provider/slos_data_source_test.go @@ -5,6 +5,8 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/require" + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -62,14 +64,9 @@ func TestAcc_SLOsDataSource(t *testing.T) { for i, tc := range testData { sli, err := c.DerivedColumns.Create(ctx, dataset, &tc.SLI) - if err != nil { - t.Error(err) - } - + require.NoError(t, err) slo, err := c.SLOs.Create(ctx, dataset, &tc.SLO) - if err != nil { - t.Error(err) - } + require.NoError(t, err) // update IDs for removal later testData[i].SLI.ID = sli.ID diff --git a/internal/provider/trigger_resource_test.go b/internal/provider/trigger_resource_test.go index b0a20d38..2ecb8343 100644 --- a/internal/provider/trigger_resource_test.go +++ b/internal/provider/trigger_resource_test.go @@ -5,6 +5,8 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/require" + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -97,9 +99,7 @@ func TestAcc_TriggerResourceUpdateRecipientByID(t *testing.T) { for i, r := range testRecipients { rcpt, err := c.Recipients.Create(ctx, &r) - if err != nil { - t.Error(err) - } + require.NoError(t, err) // update ID for removal later testRecipients[i].ID = rcpt.ID }