Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial attempt #112

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions client/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -27,15 +28,15 @@ 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"
// },
// "object2": {
// "key": "value"
// }
//},
// },
lsCustomData := make(map[string]map[string]string)

// This is what we have (terraform doesn't support a map of maps natively.
Expand Down Expand Up @@ -70,6 +71,7 @@ func (c *Client) CreateStream(
projectName string,
name string,
query string,
uqlQuery string,
customData []interface{},
) (Stream, error) {

Expand All @@ -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,
},
})
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lightstep/data_source_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
},
},
}
}
Expand All @@ -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
}
16 changes: 13 additions & 3 deletions lightstep/resource_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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))
Expand Down Expand Up @@ -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
}
Expand All @@ -189,15 +199,15 @@ 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"
// },
// "object2": {
// "key": "value"
// }
//},
// },

// This is what terraform expects
// custom_data = [
Expand Down
28 changes: 28 additions & 0 deletions lightstep/resource_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<EOT
spans count | filter ((service == "aggie") && (error == true)) | rate | group_by [], sum
EOT
custom_data = [
{
// This name field is special and becomes the key
"name" = "object1"
"url" = "https://lightstep.atlassian.net/l/c/M7b0rBsj",
"key_other" = "value_other",
},
]
}
`
updatedNameQuery := `
resource "lightstep_stream" "aggie_errors" {
project_name = ` + fmt.Sprintf("\"%s\"", test_project) + `
Expand Down Expand Up @@ -79,6 +96,16 @@ resource "lightstep_stream" "aggie_errors" {
resource.TestCheckResourceAttr("lightstep_stream.aggie_errors", "custom_data.0.url", "https://lightstep.atlassian.net/l/c/M7b0rBsj"),
),
},
{
Config: uqlStreamConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckStreamExists("lightstep_stream.aggie_errors", &stream),
resource.TestCheckResourceAttr("lightstep_stream.aggie_errors", "stream_name", "Aggie Errors uql"),
resource.TestCheckResourceAttr("lightstep_stream.aggie_errors", "uql_query", `spans count | filter ((service == "aggie") && (error == true)) | rate | group_by [], sum`),
resource.TestCheckResourceAttr("lightstep_stream.aggie_errors", "custom_data.0.name", "object1"),
resource.TestCheckResourceAttr("lightstep_stream.aggie_errors", "custom_data.0.url", "https://lightstep.atlassian.net/l/c/M7b0rBsj"),
),
},
{
Config: updatedNameQuery,
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -188,6 +215,7 @@ func testAccCheckStreamExists(resourceName string, stream *client.Stream) resour
return fmt.Errorf("id is not set")
}

fmt.Printf("id: %v\n", tfStream.Primary.ID)
// get stream from LS
client := testAccProvider.Meta().(*client.Client)
str, err := client.GetStream(context.Background(), test_project, tfStream.Primary.ID)
Expand Down