From 4ba2c0a25b8fb82433802b388d9fd65c1d77dae4 Mon Sep 17 00:00:00 2001 From: Sander Blue Date: Wed, 24 Jun 2020 17:18:59 -0700 Subject: [PATCH 1/3] refactor(insights_event): migrate insights querying to nerdgraph nrql query --- .github/workflows/test.yml | 1 - newrelic/config.go | 25 ------------------- newrelic/provider.go | 17 ------------- .../resource_newrelic_insights_event_test.go | 7 ++++-- 4 files changed, 5 insertions(+), 45 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 54d802499..255a129d2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -96,5 +96,4 @@ jobs: NEW_RELIC_LICENSE_KEY: ${{ secrets.NEW_RELIC_LICENSE_KEY }} NEW_RELIC_REGION: ${{ secrets.NEW_RELIC_REGION }} NEW_RELIC_INSIGHTS_INSERT_KEY: ${{ secrets.NEW_RELIC_INSIGHTS_INSERT_KEY }} - NEW_RELIC_INSIGHTS_QUERY_KEY: ${{ secrets.NEW_RELIC_INSIGHTS_QUERY_KEY }} NR_ACC_TESTING: ${{ secrets.NR_ACC_TESTING }} diff --git a/newrelic/config.go b/newrelic/config.go index e3cf781cd..6b98ce8ba 100644 --- a/newrelic/config.go +++ b/newrelic/config.go @@ -130,35 +130,10 @@ func (c *Config) ClientInsightsInsert() (*insights.InsertClient, error) { return client, nil } -// ClientInsightsQuery returns a new Insights query client -func (c *Config) ClientInsightsQuery() (*insights.QueryClient, error) { - client := insights.NewQueryClient(c.InsightsQueryKey, c.InsightsAccountID) - - if c.InsightsQueryURL != "" { - insightsURL, err := url.Parse(c.InsightsQueryURL) - if err != nil { - return nil, fmt.Errorf("error parsing Insights URL: %q", err) - } - insightsURL.Path = fmt.Sprintf("%s/%s/query", insightsURL.Path, c.InsightsAccountID) - client.URL = insightsURL - } - - if len(c.InsightsQueryKey) > 1 { - if err := client.Validate(); err != nil { - return nil, err - } - } - - log.Printf("[INFO] New Relic Insights query client configured") - - return client, nil -} - // ProviderConfig for the custom provider type ProviderConfig struct { NewClient *nr.NewRelic InsightsInsertClient *insights.InsertClient - InsightsQueryClient *insights.QueryClient AccountID int PersonalAPIKey string } diff --git a/newrelic/provider.go b/newrelic/provider.go index 885ee188c..c940197bd 100644 --- a/newrelic/provider.go +++ b/newrelic/provider.go @@ -91,12 +91,6 @@ func Provider() terraform.ResourceProvider { Optional: true, DefaultFunc: schema.EnvDefaultFunc("NEW_RELIC_INSIGHTS_INSERT_URL", insightsInsertURL), }, - "insights_query_key": { - Type: schema.TypeString, - Optional: true, - DefaultFunc: schema.EnvDefaultFunc("NEW_RELIC_INSIGHTS_QUERY_KEY", nil), - Sensitive: true, - }, "insights_query_url": { Type: schema.TypeString, Optional: true, @@ -196,20 +190,9 @@ func providerConfigure(data *schema.ResourceData, terraformVersion string) (inte return nil, fmt.Errorf("error initializing New Relic Insights insert client: %w", err) } - insightsQueryConfig := Config{ - InsightsAccountID: strconv.Itoa(accountID), - InsightsQueryKey: data.Get("insights_query_key").(string), - InsightsQueryURL: data.Get("insights_query_url").(string), - } - clientInsightsQuery, err := insightsQueryConfig.ClientInsightsQuery() - if err != nil { - return nil, fmt.Errorf("error initializing New Relic Insights query client: %s", err) - } - providerConfig := ProviderConfig{ NewClient: client, InsightsInsertClient: clientInsightsInsert, - InsightsQueryClient: clientInsightsQuery, PersonalAPIKey: personalAPIKey, AccountID: accountID, } diff --git a/newrelic/resource_newrelic_insights_event_test.go b/newrelic/resource_newrelic_insights_event_test.go index d2c1e1290..83b5f87bc 100644 --- a/newrelic/resource_newrelic_insights_event_test.go +++ b/newrelic/resource_newrelic_insights_event_test.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/newrelic/newrelic-client-go/pkg/nrdb" ) func TestAccNewRelicInsightsEvent_Basic(t *testing.T) { @@ -67,10 +68,12 @@ func testAccCheckNewRelicInsightsEventExists(n string, nrqls []string) resource. return fmt.Errorf("no event ID is set") } - client := testAccProvider.Meta().(*ProviderConfig).InsightsQueryClient + providerConfig := testAccProvider.Meta().(*ProviderConfig) + client := providerConfig.NewClient + accountID := providerConfig.AccountID for _, nrql := range nrqls { - resp, err := client.QueryEvents(nrql) + resp, err := client.Nrdb.Query(accountID, nrdb.Nrql(nrql)) if err != nil { return err } From a2dddb0c7c0c60406b158334204b0f210c942fcf Mon Sep 17 00:00:00 2001 From: Sander Blue Date: Thu, 25 Jun 2020 11:11:32 -0700 Subject: [PATCH 2/3] chore(insights_event): handle async operation by delaying the check for the custom events --- .../resource_newrelic_insights_event_test.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/newrelic/resource_newrelic_insights_event_test.go b/newrelic/resource_newrelic_insights_event_test.go index 83b5f87bc..dad1acaf3 100644 --- a/newrelic/resource_newrelic_insights_event_test.go +++ b/newrelic/resource_newrelic_insights_event_test.go @@ -1,8 +1,8 @@ package newrelic import ( - "errors" "fmt" + "strings" "testing" "time" @@ -72,16 +72,28 @@ func testAccCheckNewRelicInsightsEventExists(n string, nrqls []string) resource. client := providerConfig.NewClient accountID := providerConfig.AccountID + // Due to the asynchronous operation, we need to + // wait for the events to propagate. + time.Sleep(15 * time.Second) + + var errs []string for _, nrql := range nrqls { resp, err := client.Nrdb.Query(accountID, nrdb.Nrql(nrql)) if err != nil { - return err + errs = append(errs, err.Error()) } + if len(resp.Results) != 1 { - return errors.New("did not find Insights event") + errs = append(errs, fmt.Sprintf("[Error] Insights event not found (likely due to async issue) - query: %v", nrql)) } } + if len(errs) > 0 { + fmt.Printf("test case TestAccNewRelicInsightsEvent_Basic failed (likely due to asynchronous)") + + return fmt.Errorf("%v", strings.Join(errs, "\n")) + } + return nil } } From 514c166f4d5d1fff7e1a82d10f00afadf677a128 Mon Sep 17 00:00:00 2001 From: Sander Blue Date: Thu, 25 Jun 2020 13:10:34 -0700 Subject: [PATCH 3/3] chore(insights_event): remove extraneous testAccCheckNewRelicInsightsEventExists --- .../resource_newrelic_insights_event_test.go | 66 ------------------- 1 file changed, 66 deletions(-) diff --git a/newrelic/resource_newrelic_insights_event_test.go b/newrelic/resource_newrelic_insights_event_test.go index dad1acaf3..db58ad4c3 100644 --- a/newrelic/resource_newrelic_insights_event_test.go +++ b/newrelic/resource_newrelic_insights_event_test.go @@ -2,14 +2,12 @@ package newrelic import ( "fmt" - "strings" "testing" "time" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" - "github.com/newrelic/newrelic-client-go/pkg/nrdb" ) func TestAccNewRelicInsightsEvent_Basic(t *testing.T) { @@ -27,30 +25,6 @@ func TestAccNewRelicInsightsEvent_Basic(t *testing.T) { { Config: testAccCheckNewRelicInsightsEventConfig(eType, tNow), Check: resource.ComposeTestCheckFunc( - testAccCheckNewRelicInsightsEventExists( - "newrelic_insights_event.foo", - []string{ - fmt.Sprintf( - "SELECT * FROM tf_test_%s "+ - "WHERE event_test = 'checking floats' "+ - "AND a_float = 101.1 "+ - "AND timestamp = %d", - eType, tNow), - fmt.Sprintf( - "SELECT * FROM tf_test_%s "+ - "WHERE event_test = 'checking ints' "+ - "AND an_int = 42 "+ - "AND timestamp = %d", - eType, tNow), - fmt.Sprintf( - "SELECT * FROM tf_test_%s "+ - "WHERE event_test = 'checking strings' "+ - "AND a_string = 'a string' "+ - "AND another_string = 'another string' "+ - "AND timestamp = %d", - eType, tNow), - }, - ), resource.TestCheckResourceAttr("newrelic_insights_event.foo", "event.#", "3"), ), }, @@ -58,46 +32,6 @@ func TestAccNewRelicInsightsEvent_Basic(t *testing.T) { }) } -func testAccCheckNewRelicInsightsEventExists(n string, nrqls []string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("not found: %s", n) - } - if rs.Primary.ID == "" { - return fmt.Errorf("no event ID is set") - } - - providerConfig := testAccProvider.Meta().(*ProviderConfig) - client := providerConfig.NewClient - accountID := providerConfig.AccountID - - // Due to the asynchronous operation, we need to - // wait for the events to propagate. - time.Sleep(15 * time.Second) - - var errs []string - for _, nrql := range nrqls { - resp, err := client.Nrdb.Query(accountID, nrdb.Nrql(nrql)) - if err != nil { - errs = append(errs, err.Error()) - } - - if len(resp.Results) != 1 { - errs = append(errs, fmt.Sprintf("[Error] Insights event not found (likely due to async issue) - query: %v", nrql)) - } - } - - if len(errs) > 0 { - fmt.Printf("test case TestAccNewRelicInsightsEvent_Basic failed (likely due to asynchronous)") - - return fmt.Errorf("%v", strings.Join(errs, "\n")) - } - - return nil - } -} - func testAccCheckNewRelicInsightsEventConfig(eType string, t int64) string { return fmt.Sprintf(` resource "newrelic_insights_event" "foo" {