|
| 1 | +package newrelic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/newrelic/newrelic-client-go/v2/pkg/ai" |
| 10 | + "github.com/newrelic/newrelic-client-go/v2/pkg/errors" |
| 11 | + "github.com/newrelic/newrelic-client-go/v2/pkg/notifications" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | +) |
| 16 | + |
| 17 | +func dataSourceNewRelicNotificationDestination() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + ReadContext: dataSourceNewRelicNotificationDestinationRead, |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "id": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + Description: "The ID of the destination.", |
| 25 | + }, |
| 26 | + "account_id": { |
| 27 | + Type: schema.TypeInt, |
| 28 | + Optional: true, |
| 29 | + Computed: true, |
| 30 | + Description: "The account ID under which to put the destination.", |
| 31 | + }, |
| 32 | + "name": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + Description: "The name of the destination.", |
| 36 | + }, |
| 37 | + "type": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + Description: fmt.Sprintf("The type of the destination. One of: (%s).", strings.Join(listValidNotificationsDestinationTypes(), ", ")), |
| 41 | + }, |
| 42 | + "property": { |
| 43 | + Type: schema.TypeSet, |
| 44 | + Optional: true, |
| 45 | + Description: "Notification destination property type.", |
| 46 | + Elem: notificationsPropertySchema(), |
| 47 | + }, |
| 48 | + "auth_basic": { |
| 49 | + Type: schema.TypeList, |
| 50 | + Optional: true, |
| 51 | + MaxItems: 1, |
| 52 | + ConflictsWith: []string{"auth_token"}, |
| 53 | + Description: "Basic username and password authentication credentials.", |
| 54 | + Elem: &schema.Resource{ |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "user": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + "auth_token": { |
| 64 | + Type: schema.TypeList, |
| 65 | + Optional: true, |
| 66 | + MaxItems: 1, |
| 67 | + ConflictsWith: []string{"auth_basic"}, |
| 68 | + Description: "Token authentication credentials.", |
| 69 | + Elem: &schema.Resource{ |
| 70 | + Schema: map[string]*schema.Schema{ |
| 71 | + "prefix": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Optional: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + "active": { |
| 79 | + Type: schema.TypeBool, |
| 80 | + Optional: true, |
| 81 | + Description: "Indicates whether the destination is active.", |
| 82 | + Default: true, |
| 83 | + }, |
| 84 | + "status": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + Description: "The status of the destination.", |
| 88 | + }, |
| 89 | + "last_sent": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Computed: true, |
| 92 | + Description: "The last time a notification was sent.", |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func dataSourceNewRelicNotificationDestinationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 99 | + client := meta.(*ProviderConfig).NewClient |
| 100 | + |
| 101 | + log.Printf("[INFO] Reading New Relic Notification Destination") |
| 102 | + |
| 103 | + providerConfig := meta.(*ProviderConfig) |
| 104 | + accountID := selectAccountID(providerConfig, d) |
| 105 | + updatedContext := updateContextWithAccountID(ctx, accountID) |
| 106 | + id := d.Id() |
| 107 | + filters := ai.AiNotificationsDestinationFilter{ID: id} |
| 108 | + sorter := notifications.AiNotificationsDestinationSorter{} |
| 109 | + |
| 110 | + destinationResponse, err := client.Notifications.GetDestinationsWithContext(updatedContext, accountID, "", filters, sorter) |
| 111 | + if err != nil { |
| 112 | + if _, ok := err.(*errors.NotFound); ok { |
| 113 | + d.SetId("") |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + return diag.FromErr(err) |
| 118 | + } |
| 119 | + |
| 120 | + if len(destinationResponse.Entities) == 0 { |
| 121 | + d.SetId("") |
| 122 | + return diag.FromErr(fmt.Errorf("the id '%s' does not match any New Relic notification destination", id)) |
| 123 | + } |
| 124 | + |
| 125 | + errors := buildAiNotificationsResponseErrors(destinationResponse.Errors) |
| 126 | + if len(errors) > 0 { |
| 127 | + return errors |
| 128 | + } |
| 129 | + |
| 130 | + return diag.FromErr(flattenNotificationDestination(&destinationResponse.Entities[0], d)) |
| 131 | +} |
0 commit comments