-
Notifications
You must be signed in to change notification settings - Fork 0
/
restya-cli.py
executable file
·330 lines (273 loc) · 10.9 KB
/
restya-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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import click
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/modules')
from restya.restya import *
@click.group()
@click.pass_context
def cli(ctx):
"""First paragraph.
This is a very long second paragraph and as you
can see wrapped very early in the source text
but will be rewrapped to the terminal width in
the final output.
\b
This is
a paragraph
without rewrapping.
And this is a paragraph
that will be rewrapped again.
"""
ctx.obj = {}
ctx.obj['restyaboard'] = Restyaboard({
'username': 'admin',
'password': 'restya',
'server': 'centos-docker',
'port': 1234,
})
# Get access token
ctx.obj['restyaboard'].get_access_token()
# Login to the system
ctx.obj['restyaboard'].login('[email protected]', 'restya')
# pass
##########################
# Organizations function #
##########################
def organization_create(restya, params):
if restya.create_organization(params['org_name'], 1) is not None:
print("Organizaton %s created." % (params['org_name']))
else:
print("Organization '%s' already exists." % (params['org_name']))
def organization_delete(restya, params):
if restya.delete_organization({'name': params['org_name']}) is not None:
print("Organization %s deleted successfully" % (params['org_name']))
else:
print("Organization %s unknown" % (params['org_name']))
def organization_list(restya, params):
for org in restya.get_all_organizations():
print("Name: %s, id: %d" % (org.name, org.id))
def organization_list_boards(restya, params):
org = restya.get_organization({'name': params['org_name']})
if org is not None:
for b in org.get_all_boards():
print("Name: %s, id: %d" % (b.name, b.id))
else:
print("Organization %s unknown" % (params['org_name']))
def organization_close_board(restya, params):
org = restya.get_organization({'name': params['org_name']})
if org is not None:
if org.get_board({'name': params['board_name']}) is not None:
org.close_board({'name': params['board_name']})
print("Board '%s' closed successfully." % (params['board_name']))
else:
print("board not found")
else:
print("org not found")
# Todo
# - Manage visibility
def organization_create_board(restya, params):
org = restya.get_organization({'name': params['org_name']})
if org is not None:
if org.get_board({'name': params['board_name']}) is None:
org.create_board(params['board_name'], 1)
print("Board '%s' created inside '%s' successfully." % (params['board_name'], params['org_name']))
else:
print("Board already exists")
else:
print("Organization %s unknown" % (params['org_name']))
def organization_delete_board(restya, params):
org = restya.get_organization({'name': params['org_name']})
if org is not None:
if org.get_board({'name': params['board_name']}) is not None:
org.delete_board(params['board_name'])
print("Board '%s' deleted successfully from '%s'." % (params['board_name'], params['org_name']))
else:
print("board not found")
else:
print("org not found")
def validate_actions(ctx, param, value):
possible_values = ('close-board', 'create', 'create-board', 'delete', 'delete-board', 'list', 'list-boards')
if value in possible_values:
return(value)
else:
raise click.BadParameter("must be one of " + ', '.join(possible_values))
@cli.command(short_help='Action on organizations')
@click.argument('action', callback=validate_actions)
@click.option('--org-name', '-o', help='Name of the organization')
@click.option('--board-name', '-b', help='Name of the board')
@click.pass_context
def organization(ctx, **kwargs):
"""\b
ACTION can be on the following keywords:
close-board
create
create-board
delete
delete-board
list
list-boards
"""
dispatcher = {
'close-board': organization_close_board,
'create': organization_create,
'create-board': organization_create_board,
'delete': organization_delete,
'delete-board': organization_delete_board,
'list': organization_list,
'list-boards': organization_list_boards,
}
# if kwargs['action'] in ('create', 'delete', 'list-boards'):
if kwargs['org_name'] is None and kwargs['action'] != 'list':
print("You must specify an organization name.")
sys.exit(1)
if kwargs['board_name'] is None and kwargs['action'] in ('close-board', 'create-board', 'delete-board'):
print("You must specify a board name.")
sys.exit(1)
dispatcher[kwargs['action']](ctx.obj['restyaboard'], kwargs)
###################
# Boards function #
###################
def board_add_list(restya, params):
org = restya.get_organization({'name': params['org_name']})
if org is not None:
board = org.get_board({'name': params['board_name']})
if board is not None:
last_position = board.get_last_position() + 1
if board.get_list({'name': params['list_name']}) is None:
board.create_list(params['list_name'], last_position)
print("List '%s' successfully created into '%s'." % (params['list_name'], params['board_name']))
else:
print("Skipped - card '%s' already exists." % (params['list_name']))
else:
print("board not found")
else:
print("org not found")
def board_list_lists(restya, params):
org = restya.get_organization({'name': params['org_name']})
if org is not None:
board = org.get_board({'name': params['board_name']})
if board is not None:
b_list = board.get_all_lists()
if b_list is not None:
for l in b_list:
print("Name: %s, id: %d, position: %s" % (l.name, l.id, l.position))
else:
print("This board has no lists yet.")
else:
print("board not found")
else:
print("org not found")
return(True)
def validate_actions_board(ctx, param, value):
possible_values = ('add-list', 'close', 'create', 'delete', 'list-lists')
if value in possible_values:
return(value)
else:
raise click.BadParameter("must be one of " + ', '.join(possible_values))
@cli.command(short_help='Action on boards')
@click.argument('action', callback=validate_actions_board)
@click.option('--org-name', '-o', help='Name of the organization')
@click.option('--board-name', '-b', help='Name of the board')
@click.option('--list-name', '-l', help='Name of the list')
@click.pass_context
def board(ctx, **kwargs):
"""\b
ACTION can be on the following keywords:
add-list Add a list to the specified board. The list will be placed at the end
list-lists Display all the lists of the specified board
"""
dispatcher = {
'add-list': board_add_list,
'list-lists': board_list_lists,
}
if kwargs['org_name'] is None:
print('You must specify an organization name with --org-name')
sys.exit(1)
if kwargs['board_name'] is None:
print('You must specify a board name with --board-name')
sys.exit(1)
if kwargs['action'] == 'add-list' and kwargs['list_name'] is None:
print('You must specify a list name with --list-name')
sys.exit(1)
dispatcher[kwargs['action']](ctx.obj['restyaboard'], kwargs)
##################
# lists function #
##################
def list_add_card(restya, params):
org = restya.get_organization({'name': params['org_name']})
if org is not None:
board = org.get_board({'name': params['board_name']})
if board is not None:
b_list = board.get_list({'name': params['list_name']})
if b_list is not None:
if b_list.create_card(params['card_name'], None) is not None:
print("Card '%s' successfully created into '%s'." % (params['card_name'], params['list_name']))
else:
print("Card '%s' already exists." % (params['card_name']))
else:
print("List '%s' not found" % (params['list_name']))
else:
print("board not found")
else:
print("org not found")
def board_list_cards(restya, params):
org = restya.get_organization({'name': params['org_name']})
if org is not None:
board = org.get_board({'name': params['board_name']})
if board is not None:
b_list = board.get_list({'name': params['list_name']})
if b_list is not None:
try:
for card in b_list.get_all_cards():
print(card)
except TypeError:
print("No cards.")
else:
print("board not found")
else:
print("org not found")
return(True)
def validate_actions_list(ctx, param, value):
possible_values = ('add-card', 'delete', 'list-cards', 'move-all-cards')
if value in possible_values:
return(value)
else:
raise click.BadParameter("must be one of " + ', '.join(possible_values))
@cli.command(short_help='Action on lists')
@click.argument('action', callback=validate_actions_list)
@click.option('--org-name', '-o', help='Name of the organization')
@click.option('--board-name', '-b', help='Name of the board')
@click.option('--list-name', '-l', help='Name of the list')
@click.option('--card-name', '-c', help='Name of the card')
@click.pass_context
def list(ctx, **kwargs):
"""\b
ACTION can be on the following keywords:
add-card Add a card to the specified
delete Delete the specified list from the specified board
list-cards Display all the lists of the specified board
move-all-cards Move all cards from the lists to a new list in the same board
"""
dispatcher = {
'add-card': list_add_card,
'delete-card': board_delete,
'list-cards': board_list_cards,
'move-all-cards': True,
}
if kwargs['org_name'] is None:
print('You must specify a board name with --org-name')
sys.exit(1)
if kwargs['board_name'] is None:
print('You must specify an organization name with --board-name')
sys.exit(1)
if kwargs['list_name'] is None:
print('You must specify a list name with --list-name')
sys.exit(1)
if kwargs['action'] == 'add-card' and kwargs['card_name'] is None:
print('You must specify a card name with --card-name')
sys.exit(1)
dispatcher[kwargs['action']](ctx.obj['restyaboard'], kwargs)
if __name__ == '__main__':
cli()