blob: 7a4c0c473e244210f22dfc4d4b37977fc2109237 [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
Ales Komarekb0fcc252016-09-14 19:29:37 +0200204
205 path = 'accounts/%s/sshkeys' % (account_id)
Ales Komarekb0fcc252016-09-14 19:29:37 +0200206
207 kwargs = {
208 "data": ssh_public_key
209 }
210 kwargs.update(gerrit.kwargs.copy())
211
212 response = requests.put(gerrit.make_url(path), **kwargs)
213
Ales Komarekb0fcc252016-09-14 19:29:37 +0200214
215def _create_group_membership(gerrit, account_id, group_id):
216 logging.info('Creating membership of %s in group %s', account_id, group_id)
217# group_id = _group_name2id(gerrit, group_id)
218 print group_id
219 import json
220 path = 'groups/%s/members/%s' % (_quote(group_id), account_id)
221 gerrit.put(path, data=json.dumps({}))
222
223
224def _ensure_only_member_of_these_groups(gerrit, account_id, salt_groups):
225 path = 'accounts/%s' % account_id
226 group_info_list = _get_list(gerrit, path + '/groups')
227
228 changed = False
229 gerrit_groups = []
230 for group_info in group_info_list:
231 if group_info['name'] in salt_groups:
232 logging.info("Preserving %s membership of group %s", path,
233 group_info)
234 gerrit_groups.append(group_info['name'])
235 else:
236 logging.info("Removing %s from group %s", path, group_info)
237 membership_path = 'groups/%s/members/%s' % (
238 _quote(group_info['id']), account_id)
239 try:
240 gerrit.delete(membership_path)
241 changed = True
242 except requests.exceptions.HTTPError as e:
243 if e.response.status_code == 404:
244 # This is a kludge, it'd be better to work out in advance
245 # which groups the user is a member of only via membership
246 # in a different. That's not trivial though with the
247 # current API Gerrit provides.
248 logging.info(
249 "Ignored %s; assuming membership of this group is due "
250 "to membership of a group that includes it.", e)
251 else:
252 raise
253
254 # If the user gave group IDs instead of group names, this will
255 # needlessly recreate the membership. The only actual issue will be that
256 # Ansible reports 'changed' when nothing really did change, I think.
257 #
258 # We might receive [""] when the user tries to pass in an empty list, so
259 # handle that.
260 for new_group in set(salt_groups).difference(gerrit_groups):
261 if len(new_group) > 0:
262 _create_group_membership(gerrit, account_id, new_group)
263 gerrit_groups.append(new_group)
264 changed = True
265
266 return gerrit_groups, changed
267
268
269def _ensure_only_one_account_email(gerrit, account_id, email):
270 path = 'accounts/%s' % account_id
271 email_info_list = _get_list(gerrit, path + '/emails')
272
273 changed = False
274 found_email = False
275 for email_info in email_info_list:
276 existing_email = email_info['email']
277 if existing_email == email:
278 # Since we're deleting all emails except this one, there's no need
279 # to care whether it's the 'preferred' one. It soon will be!
280 logging.info("Keeping %s email %s", path, email)
281 found_email = True
282 else:
283 logging.info("Removing %s email %s", path, existing_email)
284 gerrit.delete(path + '/emails/%s' % _quote(existing_email))
285 changed = True
286
287 if len(email) > 0 and not found_email:
288 _create_account_email(gerrit, account_id, email,
289 preferred=True, no_confirmation=True)
290 changed = True
291
292 return email, changed
293
294
295def _ensure_only_one_account_ssh_key(gerrit, account_id, ssh_public_key):
296 path = 'accounts/%s' % account_id
297 ssh_key_info_list = _get_list(gerrit, path + '/sshkeys')
298
299 changed = False
300 found_ssh_key = False
301 for ssh_key_info in ssh_key_info_list:
302 if ssh_key_info['ssh_public_key'] == ssh_public_key:
303 logging.info("Keeping %s SSH key %s", path, ssh_key_info)
304 found_ssh_key = True
305 else:
306 logging.info("Removing %s SSH key %s", path, ssh_key_info)
307 gerrit.delete(path + '/sshkeys/%i' % ssh_key_info['seq'])
308 changed = True
309
310 if len(ssh_public_key) > 0 and not found_ssh_key:
311 _create_account_ssh_key(gerrit, account_id, ssh_public_key)
312 changed = True
313
314 return ssh_public_key, changed
315
316
317def _update_account(gerrit, username=None, **params):
318 change = False
319
320 try:
321 account_info = gerrit.get('/accounts/%s' % _quote(username))
322 except requests.exceptions.HTTPError as e:
323 if e.response.status_code == 404:
324 logging.info("Account %s not found, creating it.", username)
325 account_info = _create_account(gerrit, username)
326 change = True
327 else:
328 raise
329
330 logging.debug(
331 'Existing account info for account %s: %s', username,
332 json.dumps(account_info, indent=4))
333
334 account_id = account_info['_account_id']
335 path = 'accounts/%s' % account_id
336
337 output = {}
338 output['username'] = username
339 output['id'] = account_id
340
341 fullname, fullname_changed = _maybe_update_field(
342 gerrit, path, 'name', account_info.get('name'), params.get('fullname'))
343 output['fullname'] = fullname
344 change |= fullname_changed
345
346 # Set the value of params that the user did not provide to None.
347
348 if params.get('active') is not None:
349 active = _get_boolean(gerrit, path + '/active')
350 active, active_changed = _maybe_update_field(
351 gerrit, path, 'active', active, params['active'], type='bool')
352 output['active'] = active
353 change |= active_changed
354
355 if params.get('email') is not None:
356 email, emails_changed = _ensure_only_one_account_email(
357 gerrit, account_id, params['email'])
358 output['email'] = email
359 change |= emails_changed
360
361 if params.get('groups') is not None:
362 groups, groups_changed = _ensure_only_member_of_these_groups(
363 gerrit, account_info.get('name'), params['groups'])
364 output['groups'] = groups
365 change |= groups_changed
366
367 if params.get('http_password') is not None:
368 http_password = _get_string(gerrit, path + '/password.http')
369 http_password, http_password_changed = _maybe_update_field(
370 gerrit, path, 'http_password', http_password,
371 params.get('http_password'),
372 gerrit_api_path='password.http')
373 output['http_password'] = http_password
374 change |= http_password_changed
375
376 if params.get('ssh_key') is not None:
377 ssh_key, ssh_keys_changed = _ensure_only_one_account_ssh_key(
378 gerrit, account_id, params['ssh_key'])
379 output['ssh_key'] = ssh_key
380 change |= ssh_keys_changed
381
382 return output, change
383
384
385def _update_group(gerrit, name=None, **params):
386 change = False
387
388 try:
389 group_info = gerrit.get('/groups/%s' % _quote(name))
390 except requests.exceptions.HTTPError as e:
391 if e.response.status_code == 404:
392 logging.info("Group %s not found, creating it.", name)
393 group_info = _create_group(gerrit, name)
394 change = True
395 else:
396 raise
397
398 logging.debug(
399 'Existing info for group %s: %s', name,
400 json.dumps(group_info, indent=4))
401
402 output = {group_info['name']: group_info}
403
404 return output, change
405
406
407# Gerrit client connectors
Michael Kutý099c5342016-09-09 14:44:13 +0200408
409
Ales Komarek07d16552016-09-12 21:39:18 +0200410def _gerrit_ssh_connection(**connection_args):
411 '''
412 Set up gerrit credentials
413
414 Only intended to be used within gerrit-enabled modules
415 '''
416
417 prefix = "gerrit"
418
419 # look in connection_args first, then default to config file
420 def get(key, default=None):
421 return connection_args.get('connection_' + key,
422 __salt__['config.get'](prefix, {})).get(key, default)
423
424 host = get('host', 'localhost')
425 user = get('user', 'admin')
426 keyfile = get('keyfile', '/var/cache/salt/minion/gerrit_rsa')
427
428 gerrit_client = gerrit.Gerrit(host, user, keyfile=keyfile)
429
430 return gerrit_client
431
432
433def _gerrit_http_connection(**connection_args):
Michael Kutý099c5342016-09-09 14:44:13 +0200434
435 prefix = "gerrit"
436
437 # look in connection_args first, then default to config file
438 def get(key, default=None):
439 return connection_args.get(
440 'connection_' + key,
441 __salt__['config.get'](prefix, {})).get(key, default)
442
443 host = get('host', 'localhost')
Ales Komarek07d16552016-09-12 21:39:18 +0200444 http_port = get('http_port', '8082')
445 protocol = get('protocol', 'http')
Michael Kutý099c5342016-09-09 14:44:13 +0200446 username = get('user', 'admin')
447 password = get('password', 'admin')
448
Ales Komarek07d16552016-09-12 21:39:18 +0200449 url = protocol+"://"+str(host)+':'+str(http_port)
450
Michael Kutý099c5342016-09-09 14:44:13 +0200451 auth = requests.auth.HTTPDigestAuth(
452 username, password)
453
454 gerrit = pygerrit.rest.GerritRestAPI(
Ales Komarek07d16552016-09-12 21:39:18 +0200455 url=url, auth=auth)
Michael Kutý099c5342016-09-09 14:44:13 +0200456
457 return gerrit
458
459
Ales Komarekb0fcc252016-09-14 19:29:37 +0200460# Salt modules
Ales Komarek07d16552016-09-12 21:39:18 +0200461
462
463def 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 +0200464 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200465 Create a gerrit account
466
467 :param username: username
468 :param fullname: fullname
469 :param email: email
470 :param active: active
471 :param groups: array of strings
472 groups:
473 - Non-Interactive Users
474 - Testers
475 :param ssh_key: public ssh key
476 :param http_password: http password
477
478 CLI Examples:
479
480 .. code-block:: bash
481
482 salt '*' gerrit.account_create username "full name" "mail@domain.com"
483
484 '''
485 gerrit_client = _gerrit_http_connection(**kwargs)
486 output, changed = _update_account(
487 gerrit_client, **{
488 'username': username,
489 'fullname': fullname,
490 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200491# 'active': active,
Ales Komarek07d16552016-09-12 21:39:18 +0200492 'groups': groups,
493 'ssh_key': ssh_key,
494 'http_password': http_password
495 })
496 return output
497
498
499def account_update(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs):
500 '''
501 Create a gerrit account
Michael Kutý099c5342016-09-09 14:44:13 +0200502
503 :param username: username
504 :param fullname: fullname
505 :param email: email
Ales Komarekb0fcc252016-09-14 19:29:37 +0200506 :param active: active
Michael Kutý099c5342016-09-09 14:44:13 +0200507 :param groups: array of strings
508 groups:
509 - Non-Interactive Users
510 - Testers
Ales Komarek07d16552016-09-12 21:39:18 +0200511 :param ssh_key: public ssh key
512 :param http_password: http password
513
Michael Kutý099c5342016-09-09 14:44:13 +0200514 CLI Examples:
515
516 .. code-block:: bash
517
Ales Komarek07d16552016-09-12 21:39:18 +0200518 salt '*' gerrit.account_create username "full name" "mail@domain.com"
Michael Kutý099c5342016-09-09 14:44:13 +0200519
520 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200521 gerrit_client = _gerrit_http_connection(**kwargs)
522 output, changed = _update_account(
Michael Kutý099c5342016-09-09 14:44:13 +0200523 gerrit_client, **{
524 'username': username,
525 'fullname': fullname,
526 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200527# 'active': active,
Michael Kutý099c5342016-09-09 14:44:13 +0200528 'groups': groups,
Ales Komarek07d16552016-09-12 21:39:18 +0200529 'ssh_key': ssh_key,
530 'http_password': http_password
Michael Kutý099c5342016-09-09 14:44:13 +0200531 })
Michael Kutý099c5342016-09-09 14:44:13 +0200532 return output
533
Ales Komarek07d16552016-09-12 21:39:18 +0200534def account_list(**kwargs):
535 '''
536 List gerrit accounts
537
538 CLI Examples:
539
540 .. code-block:: bash
541
542 salt '*' gerrit.account_list
543
544 '''
545 gerrit_client = _gerrit_http_connection(**kwargs)
546 ret_list = gerrit_client.get('/accounts/?q=*&n=10000')
547 ret = {}
548 for item in ret_list:
549 ret[item['username']] = item
550 return ret
551
552
Ales Komarek2fc39002016-09-14 11:43:56 +0200553def account_get(name, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200554 '''
555 Get gerrit account
556
557 CLI Examples:
558
559 .. code-block:: bash
560
Ales Komarek2fc39002016-09-14 11:43:56 +0200561 salt '*' gerrit.account_get name
Ales Komarek07d16552016-09-12 21:39:18 +0200562
563 '''
564 gerrit_client = _gerrit_http_connection(**kwargs)
Ales Komarek2fc39002016-09-14 11:43:56 +0200565 accounts = account_list(**kwargs)
566 if(name in accounts):
567 ret = accounts.pop(name)
568 else:
569 ret = {'Error': 'Error in retrieving account'}
Ales Komarek07d16552016-09-12 21:39:18 +0200570 return ret
571
572
573def group_list(**kwargs):
574 '''
575 List gerrit groups
576
577 CLI Examples:
578
579 .. code-block:: bash
580
581 salt '*' gerrit.group_list
582
583 '''
584 gerrit_client = _gerrit_http_connection(**kwargs)
585 return gerrit_client.get('/groups/')
586
587
588def group_get(groupname, **kwargs):
589 '''
590 Get gerrit group
591
592 CLI Examples:
593
594 .. code-block:: bash
595
596 salt '*' gerrit.group_get groupname
597
598 '''
599 gerrit_client = _gerrit_http_connection(**kwargs)
600 try:
601 item = gerrit_client.get('/groups/%s' % groupname)
602 ret = {item['name']: item}
603 except:
604 ret = {'Error': 'Error in retrieving account'}
605 return ret
606
607
Ales Komarek2fc39002016-09-14 11:43:56 +0200608def group_create(name, description=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200609 '''
610 Create a gerrit group
611
612 :param name: name
613
614 CLI Examples:
615
616 .. code-block:: bash
617
Ales Komarekb0fcc252016-09-14 19:29:37 +0200618 salt '*' gerrit.group_create group-name description
Ales Komarek07d16552016-09-12 21:39:18 +0200619
620 '''
621 gerrit_client = _gerrit_http_connection(**kwargs)
622 ret, changed = _update_group(
Ales Komarek2fc39002016-09-14 11:43:56 +0200623 gerrit_client, **{'name': name, 'description': description})
Ales Komarek07d16552016-09-12 21:39:18 +0200624 return ret
625
Michael Kutý099c5342016-09-09 14:44:13 +0200626
Ales Komarek49a37292016-08-31 16:18:31 +0200627def project_create(name, **kwargs):
628 '''
629 Create a gerrit project
630
631 :param name: new project name
632
633 CLI Examples:
634
635 .. code-block:: bash
636
637 salt '*' gerrit.project_create namespace/nova description='nova project'
Michael Kutý099c5342016-09-09 14:44:13 +0200638
Ales Komarek49a37292016-08-31 16:18:31 +0200639 '''
640 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200641 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200642
643 project = project_get(name, **kwargs)
644
645 if project and not "Error" in project:
646 LOG.debug("Project {0} exists".format(name))
647 return project
648
649 new = gerrit_client.createProject(name)
650 return project_get(name, **kwargs)
651
Michael Kutý099c5342016-09-09 14:44:13 +0200652
Ales Komarek49a37292016-08-31 16:18:31 +0200653def project_get(name, **kwargs):
654 '''
655 Return a specific project
656
657 CLI Examples:
658
659 .. code-block:: bash
660
661 salt '*' gerrit.project_get projectname
662 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200663 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200664 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200665 projects = gerrit_client.listProjects()
666 if not name in projects:
667 return {'Error': 'Error in retrieving project'}
668 ret[name] = {'name': name}
669 return ret
670
671
672def project_list(**connection_args):
673 '''
674 Return a list of available projects
675
676 CLI Example:
677
678 .. code-block:: bash
679
680 salt '*' gerrit.project_list
681 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200682 gerrit_client = _gerrit_ssh_connection(**connection_args)
Ales Komarek49a37292016-08-31 16:18:31 +0200683 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200684 projects = gerrit_client.listProjects()
Ales Komarek49a37292016-08-31 16:18:31 +0200685 for project in projects:
686 ret[project] = {
687 'name': project
688 }
689 return ret
690
691
692def query(change, **kwargs):
693 '''
694 Query gerrit
695
696 :param change: Query content
697
698 CLI Examples:
699
700 .. code-block:: bash
701
702 salt '*' gerrit.query 'status:open project:tools/gerrit limit:2'
Michael Kutý099c5342016-09-09 14:44:13 +0200703
Ales Komarek49a37292016-08-31 16:18:31 +0200704 '''
705 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200706 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200707 msg = gerrit_client.query(change)
708 ret['query'] = msg
709 return ret