Skip to content

Commit

Permalink
fix(log_parsing): fix to the update method to discard checking for ex…
Browse files Browse the repository at this point in the history
…isting rules with the name (#2483)
  • Loading branch information
pranav-new-relic authored Oct 9, 2023
1 parent 9b5e0b1 commit e310183
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
14 changes: 8 additions & 6 deletions newrelic/resource_newrelic_log_parsing_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,17 @@ func resourceNewRelicLogParsingRuleUpdate(ctx context.Context, d *schema.Resourc
client := meta.(*ProviderConfig).NewClient
accountID := selectAccountID(meta.(*ProviderConfig), d)
if e, ok := d.GetOk("name"); ok {
rule, err := getLogParsingRuleByName(ctx, client, accountID, e.(string))
if (rule != nil && err != nil) || (rule == nil && err != nil) {
return diag.FromErr(err)
if o, n := d.GetChange("name"); o != n {
rule, err := getLogParsingRuleByName(ctx, client, accountID, e.(string))
if (rule != nil && err != nil) || (rule == nil && err != nil) {
return diag.FromErr(err)
}
}
}

updateInput := expandLogParsingRuleUpdateInput(d)

log.Printf("[INFO] Updating New Relic logging parsing rule %s", d.Id())
log.Printf("[INFO] Updating New Relic log parsing rule %s", d.Id())

ruleID := d.Id()

Expand Down Expand Up @@ -231,12 +233,12 @@ func expandLogParsingRuleUpdateInput(d *schema.ResourceData) logconfigurations.L
return updateInp
}

// Delete the logging parsing rule
// Delete the log parsing rule
func resourceNewRelicLogParsingRuleDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConfig := meta.(*ProviderConfig)
client := providerConfig.NewClient

log.Printf("[INFO] Deleting New Relic logging parsing rule id %s", d.Id())
log.Printf("[INFO] Deleting New Relic log parsing rule id %s", d.Id())

accountID := selectAccountID(meta.(*ProviderConfig), d)
ruleID := d.Id()
Expand Down
53 changes: 53 additions & 0 deletions newrelic/resource_newrelic_log_parsing_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,45 @@ func TestAccNewRelicLogParsingRule_Basic(t *testing.T) {
})
}

// TestAccNewRelicLogParsingRule_UpdateNameAndMore tests the create and update operations of log parsing rules
// by performing two updates; the first with the name changed and the second, with the name kept the
// same, and changes made only to all other arguments of the resource.
func TestAccNewRelicLogParsingRule_UpdateNameAndMore(t *testing.T) {
resourceName := "newrelic_log_parsing_rule.foo"
rName := generateNameForIntegrationTestResource()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicLogParsingRuleDestroy,
Steps: []resource.TestStep{
//create
{
Config: testAccNewRelicLogParsingRuleConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNewRelicLogParsingRuleExists(resourceName)),
},
//update
{
Config: testAccNewRelicLogParsingRuleUpdate(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNewRelicLogParsingRuleExists(resourceName)),
},
//update attributes but not the name
{
Config: testAccNewRelicLogParsingRuleUpdateArgumentsOtherThanName(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNewRelicLogParsingRuleExists(resourceName)),
},
//import
{
ImportState: true,
ImportStateVerify: true,
ResourceName: resourceName,
},
},
})
}

func TestAccNewRelicLogParsingRule_Unique_Name_Update(t *testing.T) {
resourceName := "newrelic_log_parsing_rule.foo"
expectedErrorMsg := regexp.MustCompile("name is already in use by another rule")
Expand Down Expand Up @@ -332,3 +371,17 @@ resource "newrelic_log_parsing_rule" "foo"{
}
`, testAccountID, name, testAccExpectedApplicationName)
}

func testAccNewRelicLogParsingRuleUpdateArgumentsOtherThanName(name string) string {
return fmt.Sprintf(`
resource "newrelic_log_parsing_rule" "foo"{
account_id = %[1]d
name = "%[2]s_update"
attribute = "%[3]s"
enabled = false
grok = "customattribute='%%%%{BASE10NUM:test:float}"
lucene = "responseCode:500 AND requestMethod:GET"
nrql = "SELECT * FROM Log WHERE logtype = 'nginx_error'"
}
`, testAccountID, name, testAccExpectedApplicationName)
}

0 comments on commit e310183

Please sign in to comment.