From 30d655e946207a3bb921998e89bad2632501aeeb Mon Sep 17 00:00:00 2001 From: Dan Tulovsky Date: Fri, 2 Dec 2022 14:47:11 -0500 Subject: [PATCH] initial attempt --- client/streams.go | 17 +++++++++++++---- lightstep/data_source_stream.go | 10 ++++++++++ lightstep/resource_stream.go | 16 +++++++++++++--- lightstep/resource_stream_test.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/client/streams.go b/client/streams.go index ccbecf20..6a34d8bc 100644 --- a/client/streams.go +++ b/client/streams.go @@ -14,8 +14,9 @@ type Stream struct { } type StreamAttributes struct { - Name string `json:"name"` - Query string `json:"query"` + Name string `json:"name"` + Query string `json:"query,omitempty"` + UQLQuery string `json:"uql_query,omitempty"` // "custom_data" on set, but "custom-data" on get CustomData map[string]map[string]string `json:"custom_data,omitempty"` @@ -27,7 +28,7 @@ type StreamAttributes struct { func CustomDataConvert(customData []interface{}) map[string]map[string]string { // This is what Lightstep expects - //"custom_data": { + // "custom_data": { // "object1": { // "url": "http://", // "key": "value" @@ -35,7 +36,7 @@ func CustomDataConvert(customData []interface{}) map[string]map[string]string { // "object2": { // "key": "value" // } - //}, + // }, lsCustomData := make(map[string]map[string]string) // This is what we have (terraform doesn't support a map of maps natively. @@ -70,6 +71,7 @@ func (c *Client) CreateStream( projectName string, name string, query string, + uqlQuery string, customData []interface{}, ) (Stream, error) { @@ -80,12 +82,17 @@ func (c *Client) CreateStream( lsCustomData := CustomDataConvert(customData) + if query != "" && uqlQuery != "" { + return s, fmt.Errorf("only one of [query, uql_query] should be set") + } + bytes, err := json.Marshal( Stream{ Type: "stream", Attributes: StreamAttributes{ Name: name, Query: query, + UQLQuery: uqlQuery, CustomData: lsCustomData, }, }) @@ -138,9 +145,11 @@ func (c *Client) GetStream(ctx context.Context, projectName string, StreamID str if err != nil { return nil, err } + fmt.Printf("getting: projects/%v/streams/%v\n", projectName, StreamID) err = json.Unmarshal(resp.Data, &s) if err != nil { + fmt.Printf("resp: %#v\n\n", string(resp.Data)) return s, err } return s, err diff --git a/lightstep/data_source_stream.go b/lightstep/data_source_stream.go index e681eb22..5400d880 100644 --- a/lightstep/data_source_stream.go +++ b/lightstep/data_source_stream.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/lightstep/terraform-provider-lightstep/client" ) @@ -33,6 +34,11 @@ func dataSourceStream() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "stream_uql_query": { + Description: "Stream uql query", + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -56,5 +62,9 @@ func dataSourceLightstepStreamRead(ctx context.Context, d *schema.ResourceData, if err := d.Set("stream_query", s.Attributes.Query); err != nil { return diag.FromErr(err) } + + if err := d.Set("stream_uql_query", s.Attributes.UQLQuery); err != nil { + return diag.FromErr(err) + } return nil } diff --git a/lightstep/resource_stream.go b/lightstep/resource_stream.go index a9ed1733..def4dc28 100644 --- a/lightstep/resource_stream.go +++ b/lightstep/resource_stream.go @@ -34,7 +34,12 @@ func resourceStream() *schema.Resource { }, "query": { Type: schema.TypeString, - Required: true, + Optional: true, + ForceNew: true, + }, + "uql_query": { + Type: schema.TypeString, + Optional: true, ForceNew: true, }, "custom_data": { @@ -57,11 +62,13 @@ func resourceStreamCreate(ctx context.Context, d *schema.ResourceData, m interfa c := m.(*client.Client) if err := resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { origQuery := d.Get("query").(string) + origUQLQuery := d.Get("uql_query").(string) stream, err := c.CreateStream( ctx, d.Get("project_name").(string), d.Get("stream_name").(string), d.Get("query").(string), + d.Get("uql_query").(string), d.Get("custom_data").([]interface{}), ) if err != nil { @@ -73,6 +80,7 @@ func resourceStreamCreate(ctx context.Context, d *schema.ResourceData, m interfa } } + // This is empty when creating a stream with UQL d.SetId(stream.ID) if err := resourceStreamRead(ctx, d, m); err != nil { if len(err) == 0 { @@ -83,6 +91,7 @@ func resourceStreamCreate(ctx context.Context, d *schema.ResourceData, m interfa } // workaround: if read succeeds, persist the *client-side* query expression to avoid backend normalization issue d.Set("query", origQuery) + d.Set("uql_query", origUQLQuery) return nil }); err != nil { return diag.FromErr(fmt.Errorf("failed to create stream: %v", err)) @@ -176,6 +185,7 @@ func resourceStreamImport(ctx context.Context, d *schema.ResourceData, m interfa return []*schema.ResourceData{}, fmt.Errorf("failed to set stream from API response to terraform state: %v", err) } d.Set("query", stream.Attributes.Query) + d.Set("uql_query", stream.Attributes.UQLQuery) return []*schema.ResourceData{d}, nil } @@ -189,7 +199,7 @@ func setResourceDataFromStream(d *schema.ResourceData, s client.Stream) error { customData := []map[string]string{} // This is what Lightstep sends - //"custom_data": { + // "custom_data": { // "object1": { // "url": "http://", // "key": "value" @@ -197,7 +207,7 @@ func setResourceDataFromStream(d *schema.ResourceData, s client.Stream) error { // "object2": { // "key": "value" // } - //}, + // }, // This is what terraform expects // custom_data = [ diff --git a/lightstep/resource_stream_test.go b/lightstep/resource_stream_test.go index 9c8fb178..453003fa 100644 --- a/lightstep/resource_stream_test.go +++ b/lightstep/resource_stream_test.go @@ -42,6 +42,23 @@ resource "lightstep_stream" "aggie_errors" { } ` + uqlStreamConfig := ` +resource "lightstep_stream" "aggie_errors" { + project_name = ` + fmt.Sprintf("\"%s\"", test_project) + ` + stream_name = "Aggie Errors" + uql_query = <