blob: 2f520b99bd9a8bf5f41198d7e2e6ff318a3a33a5 [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
Michael Kutý099c5342016-09-09 14:44:13 +020065# COMMON
66
67
68def value_from_param(field, spec, param_value):
69 if 'choices' in spec:
70 if param_value not in spec['choices']:
71 raise ValueError(
72 "'%s' is not valid for field %s" % (param_value, field))
73 value = param_value.upper()
74 else:
75 value = param_value
76 return value
77
78
79def value_from_config_info(field, spec, info_value):
80 if isinstance(info_value, dict):
81 # This is a ConfigParameterInfo field. We need to figure out if the
82 # value is TRUE, FALSE or INHERIT.
83 if 'configured_value' in info_value:
84 value = info_value['configured_value']
85 else:
86 value = 'INHERIT'
87 else:
88 value = info_value
89 return value
90
91
92def get_boolean(gerrit, path):
93 response = gerrit.get(path)
94 if response == 'ok':
95 value = True
96 elif response == '':
97 value = False
98 else:
99 raise AnsibleGerritError(
100 "Unexpected response for %s: %s" % (path, response))
101 return value
102
103
104def get_list(gerrit, path):
105 values = gerrit.get(path)
106 return values
107
108
109def get_string(gerrit, path):
110 try:
111 value = gerrit.get(path)
112 except requests.exceptions.HTTPError as e:
113 if e.response.status_code == 404:
114 logging.debug("Ignoring exception %s", e)
115 logging.debug("Got %s", e.response.__dict__)
116 value = None
117 else:
118 raise
119 return value
120
121
122def set_boolean(gerrit, path, value):
123 if value:
124 gerrit.put(path)
125 else:
126 gerrit.delete(path)
127
128
129def set_string(gerrit, path, value, field_name=None):
130 field_name = field_name or os.path.basename(path)
131
132 # Setting to '' is equivalent to deleting, so we have no need for the
133 # DELETE method.
134 headers = {'content-type': 'application/json'}
135 data = json.dumps({field_name: value})
136 gerrit.put(path, data=data, headers=headers)
137
138
139def maybe_update_field(gerrit, path, field, gerrit_value, ansible_value,
140 type='str', gerrit_api_path=None):
141
142 gerrit_api_path = gerrit_api_path or field
143 fullpath = path + '/' + gerrit_api_path
144
145 if gerrit_value == ansible_value:
146 logging.info("Not updating %s: same value specified: %s", fullpath,
147 gerrit_value)
148 value = gerrit_value
149 changed = False
150 elif ansible_value is None:
151 logging.info("Not updating %s: no value specified, value stays as %s",
152 fullpath, gerrit_value)
153 value = gerrit_value
154 changed = False
155 else:
156 logging.info("Changing %s from %s to %s", fullpath, gerrit_value,
157 ansible_value)
158 if type == 'str':
159 set_string(gerrit, fullpath, ansible_value, field_name=field)
160 elif type == 'bool':
161 set_boolean(gerrit, fullpath, ansible_value)
162 else:
163 raise AssertionError("Unknown Ansible parameter type '%s'" % type)
164
165 value = ansible_value
166 changed = True
167 return value, changed
168
Ales Komarek07d16552016-09-12 21:39:18 +0200169
170def quote(name):
171 return urllib.quote(name, safe="")
172
173
Michael Kutý099c5342016-09-09 14:44:13 +0200174# END COMMON
175
176
Ales Komarek07d16552016-09-12 21:39:18 +0200177def _gerrit_ssh_connection(**connection_args):
178 '''
179 Set up gerrit credentials
180
181 Only intended to be used within gerrit-enabled modules
182 '''
183
184 prefix = "gerrit"
185
186 # look in connection_args first, then default to config file
187 def get(key, default=None):
188 return connection_args.get('connection_' + key,
189 __salt__['config.get'](prefix, {})).get(key, default)
190
191 host = get('host', 'localhost')
192 user = get('user', 'admin')
193 keyfile = get('keyfile', '/var/cache/salt/minion/gerrit_rsa')
194
195 gerrit_client = gerrit.Gerrit(host, user, keyfile=keyfile)
196
197 return gerrit_client
198
199
200def _gerrit_http_connection(**connection_args):
Michael Kutý099c5342016-09-09 14:44:13 +0200201
202 prefix = "gerrit"
203
204 # look in connection_args first, then default to config file
205 def get(key, default=None):
206 return connection_args.get(
207 'connection_' + key,
208 __salt__['config.get'](prefix, {})).get(key, default)
209
210 host = get('host', 'localhost')
Ales Komarek07d16552016-09-12 21:39:18 +0200211 http_port = get('http_port', '8082')
212 protocol = get('protocol', 'http')
Michael Kutý099c5342016-09-09 14:44:13 +0200213 username = get('user', 'admin')
214 password = get('password', 'admin')
215
Ales Komarek07d16552016-09-12 21:39:18 +0200216 url = protocol+"://"+str(host)+':'+str(http_port)
217
Michael Kutý099c5342016-09-09 14:44:13 +0200218 auth = requests.auth.HTTPDigestAuth(
219 username, password)
220
221 gerrit = pygerrit.rest.GerritRestAPI(
Ales Komarek07d16552016-09-12 21:39:18 +0200222 url=url, auth=auth)
Michael Kutý099c5342016-09-09 14:44:13 +0200223
224 return gerrit
225
226
Ales Komarek2fc39002016-09-14 11:43:56 +0200227def _account_name2id(gerrit, name=None):
Ales Komarek07d16552016-09-12 21:39:18 +0200228 # Although we could pass an AccountInput entry here to set details in one
229 # go, it's left up to the _update_group() function, to avoid having a
230 # totally separate code path for create vs. update.
Ales Komarek2fc39002016-09-14 11:43:56 +0200231 info = gerrit.get('/accounts/%s' % quote(name))
232 return info['_account_id']
233
234
235def _group_name2id(gerrit, name=None):
236 # Although we could pass an AccountInput entry here to set details in one
237 # go, it's left up to the _update_group() function, to avoid having a
238 # totally separate code path for create vs. update.
239 info = gerrit.get('/groups/%s' % quote(name))
240 return info['id']
Ales Komarek07d16552016-09-12 21:39:18 +0200241
242
243def _create_group(gerrit, name=None):
244 # Although we could pass an AccountInput entry here to set details in one
245 # go, it's left up to the _update_group() function, to avoid having a
246 # totally separate code path for create vs. update.
247 group_info = gerrit.put('/groups/%s' % quote(name))
248 return group_info
249
250
Michael Kutý099c5342016-09-09 14:44:13 +0200251def create_account(gerrit, username=None):
252 # Although we could pass an AccountInput entry here to set details in one
Ales Komarek07d16552016-09-12 21:39:18 +0200253 # go, it's left up to the _update_account() function, to avoid having a
Michael Kutý099c5342016-09-09 14:44:13 +0200254 # totally separate code path for create vs. update.
255 account_info = gerrit.put('/accounts/%s' % quote(username))
256 return account_info
257
258
259def create_account_email(gerrit, account_id, email, preferred=False,
260 no_confirmation=False):
261 logging.info('Creating email %s for account %s', email, account_id)
262
263 email_input = {
264 # Setting 'email' is optional (it's already in the URL) but it's good
265 # to double check that the email is encoded in the URL properly.
266 'email': email,
267 'preferred': preferred,
268 'no_confirmation': no_confirmation,
269 }
270 logging.debug(email_input)
271
272 path = 'accounts/%s/emails/%s' % (account_id, quote(email))
273 headers = {'content-type': 'application/json'}
274 gerrit.post(path, data=json.dumps(email_input), headers=headers)
275
276
277def create_account_ssh_key(gerrit, account_id, ssh_public_key):
278 logging.info('Creating SSH key %s for account %s', ssh_public_key,
279 account_id)
280
281 path = 'accounts/%s/sshkeys' % (account_id)
282 gerrit.post(path, data=ssh_public_key)
283
284
285def create_group_membership(gerrit, account_id, group_id):
286 logging.info('Creating membership of %s in group %s', account_id, group_id)
Ales Komarek2fc39002016-09-14 11:43:56 +0200287# group_id = _group_name2id(gerrit, group_id)
288 print group_id
Michael Kutý099c5342016-09-09 14:44:13 +0200289 path = 'groups/%s/members/%s' % (quote(group_id), account_id)
290 gerrit.put(path)
291
292
293def ensure_only_member_of_these_groups(gerrit, account_id, ansible_groups):
294 path = 'accounts/%s' % account_id
295 group_info_list = get_list(gerrit, path + '/groups')
296
297 changed = False
298 gerrit_groups = []
299 for group_info in group_info_list:
300 if group_info['name'] in ansible_groups:
301 logging.info("Preserving %s membership of group %s", path,
302 group_info)
303 gerrit_groups.append(group_info['name'])
304 else:
305 logging.info("Removing %s from group %s", path, group_info)
306 membership_path = 'groups/%s/members/%s' % (
307 quote(group_info['id']), account_id)
308 try:
309 gerrit.delete(membership_path)
310 changed = True
311 except requests.exceptions.HTTPError as e:
312 if e.response.status_code == 404:
313 # This is a kludge, it'd be better to work out in advance
314 # which groups the user is a member of only via membership
315 # in a different. That's not trivial though with the
316 # current API Gerrit provides.
317 logging.info(
318 "Ignored %s; assuming membership of this group is due "
319 "to membership of a group that includes it.", e)
320 else:
321 raise
322
323 # If the user gave group IDs instead of group names, this will
324 # needlessly recreate the membership. The only actual issue will be that
325 # Ansible reports 'changed' when nothing really did change, I think.
326 #
327 # We might receive [""] when the user tries to pass in an empty list, so
328 # handle that.
329 for new_group in set(ansible_groups).difference(gerrit_groups):
330 if len(new_group) > 0:
331 create_group_membership(gerrit, account_id, new_group)
332 gerrit_groups.append(new_group)
333 changed = True
334
335 return gerrit_groups, changed
336
337
338def ensure_only_one_account_email(gerrit, account_id, email):
339 path = 'accounts/%s' % account_id
340 email_info_list = get_list(gerrit, path + '/emails')
341
342 changed = False
343 found_email = False
344 for email_info in email_info_list:
345 existing_email = email_info['email']
346 if existing_email == email:
347 # Since we're deleting all emails except this one, there's no need
348 # to care whether it's the 'preferred' one. It soon will be!
349 logging.info("Keeping %s email %s", path, email)
350 found_email = True
351 else:
352 logging.info("Removing %s email %s", path, existing_email)
353 gerrit.delete(path + '/emails/%s' % quote(existing_email))
354 changed = True
355
356 if len(email) > 0 and not found_email:
357 create_account_email(gerrit, account_id, email,
358 preferred=True, no_confirmation=True)
359 changed = True
360
361 return email, changed
362
363
364def ensure_only_one_account_ssh_key(gerrit, account_id, ssh_public_key):
365 path = 'accounts/%s' % account_id
366 ssh_key_info_list = get_list(gerrit, path + '/sshkeys')
367
368 changed = False
369 found_ssh_key = False
370 for ssh_key_info in ssh_key_info_list:
371 if ssh_key_info['ssh_public_key'] == ssh_public_key:
372 logging.info("Keeping %s SSH key %s", path, ssh_key_info)
373 found_ssh_key = True
374 else:
375 logging.info("Removing %s SSH key %s", path, ssh_key_info)
376 gerrit.delete(path + '/sshkeys/%i' % ssh_key_info['seq'])
377 changed = True
378
379 if len(ssh_public_key) > 0 and not found_ssh_key:
380 create_account_ssh_key(gerrit, account_id, ssh_public_key)
381 changed = True
382
383 return ssh_public_key, changed
384
385
Ales Komarek07d16552016-09-12 21:39:18 +0200386def _update_account(gerrit, username=None, **params):
Michael Kutý099c5342016-09-09 14:44:13 +0200387 change = False
388
389 try:
390 account_info = gerrit.get('/accounts/%s' % quote(username))
391 except requests.exceptions.HTTPError as e:
392 if e.response.status_code == 404:
393 logging.info("Account %s not found, creating it.", username)
394 account_info = create_account(gerrit, username)
395 change = True
396 else:
397 raise
398
399 logging.debug(
400 'Existing account info for account %s: %s', username,
401 json.dumps(account_info, indent=4))
402
403 account_id = account_info['_account_id']
404 path = 'accounts/%s' % account_id
405
406 output = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200407 output['username'] = username
Michael Kutý099c5342016-09-09 14:44:13 +0200408 output['id'] = account_id
409
410 fullname, fullname_changed = maybe_update_field(
411 gerrit, path, 'name', account_info.get('name'), params.get('fullname'))
412 output['fullname'] = fullname
413 change |= fullname_changed
414
Ales Komarek07d16552016-09-12 21:39:18 +0200415 # Set the value of params that the user did not provide to None.
Michael Kutý099c5342016-09-09 14:44:13 +0200416
417 if params.get('active') is not None:
418 active = get_boolean(gerrit, path + '/active')
419 active, active_changed = maybe_update_field(
420 gerrit, path, 'active', active, params['active'], type='bool')
421 output['active'] = active
422 change |= active_changed
423
424 if params.get('email') is not None:
425 email, emails_changed = ensure_only_one_account_email(
426 gerrit, account_id, params['email'])
427 output['email'] = email
428 change |= emails_changed
429
Ales Komarek2fc39002016-09-14 11:43:56 +0200430 if params.get('groups') is not None:
431 groups, groups_changed = ensure_only_member_of_these_groups(
432 gerrit, account_info.get('name'), params['groups'])
433 output['groups'] = groups
434 change |= groups_changed
Michael Kutý099c5342016-09-09 14:44:13 +0200435
436 if params.get('http_password') is not None:
437 http_password = get_string(gerrit, path + '/password.http')
438 http_password, http_password_changed = maybe_update_field(
439 gerrit, path, 'http_password', http_password,
440 params.get('http_password'),
441 gerrit_api_path='password.http')
442 output['http_password'] = http_password
443 change |= http_password_changed
444
445 if params.get('ssh_key') is not None:
446 ssh_key, ssh_keys_changed = ensure_only_one_account_ssh_key(
447 gerrit, account_id, params['ssh_key'])
448 output['ssh_key'] = ssh_key
449 change |= ssh_keys_changed
450
451 return output, change
452
453
Ales Komarek07d16552016-09-12 21:39:18 +0200454def _update_group(gerrit, name=None, **params):
455 change = False
456
457 try:
458 group_info = gerrit.get('/groups/%s' % quote(name))
459 except requests.exceptions.HTTPError as e:
460 if e.response.status_code == 404:
461 logging.info("Group %s not found, creating it.", name)
462 group_info = _create_group(gerrit, name)
463 change = True
464 else:
465 raise
466
467 logging.debug(
468 'Existing info for group %s: %s', name,
469 json.dumps(group_info, indent=4))
470
471 output = {group_info['name']: group_info}
472
473 return output, change
474
475
476def 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 +0200477 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200478 Create a gerrit account
479
480 :param username: username
481 :param fullname: fullname
482 :param email: email
483 :param active: active
484 :param groups: array of strings
485 groups:
486 - Non-Interactive Users
487 - Testers
488 :param ssh_key: public ssh key
489 :param http_password: http password
490
491 CLI Examples:
492
493 .. code-block:: bash
494
495 salt '*' gerrit.account_create username "full name" "mail@domain.com"
496
497 '''
498 gerrit_client = _gerrit_http_connection(**kwargs)
499 output, changed = _update_account(
500 gerrit_client, **{
501 'username': username,
502 'fullname': fullname,
503 'email': email,
504 'groups': groups,
505 'ssh_key': ssh_key,
506 'http_password': http_password
507 })
508 return output
509
510
511def account_update(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs):
512 '''
513 Create a gerrit account
Michael Kutý099c5342016-09-09 14:44:13 +0200514
515 :param username: username
516 :param fullname: fullname
517 :param email: email
518 :param groups: array of strings
519 groups:
520 - Non-Interactive Users
521 - Testers
Ales Komarek07d16552016-09-12 21:39:18 +0200522 :param ssh_key: public ssh key
523 :param http_password: http password
524
Michael Kutý099c5342016-09-09 14:44:13 +0200525 CLI Examples:
526
527 .. code-block:: bash
528
Ales Komarek07d16552016-09-12 21:39:18 +0200529 salt '*' gerrit.account_create username "full name" "mail@domain.com"
Michael Kutý099c5342016-09-09 14:44:13 +0200530
531 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200532 gerrit_client = _gerrit_http_connection(**kwargs)
533 output, changed = _update_account(
Michael Kutý099c5342016-09-09 14:44:13 +0200534 gerrit_client, **{
535 'username': username,
536 'fullname': fullname,
537 'email': email,
538 'groups': groups,
Ales Komarek07d16552016-09-12 21:39:18 +0200539 'ssh_key': ssh_key,
540 'http_password': http_password
Michael Kutý099c5342016-09-09 14:44:13 +0200541 })
Michael Kutý099c5342016-09-09 14:44:13 +0200542 return output
543
Ales Komarek07d16552016-09-12 21:39:18 +0200544def account_list(**kwargs):
545 '''
546 List gerrit accounts
547
548 CLI Examples:
549
550 .. code-block:: bash
551
552 salt '*' gerrit.account_list
553
554 '''
555 gerrit_client = _gerrit_http_connection(**kwargs)
556 ret_list = gerrit_client.get('/accounts/?q=*&n=10000')
557 ret = {}
558 for item in ret_list:
559 ret[item['username']] = item
560 return ret
561
562
Ales Komarek2fc39002016-09-14 11:43:56 +0200563def account_get(name, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200564 '''
565 Get gerrit account
566
567 CLI Examples:
568
569 .. code-block:: bash
570
Ales Komarek2fc39002016-09-14 11:43:56 +0200571 salt '*' gerrit.account_get name
Ales Komarek07d16552016-09-12 21:39:18 +0200572
573 '''
574 gerrit_client = _gerrit_http_connection(**kwargs)
Ales Komarek2fc39002016-09-14 11:43:56 +0200575 accounts = account_list(**kwargs)
576 if(name in accounts):
577 ret = accounts.pop(name)
578 else:
579 ret = {'Error': 'Error in retrieving account'}
Ales Komarek07d16552016-09-12 21:39:18 +0200580 return ret
581
582
583def group_list(**kwargs):
584 '''
585 List gerrit groups
586
587 CLI Examples:
588
589 .. code-block:: bash
590
591 salt '*' gerrit.group_list
592
593 '''
594 gerrit_client = _gerrit_http_connection(**kwargs)
595 return gerrit_client.get('/groups/')
596
597
598def group_get(groupname, **kwargs):
599 '''
600 Get gerrit group
601
602 CLI Examples:
603
604 .. code-block:: bash
605
606 salt '*' gerrit.group_get groupname
607
608 '''
609 gerrit_client = _gerrit_http_connection(**kwargs)
610 try:
611 item = gerrit_client.get('/groups/%s' % groupname)
612 ret = {item['name']: item}
613 except:
614 ret = {'Error': 'Error in retrieving account'}
615 return ret
616
617
Ales Komarek2fc39002016-09-14 11:43:56 +0200618def group_create(name, description=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200619 '''
620 Create a gerrit group
621
622 :param name: name
623
624 CLI Examples:
625
626 .. code-block:: bash
627
Ales Komarek2fc39002016-09-14 11:43:56 +0200628 salt '*' gerrit.group_create group-name fsdfgwegfe
Ales Komarek07d16552016-09-12 21:39:18 +0200629
630 '''
631 gerrit_client = _gerrit_http_connection(**kwargs)
632 ret, changed = _update_group(
Ales Komarek2fc39002016-09-14 11:43:56 +0200633 gerrit_client, **{'name': name, 'description': description})
Ales Komarek07d16552016-09-12 21:39:18 +0200634 return ret
635
Michael Kutý099c5342016-09-09 14:44:13 +0200636
Ales Komarek49a37292016-08-31 16:18:31 +0200637def project_create(name, **kwargs):
638 '''
639 Create a gerrit project
640
641 :param name: new project name
642
643 CLI Examples:
644
645 .. code-block:: bash
646
647 salt '*' gerrit.project_create namespace/nova description='nova project'
Michael Kutý099c5342016-09-09 14:44:13 +0200648
Ales Komarek49a37292016-08-31 16:18:31 +0200649 '''
650 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200651 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200652
653 project = project_get(name, **kwargs)
654
655 if project and not "Error" in project:
656 LOG.debug("Project {0} exists".format(name))
657 return project
658
659 new = gerrit_client.createProject(name)
660 return project_get(name, **kwargs)
661
Michael Kutý099c5342016-09-09 14:44:13 +0200662
Ales Komarek49a37292016-08-31 16:18:31 +0200663def project_get(name, **kwargs):
664 '''
665 Return a specific project
666
667 CLI Examples:
668
669 .. code-block:: bash
670
671 salt '*' gerrit.project_get projectname
672 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200673 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200674 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200675 projects = gerrit_client.listProjects()
676 if not name in projects:
677 return {'Error': 'Error in retrieving project'}
678 ret[name] = {'name': name}
679 return ret
680
681
682def project_list(**connection_args):
683 '''
684 Return a list of available projects
685
686 CLI Example:
687
688 .. code-block:: bash
689
690 salt '*' gerrit.project_list
691 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200692 gerrit_client = _gerrit_ssh_connection(**connection_args)
Ales Komarek49a37292016-08-31 16:18:31 +0200693 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200694 projects = gerrit_client.listProjects()
Ales Komarek49a37292016-08-31 16:18:31 +0200695 for project in projects:
696 ret[project] = {
697 'name': project
698 }
699 return ret
700
701
702def query(change, **kwargs):
703 '''
704 Query gerrit
705
706 :param change: Query content
707
708 CLI Examples:
709
710 .. code-block:: bash
711
712 salt '*' gerrit.query 'status:open project:tools/gerrit limit:2'
Michael Kutý099c5342016-09-09 14:44:13 +0200713
Ales Komarek49a37292016-08-31 16:18:31 +0200714 '''
715 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200716 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200717 msg = gerrit_client.query(change)
718 ret['query'] = msg
719 return ret