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

fix(key transaction): Migrate key transaction data source from Rest api to NerdGraph #2753

Merged
merged 4 commits into from
Oct 9, 2024
Merged
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
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
Loading