Skip to content

Commit

Permalink
maint: prune dead code and tidy up
Browse files Browse the repository at this point in the history
With the refactors going on there was some dead code, some code
that could be easult 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
timeboxed effort.
  • Loading branch information
jharley committed Nov 8, 2023
1 parent 04ffa24 commit 50a235a
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 341 deletions.
7 changes: 6 additions & 1 deletion client/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions client/query_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -188,7 +194,6 @@ func CalculationOps() []CalculationOp {
CalculationOpP95,
CalculationOpP99,
CalculationOpP999,
CalculationOpHeatmap,
CalculationOpRateAvg,
CalculationOpRateSum,
CalculationOpRateMax,
Expand Down
6 changes: 3 additions & 3 deletions client/type_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package client

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEquivalent(t *testing.T) {
Expand All @@ -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))
})
}
}
8 changes: 5 additions & 3 deletions honeycombio/data_source_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions honeycombio/data_source_columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions honeycombio/data_source_query_specification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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),
},
},
},
Expand Down
8 changes: 5 additions & 3 deletions honeycombio/data_source_recipient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions honeycombio/data_source_recipients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion honeycombio/data_source_trigger_recipient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
36 changes: 30 additions & 6 deletions honeycombio/data_source_trigger_recipient_test.go
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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: "[email protected]",
},
},
})
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),
Expand All @@ -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)
}
52 changes: 0 additions & 52 deletions honeycombio/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
}
6 changes: 4 additions & 2 deletions honeycombio/resource_board.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 50a235a

Please sign in to comment.