|
| 1 | +package newrelic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/newrelic/newrelic-client-go/v2/pkg/common" |
| 11 | + "github.com/newrelic/newrelic-client-go/v2/pkg/entities" |
| 12 | + "github.com/newrelic/newrelic-client-go/v2/pkg/keytransaction" |
| 13 | +) |
| 14 | + |
| 15 | +func resourceNewRelicKeyTransaction() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + CreateContext: resourceNewRelicKeyTransactionCreate, |
| 18 | + ReadContext: resourceNewRelicKeyTransactionRead, |
| 19 | + UpdateContext: resourceNewRelicKeyTransactionUpdate, |
| 20 | + DeleteContext: resourceNewRelicKeyTransactionDelete, |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + StateContext: schema.ImportStatePassthroughContext, |
| 23 | + }, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "apdex_index": { |
| 26 | + Type: schema.TypeFloat, |
| 27 | + Description: "The acceptable amount of the time spent in the backend before customers get frustrated (Apdex target)", |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + "application_guid": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Description: "The GUID of the application.", |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + }, |
| 36 | + "browser_apdex_target": { |
| 37 | + Type: schema.TypeFloat, |
| 38 | + Description: "The acceptable amount of time for rendering a page in a browser before customers get frustrated (browser Apdex target).", |
| 39 | + Required: true, |
| 40 | + }, |
| 41 | + "metric_name": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Description: "The name of the metric underlying this key transaction", |
| 44 | + Required: true, |
| 45 | + ForceNew: true, |
| 46 | + }, |
| 47 | + "name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Description: "The name of the key transaction.", |
| 50 | + Required: true, |
| 51 | + }, |
| 52 | + "domain": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "Domain of the entity.", |
| 55 | + Required: false, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "type": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "Type of the entity.", |
| 61 | + Required: false, |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func resourceNewRelicKeyTransactionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 69 | + client := meta.(*ProviderConfig).NewClient |
| 70 | + |
| 71 | + apdexIndex, |
| 72 | + applicationGUID, |
| 73 | + browserApdexTarget, |
| 74 | + metricName, |
| 75 | + name := resourceNewRelicKeyTransactionFetchValuesFromConfig(d) |
| 76 | + |
| 77 | + log.Printf("[INFO] Creating New Relic Key Transaction %s", name) |
| 78 | + createKeyTransactionResult, err := client.KeyTransaction.KeyTransactionCreate( |
| 79 | + apdexIndex, |
| 80 | + keytransaction.EntityGUID(applicationGUID), |
| 81 | + browserApdexTarget, |
| 82 | + metricName, |
| 83 | + name, |
| 84 | + ) |
| 85 | + |
| 86 | + if err != nil { |
| 87 | + return diag.FromErr(err) |
| 88 | + } |
| 89 | + if createKeyTransactionResult == nil { |
| 90 | + return diag.FromErr(fmt.Errorf("something went wrong while creating the key transaction")) |
| 91 | + } |
| 92 | + |
| 93 | + resourceNewRelicKeyTransactionSetValuesToState(d, *createKeyTransactionResult, keytransaction.KeyTransactionUpdateResult{}) |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func resourceNewRelicKeyTransactionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 99 | + providerConfig := meta.(*ProviderConfig) |
| 100 | + client := providerConfig.NewClient |
| 101 | + |
| 102 | + guid := d.Id() |
| 103 | + |
| 104 | + resp, err := client.Entities.GetEntityWithContext(ctx, common.EntityGUID(guid)) |
| 105 | + if err != nil { |
| 106 | + return diag.FromErr(err) |
| 107 | + } |
| 108 | + |
| 109 | + if resp == nil { |
| 110 | + d.SetId("") |
| 111 | + return diag.FromErr(fmt.Errorf("no key transaction found with given guid %s", guid)) |
| 112 | + } |
| 113 | + |
| 114 | + switch (*resp).(type) { |
| 115 | + case *entities.KeyTransactionEntity: |
| 116 | + entity := (*resp).(*entities.KeyTransactionEntity) |
| 117 | + _ = d.Set("apdex_index", entity.ApdexTarget) |
| 118 | + _ = d.Set("application_guid", entity.Application.GUID) |
| 119 | + _ = d.Set("browser_apdex_target", entity.BrowserApdexTarget) |
| 120 | + _ = d.Set("metric_name", entity.MetricName) |
| 121 | + _ = d.Set("name", entity.Name) |
| 122 | + default: |
| 123 | + return diag.FromErr(fmt.Errorf("entity with GUID %s was not a key transaction", guid)) |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func resourceNewRelicKeyTransactionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 129 | + client := meta.(*ProviderConfig).NewClient |
| 130 | + |
| 131 | + apdexIndex, |
| 132 | + _, |
| 133 | + browserApdexTarget, |
| 134 | + _, |
| 135 | + name := resourceNewRelicKeyTransactionFetchValuesFromConfig(d) |
| 136 | + |
| 137 | + guid := keytransaction.EntityGUID(d.Id()) |
| 138 | + log.Printf("[INFO] Updating New Relic Key Transaction %s", name) |
| 139 | + |
| 140 | + updateKeyTransactionResult, err := client.KeyTransaction.KeyTransactionUpdate(apdexIndex, browserApdexTarget, guid, name) |
| 141 | + |
| 142 | + if err != nil { |
| 143 | + |
| 144 | + return diag.FromErr(err) |
| 145 | + } |
| 146 | + if updateKeyTransactionResult == nil { |
| 147 | + return diag.FromErr(fmt.Errorf("something went wrong while updating the key transaction")) |
| 148 | + } |
| 149 | + |
| 150 | + resourceNewRelicKeyTransactionSetValuesToState(d, keytransaction.KeyTransactionCreateResult{}, *updateKeyTransactionResult) |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func resourceNewRelicKeyTransactionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 155 | + client := meta.(*ProviderConfig).NewClient |
| 156 | + keyTransactionGUID := keytransaction.EntityGUID(d.Id()) |
| 157 | + |
| 158 | + log.Printf("[INFO] Deleting New Relic Key Transaction %s", d.Id()) |
| 159 | + _, err := client.KeyTransaction.KeyTransactionDelete(keyTransactionGUID) |
| 160 | + |
| 161 | + if err != nil { |
| 162 | + return diag.FromErr(err) |
| 163 | + } |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func resourceNewRelicKeyTransactionFetchValuesFromConfig(d *schema.ResourceData) ( |
| 168 | + apdexIndex float64, |
| 169 | + applicationGUID string, |
| 170 | + browserApdexTarget float64, |
| 171 | + metricName string, |
| 172 | + name string, |
| 173 | +) { |
| 174 | + apdexIndex = d.Get("apdex_index").(float64) |
| 175 | + applicationGUID = d.Get("application_guid").(string) |
| 176 | + browserApdexTarget = d.Get("browser_apdex_target").(float64) |
| 177 | + metricName = d.Get("metric_name").(string) |
| 178 | + name = d.Get("name").(string) |
| 179 | + |
| 180 | + return apdexIndex, applicationGUID, browserApdexTarget, metricName, name |
| 181 | +} |
| 182 | + |
| 183 | +func resourceNewRelicKeyTransactionSetValuesToState( |
| 184 | + d *schema.ResourceData, |
| 185 | + createKeyTransactionResult keytransaction.KeyTransactionCreateResult, |
| 186 | + updateKeyTransactionResult keytransaction.KeyTransactionUpdateResult, |
| 187 | +) { |
| 188 | + if createKeyTransactionResult.GUID != "" && updateKeyTransactionResult.Name == "" { |
| 189 | + d.SetId(string(createKeyTransactionResult.GUID)) |
| 190 | + _ = d.Set("apdex_index", createKeyTransactionResult.ApdexTarget) |
| 191 | + _ = d.Set("application_guid", createKeyTransactionResult.Application.GUID) |
| 192 | + _ = d.Set("browser_apdex_target", createKeyTransactionResult.BrowserApdexTarget) |
| 193 | + _ = d.Set("metric_name", createKeyTransactionResult.MetricName) |
| 194 | + _ = d.Set("name", createKeyTransactionResult.Name) |
| 195 | + entity := createKeyTransactionResult.Application.Entity.(*keytransaction.ApmApplicationEntityOutline) |
| 196 | + _ = d.Set("domain", entity.Domain) |
| 197 | + _ = d.Set("type", entity.Type) |
| 198 | + } else if createKeyTransactionResult.GUID == "" && updateKeyTransactionResult.Name != "" { |
| 199 | + _ = d.Set("apdex_index", updateKeyTransactionResult.ApdexTarget) |
| 200 | + _ = d.Set("application_guid", updateKeyTransactionResult.Application.GUID) |
| 201 | + _ = d.Set("browser_apdex_target", updateKeyTransactionResult.BrowserApdexTarget) |
| 202 | + _ = d.Set("name", updateKeyTransactionResult.Name) |
| 203 | + entity := updateKeyTransactionResult.Application.Entity.(*keytransaction.ApmApplicationEntityOutline) |
| 204 | + _ = d.Set("domain", entity.Domain) |
| 205 | + _ = d.Set("type", entity.Type) |
| 206 | + } |
| 207 | +} |
0 commit comments