blob: 8db9e4ac78d7705b4ccc188196560a6c381cb536 [file] [log] [blame]
Ales Komarek49a37292016-08-31 16:18:31 +02001# -*- coding: utf-8 -*-
2'''
3Module for handling gerrit calls.
4
Michael Kutý099c5342016-09-09 14:44:13 +02005:optdepends: - gerritlib/pygerrit Python adapter
Ales Komarek49a37292016-08-31 16:18:31 +02006:configuration: This module is not usable until the following are specified
7 either in a pillar or in the minion's config file::
8
9 gerrit.host: localhost
10 gerrit.user: admin
11 gerrit.key: |
12 -----BEGIN RSA PRIVATE KEY-----
13 MIIEowIBAAKCAQEAs0Y8mxS3dfs5zG8Du5vdBkfOCOng1IEUmFZIirJ8oBgJOd54
14 ...
15 jvMXms60iD/A5OpG33LWHNNzQBP486SxG75LB+Xs5sp5j2/b7VF5LJLhpGiJv9Mk
16 ydbuy8iuuvali2uF133kAlLqnrWfVTYQQI1OfW5glOv1L6kv94dU
17 -----END RSA PRIVATE KEY-----
18
Michael Kutý099c5342016-09-09 14:44:13 +020019Examples:
20- gerrit_account:
21 username: Jenkins
22 fullname: Jenkins continuous integration tool
23 email: admin@example.com
24 groups:
25 - Non-Interactive Users
26 - Testers
27 gerrit_url: http://gerrit.example.com:8080/
28 gerrit_admin_username: dicky
29 gerrit_admin_password: b0sst0nes
Ales Komarek49a37292016-08-31 16:18:31 +020030'''
31
32from __future__ import absolute_import
33
Michael Kutý099c5342016-09-09 14:44:13 +020034import json
Ales Komarek49a37292016-08-31 16:18:31 +020035import logging
36import os
Michael Kutý099c5342016-09-09 14:44:13 +020037import urllib
Michael Kutý099c5342016-09-09 14:44:13 +020038import requests.auth
Ales Komarek49a37292016-08-31 16:18:31 +020039
40LOG = logging.getLogger(__name__)
41
42# Import third party libs
43HAS_GERRIT = False
44try:
45 from gerritlib import gerrit
Ales Komarek92d0d342016-09-14 19:32:17 +020046 import pygerrit.rest
Ales Komarek49a37292016-08-31 16:18:31 +020047 HAS_GERRIT = True
48except ImportError:
49 pass
50
51
52def __virtual__():
53 '''
54 Only load this module if gerrit
55 is installed on this minion.
56 '''
57 if HAS_GERRIT:
58 return 'gerrit'
59 return False
60
61__opts__ = {}
62
63
Ales Komarekb0fcc252016-09-14 19:29:37 +020064# Common functions
Michael Kutý099c5342016-09-09 14:44:13 +020065
66
Ales Komarekb0fcc252016-09-14 19:29:37 +020067def _get_boolean(gerrit, path):
Michael Kutý099c5342016-09-09 14:44:13 +020068 response = gerrit.get(path)
69 if response == 'ok':
70 value = True
71 elif response == '':
72 value = False
73 else:
74 raise AnsibleGerritError(
75 "Unexpected response for %s: %s" % (path, response))
76 return value
77
78
Ales Komarekb0fcc252016-09-14 19:29:37 +020079def _get_list(gerrit, path):
Michael Kutý099c5342016-09-09 14:44:13 +020080 values = gerrit.get(path)
81 return values
82
83
Ales Komarekb0fcc252016-09-14 19:29:37 +020084def _get_string(gerrit, path):
Michael Kutý099c5342016-09-09 14:44:13 +020085 try:
86 value = gerrit.get(path)
87 except requests.exceptions.HTTPError as e:
88 if e.response.status_code == 404:
89 logging.debug("Ignoring exception %s", e)
90 logging.debug("Got %s", e.response.__dict__)
91 value = None
92 else:
93 raise
94 return value
95
96
Ales Komarekb0fcc252016-09-14 19:29:37 +020097def _set_boolean(gerrit, path, value):
Michael Kutý099c5342016-09-09 14:44:13 +020098 if value:
99 gerrit.put(path)
100 else:
101 gerrit.delete(path)
102
103
Ales Komarekb0fcc252016-09-14 19:29:37 +0200104def _set_string(gerrit, path, value, field_name=None):
Michael Kutý099c5342016-09-09 14:44:13 +0200105 field_name = field_name or os.path.basename(path)
106
107 # Setting to '' is equivalent to deleting, so we have no need for the
108 # DELETE method.
109 headers = {'content-type': 'application/json'}
110 data = json.dumps({field_name: value})
111 gerrit.put(path, data=data, headers=headers)
112
113
Ales Komarekb0fcc252016-09-14 19:29:37 +0200114def _maybe_update_field(gerrit, path, field, gerrit_value, salt_value,
Michael Kutý099c5342016-09-09 14:44:13 +0200115 type='str', gerrit_api_path=None):
116
117 gerrit_api_path = gerrit_api_path or field
118 fullpath = path + '/' + gerrit_api_path
119
Ales Komarekb0fcc252016-09-14 19:29:37 +0200120 if gerrit_value == salt_value:
Michael Kutý099c5342016-09-09 14:44:13 +0200121 logging.info("Not updating %s: same value specified: %s", fullpath,
122 gerrit_value)
123 value = gerrit_value
124 changed = False
Ales Komarekb0fcc252016-09-14 19:29:37 +0200125 elif salt_value is None:
Michael Kutý099c5342016-09-09 14:44:13 +0200126 logging.info("Not updating %s: no value specified, value stays as %s",
127 fullpath, gerrit_value)
128 value = gerrit_value
129 changed = False
130 else:
131 logging.info("Changing %s from %s to %s", fullpath, gerrit_value,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200132 salt_value)
Michael Kutý099c5342016-09-09 14:44:13 +0200133 if type == 'str':
Ales Komarekb0fcc252016-09-14 19:29:37 +0200134 _set_string(gerrit, fullpath, salt_value, field_name=field)
Michael Kutý099c5342016-09-09 14:44:13 +0200135 elif type == 'bool':
Ales Komarekb0fcc252016-09-14 19:29:37 +0200136 _set_boolean(gerrit, fullpath, salt_value)
Michael Kutý099c5342016-09-09 14:44:13 +0200137 else:
138 raise AssertionError("Unknown Ansible parameter type '%s'" % type)
139
Ales Komarekb0fcc252016-09-14 19:29:37 +0200140 value = salt_value
Michael Kutý099c5342016-09-09 14:44:13 +0200141 changed = True
142 return value, changed
143
Ales Komarek07d16552016-09-12 21:39:18 +0200144
Ales Komarekb0fcc252016-09-14 19:29:37 +0200145def _quote(name):
Ales Komarek07d16552016-09-12 21:39:18 +0200146 return urllib.quote(name, safe="")
147
148
Ales Komarekb0fcc252016-09-14 19:29:37 +0200149def _account_name2id(gerrit, name=None):
150 # Although we could pass an AccountInput entry here to set details in one
151 # go, it's left up to the _update_group() function, to avoid having a
152 # totally separate code path for create vs. update.
153 info = gerrit.get('/accounts/%s' % _quote(name))
154 return info['_account_id']
155
156
157def _group_name2id(gerrit, name=None):
158 # Although we could pass an AccountInput entry here to set details in one
159 # go, it's left up to the _update_group() function, to avoid having a
160 # totally separate code path for create vs. update.
161 info = gerrit.get('/groups/%s' % _quote(name))
162 return info['id']
163
164
165def _create_group(gerrit, name=None):
166 # Although we could pass an AccountInput entry here to set details in one
167 # go, it's left up to the _update_group() function, to avoid having a
168 # totally separate code path for create vs. update.
169 group_info = gerrit.put('/groups/%s' % _quote(name))
170 return group_info
171
172
173def _create_account(gerrit, username=None):
174 # Although we could pass an AccountInput entry here to set details in one
175 # go, it's left up to the _update_account() function, to avoid having a
176 # totally separate code path for create vs. update.
177 account_info = gerrit.put('/accounts/%s' % _quote(username))
178 return account_info
179
180
181def _create_account_email(gerrit, account_id, email, preferred=False,
182 no_confirmation=False):
183 logging.info('Creating email %s for account %s', email, account_id)
184
185 email_input = {
186 # Setting 'email' is optional (it's already in the URL) but it's good
187 # to double check that the email is encoded in the URL properly.
188 'email': email,
189 'preferred': preferred,
190 'no_confirmation': no_confirmation,
191 }
192 logging.debug(email_input)
193
194 path = 'accounts/%s/emails/%s' % (account_id, _quote(email))
195 headers = {'content-type': 'application/json'}
196 gerrit.post(path, data=json.dumps(email_input), headers=headers)
197
198
199def _create_account_ssh_key(gerrit, account_id, ssh_public_key):
200 logging.info('Creating SSH key %s for account %s', ssh_public_key,
201 account_id)
202
203 import requests
204 from pygerrit import decode_response
205
206 path = 'accounts/%s/sshkeys' % (account_id)
207 # gerrit.post(path, data=ssh_public_key)
208
209 kwargs = {
210 "data": ssh_public_key
211 }
212 kwargs.update(gerrit.kwargs.copy())
213
214 response = requests.put(gerrit.make_url(path), **kwargs)
215
216 return gerrit.decode_response(response)
217
218
219def _create_group_membership(gerrit, account_id, group_id):
220 logging.info('Creating membership of %s in group %s', account_id, group_id)
221# group_id = _group_name2id(gerrit, group_id)
222 print group_id
223 import json
224 path = 'groups/%s/members/%s' % (_quote(group_id), account_id)
225 gerrit.put(path, data=json.dumps({}))
226
227
228def _ensure_only_member_of_these_groups(gerrit, account_id, salt_groups):
229 path = 'accounts/%s' % account_id
230 group_info_list = _get_list(gerrit, path + '/groups')
231
232 changed = False
233 gerrit_groups = []
234 for group_info in group_info_list:
235 if group_info['name'] in salt_groups:
236 logging.info("Preserving %s membership of group %s", path,
237 group_info)
238 gerrit_groups.append(group_info['name'])
239 else:
240 logging.info("Removing %s from group %s", path, group_info)
241 membership_path = 'groups/%s/members/%s' % (
242 _quote(group_info['id']), account_id)
243 try:
244 gerrit.delete(membership_path)
245 changed = True
246 except requests.exceptions.HTTPError as e:
247 if e.response.status_code == 404:
248 # This is a kludge, it'd be better to work out in advance
249 # which groups the user is a member of only via membership
250 # in a different. That's not trivial though with the
251 # current API Gerrit provides.
252 logging.info(
253 "Ignored %s; assuming membership of this group is due "
254 "to membership of a group that includes it.", e)
255 else:
256 raise
257
258 # If the user gave group IDs instead of group names, this will
259 # needlessly recreate the membership. The only actual issue will be that
260 # Ansible reports 'changed' when nothing really did change, I think.
261 #
262 # We might receive [""] when the user tries to pass in an empty list, so
263 # handle that.
264 for new_group in set(salt_groups).difference(gerrit_groups):
265 if len(new_group) > 0:
266 _create_group_membership(gerrit, account_id, new_group)
267 gerrit_groups.append(new_group)
268 changed = True
269
270 return gerrit_groups, changed
271
272
273def _ensure_only_one_account_email(gerrit, account_id, email):
274 path = 'accounts/%s' % account_id
275 email_info_list = _get_list(gerrit, path + '/emails')
276
277 changed = False
278 found_email = False
279 for email_info in email_info_list:
280 existing_email = email_info['email']
281 if existing_email == email:
282 # Since we're deleting all emails except this one, there's no need
283 # to care whether it's the 'preferred' one. It soon will be!
284 logging.info("Keeping %s email %s", path, email)
285 found_email = True
286 else:
287 logging.info("Removing %s email %s", path, existing_email)
288 gerrit.delete(path + '/emails/%s' % _quote(existing_email))
289 changed = True
290
291 if len(email) > 0 and not found_email:
292 _create_account_email(gerrit, account_id, email,
293 preferred=True, no_confirmation=True)
294 changed = True
295
296 return email, changed
297
298
299def _ensure_only_one_account_ssh_key(gerrit, account_id, ssh_public_key):
300 path = 'accounts/%s' % account_id
301 ssh_key_info_list = _get_list(gerrit, path + '/sshkeys')
302
303 changed = False
304 found_ssh_key = False
305 for ssh_key_info in ssh_key_info_list:
306 if ssh_key_info['ssh_public_key'] == ssh_public_key:
307 logging.info("Keeping %s SSH key %s", path, ssh_key_info)
308 found_ssh_key = True
309 else:
310 logging.info("Removing %s SSH key %s", path, ssh_key_info)
311 gerrit.delete(path + '/sshkeys/%i' % ssh_key_info['seq'])
312 changed = True
313
314 if len(ssh_public_key) > 0 and not found_ssh_key:
315 _create_account_ssh_key(gerrit, account_id, ssh_public_key)
316 changed = True
317
318 return ssh_public_key, changed
319
320
321def _update_account(gerrit, username=None, **params):
322 change = False
323
324 try:
325 account_info = gerrit.get('/accounts/%s' % _quote(username))
326 except requests.exceptions.HTTPError as e:
327 if e.response.status_code == 404:
328 logging.info("Account %s not found, creating it.", username)
329 account_info = _create_account(gerrit, username)
330 change = True
331 else:
332 raise
333
334 logging.debug(
335 'Existing account info for account %s: %s', username,
336 json.dumps(account_info, indent=4))
337
338 account_id = account_info['_account_id']
339 path = 'accounts/%s' % account_id
340
341 output = {}
342 output['username'] = username
343 output['id'] = account_id
344
345 fullname, fullname_changed = _maybe_update_field(
346 gerrit, path, 'name', account_info.get('name'), params.get('fullname'))
347 output['fullname'] = fullname
348 change |= fullname_changed
349
350 # Set the value of params that the user did not provide to None.
351
352 if params.get('active') is not None:
353 active = _get_boolean(gerrit, path + '/active')
354 active, active_changed = _maybe_update_field(
355 gerrit, path, 'active', active, params['active'], type='bool')
356 output['active'] = active
357 change |= active_changed
358
359 if params.get('email') is not None:
360 email, emails_changed = _ensure_only_one_account_email(
361 gerrit, account_id, params['email'])
362 output['email'] = email
363 change |= emails_changed
364
365 if params.get('groups') is not None:
366 groups, groups_changed = _ensure_only_member_of_these_groups(
367 gerrit, account_info.get('name'), params['groups'])
368 output['groups'] = groups
369 change |= groups_changed
370
371 if params.get('http_password') is not None:
372 http_password = _get_string(gerrit, path + '/password.http')
373 http_password, http_password_changed = _maybe_update_field(
374 gerrit, path, 'http_password', http_password,
375 params.get('http_password'),
376 gerrit_api_path='password.http')
377 output['http_password'] = http_password
378 change |= http_password_changed
379
380 if params.get('ssh_key') is not None:
381 ssh_key, ssh_keys_changed = _ensure_only_one_account_ssh_key(
382 gerrit, account_id, params['ssh_key'])
383 output['ssh_key'] = ssh_key
384 change |= ssh_keys_changed
385
386 return output, change
387
388
389def _update_group(gerrit, name=None, **params):
390 change = False
391
392 try:
393 group_info = gerrit.get('/groups/%s' % _quote(name))
394 except requests.exceptions.HTTPError as e:
395 if e.response.status_code == 404:
396 logging.info("Group %s not found, creating it.", name)
397 group_info = _create_group(gerrit, name)
398 change = True
399 else:
400 raise
401
402 logging.debug(
403 'Existing info for group %s: %s', name,
404 json.dumps(group_info, indent=4))
405
406 output = {group_info['name']: group_info}
407
408 return output, change
409
410
411# Gerrit client connectors
Michael Kutý099c5342016-09-09 14:44:13 +0200412
413
Ales Komarek07d16552016-09-12 21:39:18 +0200414def _gerrit_ssh_connection(**connection_args):
415 '''
416 Set up gerrit credentials
417
418 Only intended to be used within gerrit-enabled modules
419 '''
420
421 prefix = "gerrit"
422
423 # look in connection_args first, then default to config file
424 def get(key, default=None):
425 return connection_args.get('connection_' + key,
426 __salt__['config.get'](prefix, {})).get(key, default)
427
428 host = get('host', 'localhost')
429 user = get('user', 'admin')
430 keyfile = get('keyfile', '/var/cache/salt/minion/gerrit_rsa')
431
432 gerrit_client = gerrit.Gerrit(host, user, keyfile=keyfile)
433
434 return gerrit_client
435
436
437def _gerrit_http_connection(**connection_args):
Michael Kutý099c5342016-09-09 14:44:13 +0200438
439 prefix = "gerrit"
440
441 # look in connection_args first, then default to config file
442 def get(key, default=None):
443 return connection_args.get(
444 'connection_' + key,
445 __salt__['config.get'](prefix, {})).get(key, default)
446
447 host = get('host', 'localhost')
Ales Komarek07d16552016-09-12 21:39:18 +0200448 http_port = get('http_port', '8082')
449 protocol = get('protocol', 'http')
Michael Kutý099c5342016-09-09 14:44:13 +0200450 username = get('user', 'admin')
451 password = get('password', 'admin')
452
Ales Komarek07d16552016-09-12 21:39:18 +0200453 url = protocol+"://"+str(host)+':'+str(http_port)
454
Michael Kutý099c5342016-09-09 14:44:13 +0200455 auth = requests.auth.HTTPDigestAuth(
456 username, password)
457
458 gerrit = pygerrit.rest.GerritRestAPI(
Ales Komarek07d16552016-09-12 21:39:18 +0200459 url=url, auth=auth)
Michael Kutý099c5342016-09-09 14:44:13 +0200460
461 return gerrit
462
463
Ales Komarekb0fcc252016-09-14 19:29:37 +0200464# Salt modules
Ales Komarek07d16552016-09-12 21:39:18 +0200465
466
467def account_create(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs):
Michael Kutý099c5342016-09-09 14:44:13 +0200468 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200469 Create a gerrit account
470
471 :param username: username
472 :param fullname: fullname
473 :param email: email
474 :param active: active
475 :param groups: array of strings
476 groups:
477 - Non-Interactive Users
478 - Testers
479 :param ssh_key: public ssh key
480 :param http_password: http password
481
482 CLI Examples:
483
484 .. code-block:: bash
485
486 salt '*' gerrit.account_create username "full name" "mail@domain.com"
487
488 '''
489 gerrit_client = _gerrit_http_connection(**kwargs)
490 output, changed = _update_account(
491 gerrit_client, **{
492 'username': username,
493 'fullname': fullname,
494 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200495# 'active': active,
Ales Komarek07d16552016-09-12 21:39:18 +0200496 'groups': groups,
497 'ssh_key': ssh_key,
498 'http_password': http_password
499 })
500 return output
501
502
503def account_update(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs):
504 '''
505 Create a gerrit account
Michael Kutý099c5342016-09-09 14:44:13 +0200506
507 :param username: username
508 :param fullname: fullname
509 :param email: email
Ales Komarekb0fcc252016-09-14 19:29:37 +0200510 :param active: active
Michael Kutý099c5342016-09-09 14:44:13 +0200511 :param groups: array of strings
512 groups:
513 - Non-Interactive Users
514 - Testers
Ales Komarek07d16552016-09-12 21:39:18 +0200515 :param ssh_key: public ssh key
516 :param http_password: http password
517
Michael Kutý099c5342016-09-09 14:44:13 +0200518 CLI Examples:
519
520 .. code-block:: bash
521
Ales Komarek07d16552016-09-12 21:39:18 +0200522 salt '*' gerrit.account_create username "full name" "mail@domain.com"
Michael Kutý099c5342016-09-09 14:44:13 +0200523
524 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200525 gerrit_client = _gerrit_http_connection(**kwargs)
526 output, changed = _update_account(
Michael Kutý099c5342016-09-09 14:44:13 +0200527 gerrit_client, **{
528 'username': username,
529 'fullname': fullname,
530 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200531# 'active': active,
Michael Kutý099c5342016-09-09 14:44:13 +0200532 'groups': groups,
Ales Komarek07d16552016-09-12 21:39:18 +0200533 'ssh_key': ssh_key,
534 'http_password': http_password
Michael Kutý099c5342016-09-09 14:44:13 +0200535 })
Michael Kutý099c5342016-09-09 14:44:13 +0200536 return output
537
Ales Komarek07d16552016-09-12 21:39:18 +0200538def account_list(**kwargs):
539 '''
540 List gerrit accounts
541
542 CLI Examples:
543
544 .. code-block:: bash
545
546 salt '*' gerrit.account_list
547
548 '''
549 gerrit_client = _gerrit_http_connection(**kwargs)
550 ret_list = gerrit_client.get('/accounts/?q=*&n=10000')
551 ret = {}
552 for item in ret_list:
553 ret[item['username']] = item
554 return ret
555
556
Ales Komarek2fc39002016-09-14 11:43:56 +0200557def account_get(name, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200558 '''
559 Get gerrit account
560
561 CLI Examples:
562
563 .. code-block:: bash
564
Ales Komarek2fc39002016-09-14 11:43:56 +0200565 salt '*' gerrit.account_get name
Ales Komarek07d16552016-09-12 21:39:18 +0200566
567 '''
568 gerrit_client = _gerrit_http_connection(**kwargs)
Ales Komarek2fc39002016-09-14 11:43:56 +0200569 accounts = account_list(**kwargs)
570 if(name in accounts):
571 ret = accounts.pop(name)
572 else:
573 ret = {'Error': 'Error in retrieving account'}
Ales Komarek07d16552016-09-12 21:39:18 +0200574 return ret
575
576
577def group_list(**kwargs):
578 '''
579 List gerrit groups
580
581 CLI Examples:
582
583 .. code-block:: bash
584
585 salt '*' gerrit.group_list
586
587 '''
588 gerrit_client = _gerrit_http_connection(**kwargs)
589 return gerrit_client.get('/groups/')
590
591
592def group_get(groupname, **kwargs):
593 '''
594 Get gerrit group
595
596 CLI Examples:
597
598 .. code-block:: bash
599
600 salt '*' gerrit.group_get groupname
601
602 '''
603 gerrit_client = _gerrit_http_connection(**kwargs)
604 try:
605 item = gerrit_client.get('/groups/%s' % groupname)
606 ret = {item['name']: item}
607 except:
608 ret = {'Error': 'Error in retrieving account'}
609 return ret
610
611
Ales Komarek2fc39002016-09-14 11:43:56 +0200612def group_create(name, description=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200613 '''
614 Create a gerrit group
615
616 :param name: name
617
618 CLI Examples:
619
620 .. code-block:: bash
621
Ales Komarekb0fcc252016-09-14 19:29:37 +0200622 salt '*' gerrit.group_create group-name description
Ales Komarek07d16552016-09-12 21:39:18 +0200623
624 '''
625 gerrit_client = _gerrit_http_connection(**kwargs)
626 ret, changed = _update_group(
Ales Komarek2fc39002016-09-14 11:43:56 +0200627 gerrit_client, **{'name': name, 'description': description})
Ales Komarek07d16552016-09-12 21:39:18 +0200628 return ret
629
Michael Kutý099c5342016-09-09 14:44:13 +0200630
Ales Komarek49a37292016-08-31 16:18:31 +0200631def project_create(name, **kwargs):
632 '''
633 Create a gerrit project
634
635 :param name: new project name
636
637 CLI Examples:
638
639 .. code-block:: bash
640
641 salt '*' gerrit.project_create namespace/nova description='nova project'
Michael Kutý099c5342016-09-09 14:44:13 +0200642
Ales Komarek49a37292016-08-31 16:18:31 +0200643 '''
644 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200645 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200646
647 project = project_get(name, **kwargs)
648
649 if project and not "Error" in project:
650 LOG.debug("Project {0} exists".format(name))
651 return project
652
653 new = gerrit_client.createProject(name)
654 return project_get(name, **kwargs)
655
Michael Kutý099c5342016-09-09 14:44:13 +0200656
Ales Komarek49a37292016-08-31 16:18:31 +0200657def project_get(name, **kwargs):
658 '''
659 Return a specific project
660
661 CLI Examples:
662
663 .. code-block:: bash
664
665 salt '*' gerrit.project_get projectname
666 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200667 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200668 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200669 projects = gerrit_client.listProjects()
670 if not name in projects:
671 return {'Error': 'Error in retrieving project'}
672 ret[name] = {'name': name}
673 return ret
674
675
676def project_list(**connection_args):
677 '''
678 Return a list of available projects
679
680 CLI Example:
681
682 .. code-block:: bash
683
684 salt '*' gerrit.project_list
685 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200686 gerrit_client = _gerrit_ssh_connection(**connection_args)
Ales Komarek49a37292016-08-31 16:18:31 +0200687 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200688 projects = gerrit_client.listProjects()
Ales Komarek49a37292016-08-31 16:18:31 +0200689 for project in projects:
690 ret[project] = {
691 'name': project
692 }
693 return ret
694
695
696def query(change, **kwargs):
697 '''
698 Query gerrit
699
700 :param change: Query content
701
702 CLI Examples:
703
704 .. code-block:: bash
705
706 salt '*' gerrit.query 'status:open project:tools/gerrit limit:2'
Michael Kutý099c5342016-09-09 14:44:13 +0200707
Ales Komarek49a37292016-08-31 16:18:31 +0200708 '''
709 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200710 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200711 msg = gerrit_client.query(change)
712 ret['query'] = msg
713 return ret