NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

tag.py (4092B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\datadog\dogshell\tag.py
      2 from __future__ import print_function
      3 from datadog import api
      4 from datadog.dogshell.common import report_errors, report_warnings
      5 from datadog.util.compat import json
      6 
      7 class TagClient(object):
      8 
      9     @classmethod
     10     def setup_parser(cls, subparsers):
     11         parser = subparsers.add_parser('tag', help='View and modify host tags.')
     12         verb_parsers = parser.add_subparsers(title='Verbs', dest='verb')
     13         verb_parsers.required = True
     14         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.')
     15         add_parser.add_argument('host', help='host to add')
     16         add_parser.add_argument('tag', help='tag to add host to (one or more, space separated)', nargs='+')
     17         add_parser.set_defaults(func=cls._add)
     18         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.')
     19         replace_parser.add_argument('host', help='host to modify')
     20         replace_parser.add_argument('tag', help='list of tags to add host to', nargs='+')
     21         replace_parser.set_defaults(func=cls._replace)
     22         show_parser = verb_parsers.add_parser('show', help='Show host tags.', description='Hosts can be specified by name or id.')
     23         show_parser.add_argument('host', help="host to show (or 'all' to show all tags)")
     24         show_parser.set_defaults(func=cls._show)
     25         detach_parser = verb_parsers.add_parser('detach', help='Remove a host from all tags.', description='Hosts can be specified by name or id.')
     26         detach_parser.add_argument('host', help='host to detach')
     27         detach_parser.set_defaults(func=cls._detach)
     28 
     29     @classmethod
     30     def _add(cls, args):
     31         api._timeout = args.timeout
     32         format = args.format
     33         res = api.Tag.create(args.host, tags=args.tag)
     34         report_warnings(res)
     35         report_errors(res)
     36         if format == 'pretty':
     37             print("Tags for '%s':" % res['host'])
     38             for c in res['tags']:
     39                 print('  ' + c)
     40 
     41         elif format == 'raw':
     42             print(json.dumps(res))
     43         else:
     44             for c in res['tags']:
     45                 print(c)
     46 
     47     @classmethod
     48     def _replace(cls, args):
     49         api._timeout = args.timeout
     50         format = args.format
     51         res = api.Tag.update(args.host, tags=args.tag)
     52         report_warnings(res)
     53         report_errors(res)
     54         if format == 'pretty':
     55             print("Tags for '%s':" % res['host'])
     56             for c in res['tags']:
     57                 print('  ' + c)
     58 
     59         elif format == 'raw':
     60             print(json.dumps(res))
     61         else:
     62             for c in res['tags']:
     63                 print(c)
     64 
     65     @classmethod
     66     def _show(cls, args):
     67         api._timeout = args.timeout
     68         format = args.format
     69         if args.host == 'all':
     70             res = api.Tag.get_all()
     71         else:
     72             res = api.Tag.get(args.host)
     73         report_warnings(res)
     74         report_errors(res)
     75         if args.host == 'all':
     76             if format == 'pretty':
     77                 for tag, hosts in list(res['tags'].items()):
     78                     for host in hosts:
     79                         print(tag)
     80                         print('  ' + host)
     81 
     82                     print()
     83 
     84             elif format == 'raw':
     85                 print(json.dumps(res))
     86             else:
     87                 for tag, hosts in list(res['tags'].items()):
     88                     for host in hosts:
     89                         print(tag + '\t' + host)
     90 
     91         elif format == 'pretty':
     92             for tag in res['tags']:
     93                 print(tag)
     94 
     95         elif format == 'raw':
     96             print(json.dumps(res))
     97         else:
     98             for tag in res['tags']:
     99                 print(tag)
    100 
    101     @classmethod
    102     def _detach(cls, args):
    103         api._timeout = args.timeout
    104         res = api.Tag.delete(args.host)
    105         if res is not None:
    106             report_warnings(res)
    107             report_errors(res)