This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
applications.go
73 lines (61 loc) · 1.79 KB
/
applications.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
package api
import (
"fmt"
"net/url"
"strconv"
)
// ApplicationsFilters represents a set of filters to be
// used when querying New Relic applications.
type ApplicationsFilters struct {
Name *string
Host *string
IDs []int
Language *string
}
// QueryApplications queries for New Relic applications
// with filters to narrow down the result set. This can
// result in less paging required, for instance if you know
// the name of the application you are searching for ahead of time.
func (c *Client) QueryApplications(filters ApplicationsFilters) ([]Application, error) {
applications := []Application{}
reqURL, err := url.Parse("/applications.json")
if err != nil {
return nil, err
}
qs := reqURL.Query()
if filters.Name != nil {
qs.Set("filter[name]", *filters.Name)
}
if filters.Host != nil {
qs.Set("filter[host]", *filters.Host)
}
for _, id := range filters.IDs {
qs.Add("filter[ids]", strconv.Itoa(id))
}
if filters.Language != nil {
qs.Set("filter[language]", *filters.Language)
}
reqURL.RawQuery = qs.Encode()
nextPath := reqURL.String()
for nextPath != "" {
resp := struct {
Applications []Application `json:"applications,omitempty"`
}{}
nextPath, err = c.Do("GET", nextPath, nil, &resp)
if err != nil {
return nil, err
}
applications = append(applications, resp.Applications...)
}
return applications, nil
}
// ListApplications lists all the applications you have access to.
func (c *Client) ListApplications() ([]Application, error) {
return c.QueryApplications(ApplicationsFilters{})
}
// DeleteApplication deletes a non-reporting application from your account.
func (c *Client) DeleteApplication(id int) error {
u := &url.URL{Path: fmt.Sprintf("/applications/%v.json", id)}
_, err := c.Do("DELETE", u.String(), nil, nil)
return err
}