Skip to content

Commit

Permalink
Merge pull request #24 from kvrhdn/extend_query_spec
Browse files Browse the repository at this point in the history
Extend honeycombio_query
  • Loading branch information
Koenraad Verheyden authored Aug 15, 2020
2 parents d5e726a + 74cb508 commit e32a5e5
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docs/data-sources/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ The following arguments are supported:
* `filter` - (Optional) Zero or more configuration blocks (described below) with the filters that should be applied.
* `filter_combination` - (Optional) How to combine multiple filters, either `AND` (default) or `OR`.
* `breakdowns` - (Optional) A list of fields to group by.
* `order` - (Optional) Zero or more configuration blocks (described below) describing how to order the query results. Each term must appear in either `calculation` or `breakdowns`.
* `limit` - (Optional) The maximum number of query results, must be between 1 and 1000.

Each query configuration may have zero or more `calculation` blocks, which each accept the following arguments:

Expand All @@ -56,6 +58,11 @@ Each query configuration may have zero or more `filter` blocks, which each accep
* `op` - (Required) The operator to apply, see the supported list of filter operators at [Filter Operators](https://docs.honeycomb.io/api/query-specification/#filter-operators).
* `value` - (Optional) The value to be used with the operator, not all operators require a value.

Each query configuration may have zero or more `order` blocks, which each accept the following arguments. An order term can refer to a `calculation` or a column set in `breakdowns`. When referring to a `calculation`, `op` and `column` must be the same as the calculation.

* `op` - (Optional) The calculation operator to apply, see the supported list of calculation operators at [Calculation Operators](https://docs.honeycomb.io/api/query-specification/#calculation-operators).
* `column` - (Optional) Either a column present in `breakdown` or a column to `op` applies to.
* `order` - (Optional) The sort direction, if set must be `ascending` or `descending`.

## Attribute Reference

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.14
require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.1
github.com/kvrhdn/go-honeycombio v0.0.13
github.com/kvrhdn/go-honeycombio v0.0.14
github.com/stretchr/testify v1.6.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kvrhdn/go-honeycombio v0.0.13 h1:/fzoV5LjR8Hzzu0orBX1avb3sxLhPm9hug6Lt7ScWzc=
github.com/kvrhdn/go-honeycombio v0.0.13/go.mod h1:UA4oLwic817uNHzdzZotn5j0xSk9paBaPYODFhEDci8=
github.com/kvrhdn/go-honeycombio v0.0.14 h1:DjTV1GrIXQZDbEaTgNRmPw32ueUjcsJ5CSirHMaAGXM=
github.com/kvrhdn/go-honeycombio v0.0.14/go.mod h1:UA4oLwic817uNHzdzZotn5j0xSk9paBaPYODFhEDci8=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
Expand Down
66 changes: 66 additions & 0 deletions honeycombio/data_source_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ func dataSourceHoneycombioQuery() *schema.Resource {
Type: schema.TypeString,
},
},
"order": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"op": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(validQueryCalculationOps, false),
},
"column": {
Type: schema.TypeString,
Optional: true,
},
"order": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"ascending", "descending"}, false),
},
},
},
},
"limit": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 1000),
},
"json": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -173,11 +200,50 @@ func dataSourceHoneycombioQueryRead(ctx context.Context, d *schema.ResourceData,
breakdowns[i] = b.(string)
}

var limit *int
l := d.Get("limit").(int)
if l != 0 {
limit = &l
}

orderSchemas := d.Get("order").([]interface{})
orders := make([]honeycombio.OrderSpec, len(orderSchemas))

for i, o := range orderSchemas {
oMap := o.(map[string]interface{})

var op *honeycombio.CalculationOp
opValue := honeycombio.CalculationOp(oMap["op"].(string))
if opValue != "" {
op = &opValue
}

var column *string
columnValue := oMap["column"].(string)
if columnValue != "" {
column = &columnValue
}

var sortOrder *honeycombio.SortOrder
sortOrderValue := honeycombio.SortOrder(oMap["order"].(string))
if sortOrderValue != "" {
sortOrder = &sortOrderValue
}

orders[i] = honeycombio.OrderSpec{
Op: op,
Column: column,
Order: sortOrder,
}
}

query := &honeycombio.QuerySpec{
Calculations: calculations,
Filters: filters,
FilterCombination: &filterCombination,
Breakdowns: breakdowns,
Orders: orders,
Limit: limit,
}

jsonQuery, err := encodeQuery(query)
Expand Down
53 changes: 50 additions & 3 deletions honeycombio/data_source_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package honeycombio

import (
"fmt"
"regexp"
"testing"

Expand Down Expand Up @@ -34,12 +35,24 @@ data "honeycombio_query" "test" {
column = "trace.parent_id"
op = "does-not-exist"
}
filter {
column = "app.tenant"
op = "="
value = "ThatSpecialTenant"
}
breakdowns = ["column_1"]
order {
op = "AVG"
column = "duration_ms"
}
order {
column = "column_1"
order = "descending"
}
limit = 250
}
output "query_json" {
Expand All @@ -65,12 +78,26 @@ const expectedJSON string = `{
"value": "ThatSpecialTenant"
}
],
"filter_combination": "AND"
"filter_combination": "AND",
"breakdowns": [
"column_1"
],
"orders": [
{
"op": "AVG",
"column": "duration_ms"
},
{
"column": "column_1",
"order": "descending"
}
],
"limit": 250
}`

//Honeycomb API blows up if you send a Value with a FilterOp of `exists` or `does-not-exist`
func TestAccDataSourceHoneycombioQuery_noValueForExists(t *testing.T) {
resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
Expand All @@ -82,6 +109,18 @@ func TestAccDataSourceHoneycombioQuery_noValueForExists(t *testing.T) {
Config: testBadCountQuery(),
ExpectError: regexp.MustCompile("calculation op COUNT should not have an accompanying column"),
},
{
Config: testQueryWithLimit(0),
ExpectError: regexp.MustCompile("expected limit to be in the range \\(1 - 1000\\)"),
},
{
Config: testQueryWithLimit(-5),
ExpectError: regexp.MustCompile("expected limit to be in the range \\(1 - 1000\\)"),
},
{
Config: testQueryWithLimit(1200),
ExpectError: regexp.MustCompile("expected limit to be in the range \\(1 - 1000\\)"),
},
},
})
}
Expand All @@ -108,3 +147,11 @@ data "honeycombio_query" "test" {
}
`
}

func testQueryWithLimit(limit int) string {
return fmt.Sprintf(`
data "honeycombio_query" "test" {
limit = %d
}
`, limit)
}

0 comments on commit e32a5e5

Please sign in to comment.