Skip to content

Commit

Permalink
adds description and tooltip_fields to the event query schema (#216)
Browse files Browse the repository at this point in the history
* probably fine

* added terraform docs

* version

---------

Co-authored-by: MisterSquishy <MisterSquishy@users.noreply.github.com>
  • Loading branch information
MisterSquishy and MisterSquishy authored Mar 6, 2024
1 parent 7860db1 commit 926ad4d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1.91.7
1.92.0

12 changes: 7 additions & 5 deletions client/event_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
)

type EventQueryAttributes struct {
ID string `json:"id"`
Name string `json:"name"`
QueryString string `json:"query_string"`
Source string `json:"source"`
Type string `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
QueryString string `json:"query_string"`
Source string `json:"source"`
Type string `json:"type"`
Description string `json:"description"`
TooltipFields []string `json:"tooltip_fields"`
}

type WireEventQueryAttributes struct {
Expand Down
5 changes: 5 additions & 0 deletions docs/resources/event_query.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ description: |-
- `source` (String)
- `type` (String)

### Optional

- `description` (String)
- `tooltip_fields` (List of String)

### Read-Only

- `id` (String) The ID of this resource.
54 changes: 46 additions & 8 deletions lightstep/resource_event_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func resourceEventQuery() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"tooltip_fields": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand Down Expand Up @@ -78,18 +89,37 @@ func resourceEventQueryRead(ctx context.Context, d *schema.ResourceData, m inter
if err := d.Set("source", eq.Source); err != nil {
return diag.FromErr(fmt.Errorf("unable to set query string: %v", err))
}
if err := d.Set("description", eq.Description); err != nil {
return diag.FromErr(fmt.Errorf("unable to set description: %v", err))
}
if err := d.Set("tooltip_fields", eq.TooltipFields); err != nil {
return diag.FromErr(fmt.Errorf("unable to set tooltip fields: %v", err))
}

return diags
}

func resourceDataToStringSlice(resourceData *schema.ResourceData, fieldName string) []string {
resource := resourceData.Get(fieldName)

asInterfaceSlice := resource.([]interface{})
stringSlice := make([]string, len(asInterfaceSlice))
for i, element := range asInterfaceSlice {
stringSlice[i] = element.(string)
}
return stringSlice
}

func resourceEventQueryCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Client)

attrs := client.EventQueryAttributes{
Type: d.Get("type").(string),
Name: d.Get("name").(string),
Source: d.Get("source").(string),
QueryString: d.Get("query_string").(string),
Type: d.Get("type").(string),
Name: d.Get("name").(string),
Source: d.Get("source").(string),
QueryString: d.Get("query_string").(string),
Description: d.Get("description").(string),
TooltipFields: resourceDataToStringSlice(d, "tooltip_fields"),
}
eq, err := c.CreateEventQuery(ctx, d.Get("project_name").(string), attrs)
if err != nil {
Expand Down Expand Up @@ -130,17 +160,25 @@ func resourceEventQueryImport(ctx context.Context, d *schema.ResourceData, m int
if err := d.Set("source", eq.Source); err != nil {
return nil, fmt.Errorf("unable to set query string: %v", err)
}
if err := d.Set("description", eq.Description); err != nil {
return nil, fmt.Errorf("unable to set description: %v", err)
}
if err := d.Set("tooltip_fields", eq.TooltipFields); err != nil {
return nil, fmt.Errorf("unable to set tooltip fields: %v", err)
}
return []*schema.ResourceData{d}, nil
}

func resourceEventQueryUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*client.Client)

attrs := client.EventQueryAttributes{
Type: d.Get("type").(string),
Name: d.Get("name").(string),
Source: d.Get("source").(string),
QueryString: d.Get("query_string").(string),
Type: d.Get("type").(string),
Name: d.Get("name").(string),
Source: d.Get("source").(string),
QueryString: d.Get("query_string").(string),
Description: d.Get("description").(string),
TooltipFields: resourceDataToStringSlice(d, "tooltip_fields"),
}
eq, err := c.UpdateEventQuery(ctx, d.Get("project_name").(string), d.Id(), attrs)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions lightstep/resource_event_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ resource "lightstep_event_query" "terraform" {
type = "test-type"
source = "test-source"
query_string = "logs"
description = "a test event query, for the terraform acceptance tests!"
tooltip_fields = ["foo", "bar"]
}
`

Expand All @@ -29,6 +31,7 @@ resource "lightstep_event_query" "terraform" {
type = "test-type"
source = "test-source"
query_string = "logs | filter foo == bar"
description = "this is one heck of a query"
}
`
resource.Test(t, resource.TestCase{
Expand All @@ -44,6 +47,10 @@ resource "lightstep_event_query" "terraform" {
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "type", "test-type"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "source", "test-source"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "query_string", "logs"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "description", "a test event query, for the terraform acceptance tests!"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "tooltip_fields.#", "2"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "tooltip_fields.0", "foo"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "tooltip_fields.1", "bar"),
),
},
{
Expand All @@ -54,6 +61,8 @@ resource "lightstep_event_query" "terraform" {
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "type", "test-type"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "source", "test-source"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "query_string", "logs | filter foo == bar"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "description", "this is one heck of a query"),
resource.TestCheckResourceAttr("lightstep_event_query.terraform", "tooltip_fields.#", "0"),
),
},
},
Expand Down

0 comments on commit 926ad4d

Please sign in to comment.