File: tags.go

package info (click to toggle)
golang-github-zorkian-go-datadog-api 2.30.0-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,616 kB
  • sloc: makefile: 28; sh: 13
file content (93 lines) | stat: -rw-r--r-- 2,756 bytes parent folder | download | duplicates (3)
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
/*
 * Datadog API for Go
 *
 * Please see the included LICENSE file for licensing information.
 *
 * Copyright 2013 by authors and contributors.
 */

package datadog

// TagMap is used to receive the format given to us by the API.
type TagMap map[string][]string

// reqGetTags is the container for receiving tags.
type reqGetTags struct {
	Tags *TagMap `json:"tags,omitempty"`
}

// regGetHostTags is for receiving a slice of tags.
type reqGetHostTags struct {
	Tags []string `json:"tags,omitempty"`
}

// GetTags returns a map of tags.
func (client *Client) GetTags(source string) (TagMap, error) {
	var out reqGetTags
	uri := "/v1/tags/hosts"
	if source != "" {
		uri += "?source=" + source
	}
	if err := client.doJsonRequest("GET", uri, nil, &out); err != nil {
		return nil, err
	}
	return *out.Tags, nil
}

// GetHostTags returns a slice of tags for a given host and source.
func (client *Client) GetHostTags(host, source string) ([]string, error) {
	var out reqGetHostTags
	uri := "/v1/tags/hosts/" + host
	if source != "" {
		uri += "?source=" + source
	}
	if err := client.doJsonRequest("GET", uri, nil, &out); err != nil {
		return nil, err
	}
	return out.Tags, nil
}

// GetHostTagsBySource is a different way of viewing the tags. It returns a map
// of source:[tag,tag].
func (client *Client) GetHostTagsBySource(host, source string) (TagMap, error) {
	var out reqGetTags
	uri := "/v1/tags/hosts/" + host + "?by_source=true"
	if source != "" {
		uri += "&source=" + source
	}
	if err := client.doJsonRequest("GET", uri, nil, &out); err != nil {
		return nil, err
	}
	return *out.Tags, nil
}

// AddTagsToHost does exactly what it says on the tin. Given a list of tags,
// add them to the host. The source is optionally specified, and defaults to
// "users" as per the API documentation.
func (client *Client) AddTagsToHost(host, source string, tags []string) error {
	uri := "/v1/tags/hosts/" + host
	if source != "" {
		uri += "?source=" + source
	}
	return client.doJsonRequest("POST", uri, reqGetHostTags{Tags: tags}, nil)
}

// UpdateHostTags overwrites existing tags for a host, allowing you to specify
// a new set of tags for the given source. This defaults to "users".
func (client *Client) UpdateHostTags(host, source string, tags []string) error {
	uri := "/v1/tags/hosts/" + host
	if source != "" {
		uri += "?source=" + source
	}
	return client.doJsonRequest("PUT", uri, reqGetHostTags{Tags: tags}, nil)
}

// RemoveHostTags removes all tags from a host for the given source. If none is
// given, the API defaults to "users".
func (client *Client) RemoveHostTags(host, source string) error {
	uri := "/v1/tags/hosts/" + host
	if source != "" {
		uri += "?source=" + source
	}
	return client.doJsonRequest("DELETE", uri, nil, nil)
}