-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
data_source_newrelic_application.go
65 lines (53 loc) · 1.35 KB
/
data_source_newrelic_application.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
package newrelic
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
newrelic "github.com/paultyng/go-newrelic/api"
)
func dataSourceNewRelicApplication() *schema.Resource {
return &schema.Resource{
Read: dataSourceNewRelicApplicationRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"instance_ids": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeInt},
Computed: true,
},
"host_ids": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeInt},
Computed: true,
},
},
}
}
func dataSourceNewRelicApplicationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*newrelic.Client)
log.Printf("[INFO] Reading New Relic applications")
applications, err := client.ListApplications()
if err != nil {
return err
}
var application *newrelic.Application
name := d.Get("name").(string)
for _, a := range applications {
if a.Name == name {
application = &a
break
}
}
if application == nil {
return fmt.Errorf("The name '%s' does not match any New Relic applications.", name)
}
d.SetId(strconv.Itoa(application.ID))
d.Set("name", application.Name)
d.Set("instance_ids", application.Links.InstanceIDs)
d.Set("host_ids", application.Links.HostIDs)
return nil
}