/
data_source_newrelic_entity.go
210 lines (181 loc) · 6.04 KB
/
data_source_newrelic_entity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package newrelic
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/newrelic/newrelic-client-go/pkg/entities"
)
func dataSourceNewRelicEntity() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNewRelicEntityRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the entity in New Relic One. The first entity matching this name for the given search parameters will be returned.",
},
"ignore_case": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Ignore case when searching the entity name.",
},
"type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The entity's type. Valid values are APPLICATION, DASHBOARD, HOST, MONITOR, and WORKLOAD.",
ValidateFunc: validation.StringInSlice([]string{"APPLICATION", "DASHBOARD", "HOST", "MONITOR", "WORKLOAD"}, true),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.EqualFold(old, new) // Case fold this attribute when diffing
},
},
"domain": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The entity's domain. Valid values are APM, BROWSER, INFRA, MOBILE, SYNTH, and VIZ. If not specified, all domains are searched.",
ValidateFunc: validation.StringInSlice([]string{"APM", "BROWSER", "INFRA", "MOBILE", "SYNTH", "VIZ"}, true),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.EqualFold(old, new) // Case fold this attribute when diffing
},
},
"tag": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "A tag applied to the entity.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
Description: "The tag key.",
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "The tag value.",
},
},
},
},
"account_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The New Relic account ID associated with this entity.",
},
"application_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The domain-specific ID of the entity (only returned for APM and Browser applications).",
},
"serving_apm_application_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The browser-specific ID of the backing APM entity. (only returned for Browser applications)",
},
"guid": {
Type: schema.TypeString,
Computed: true,
Description: "A unique entity identifier.",
},
},
}
}
func dataSourceNewRelicEntityRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderConfig).NewClient
log.Printf("[INFO] Reading New Relic entities")
name := d.Get("name").(string)
ignoreCase := d.Get("ignore_case").(bool)
entityType := entities.EntitySearchQueryBuilderType(strings.ToUpper(d.Get("type").(string)))
tags := expandEntityTag(d.Get("tag").([]interface{}))
domain := entities.EntitySearchQueryBuilderDomain(strings.ToUpper(d.Get("domain").(string)))
params := entities.EntitySearchQueryBuilder{
Name: name,
Type: entityType,
Tags: tags,
Domain: domain,
}
entityResults, err := client.Entities.GetEntitySearchWithContext(ctx, entities.EntitySearchOptions{}, "", params, []entities.EntitySearchSortCriteria{})
if err != nil {
return diag.FromErr(err)
}
var entity *entities.EntityOutlineInterface
for _, e := range entityResults.Results.Entities {
// Conditional on case sensitive match
if e.GetName() == name || (ignoreCase && strings.EqualFold(e.GetName(), name)) {
entity = &e
break
}
}
if entity == nil {
return diag.FromErr(fmt.Errorf("the name '%s' does not match any New Relic One entity for the given search parameters (ignore_case: %t)", name, ignoreCase))
}
return diag.FromErr(flattenEntityData(entity, d))
}
func flattenEntityData(entity *entities.EntityOutlineInterface, d *schema.ResourceData) error {
var err error
d.SetId(string((*entity).GetGUID()))
if err = d.Set("name", (*entity).GetName()); err != nil {
return err
}
if err = d.Set("guid", (*entity).GetGUID()); err != nil {
return err
}
if err = d.Set("type", (*entity).GetType()); err != nil {
return err
}
if err = d.Set("domain", (*entity).GetDomain()); err != nil {
return err
}
if err = d.Set("account_id", (*entity).GetAccountID()); err != nil {
return err
}
// store extra values per Entity Type, have to repeat code here due to
// go handling of type switching
switch e := (*entity).(type) {
case *entities.ApmApplicationEntityOutline:
if err = d.Set("application_id", e.ApplicationID); err != nil {
return err
}
case *entities.MobileApplicationEntityOutline:
if err = d.Set("application_id", e.ApplicationID); err != nil {
return err
}
case *entities.BrowserApplicationEntityOutline:
if err = d.Set("application_id", e.ApplicationID); err != nil {
return err
}
if e.ServingApmApplicationID > 0 {
err = d.Set("serving_apm_application_id", e.ServingApmApplicationID)
if err != nil {
return err
}
}
}
return nil
}
func expandEntityTag(cfg []interface{}) []entities.EntitySearchQueryBuilderTag {
var tags []entities.EntitySearchQueryBuilderTag
if len(cfg) == 0 {
return tags
}
tags = make([]entities.EntitySearchQueryBuilderTag, 0, len(cfg))
for _, t := range cfg {
cfgTag := t.(map[string]interface{})
tag := entities.EntitySearchQueryBuilderTag{}
if k, ok := cfgTag["key"]; ok {
tag.Key = k.(string)
if v, ok := cfgTag["value"]; ok {
tag.Value = v.(string)
tags = append(tags, tag)
}
}
}
return tags
}