comment.py (7172B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\datadog\dogshell\comment.py 2 from __future__ import print_function 3 import sys 4 from datadog import api 5 from datadog.dogshell.common import report_errors, report_warnings 6 from datadog.util.compat import json 7 8 class CommentClient(object): 9 10 @classmethod 11 def setup_parser(cls, subparsers): 12 parser = subparsers.add_parser('comment', help='Post, update, and delete comments.') 13 verb_parsers = parser.add_subparsers(title='Verbs', dest='verb') 14 verb_parsers.required = True 15 post_parser = verb_parsers.add_parser('post', help='Post comments.') 16 post_parser.add_argument('--handle', help='handle to post as. if unset, posts as the owner of the application key used to authenticate') 17 post_parser.add_argument('comment', help='comment message to post. if unset, reads from stdin.', nargs='?') 18 post_parser.set_defaults(func=cls._post) 19 update_parser = verb_parsers.add_parser('update', help='Update existing comments.') 20 update_parser.add_argument('comment_id', help='comment to update (by id)') 21 update_parser.add_argument('--handle', help='handle to post as. if unset, posts as the owner of the application key used to authenticate') 22 update_parser.add_argument('comment', help='comment message to post. if unset, reads from stdin.', nargs='?') 23 update_parser.set_defaults(func=cls._update) 24 reply_parser = verb_parsers.add_parser('reply', help='Reply to existing comments.') 25 reply_parser.add_argument('comment_id', help='comment to reply to (by id)') 26 reply_parser.add_argument('--handle', help='handle to post as. if unset, posts as the owner of the application key used to authenticate') 27 reply_parser.add_argument('comment', help='comment message to post. if unset, reads from stdin.', nargs='?') 28 reply_parser.set_defaults(func=cls._reply) 29 show_parser = verb_parsers.add_parser('show', help='Show comment details.') 30 show_parser.add_argument('comment_id', help='comment to show') 31 show_parser.set_defaults(func=cls._show) 32 delete_parser = verb_parsers.add_parser('delete', help='Delete comments.') 33 delete_parser.add_argument('comment_id', help='comment to delete (by id)') 34 delete_parser.set_defaults(func=cls._delete) 35 36 @classmethod 37 def _post(cls, args): 38 api._timeout = args.timeout 39 handle = args.handle 40 comment = args.comment 41 format = args.format 42 if comment is None: 43 comment = sys.stdin.read() 44 res = api.Comment.create(handle=handle, message=comment) 45 report_warnings(res) 46 report_errors(res) 47 if format == 'pretty': 48 message = res['comment']['message'] 49 lines = message.split('\n') 50 message = '\n'.join([ ' ' + line for line in lines ]) 51 print('id\t\t' + str(res['comment']['id'])) 52 print('url\t\t' + res['comment']['url']) 53 print('resource\t' + res['comment']['resource']) 54 print('handle\t\t' + res['comment']['handle']) 55 print('message\n' + message) 56 elif format == 'raw': 57 print(json.dumps(res)) 58 else: 59 print('id\t\t' + str(res['comment']['id'])) 60 print('url\t\t' + res['comment']['url']) 61 print('resource\t' + res['comment']['resource']) 62 print('handle\t\t' + res['comment']['handle']) 63 print('message\t\t' + res['comment']['message'].__repr__()) 64 65 @classmethod 66 def _update(cls, args): 67 handle = args.handle 68 comment = args.comment 69 id = args.comment_id 70 format = args.format 71 if comment is None: 72 comment = sys.stdin.read() 73 res = api.Comment.update(id, handle=handle, message=comment) 74 report_warnings(res) 75 report_errors(res) 76 if format == 'pretty': 77 message = res['comment']['message'] 78 lines = message.split('\n') 79 message = '\n'.join([ ' ' + line for line in lines ]) 80 print('id\t\t' + str(res['comment']['id'])) 81 print('url\t\t' + res['comment']['url']) 82 print('resource\t' + res['comment']['resource']) 83 print('handle\t\t' + res['comment']['handle']) 84 print('message\n' + message) 85 elif format == 'raw': 86 print(json.dumps(res)) 87 else: 88 print('id\t\t' + str(res['comment']['id'])) 89 print('url\t\t' + res['comment']['url']) 90 print('resource\t' + res['comment']['resource']) 91 print('handle\t\t' + res['comment']['handle']) 92 print('message\t\t' + res['comment']['message'].__repr__()) 93 94 @classmethod 95 def _reply(cls, args): 96 api._timeout = args.timeout 97 handle = args.handle 98 comment = args.comment 99 id = args.comment_id 100 format = args.format 101 if comment is None: 102 comment = sys.stdin.read() 103 res = api.Comment.create(handle=handle, message=comment, related_event_id=id) 104 report_warnings(res) 105 report_errors(res) 106 if format == 'pretty': 107 message = res['comment']['message'] 108 lines = message.split('\n') 109 message = '\n'.join([ ' ' + line for line in lines ]) 110 print('id\t\t' + str(res['comment']['id'])) 111 print('url\t\t' + res['comment']['url']) 112 print('resource\t' + res['comment']['resource']) 113 print('handle\t\t' + res['comment']['handle']) 114 print('message\n' + message) 115 elif format == 'raw': 116 print(json.dumps(res)) 117 else: 118 print('id\t\t' + str(res['comment']['id'])) 119 print('url\t\t' + res['comment']['url']) 120 print('resource\t' + res['comment']['resource']) 121 print('handle\t\t' + res['comment']['handle']) 122 print('message\t\t' + res['comment']['message'].__repr__()) 123 124 @classmethod 125 def _show(cls, args): 126 api._timeout = args.timeout 127 id = args.comment_id 128 format = args.format 129 res = api.Event.get(id) 130 report_warnings(res) 131 report_errors(res) 132 if format == 'pretty': 133 message = res['event']['text'] 134 lines = message.split('\n') 135 message = '\n'.join([ ' ' + line for line in lines ]) 136 print('id\t\t' + str(res['event']['id'])) 137 print('url\t\t' + res['event']['url']) 138 print('resource\t' + res['event']['resource']) 139 print('message\n' + message) 140 elif format == 'raw': 141 print(json.dumps(res)) 142 else: 143 print('id\t\t' + str(res['event']['id'])) 144 print('url\t\t' + res['event']['url']) 145 print('resource\t' + res['event']['resource']) 146 print('message\t\t' + res['event']['text'].__repr__()) 147 148 @classmethod 149 def _delete(cls, args): 150 api._timeout = args.timeout 151 id = args.comment_id 152 res = api.Comment.delete(id) 153 if res is not None: 154 report_warnings(res) 155 report_errors(res)