|
| 1 | +package newrelic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 9 | + "github.com/newrelic/newrelic-client-go/pkg/apm" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceNewRelicApplicationSettings() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceNewRelicApplicationSettingsCreate, |
| 15 | + Read: resourceNewRelicApplicationSettingsRead, |
| 16 | + Update: resourceNewRelicApplicationSettingsUpdate, |
| 17 | + Delete: resourceNewRelicApplicationSettingsDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + State: schema.ImportStatePassthrough, |
| 20 | + }, |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "name": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + }, |
| 26 | + "app_apdex_threshold": { |
| 27 | + Type: schema.TypeFloat, |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + "end_user_apdex_threshold": { |
| 31 | + Type: schema.TypeFloat, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + "enable_real_user_monitoring": { |
| 35 | + Type: schema.TypeBool, |
| 36 | + Required: true, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func resourceNewRelicApplicationSettingsCreate(d *schema.ResourceData, meta interface{}) error { |
| 43 | + client := meta.(*ProviderConfig).NewClient |
| 44 | + |
| 45 | + userApp := expandApplication(d) |
| 46 | + |
| 47 | + listParams := apm.ListApplicationsParams{ |
| 48 | + Name: userApp.Name, |
| 49 | + } |
| 50 | + |
| 51 | + result, err := client.APM.ListApplications(&listParams) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + if len(result) != 1 { |
| 57 | + return fmt.Errorf("more/less than one result from query for %s", userApp.Name) |
| 58 | + } |
| 59 | + |
| 60 | + app := *result[0] |
| 61 | + |
| 62 | + if app.Name != userApp.Name { |
| 63 | + return fmt.Errorf("the result name %s does not match requested name %s", app.Name, userApp.Name) |
| 64 | + } |
| 65 | + |
| 66 | + d.SetId(strconv.Itoa(app.ID)) |
| 67 | + |
| 68 | + log.Printf("[INFO] Importing New Relic application %v", userApp.Name) |
| 69 | + return resourceNewRelicApplicationSettingsUpdate(d, meta) |
| 70 | +} |
| 71 | + |
| 72 | +func resourceNewRelicApplicationSettingsRead(d *schema.ResourceData, meta interface{}) error { |
| 73 | + client := meta.(*ProviderConfig).NewClient |
| 74 | + |
| 75 | + userApp := expandApplication(d) |
| 76 | + log.Printf("[INFO] Reading New Relic application %+v", userApp) |
| 77 | + |
| 78 | + app, err := client.APM.GetApplication(userApp.ID) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + log.Printf("[INFO] Read found New Relic application %+v\n\n\n", app) |
| 84 | + |
| 85 | + return flattenApplication(app, d) |
| 86 | +} |
| 87 | + |
| 88 | +func resourceNewRelicApplicationSettingsUpdate(d *schema.ResourceData, meta interface{}) error { |
| 89 | + client := meta.(*ProviderConfig).NewClient |
| 90 | + |
| 91 | + userApp := expandApplication(d) |
| 92 | + |
| 93 | + updateParams := apm.UpdateApplicationParams{ |
| 94 | + Name: userApp.Name, |
| 95 | + Settings: userApp.Settings, |
| 96 | + } |
| 97 | + |
| 98 | + log.Printf("[INFO] Updating New Relic application %+v with params: %+v", userApp, updateParams) |
| 99 | + |
| 100 | + _, err := client.APM.UpdateApplication(userApp.ID, updateParams) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + return resourceNewRelicApplicationSettingsRead(d, meta) |
| 106 | +} |
| 107 | + |
| 108 | +func resourceNewRelicApplicationSettingsDelete(d *schema.ResourceData, meta interface{}) error { |
| 109 | + // You can not delete application settings |
| 110 | + return nil |
| 111 | +} |
0 commit comments