diff --git a/newrelic/data_source_newrelic_key_transaction.go b/newrelic/data_source_newrelic_key_transaction.go index 362c819be..e332bef77 100644 --- a/newrelic/data_source_newrelic_key_transaction.go +++ b/newrelic/data_source_newrelic_key_transaction.go @@ -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 { @@ -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.", + }, }, } } @@ -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, ¶ms) + 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()) } diff --git a/website/docs/d/key_transaction.html.markdown b/website/docs/d/key_transaction.html.markdown index cba2f31ad..148783b29 100644 --- a/website/docs/d/key_transaction.html.markdown +++ b/website/docs/d/key_transaction.html.markdown @@ -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.