Skip to content

Commit

Permalink
fix for un-normalized stream expressions (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
smithclay authored May 11, 2022
1 parent d9edf52 commit 267bc60
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.60.3
1.60.4
1 change: 0 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ provider "lightstep" {

- **api_key** (String) Lightstep organization api key. If not set, the provider will use the environment variable set in `api_key_env_var`.
- **api_key_env_var** (String) Environment variable to use when looking up the API Key.
- **environment** (String) The name of the Lightstep environment, must be one of: staging, meta, public.
9 changes: 5 additions & 4 deletions lightstep/resource_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ 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)
stream, err := c.CreateStream(
ctx,
d.Get("project_name").(string),
Expand All @@ -80,7 +81,8 @@ func resourceStreamCreate(ctx context.Context, d *schema.ResourceData, m interfa

return resource.NonRetryableError(fmt.Errorf(err[0].Summary))
}

// workaround: if read succeeds, persist the *client-side* query expression to avoid backend normalization issue
d.Set("query", origQuery)
return nil
}); err != nil {
return diag.FromErr(fmt.Errorf("Failed to create stream: %v", err))
Expand Down Expand Up @@ -173,6 +175,7 @@ func resourceStreamImport(ctx context.Context, d *schema.ResourceData, m interfa
if err := setResourceDataFromStream(d, *stream); err != nil {
return []*schema.ResourceData{}, fmt.Errorf("Failed to set stream from API response to terraform state: %v", err)
}
d.Set("query", stream.Attributes.Query)

return []*schema.ResourceData{d}, nil
}
Expand Down Expand Up @@ -223,9 +226,7 @@ func setResourceDataFromStream(d *schema.ResourceData, s client.Stream) error {
return fmt.Errorf("Unable to set custom_data resource field: %v", err)
}

if err := d.Set("query", s.Attributes.Query); err != nil {
return fmt.Errorf("Unable to set query resource field: %v", err)
}
// don't set query here to avoid backend normalization issue

return nil
}
62 changes: 61 additions & 1 deletion lightstep/resource_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package lightstep
import (
"context"
"fmt"
"github.com/lightstep/terraform-provider-lightstep/client"
"os"
"regexp"
"testing"

"github.com/lightstep/terraform-provider-lightstep/client"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
Expand Down Expand Up @@ -116,6 +117,65 @@ resource "lightstep_stream" "import-stream"{
})
}

func TestAccStreamQueryNormalization(t *testing.T) {
var stream client.Stream

query1 := `
resource "lightstep_stream" "query_one" {
project_name = ` + fmt.Sprintf("\"%s\"", test_project) + `
stream_name = "Query 1"
query = "\"error\" IN (\"true\") AND service IN (\"api\")"
}
`
query1updated := `
resource "lightstep_stream" "query_one" {
project_name = ` + fmt.Sprintf("\"%s\"", test_project) + `
stream_name = "Query One"
query = "\"error\" IN (\"true\") AND service IN (\"api\")"
}
`
query1updatedQuery := `
resource "lightstep_stream" "query_one" {
project_name = ` + fmt.Sprintf("\"%s\"", test_project) + `
stream_name = "Query One"
query = "service IN (\"api\") AND \"error\" IN (\"true\")"
}
`

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccStreamDestroy,
// each step is akin to running a `terraform apply`
Steps: []resource.TestStep{
{
Config: query1,
Check: resource.ComposeTestCheckFunc(
testAccCheckStreamExists("lightstep_stream.query_one", &stream),
resource.TestCheckResourceAttr("lightstep_stream.query_one", "stream_name", "Query 1"),
resource.TestCheckResourceAttr("lightstep_stream.query_one", "query", "\"error\" IN (\"true\") AND service IN (\"api\")"),
),
},
{
Config: query1updated,
Check: resource.ComposeTestCheckFunc(
testAccCheckStreamExists("lightstep_stream.query_one", &stream),
resource.TestCheckResourceAttr("lightstep_stream.query_one", "stream_name", "Query One"),
resource.TestCheckResourceAttr("lightstep_stream.query_one", "query", "\"error\" IN (\"true\") AND service IN (\"api\")"),
),
},
{
Config: query1updatedQuery,
Check: resource.ComposeTestCheckFunc(
testAccCheckStreamExists("lightstep_stream.query_one", &stream),
resource.TestCheckResourceAttr("lightstep_stream.query_one", "stream_name", "Query One"),
resource.TestCheckResourceAttr("lightstep_stream.query_one", "query", "service IN (\"api\") AND \"error\" IN (\"true\")"),
),
},
},
})
}

func testAccCheckStreamExists(resourceName string, stream *client.Stream) resource.TestCheckFunc {
return func(s *terraform.State) error {
// get stream from TF state
Expand Down

0 comments on commit 267bc60

Please sign in to comment.