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

[DO NOT MERGE] feat: use new generated eventstometrics package #2576

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ require (
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/newrelic/newrelic-client-go/v2 => /Users/mbazhlekova/Projects/newrelic-client-go
4 changes: 1 addition & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/elazarl/goproxy v0.0.0-20220417044921-416226498f94 h1:VIy7cdK7ufs7ctpTFkXJHm1uP3dJSnCGSPysEICB1so=
github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027 h1:1L0aalTpPz7YlMxETKpmQoWMBkeiuorElZIXoNmgiPE=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down Expand Up @@ -267,8 +267,6 @@ github.com/newrelic/go-agent/v3 v3.27.0 h1:Z3XB49d8FKjRcGzCyViCO9itBxiLPSpwjY1Hl
github.com/newrelic/go-agent/v3 v3.27.0/go.mod h1:TUzePinDc0BMH4Sui66rl4SBe6yOKJ5X/bRJekwuAtM=
github.com/newrelic/go-insights v1.0.3 h1:zSNp1CEZnXktzSIEsbHJk8v6ZihdPFP2WsO/fzau3OQ=
github.com/newrelic/go-insights v1.0.3/go.mod h1:A20BoT8TNkqPGX2nS/Z2fYmKl3Cqa3iKZd4whzedCY4=
github.com/newrelic/newrelic-client-go/v2 v2.23.0 h1:ULqiPv4Z0QVLjMjX6uP13wSxPDL3uiKTt5U8vFsvpCQ=
github.com/newrelic/newrelic-client-go/v2 v2.23.0/go.mod h1:VPWTvEfKvnTZLunAC7fiW33y4e0srznNfN5HJH2cOp8=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
32 changes: 19 additions & 13 deletions newrelic/resource_newrelic_events_to_metrics_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ func resourceNewRelicEventsToMetricsRuleCreate(ctx context.Context, d *schema.Re
},
}

rules, err := client.EventsToMetrics.CreateRulesWithContext(ctx, createInput)
res, err := client.EventsToMetrics.EventsToMetricsCreateRuleWithContext(ctx, createInput)
if err != nil {
return diag.FromErr(err)
}

rule := rules[0]
if len(res.Failures) > 0 {
return diag.FromErr(fmt.Errorf("create failed %s - %s", res.Failures[0].Errors[0].Reason, res.Failures[0].Errors[0].Description))
}

rule := res.Successes[0]

id := fmt.Sprintf("%d:%s", rule.AccountID, rule.ID)

Expand All @@ -95,7 +99,7 @@ func resourceNewRelicEventsToMetricsRuleCreate(ctx context.Context, d *schema.Re
},
}

_, err := client.EventsToMetrics.UpdateRulesWithContext(ctx, updateInput)
_, err := client.EventsToMetrics.EventsToMetricsUpdateRuleWithContext(ctx, updateInput)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -115,7 +119,9 @@ func resourceNewRelicEventsToMetricsRuleRead(ctx context.Context, d *schema.Reso
return diag.FromErr(err)
}

rule, err := client.EventsToMetrics.GetRuleWithContext(ctx, accountID, ruleID)
res, err := client.EventsToMetrics.GetRulesByIdWithContext(ctx, accountID, ruleID)

rule := res.Rules[0]

if err != nil {
if _, ok := err.(*nrErrors.NotFound); ok {
Expand All @@ -130,7 +136,7 @@ func resourceNewRelicEventsToMetricsRuleRead(ctx context.Context, d *schema.Reso
return diag.FromErr(err)
}

if err := d.Set("rule_id", ruleID); err != nil {
if err := d.Set("rule_id", ruleID[0]); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -170,12 +176,12 @@ func resourceNewRelicEventsToMetricsRuleUpdate(ctx context.Context, d *schema.Re
updateInput := []eventstometrics.EventsToMetricsUpdateRuleInput{
{
AccountID: accountID,
RuleId: ruleID,
RuleId: ruleID[0],
Enabled: d.Get("enabled").(bool),
},
}

_, err = client.EventsToMetrics.UpdateRulesWithContext(ctx, updateInput)
_, err = client.EventsToMetrics.EventsToMetricsUpdateRuleWithContext(ctx, updateInput)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -197,29 +203,29 @@ func resourceNewRelicEventsToMetricsRuleDelete(ctx context.Context, d *schema.Re
deleteInput := []eventstometrics.EventsToMetricsDeleteRuleInput{
{
AccountID: accountID,
RuleId: ruleID,
RuleId: ruleID[0],
},
}

_, err = client.EventsToMetrics.DeleteRulesWithContext(ctx, deleteInput)
_, err = client.EventsToMetrics.EventsToMetricsDeleteRuleWithContext(ctx, deleteInput)
if err != nil {
return diag.FromErr(err)
}

return nil
}

func getEventsToMetricsRuleIDs(id string) (int, string, error) {
func getEventsToMetricsRuleIDs(id string) (int, []string, error) {
strIDs := strings.Split(id, ":")

if len(strIDs) != 2 {
return 0, "", errors.New("could not parse events to metrics rule IDs")
return 0, []string{}, errors.New("could not parse events to metrics rule IDs")
}

accountID, err := strconv.Atoi(strIDs[0])
if err != nil {
return 0, "", err
return 0, []string{}, err
}

return accountID, strIDs[1], nil
return accountID, []string{strIDs[1]}, nil
}
6 changes: 3 additions & 3 deletions newrelic/resource_newrelic_events_to_metrics_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func testAccCheckNewRelicEventsToMetricsRuleDestroy(s *terraform.State) error {
return err
}

_, err = client.EventsToMetrics.GetRule(accountID, ruleID)
res, err := client.EventsToMetrics.GetRulesById(accountID, ruleID)

if err == nil {
if len(res.Rules) > 0 {
return fmt.Errorf("events to metrics rule still exists: %s", err)
}
}
Expand All @@ -86,7 +86,7 @@ func testAccCheckNewRelicEventsToMetricsRuleExists(n string) resource.TestCheckF
return err
}

_, err = client.EventsToMetrics.GetRule(accountID, ruleID)
_, err = client.EventsToMetrics.GetRulesById(accountID, ruleID)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions testing/newrelic.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ terraform {
provider "newrelic" {
region = "US" # US or EU
}

resource "newrelic_events_to_metrics_rule" "tech-talk" {
account_id = "2520528"
name = "tech-talk test"
description = "test description"
nrql = "SELECT uniqueCount(account_id) AS `Transaction.account_id` FROM Transaction FACET appName, name"
enabled = false
}