-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks-cli.py
117 lines (84 loc) · 3.49 KB
/
webhooks-cli.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
import argparse
import getpass
import tableauserverclient as TSC
def main():
parser = argparse.ArgumentParser(description='Interact with Webhooks on Tableau Server.')
parser.add_argument('action', choices=['create', 'get', 'list', 'delete', 'test'],
help='Action to take with Webhooks')
parser.add_argument('--server', '-S', help='Server address')
parser.add_argument('--site', '-s', help='Site name')
parser.add_argument('--username', '-u', help='Username to sign into server')
args = parser.parse_args()
server = args.server if args.server else input('Server URL: ')
site = args.site if args.site else input('Site name: ')
username = args.username if args.username else input('Username: ')
password = getpass.getpass('Password: ')
tableau_auth = TSC.TableauAuth(username, password, site)
server = TSC.Server(server, use_server_version=True)
with server.auth.sign_in(tableau_auth):
print('Signed into ' + tableau_auth.site_id + '.')
action = args.action
if action == 'create':
create_webhook(server)
elif action == 'get':
get_webhook(server)
elif action == 'list':
list_webhooks(server)
elif action == 'delete':
delete_webhook(server)
elif action == 'test':
test_webhook(server)
def create_webhook(server):
webhook_name = input('Webhook name: ')
webhook_event = input('Webhook event: ')
destination_url = input('Destination URL: ')
print('Creating a new webhook...')
new_webhook = TSC.WebhookItem()
new_webhook.name = webhook_name
new_webhook.event = webhook_event
new_webhook.url = destination_url
new_webhook = server.webhooks.create(new_webhook)
print('Successfully created a new webhook.')
owner_name = get_owner_name(server, new_webhook.owner_id)
print_webhook(new_webhook, owner_name)
def get_webhook(server):
webhook_id = input('Webhook ID: ')
print('Getting webhook ' + webhook_id + '...')
webhook = server.webhooks.get_by_id(webhook_id)
owner_name = get_owner_name(server, webhook.owner_id)
print_webhook(webhook, owner_name)
def list_webhooks(server):
print('Listing all webhooks on site...')
all_webhooks, _ = server.webhooks.get()
for webhook in all_webhooks:
owner_name = get_owner_name(server, webhook.owner_id)
print_webhook(webhook, owner_name)
total_webhooks = len(all_webhooks)
if total_webhooks == 1:
print('\tThere is 1 webhook on your site.\n')
else:
print('\tThere are ' + str(total_webhooks) + ' webhooks on your site.\n')
def delete_webhook(server):
webhook_id = input('Webhook ID: ')
print('Deleting webhook ' + webhook_id + '...')
server.webhooks.delete(webhook_id)
print('Successfully deleted webhook ' + webhook_id + '.')
def test_webhook(server):
webhook_id = input('Webhook ID: ')
print('Testing webhook ' + webhook_id + '...')
test_response = server.webhooks.test(webhook_id)
print('Test result: ')
print(test_response.__dict__)
def get_owner_name(server, owner_id):
return server.users.get_by_id(owner_id).name
def print_webhook(webhook, owner_name):
print()
print('\tID: ' + webhook.id)
print('\tName: ' + webhook.name)
print('\tEvent: ' + webhook.event)
print('\tDestination URL: ' + webhook.url)
print('\tOwner ID: ' + webhook.owner_id)
print('\tOwner Name: ' + owner_name)
print()
if __name__ == '__main__':
main()