Skip to content

Commit

Permalink
Merge pull request #725 from newrelic/refactor/ng-insights-custom-events
Browse files Browse the repository at this point in the history
refactor(insights_event): migrate insights querying to nerdgraph nrql query (#625)
  • Loading branch information
sanderblue authored Jun 25, 2020
2 parents 5067264 + 514c166 commit 7cb4963
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 94 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
25 changes: 0 additions & 25 deletions newrelic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 0 additions & 17 deletions newrelic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down
51 changes: 0 additions & 51 deletions newrelic/resource_newrelic_insights_event_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package newrelic

import (
"errors"
"fmt"
"testing"
"time"
Expand All @@ -26,63 +25,13 @@ 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"),
),
},
},
})
}

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")
}

client := testAccProvider.Meta().(*ProviderConfig).InsightsQueryClient

for _, nrql := range nrqls {
resp, err := client.QueryEvents(nrql)
if err != nil {
return err
}
if len(resp.Results) != 1 {
return errors.New("did not find Insights event")
}
}

return nil
}
}

func testAccCheckNewRelicInsightsEventConfig(eType string, t int64) string {
return fmt.Sprintf(`
resource "newrelic_insights_event" "foo" {
Expand Down

0 comments on commit 7cb4963

Please sign in to comment.