From 3d38cb603ac46e163c3802b3ecb6bf4d1661c09a Mon Sep 17 00:00:00 2001 From: Koenraad Verheyden Date: Fri, 14 Aug 2020 23:39:32 +0200 Subject: [PATCH 1/4] Add limit to honeycombio_query Closes #20 --- honeycombio/data_source_query.go | 12 +++++++++++ honeycombio/data_source_query_test.go | 29 ++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/honeycombio/data_source_query.go b/honeycombio/data_source_query.go index d0dbb953..c8bddd7a 100644 --- a/honeycombio/data_source_query.go +++ b/honeycombio/data_source_query.go @@ -104,6 +104,11 @@ func dataSourceHoneycombioQuery() *schema.Resource { Type: schema.TypeString, }, }, + "limit": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 1000), + }, "json": { Type: schema.TypeString, Computed: true, @@ -173,11 +178,18 @@ 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 + } + query := &honeycombio.QuerySpec{ Calculations: calculations, Filters: filters, FilterCombination: &filterCombination, Breakdowns: breakdowns, + Limit: limit, } jsonQuery, err := encodeQuery(query) diff --git a/honeycombio/data_source_query_test.go b/honeycombio/data_source_query_test.go index f7238783..90606d57 100644 --- a/honeycombio/data_source_query_test.go +++ b/honeycombio/data_source_query_test.go @@ -1,6 +1,7 @@ package honeycombio import ( + "fmt" "regexp" "testing" @@ -34,12 +35,13 @@ data "honeycombio_query" "test" { column = "trace.parent_id" op = "does-not-exist" } - filter { column = "app.tenant" op = "=" value = "ThatSpecialTenant" } + + limit = 250 } output "query_json" { @@ -65,12 +67,13 @@ const expectedJSON string = `{ "value": "ThatSpecialTenant" } ], - "filter_combination": "AND" + "filter_combination": "AND", + "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{ @@ -82,6 +85,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\\)"), + }, }, }) } @@ -108,3 +123,11 @@ data "honeycombio_query" "test" { } ` } + +func testQueryWithLimit(limit int) string { + return fmt.Sprintf(` +data "honeycombio_query" "test" { + limit = %d +} +`, limit) +} From 136a7ea5d373caa85e1d413af5547162821f24f9 Mon Sep 17 00:00:00 2001 From: Koenraad Verheyden Date: Sat, 15 Aug 2020 00:06:37 +0200 Subject: [PATCH 2/4] Document honeycombio_query.limit --- docs/data-sources/query.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/data-sources/query.md b/docs/data-sources/query.md index 44948599..746803fc 100644 --- a/docs/data-sources/query.md +++ b/docs/data-sources/query.md @@ -44,6 +44,7 @@ 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. +* `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: From 26b41fcdcedf15fcc31010efec6803d76d543d65 Mon Sep 17 00:00:00 2001 From: Koenraad Verheyden Date: Sat, 15 Aug 2020 00:51:31 +0200 Subject: [PATCH 3/4] Add order block to honeycombio_query Closes #19 --- go.mod | 2 +- go.sum | 4 +- honeycombio/data_source_query.go | 54 +++++++++++++++++++++++++++ honeycombio/data_source_query_test.go | 24 ++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 7dd96270..274164dd 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 4a4856f6..115fea72 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/honeycombio/data_source_query.go b/honeycombio/data_source_query.go index c8bddd7a..1bd40b1c 100644 --- a/honeycombio/data_source_query.go +++ b/honeycombio/data_source_query.go @@ -104,6 +104,28 @@ 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, @@ -184,11 +206,43 @@ func dataSourceHoneycombioQueryRead(ctx context.Context, d *schema.ResourceData, 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, } diff --git a/honeycombio/data_source_query_test.go b/honeycombio/data_source_query_test.go index 90606d57..10ffc6d6 100644 --- a/honeycombio/data_source_query_test.go +++ b/honeycombio/data_source_query_test.go @@ -41,6 +41,17 @@ data "honeycombio_query" "test" { value = "ThatSpecialTenant" } + breakdowns = ["column_1"] + + order { + op = "AVG" + column = "duration_ms" + } + order { + column = "column_1" + order = "descending" + } + limit = 250 } @@ -68,6 +79,19 @@ const expectedJSON string = `{ } ], "filter_combination": "AND", + "breakdowns": [ + "column_1" + ], + "orders": [ + { + "op": "AVG", + "column": "duration_ms" + }, + { + "column": "column_1", + "order": "descending" + } + ], "limit": 250 }` From 74cb508058047a8316db3e5c1a61251c9cbdcd84 Mon Sep 17 00:00:00 2001 From: Koenraad Verheyden Date: Sat, 15 Aug 2020 01:03:34 +0200 Subject: [PATCH 4/4] Document honeycombio_query.order --- docs/data-sources/query.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/data-sources/query.md b/docs/data-sources/query.md index 746803fc..28c1e328 100644 --- a/docs/data-sources/query.md +++ b/docs/data-sources/query.md @@ -44,6 +44,7 @@ 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: @@ -57,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