forked from DataDog/datadogpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.py
117 lines (105 loc) · 4.31 KB
/
tag.py
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
# Unless explicitly stated otherwise all files in this repository are licensed under the BSD-3-Clause License.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2015-Present Datadog, Inc
# stdlib
import json
# datadog
from datadog import api
from datadog.dogshell.common import report_errors, report_warnings
class TagClient(object):
@classmethod
def setup_parser(cls, subparsers):
parser = subparsers.add_parser('tag', help="View and modify host tags.")
verb_parsers = parser.add_subparsers(title='Verbs', dest='verb')
verb_parsers.required = True
add_parser = verb_parsers.add_parser('add', help="Add a host to one or more tags.",
description='Hosts can be specified by name or id.')
add_parser.add_argument('host', help="host to add")
add_parser.add_argument('tag', help="tag to add host to (one or more, space separated)",
nargs='+')
add_parser.set_defaults(func=cls._add)
replace_parser = verb_parsers.add_parser(
'replace', help="Replace all tags with one or more new tags.",
description='Hosts can be specified by name or id.')
replace_parser.add_argument('host', help="host to modify")
replace_parser.add_argument('tag', help="list of tags to add host to", nargs='+')
replace_parser.set_defaults(func=cls._replace)
show_parser = verb_parsers.add_parser('show', help="Show host tags.",
description='Hosts can be specified by name or id.')
show_parser.add_argument('host', help="host to show (or 'all' to show all tags)")
show_parser.set_defaults(func=cls._show)
detach_parser = verb_parsers.add_parser('detach', help="Remove a host from all tags.",
description='Hosts can be specified by name or id.')
detach_parser.add_argument('host', help="host to detach")
detach_parser.set_defaults(func=cls._detach)
@classmethod
def _add(cls, args):
api._timeout = args.timeout
format = args.format
res = api.Tag.create(args.host, tags=args.tag)
report_warnings(res)
report_errors(res)
if format == 'pretty':
print("Tags for '%s':" % res['host'])
for c in res['tags']:
print(' ' + c)
elif format == 'raw':
print(json.dumps(res))
else:
for c in res['tags']:
print(c)
@classmethod
def _replace(cls, args):
api._timeout = args.timeout
format = args.format
res = api.Tag.update(args.host, tags=args.tag)
report_warnings(res)
report_errors(res)
if format == 'pretty':
print("Tags for '%s':" % res['host'])
for c in res['tags']:
print(' ' + c)
elif format == 'raw':
print(json.dumps(res))
else:
for c in res['tags']:
print(c)
@classmethod
def _show(cls, args):
api._timeout = args.timeout
format = args.format
if args.host == 'all':
res = api.Tag.get_all()
else:
res = api.Tag.get(args.host)
report_warnings(res)
report_errors(res)
if args.host == 'all':
if format == 'pretty':
for tag, hosts in list(res['tags'].items()):
for host in hosts:
print(tag)
print(' ' + host)
print()
elif format == 'raw':
print(json.dumps(res))
else:
for tag, hosts in list(res['tags'].items()):
for host in hosts:
print(tag + '\t' + host)
else:
if format == 'pretty':
for tag in res['tags']:
print(tag)
elif format == 'raw':
print(json.dumps(res))
else:
for tag in res['tags']:
print(tag)
@classmethod
def _detach(cls, args):
api._timeout = args.timeout
res = api.Tag.delete(args.host)
if res is not None:
report_warnings(res)
report_errors(res)