Skip to content

Commit

Permalink
feat(key_transaction): migrate the key transaction resource from usin…
Browse files Browse the repository at this point in the history
…g RESTv2 to NerdGraph (#2753)

This PR addresses the migration of the Key Transaction data source from REST v2 to NerdGraph, and adds support for a couple of more exported attributes.
  • Loading branch information
vagrawal-newrelic authored Oct 9, 2024
1 parent 7acf4d9 commit 2e72740
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
70 changes: 47 additions & 23 deletions newrelic/data_source_newrelic_key_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package newrelic

import (
"context"
"errors"
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/newrelic/newrelic-client-go/v2/pkg/apm"
"github.com/newrelic/newrelic-client-go/v2/pkg/entities"
)

func dataSourceNewRelicKeyTransaction() *schema.Resource {
Expand All @@ -20,6 +20,24 @@ func dataSourceNewRelicKeyTransaction() *schema.Resource {
Required: true,
Description: "The name of the key transaction in New Relic.",
},
"guid": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "GUID of the key transaction in New Relic.",
},
"domain": {
Type: schema.TypeString,
Required: false,
Computed: true,
Description: "The Domain of the key transaction in New Relic.",
},
"type": {
Type: schema.TypeString,
Required: false,
Computed: true,
Description: "The Entity type of the key transaction in New Relic.",
},
},
}
}
Expand All @@ -28,37 +46,43 @@ func dataSourceNewRelicKeyTransactionRead(ctx context.Context, d *schema.Resourc
client := meta.(*ProviderConfig).NewClient

log.Printf("[INFO] Reading New Relic key transactions")

name := d.Get("name").(string)

params := apm.ListKeyTransactionsParams{
Name: name,
// fetch name from TF config
name, nameOk := d.GetOk("name")
if !nameOk {
return diag.FromErr(errors.New("`name` is required"))
}
// fetch guid from TF config
guid := d.Get("guid")
query := fmt.Sprintf("type = 'KEY_TRANSACTION' AND name = '%s'", name)
if guid != "" {
// if guid is provided, add it to the query to filter the results
query = fmt.Sprintf("%s AND id = '%s'", query, guid)
}

transactions, err := client.APM.ListKeyTransactionsWithContext(ctx, &params)
keyTransactionsFound, err := client.Entities.GetEntitySearchByQueryWithContext(ctx, entities.EntitySearchOptions{}, query, []entities.EntitySearchSortCriteria{})
if err != nil {
return diag.FromErr(err)
}

var transaction *apm.KeyTransaction

for _, t := range transactions {
if t.Name == name {
transaction = t
break
}
}

if transaction == nil {
return diag.FromErr(fmt.Errorf("the name '%s' does not match any New Relic key transaction", name))
if keyTransactionsFound == nil || len(keyTransactionsFound.Results.Entities) == 0 {
return diag.FromErr(fmt.Errorf("no key transaction that matches the specified parameters is found in New Relic"))
}

flattenKeyTransaction(transaction, d)
flattenKeyTransaction(keyTransactionsFound, d)

return nil
}

func flattenKeyTransaction(t *apm.KeyTransaction, d *schema.ResourceData) {
d.SetId(strconv.Itoa(t.ID))
_ = d.Set("name", t.Name)
func flattenKeyTransaction(t *entities.EntitySearch, d *schema.ResourceData) {
// iterate over the tags to get the key transaction id
for _, tag := range t.Results.Entities[0].GetTags() {
if tag.Key == "keyTransactionId" {
d.SetId(tag.Values[0])
break
}
}
_ = d.Set("guid", t.Results.Entities[0].GetGUID())
_ = d.Set("name", t.Results.Entities[0].GetName())
_ = d.Set("domain", t.Results.Entities[0].GetDomain())
_ = d.Set("type", t.Results.Entities[0].GetType())
}
7 changes: 7 additions & 0 deletions website/docs/d/key_transaction.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@ resource "newrelic_alert_condition" "foo" {
The following arguments are supported:

* `name` - (Required) The name of the key transaction in New Relic.
* `guid` - (Optional) GUID of the key transaction in New Relic.

-> **NOTE** If the `name` specified in the configuration matches the names of multiple key transactions in the account, the data source will return the first match from the list of all matching key transactions retrieved from the API. However, when using the `guid` argument as the search criterion, only the key transaction with that particular GUID is returned, as each key transaction has a unique GUID.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the application.
* `guid` - GUID of the key transaction in New Relic.
* `domain` - Domain of the key transaction in New Relic.
* `type` - Type of the key transaction in New Relic.
* `name` - Name of the key Transation in New Relic.

```
Warning: This data source will use the account ID linked to your API key. At the moment it is not possible to dynamically set the account ID.
Expand Down

0 comments on commit 2e72740

Please sign in to comment.