blob: db3cdcbda3e025e50c83c0590e57795b1b2dba57 [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
38
39import pygerrit.rest
40import requests.auth
Ales Komarek49a37292016-08-31 16:18:31 +020041
42LOG = logging.getLogger(__name__)
43
44# Import third party libs
45HAS_GERRIT = False
46try:
47 from gerritlib import gerrit
48 HAS_GERRIT = True
49except ImportError:
50 pass
51
52
53def __virtual__():
54 '''
55 Only load this module if gerrit
56 is installed on this minion.
57 '''
58 if HAS_GERRIT:
59 return 'gerrit'
60 return False
61
62__opts__ = {}
63
64
Ales Komarekb0fcc252016-09-14 19:29:37 +020065# Common functions
Michael Kutý099c5342016-09-09 14:44:13 +020066
67
Ales Komarekb0fcc252016-09-14 19:29:37 +020068def _get_boolean(gerrit, path):
Michael Kutý099c5342016-09-09 14:44:13 +020069 response = gerrit.get(path)
70 if response == 'ok':
71 value = True
72 elif response == '':
73 value = False
74 else:
75 raise AnsibleGerritError(
76 "Unexpected response for %s: %s" % (path, response))
77 return value
78
79
Ales Komarekb0fcc252016-09-14 19:29:37 +020080def _get_list(gerrit, path):
Michael Kutý099c5342016-09-09 14:44:13 +020081 values = gerrit.get(path)
82 return values
83
84
Ales Komarekb0fcc252016-09-14 19:29:37 +020085def _get_string(gerrit, path):
Michael Kutý099c5342016-09-09 14:44:13 +020086 try:
87 value = gerrit.get(path)
88 except requests.exceptions.HTTPError as e:
89 if e.response.status_code == 404:
90 logging.debug("Ignoring exception %s", e)
91 logging.debug("Got %s", e.response.__dict__)
92 value = None
93 else:
94 raise
95 return value
96
97
Ales Komarekb0fcc252016-09-14 19:29:37 +020098def _set_boolean(gerrit, path, value):
Michael Kutý099c5342016-09-09 14:44:13 +020099 if value:
100 gerrit.put(path)
101 else:
102 gerrit.delete(path)
103
104
Ales Komarekb0fcc252016-09-14 19:29:37 +0200105def _set_string(gerrit, path, value, field_name=None):
Michael Kutý099c5342016-09-09 14:44:13 +0200106 field_name = field_name or os.path.basename(path)
107
108 # Setting to '' is equivalent to deleting, so we have no need for the
109 # DELETE method.
110 headers = {'content-type': 'application/json'}
111 data = json.dumps({field_name: value})
112 gerrit.put(path, data=data, headers=headers)
113
114
Ales Komarekb0fcc252016-09-14 19:29:37 +0200115def _maybe_update_field(gerrit, path, field, gerrit_value, salt_value,
Michael Kutý099c5342016-09-09 14:44:13 +0200116 type='str', gerrit_api_path=None):
117
118 gerrit_api_path = gerrit_api_path or field
119 fullpath = path + '/' + gerrit_api_path
120
Ales Komarekb0fcc252016-09-14 19:29:37 +0200121 if gerrit_value == salt_value:
Michael Kutý099c5342016-09-09 14:44:13 +0200122 logging.info("Not updating %s: same value specified: %s", fullpath,
123 gerrit_value)
124 value = gerrit_value
125 changed = False
Ales Komarekb0fcc252016-09-14 19:29:37 +0200126 elif salt_value is None:
Michael Kutý099c5342016-09-09 14:44:13 +0200127 logging.info("Not updating %s: no value specified, value stays as %s",
128 fullpath, gerrit_value)
129 value = gerrit_value
130 changed = False
131 else:
132 logging.info("Changing %s from %s to %s", fullpath, gerrit_value,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200133 salt_value)
Michael Kutý099c5342016-09-09 14:44:13 +0200134 if type == 'str':
Ales Komarekb0fcc252016-09-14 19:29:37 +0200135 _set_string(gerrit, fullpath, salt_value, field_name=field)
Michael Kutý099c5342016-09-09 14:44:13 +0200136 elif type == 'bool':
Ales Komarekb0fcc252016-09-14 19:29:37 +0200137 _set_boolean(gerrit, fullpath, salt_value)
Michael Kutý099c5342016-09-09 14:44:13 +0200138 else:
139 raise AssertionError("Unknown Ansible parameter type '%s'" % type)
140
Ales Komarekb0fcc252016-09-14 19:29:37 +0200141 value = salt_value
Michael Kutý099c5342016-09-09 14:44:13 +0200142 changed = True
143 return value, changed
144
Ales Komarek07d16552016-09-12 21:39:18 +0200145
Ales Komarekb0fcc252016-09-14 19:29:37 +0200146def _quote(name):
Ales Komarek07d16552016-09-12 21:39:18 +0200147 return urllib.quote(name, safe="")
148
149
Ales Komarekb0fcc252016-09-14 19:29:37 +0200150def _account_name2id(gerrit, name=None):
151 # Although we could pass an AccountInput entry here to set details in one
152 # go, it's left up to the _update_group() function, to avoid having a
153 # totally separate code path for create vs. update.
154 info = gerrit.get('/accounts/%s' % _quote(name))
155 return info['_account_id']
156
157
158def _group_name2id(gerrit, name=None):
159 # Although we could pass an AccountInput entry here to set details in one
160 # go, it's left up to the _update_group() function, to avoid having a
161 # totally separate code path for create vs. update.
162 info = gerrit.get('/groups/%s' % _quote(name))
163 return info['id']
164
165
166def _create_group(gerrit, name=None):
167 # Although we could pass an AccountInput entry here to set details in one
168 # go, it's left up to the _update_group() function, to avoid having a
169 # totally separate code path for create vs. update.
170 group_info = gerrit.put('/groups/%s' % _quote(name))
171 return group_info
172
173
174def _create_account(gerrit, username=None):
175 # Although we could pass an AccountInput entry here to set details in one
176 # go, it's left up to the _update_account() function, to avoid having a
177 # totally separate code path for create vs. update.
178 account_info = gerrit.put('/accounts/%s' % _quote(username))
179 return account_info
180
181
182def _create_account_email(gerrit, account_id, email, preferred=False,
183 no_confirmation=False):
184 logging.info('Creating email %s for account %s', email, account_id)
185
186 email_input = {
187 # Setting 'email' is optional (it's already in the URL) but it's good
188 # to double check that the email is encoded in the URL properly.
189 'email': email,
190 'preferred': preferred,
191 'no_confirmation': no_confirmation,
192 }
193 logging.debug(email_input)
194
195 path = 'accounts/%s/emails/%s' % (account_id, _quote(email))
196 headers = {'content-type': 'application/json'}
197 gerrit.post(path, data=json.dumps(email_input), headers=headers)
198
199
200def _create_account_ssh_key(gerrit, account_id, ssh_public_key):
201 logging.info('Creating SSH key %s for account %s', ssh_public_key,
202 account_id)
203
204 import requests
205 from pygerrit import decode_response
206
207 path = 'accounts/%s/sshkeys' % (account_id)
208 # gerrit.post(path, data=ssh_public_key)
209
210 kwargs = {
211 "data": ssh_public_key
212 }
213 kwargs.update(gerrit.kwargs.copy())
214
215 response = requests.put(gerrit.make_url(path), **kwargs)
216
217 return gerrit.decode_response(response)
218
219
220def _create_group_membership(gerrit, account_id, group_id):
221 logging.info('Creating membership of %s in group %s', account_id, group_id)
222# group_id = _group_name2id(gerrit, group_id)
223 print group_id
224 import json
225 path = 'groups/%s/members/%s' % (_quote(group_id), account_id)
226 gerrit.put(path, data=json.dumps({}))
227
228
229def _ensure_only_member_of_these_groups(gerrit, account_id, salt_groups):
230 path = 'accounts/%s' % account_id
231 group_info_list = _get_list(gerrit, path + '/groups')
232
233 changed = False
234 gerrit_groups = []
235 for group_info in group_info_list:
236 if group_info['name'] in salt_groups:
237 logging.info("Preserving %s membership of group %s", path,
238 group_info)
239 gerrit_groups.append(group_info['name'])
240 else:
241 logging.info("Removing %s from group %s", path, group_info)
242 membership_path = 'groups/%s/members/%s' % (
243 _quote(group_info['id']), account_id)
244 try:
245 gerrit.delete(membership_path)
246 changed = True
247 except requests.exceptions.HTTPError as e:
248 if e.response.status_code == 404:
249 # This is a kludge, it'd be better to work out in advance
250 # which groups the user is a member of only via membership
251 # in a different. That's not trivial though with the
252 # current API Gerrit provides.
253 logging.info(
254 "Ignored %s; assuming membership of this group is due "
255 "to membership of a group that includes it.", e)
256 else:
257 raise
258
259 # If the user gave group IDs instead of group names, this will
260 # needlessly recreate the membership. The only actual issue will be that
261 # Ansible reports 'changed' when nothing really did change, I think.
262 #
263 # We might receive [""] when the user tries to pass in an empty list, so
264 # handle that.
265 for new_group in set(salt_groups).difference(gerrit_groups):
266 if len(new_group) > 0:
267 _create_group_membership(gerrit, account_id, new_group)
268 gerrit_groups.append(new_group)
269 changed = True
270
271 return gerrit_groups, changed
272
273
274def _ensure_only_one_account_email(gerrit, account_id, email):
275 path = 'accounts/%s' % account_id
276 email_info_list = _get_list(gerrit, path + '/emails')
277
278 changed = False
279 found_email = False
280 for email_info in email_info_list:
281 existing_email = email_info['email']
282 if existing_email == email:
283 # Since we're deleting all emails except this one, there's no need
284 # to care whether it's the 'preferred' one. It soon will be!
285 logging.info("Keeping %s email %s", path, email)
286 found_email = True
287 else:
288 logging.info("Removing %s email %s", path, existing_email)
289 gerrit.delete(path + '/emails/%s' % _quote(existing_email))
290 changed = True
291
292 if len(email) > 0 and not found_email:
293 _create_account_email(gerrit, account_id, email,
294 preferred=True, no_confirmation=True)
295 changed = True
296
297 return email, changed
298
299
300def _ensure_only_one_account_ssh_key(gerrit, account_id, ssh_public_key):
301 path = 'accounts/%s' % account_id
302 ssh_key_info_list = _get_list(gerrit, path + '/sshkeys')
303
304 changed = False
305 found_ssh_key = False
306 for ssh_key_info in ssh_key_info_list:
307 if ssh_key_info['ssh_public_key'] == ssh_public_key:
308 logging.info("Keeping %s SSH key %s", path, ssh_key_info)
309 found_ssh_key = True
310 else:
311 logging.info("Removing %s SSH key %s", path, ssh_key_info)
312 gerrit.delete(path + '/sshkeys/%i' % ssh_key_info['seq'])
313 changed = True
314
315 if len(ssh_public_key) > 0 and not found_ssh_key:
316 _create_account_ssh_key(gerrit, account_id, ssh_public_key)
317 changed = True
318
319 return ssh_public_key, changed
320
321
322def _update_account(gerrit, username=None, **params):
323 change = False
324
325 try:
326 account_info = gerrit.get('/accounts/%s' % _quote(username))
327 except requests.exceptions.HTTPError as e:
328 if e.response.status_code == 404:
329 logging.info("Account %s not found, creating it.", username)
330 account_info = _create_account(gerrit, username)
331 change = True
332 else:
333 raise
334
335 logging.debug(
336 'Existing account info for account %s: %s', username,
337 json.dumps(account_info, indent=4))
338
339 account_id = account_info['_account_id']
340 path = 'accounts/%s' % account_id
341
342 output = {}
343 output['username'] = username
344 output['id'] = account_id
345
346 fullname, fullname_changed = _maybe_update_field(
347 gerrit, path, 'name', account_info.get('name'), params.get('fullname'))
348 output['fullname'] = fullname
349 change |= fullname_changed
350
351 # Set the value of params that the user did not provide to None.
352
353 if params.get('active') is not None:
354 active = _get_boolean(gerrit, path + '/active')
355 active, active_changed = _maybe_update_field(
356 gerrit, path, 'active', active, params['active'], type='bool')
357 output['active'] = active
358 change |= active_changed
359
360 if params.get('email') is not None:
361 email, emails_changed = _ensure_only_one_account_email(
362 gerrit, account_id, params['email'])
363 output['email'] = email
364 change |= emails_changed
365
366 if params.get('groups') is not None:
367 groups, groups_changed = _ensure_only_member_of_these_groups(
368 gerrit, account_info.get('name'), params['groups'])
369 output['groups'] = groups
370 change |= groups_changed
371
372 if params.get('http_password') is not None:
373 http_password = _get_string(gerrit, path + '/password.http')
374 http_password, http_password_changed = _maybe_update_field(
375 gerrit, path, 'http_password', http_password,
376 params.get('http_password'),
377 gerrit_api_path='password.http')
378 output['http_password'] = http_password
379 change |= http_password_changed
380
381 if params.get('ssh_key') is not None:
382 ssh_key, ssh_keys_changed = _ensure_only_one_account_ssh_key(
383 gerrit, account_id, params['ssh_key'])
384 output['ssh_key'] = ssh_key
385 change |= ssh_keys_changed
386
387 return output, change
388
389
390def _update_group(gerrit, name=None, **params):
391 change = False
392
393 try:
394 group_info = gerrit.get('/groups/%s' % _quote(name))
395 except requests.exceptions.HTTPError as e:
396 if e.response.status_code == 404:
397 logging.info("Group %s not found, creating it.", name)
398 group_info = _create_group(gerrit, name)
399 change = True
400 else:
401 raise
402
403 logging.debug(
404 'Existing info for group %s: %s', name,
405 json.dumps(group_info, indent=4))
406
407 output = {group_info['name']: group_info}
408
409 return output, change
410
411
412# Gerrit client connectors
Michael Kutý099c5342016-09-09 14:44:13 +0200413
414
Ales Komarek07d16552016-09-12 21:39:18 +0200415def _gerrit_ssh_connection(**connection_args):
416 '''
417 Set up gerrit credentials
418
419 Only intended to be used within gerrit-enabled modules
420 '''
421
422 prefix = "gerrit"
423
424 # look in connection_args first, then default to config file
425 def get(key, default=None):
426 return connection_args.get('connection_' + key,
427 __salt__['config.get'](prefix, {})).get(key, default)
428
429 host = get('host', 'localhost')
430 user = get('user', 'admin')
431 keyfile = get('keyfile', '/var/cache/salt/minion/gerrit_rsa')
432
433 gerrit_client = gerrit.Gerrit(host, user, keyfile=keyfile)
434
435 return gerrit_client
436
437
438def _gerrit_http_connection(**connection_args):
Michael Kutý099c5342016-09-09 14:44:13 +0200439
440 prefix = "gerrit"
441
442 # look in connection_args first, then default to config file
443 def get(key, default=None):
444 return connection_args.get(
445 'connection_' + key,
446 __salt__['config.get'](prefix, {})).get(key, default)
447
448 host = get('host', 'localhost')
Ales Komarek07d16552016-09-12 21:39:18 +0200449 http_port = get('http_port', '8082')
450 protocol = get('protocol', 'http')
Michael Kutý099c5342016-09-09 14:44:13 +0200451 username = get('user', 'admin')
452 password = get('password', 'admin')
453
Ales Komarek07d16552016-09-12 21:39:18 +0200454 url = protocol+"://"+str(host)+':'+str(http_port)
455
Michael Kutý099c5342016-09-09 14:44:13 +0200456 auth = requests.auth.HTTPDigestAuth(
457 username, password)
458
459 gerrit = pygerrit.rest.GerritRestAPI(
Ales Komarek07d16552016-09-12 21:39:18 +0200460 url=url, auth=auth)
Michael Kutý099c5342016-09-09 14:44:13 +0200461
462 return gerrit
463
464
Ales Komarekb0fcc252016-09-14 19:29:37 +0200465# Salt modules
Ales Komarek07d16552016-09-12 21:39:18 +0200466
467
468def 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 +0200469 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200470 Create a gerrit account
471
472 :param username: username
473 :param fullname: fullname
474 :param email: email
475 :param active: active
476 :param groups: array of strings
477 groups:
478 - Non-Interactive Users
479 - Testers
480 :param ssh_key: public ssh key
481 :param http_password: http password
482
483 CLI Examples:
484
485 .. code-block:: bash
486
487 salt '*' gerrit.account_create username "full name" "mail@domain.com"
488
489 '''
490 gerrit_client = _gerrit_http_connection(**kwargs)
491 output, changed = _update_account(
492 gerrit_client, **{
493 'username': username,
494 'fullname': fullname,
495 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200496# 'active': active,
Ales Komarek07d16552016-09-12 21:39:18 +0200497 'groups': groups,
498 'ssh_key': ssh_key,
499 'http_password': http_password
500 })
501 return output
502
503
504def account_update(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs):
505 '''
506 Create a gerrit account
Michael Kutý099c5342016-09-09 14:44:13 +0200507
508 :param username: username
509 :param fullname: fullname
510 :param email: email
Ales Komarekb0fcc252016-09-14 19:29:37 +0200511 :param active: active
Michael Kutý099c5342016-09-09 14:44:13 +0200512 :param groups: array of strings
513 groups:
514 - Non-Interactive Users
515 - Testers
Ales Komarek07d16552016-09-12 21:39:18 +0200516 :param ssh_key: public ssh key
517 :param http_password: http password
518
Michael Kutý099c5342016-09-09 14:44:13 +0200519 CLI Examples:
520
521 .. code-block:: bash
522
Ales Komarek07d16552016-09-12 21:39:18 +0200523 salt '*' gerrit.account_create username "full name" "mail@domain.com"
Michael Kutý099c5342016-09-09 14:44:13 +0200524
525 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200526 gerrit_client = _gerrit_http_connection(**kwargs)
527 output, changed = _update_account(
Michael Kutý099c5342016-09-09 14:44:13 +0200528 gerrit_client, **{
529 'username': username,
530 'fullname': fullname,
531 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200532# 'active': active,
Michael Kutý099c5342016-09-09 14:44:13 +0200533 'groups': groups,
Ales Komarek07d16552016-09-12 21:39:18 +0200534 'ssh_key': ssh_key,
535 'http_password': http_password
Michael Kutý099c5342016-09-09 14:44:13 +0200536 })
Michael Kutý099c5342016-09-09 14:44:13 +0200537 return output
538
Ales Komarek07d16552016-09-12 21:39:18 +0200539def account_list(**kwargs):
540 '''
541 List gerrit accounts
542
543 CLI Examples:
544
545 .. code-block:: bash
546
547 salt '*' gerrit.account_list
548
549 '''
550 gerrit_client = _gerrit_http_connection(**kwargs)
551 ret_list = gerrit_client.get('/accounts/?q=*&n=10000')
552 ret = {}
553 for item in ret_list:
554 ret[item['username']] = item
555 return ret
556
557
Ales Komarek2fc39002016-09-14 11:43:56 +0200558def account_get(name, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200559 '''
560 Get gerrit account
561
562 CLI Examples:
563
564 .. code-block:: bash
565
Ales Komarek2fc39002016-09-14 11:43:56 +0200566 salt '*' gerrit.account_get name
Ales Komarek07d16552016-09-12 21:39:18 +0200567
568 '''
569 gerrit_client = _gerrit_http_connection(**kwargs)
Ales Komarek2fc39002016-09-14 11:43:56 +0200570 accounts = account_list(**kwargs)
571 if(name in accounts):
572 ret = accounts.pop(name)
573 else:
574 ret = {'Error': 'Error in retrieving account'}
Ales Komarek07d16552016-09-12 21:39:18 +0200575 return ret
576
577
578def group_list(**kwargs):
579 '''
580 List gerrit groups
581
582 CLI Examples:
583
584 .. code-block:: bash
585
586 salt '*' gerrit.group_list
587
588 '''
589 gerrit_client = _gerrit_http_connection(**kwargs)
590 return gerrit_client.get('/groups/')
591
592
593def group_get(groupname, **kwargs):
594 '''
595 Get gerrit group
596
597 CLI Examples:
598
599 .. code-block:: bash
600
601 salt '*' gerrit.group_get groupname
602
603 '''
604 gerrit_client = _gerrit_http_connection(**kwargs)
605 try:
606 item = gerrit_client.get('/groups/%s' % groupname)
607 ret = {item['name']: item}
608 except:
609 ret = {'Error': 'Error in retrieving account'}
610 return ret
611
612
Ales Komarek2fc39002016-09-14 11:43:56 +0200613def group_create(name, description=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200614 '''
615 Create a gerrit group
616
617 :param name: name
618
619 CLI Examples:
620
621 .. code-block:: bash
622
Ales Komarekb0fcc252016-09-14 19:29:37 +0200623 salt '*' gerrit.group_create group-name description
Ales Komarek07d16552016-09-12 21:39:18 +0200624
625 '''
626 gerrit_client = _gerrit_http_connection(**kwargs)
627 ret, changed = _update_group(
Ales Komarek2fc39002016-09-14 11:43:56 +0200628 gerrit_client, **{'name': name, 'description': description})
Ales Komarek07d16552016-09-12 21:39:18 +0200629 return ret
630
Michael Kutý099c5342016-09-09 14:44:13 +0200631
Ales Komarek49a37292016-08-31 16:18:31 +0200632def project_create(name, **kwargs):
633 '''
634 Create a gerrit project
635
636 :param name: new project name
637
638 CLI Examples:
639
640 .. code-block:: bash
641
642 salt '*' gerrit.project_create namespace/nova description='nova project'
Michael Kutý099c5342016-09-09 14:44:13 +0200643
Ales Komarek49a37292016-08-31 16:18:31 +0200644 '''
645 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200646 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200647
648 project = project_get(name, **kwargs)
649
650 if project and not "Error" in project:
651 LOG.debug("Project {0} exists".format(name))
652 return project
653
654 new = gerrit_client.createProject(name)
655 return project_get(name, **kwargs)
656
Michael Kutý099c5342016-09-09 14:44:13 +0200657
Ales Komarek49a37292016-08-31 16:18:31 +0200658def project_get(name, **kwargs):
659 '''
660 Return a specific project
661
662 CLI Examples:
663
664 .. code-block:: bash
665
666 salt '*' gerrit.project_get projectname
667 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200668 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200669 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200670 projects = gerrit_client.listProjects()
671 if not name in projects:
672 return {'Error': 'Error in retrieving project'}
673 ret[name] = {'name': name}
674 return ret
675
676
677def project_list(**connection_args):
678 '''
679 Return a list of available projects
680
681 CLI Example:
682
683 .. code-block:: bash
684
685 salt '*' gerrit.project_list
686 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200687 gerrit_client = _gerrit_ssh_connection(**connection_args)
Ales Komarek49a37292016-08-31 16:18:31 +0200688 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200689 projects = gerrit_client.listProjects()
Ales Komarek49a37292016-08-31 16:18:31 +0200690 for project in projects:
691 ret[project] = {
692 'name': project
693 }
694 return ret
695
696
697def query(change, **kwargs):
698 '''
699 Query gerrit
700
701 :param change: Query content
702
703 CLI Examples:
704
705 .. code-block:: bash
706
707 salt '*' gerrit.query 'status:open project:tools/gerrit limit:2'
Michael Kutý099c5342016-09-09 14:44:13 +0200708
Ales Komarek49a37292016-08-31 16:18:31 +0200709 '''
710 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200711 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200712 msg = gerrit_client.query(change)
713 ret['query'] = msg
714 return ret