blob: 369c2a2c3a9be91a8898d2ea722c0122b960a376 [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:
Filip Pytloun512baf42017-03-09 22:14:00 +010074 raise Exception(
Michael Kutý099c5342016-09-09 14:44:13 +020075 "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)
Filip Pytloun9c1b92f2017-02-28 19:26:49 +010087 except Exception as e:
Filip Pytloun693ebbe2017-03-21 12:55:09 +010088 try:
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 except Exception:
96 raise e
Michael Kutý099c5342016-09-09 14:44:13 +020097 return value
98
99
Ales Komarekb0fcc252016-09-14 19:29:37 +0200100def _set_boolean(gerrit, path, value):
Michael Kutý099c5342016-09-09 14:44:13 +0200101 if value:
102 gerrit.put(path)
103 else:
104 gerrit.delete(path)
105
106
Ales Komarekb0fcc252016-09-14 19:29:37 +0200107def _set_string(gerrit, path, value, field_name=None):
Michael Kutý099c5342016-09-09 14:44:13 +0200108 field_name = field_name or os.path.basename(path)
109
110 # Setting to '' is equivalent to deleting, so we have no need for the
111 # DELETE method.
112 headers = {'content-type': 'application/json'}
113 data = json.dumps({field_name: value})
114 gerrit.put(path, data=data, headers=headers)
115
116
Ales Komarekb0fcc252016-09-14 19:29:37 +0200117def _maybe_update_field(gerrit, path, field, gerrit_value, salt_value,
Michael Kutý099c5342016-09-09 14:44:13 +0200118 type='str', gerrit_api_path=None):
119
120 gerrit_api_path = gerrit_api_path or field
121 fullpath = path + '/' + gerrit_api_path
122
Ales Komarekb0fcc252016-09-14 19:29:37 +0200123 if gerrit_value == salt_value:
Michael Kutý099c5342016-09-09 14:44:13 +0200124 logging.info("Not updating %s: same value specified: %s", fullpath,
125 gerrit_value)
126 value = gerrit_value
127 changed = False
Ales Komarekb0fcc252016-09-14 19:29:37 +0200128 elif salt_value is None:
Michael Kutý099c5342016-09-09 14:44:13 +0200129 logging.info("Not updating %s: no value specified, value stays as %s",
130 fullpath, gerrit_value)
131 value = gerrit_value
132 changed = False
133 else:
134 logging.info("Changing %s from %s to %s", fullpath, gerrit_value,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200135 salt_value)
Michael Kutý099c5342016-09-09 14:44:13 +0200136 if type == 'str':
Ales Komarekb0fcc252016-09-14 19:29:37 +0200137 _set_string(gerrit, fullpath, salt_value, field_name=field)
Michael Kutý099c5342016-09-09 14:44:13 +0200138 elif type == 'bool':
Ales Komarekb0fcc252016-09-14 19:29:37 +0200139 _set_boolean(gerrit, fullpath, salt_value)
Michael Kutý099c5342016-09-09 14:44:13 +0200140 else:
141 raise AssertionError("Unknown Ansible parameter type '%s'" % type)
142
Ales Komarekb0fcc252016-09-14 19:29:37 +0200143 value = salt_value
Michael Kutý099c5342016-09-09 14:44:13 +0200144 changed = True
145 return value, changed
146
Ales Komarek07d16552016-09-12 21:39:18 +0200147
Ales Komarekb0fcc252016-09-14 19:29:37 +0200148def _quote(name):
Ales Komarek07d16552016-09-12 21:39:18 +0200149 return urllib.quote(name, safe="")
150
151
Ales Komarekb0fcc252016-09-14 19:29:37 +0200152def _account_name2id(gerrit, name=None):
153 # Although we could pass an AccountInput entry here to set details in one
154 # go, it's left up to the _update_group() function, to avoid having a
155 # totally separate code path for create vs. update.
156 info = gerrit.get('/accounts/%s' % _quote(name))
157 return info['_account_id']
158
159
160def _group_name2id(gerrit, name=None):
161 # Although we could pass an AccountInput entry here to set details in one
162 # go, it's left up to the _update_group() function, to avoid having a
163 # totally separate code path for create vs. update.
164 info = gerrit.get('/groups/%s' % _quote(name))
165 return info['id']
166
167
168def _create_group(gerrit, name=None):
169 # Although we could pass an AccountInput entry here to set details in one
170 # go, it's left up to the _update_group() function, to avoid having a
171 # totally separate code path for create vs. update.
172 group_info = gerrit.put('/groups/%s' % _quote(name))
173 return group_info
174
175
176def _create_account(gerrit, username=None):
177 # Although we could pass an AccountInput entry here to set details in one
178 # go, it's left up to the _update_account() function, to avoid having a
179 # totally separate code path for create vs. update.
180 account_info = gerrit.put('/accounts/%s' % _quote(username))
181 return account_info
182
183
184def _create_account_email(gerrit, account_id, email, preferred=False,
185 no_confirmation=False):
186 logging.info('Creating email %s for account %s', email, account_id)
187
188 email_input = {
189 # Setting 'email' is optional (it's already in the URL) but it's good
190 # to double check that the email is encoded in the URL properly.
191 'email': email,
192 'preferred': preferred,
193 'no_confirmation': no_confirmation,
194 }
195 logging.debug(email_input)
196
197 path = 'accounts/%s/emails/%s' % (account_id, _quote(email))
198 headers = {'content-type': 'application/json'}
199 gerrit.post(path, data=json.dumps(email_input), headers=headers)
200
201
202def _create_account_ssh_key(gerrit, account_id, ssh_public_key):
203 logging.info('Creating SSH key %s for account %s', ssh_public_key,
204 account_id)
205
206 import requests
Ales Komarekb0fcc252016-09-14 19:29:37 +0200207
208 path = 'accounts/%s/sshkeys' % (account_id)
Ales Komarekb0fcc252016-09-14 19:29:37 +0200209
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
Ales Komarekb0fcc252016-09-14 19:29:37 +0200217
218def _create_group_membership(gerrit, account_id, group_id):
219 logging.info('Creating membership of %s in group %s', account_id, group_id)
220# group_id = _group_name2id(gerrit, group_id)
221 print group_id
222 import json
223 path = 'groups/%s/members/%s' % (_quote(group_id), account_id)
224 gerrit.put(path, data=json.dumps({}))
225
226
227def _ensure_only_member_of_these_groups(gerrit, account_id, salt_groups):
228 path = 'accounts/%s' % account_id
229 group_info_list = _get_list(gerrit, path + '/groups')
230
231 changed = False
232 gerrit_groups = []
233 for group_info in group_info_list:
234 if group_info['name'] in salt_groups:
235 logging.info("Preserving %s membership of group %s", path,
236 group_info)
237 gerrit_groups.append(group_info['name'])
238 else:
239 logging.info("Removing %s from group %s", path, group_info)
240 membership_path = 'groups/%s/members/%s' % (
241 _quote(group_info['id']), account_id)
242 try:
243 gerrit.delete(membership_path)
244 changed = True
Filip Pytloun9c1b92f2017-02-28 19:26:49 +0100245 except Exception as e:
Filip Pytloun693ebbe2017-03-21 12:55:09 +0100246 try:
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 except Exception:
258 raise e
Ales Komarekb0fcc252016-09-14 19:29:37 +0200259
260 # If the user gave group IDs instead of group names, this will
261 # needlessly recreate the membership. The only actual issue will be that
262 # Ansible reports 'changed' when nothing really did change, I think.
263 #
264 # We might receive [""] when the user tries to pass in an empty list, so
265 # handle that.
266 for new_group in set(salt_groups).difference(gerrit_groups):
267 if len(new_group) > 0:
268 _create_group_membership(gerrit, account_id, new_group)
269 gerrit_groups.append(new_group)
270 changed = True
271
272 return gerrit_groups, changed
273
274
275def _ensure_only_one_account_email(gerrit, account_id, email):
276 path = 'accounts/%s' % account_id
277 email_info_list = _get_list(gerrit, path + '/emails')
278
279 changed = False
280 found_email = False
281 for email_info in email_info_list:
282 existing_email = email_info['email']
283 if existing_email == email:
284 # Since we're deleting all emails except this one, there's no need
285 # to care whether it's the 'preferred' one. It soon will be!
286 logging.info("Keeping %s email %s", path, email)
287 found_email = True
288 else:
289 logging.info("Removing %s email %s", path, existing_email)
290 gerrit.delete(path + '/emails/%s' % _quote(existing_email))
291 changed = True
292
293 if len(email) > 0 and not found_email:
294 _create_account_email(gerrit, account_id, email,
295 preferred=True, no_confirmation=True)
296 changed = True
297
298 return email, changed
299
300
301def _ensure_only_one_account_ssh_key(gerrit, account_id, ssh_public_key):
302 path = 'accounts/%s' % account_id
303 ssh_key_info_list = _get_list(gerrit, path + '/sshkeys')
304
305 changed = False
306 found_ssh_key = False
307 for ssh_key_info in ssh_key_info_list:
308 if ssh_key_info['ssh_public_key'] == ssh_public_key:
309 logging.info("Keeping %s SSH key %s", path, ssh_key_info)
310 found_ssh_key = True
311 else:
312 logging.info("Removing %s SSH key %s", path, ssh_key_info)
313 gerrit.delete(path + '/sshkeys/%i' % ssh_key_info['seq'])
314 changed = True
315
316 if len(ssh_public_key) > 0 and not found_ssh_key:
317 _create_account_ssh_key(gerrit, account_id, ssh_public_key)
318 changed = True
319
320 return ssh_public_key, changed
321
322
323def _update_account(gerrit, username=None, **params):
324 change = False
325
326 try:
327 account_info = gerrit.get('/accounts/%s' % _quote(username))
Filip Pytloun9c1b92f2017-02-28 19:26:49 +0100328 except Exception as e:
Filip Pytloun693ebbe2017-03-21 12:55:09 +0100329 try:
330 if e.response.status_code == 404:
331 logging.info("Account %s not found, creating it.", username)
332 account_info = _create_account(gerrit, username)
333 change = True
334 else:
335 raise
336 except Exception:
337 raise e
Ales Komarekb0fcc252016-09-14 19:29:37 +0200338
339 logging.debug(
340 'Existing account info for account %s: %s', username,
341 json.dumps(account_info, indent=4))
342
343 account_id = account_info['_account_id']
344 path = 'accounts/%s' % account_id
345
346 output = {}
347 output['username'] = username
348 output['id'] = account_id
349
350 fullname, fullname_changed = _maybe_update_field(
351 gerrit, path, 'name', account_info.get('name'), params.get('fullname'))
352 output['fullname'] = fullname
353 change |= fullname_changed
354
355 # Set the value of params that the user did not provide to None.
356
357 if params.get('active') is not None:
358 active = _get_boolean(gerrit, path + '/active')
359 active, active_changed = _maybe_update_field(
360 gerrit, path, 'active', active, params['active'], type='bool')
361 output['active'] = active
362 change |= active_changed
363
364 if params.get('email') is not None:
365 email, emails_changed = _ensure_only_one_account_email(
366 gerrit, account_id, params['email'])
367 output['email'] = email
368 change |= emails_changed
369
370 if params.get('groups') is not None:
371 groups, groups_changed = _ensure_only_member_of_these_groups(
372 gerrit, account_info.get('name'), params['groups'])
373 output['groups'] = groups
374 change |= groups_changed
375
376 if params.get('http_password') is not None:
377 http_password = _get_string(gerrit, path + '/password.http')
378 http_password, http_password_changed = _maybe_update_field(
379 gerrit, path, 'http_password', http_password,
380 params.get('http_password'),
381 gerrit_api_path='password.http')
382 output['http_password'] = http_password
383 change |= http_password_changed
384
385 if params.get('ssh_key') is not None:
386 ssh_key, ssh_keys_changed = _ensure_only_one_account_ssh_key(
387 gerrit, account_id, params['ssh_key'])
388 output['ssh_key'] = ssh_key
389 change |= ssh_keys_changed
390
391 return output, change
392
393
394def _update_group(gerrit, name=None, **params):
395 change = False
396
397 try:
398 group_info = gerrit.get('/groups/%s' % _quote(name))
Filip Pytloun9c1b92f2017-02-28 19:26:49 +0100399 except Exception as e:
Filip Pytloun693ebbe2017-03-21 12:55:09 +0100400 try:
401 if e.response.status_code == 404:
402 logging.info("Group %s not found, creating it.", name)
403 group_info = _create_group(gerrit, name)
404 change = True
405 else:
406 raise
407 except Exception:
408 raise e
Ales Komarekb0fcc252016-09-14 19:29:37 +0200409
410 logging.debug(
411 'Existing info for group %s: %s', name,
412 json.dumps(group_info, indent=4))
413
414 output = {group_info['name']: group_info}
415
416 return output, change
417
418
419# Gerrit client connectors
Michael Kutý099c5342016-09-09 14:44:13 +0200420
421
Ales Komarek07d16552016-09-12 21:39:18 +0200422def _gerrit_ssh_connection(**connection_args):
423 '''
424 Set up gerrit credentials
425
426 Only intended to be used within gerrit-enabled modules
427 '''
428
429 prefix = "gerrit"
430
431 # look in connection_args first, then default to config file
432 def get(key, default=None):
433 return connection_args.get('connection_' + key,
434 __salt__['config.get'](prefix, {})).get(key, default)
435
436 host = get('host', 'localhost')
437 user = get('user', 'admin')
438 keyfile = get('keyfile', '/var/cache/salt/minion/gerrit_rsa')
439
440 gerrit_client = gerrit.Gerrit(host, user, keyfile=keyfile)
441
442 return gerrit_client
443
444
445def _gerrit_http_connection(**connection_args):
Michael Kutý099c5342016-09-09 14:44:13 +0200446
447 prefix = "gerrit"
448
449 # look in connection_args first, then default to config file
450 def get(key, default=None):
451 return connection_args.get(
452 'connection_' + key,
453 __salt__['config.get'](prefix, {})).get(key, default)
454
455 host = get('host', 'localhost')
Ales Komarek07d16552016-09-12 21:39:18 +0200456 http_port = get('http_port', '8082')
457 protocol = get('protocol', 'http')
Michael Kutý099c5342016-09-09 14:44:13 +0200458 username = get('user', 'admin')
459 password = get('password', 'admin')
Filip Pytlounb5306162017-03-28 09:51:06 +0200460 auth_method = get('auth_method', 'digest')
Michael Kutý099c5342016-09-09 14:44:13 +0200461
Ales Komarek07d16552016-09-12 21:39:18 +0200462 url = protocol+"://"+str(host)+':'+str(http_port)
463
Filip Pytlounb5306162017-03-28 09:51:06 +0200464 if auth_method == 'digest':
465 auth = requests.auth.HTTPDigestAuth(username, password)
466 elif auth_method == 'basic':
467 auth = requests.auth.HTTPBasicAuth(username, password)
468 else:
469 raise Exception("Unknown auth_method %s" % auth_method)
Michael Kutý099c5342016-09-09 14:44:13 +0200470
471 gerrit = pygerrit.rest.GerritRestAPI(
Ales Komarek07d16552016-09-12 21:39:18 +0200472 url=url, auth=auth)
Michael Kutý099c5342016-09-09 14:44:13 +0200473
474 return gerrit
475
476
Ales Komarekb0fcc252016-09-14 19:29:37 +0200477# Salt modules
Ales Komarek07d16552016-09-12 21:39:18 +0200478
479
480def 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 +0200481 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200482 Create a gerrit account
483
484 :param username: username
485 :param fullname: fullname
486 :param email: email
487 :param active: active
488 :param groups: array of strings
489 groups:
490 - Non-Interactive Users
491 - Testers
492 :param ssh_key: public ssh key
493 :param http_password: http password
494
495 CLI Examples:
496
497 .. code-block:: bash
498
499 salt '*' gerrit.account_create username "full name" "mail@domain.com"
500
501 '''
502 gerrit_client = _gerrit_http_connection(**kwargs)
503 output, changed = _update_account(
504 gerrit_client, **{
505 'username': username,
506 'fullname': fullname,
507 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200508# 'active': active,
Ales Komarek07d16552016-09-12 21:39:18 +0200509 'groups': groups,
510 'ssh_key': ssh_key,
511 'http_password': http_password
512 })
513 return output
514
515
516def account_update(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs):
517 '''
518 Create a gerrit account
Michael Kutý099c5342016-09-09 14:44:13 +0200519
520 :param username: username
521 :param fullname: fullname
522 :param email: email
Ales Komarekb0fcc252016-09-14 19:29:37 +0200523 :param active: active
Michael Kutý099c5342016-09-09 14:44:13 +0200524 :param groups: array of strings
525 groups:
526 - Non-Interactive Users
527 - Testers
Ales Komarek07d16552016-09-12 21:39:18 +0200528 :param ssh_key: public ssh key
529 :param http_password: http password
530
Michael Kutý099c5342016-09-09 14:44:13 +0200531 CLI Examples:
532
533 .. code-block:: bash
534
Ales Komarek07d16552016-09-12 21:39:18 +0200535 salt '*' gerrit.account_create username "full name" "mail@domain.com"
Michael Kutý099c5342016-09-09 14:44:13 +0200536
537 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200538 gerrit_client = _gerrit_http_connection(**kwargs)
539 output, changed = _update_account(
Michael Kutý099c5342016-09-09 14:44:13 +0200540 gerrit_client, **{
541 'username': username,
542 'fullname': fullname,
543 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200544# 'active': active,
Michael Kutý099c5342016-09-09 14:44:13 +0200545 'groups': groups,
Ales Komarek07d16552016-09-12 21:39:18 +0200546 'ssh_key': ssh_key,
547 'http_password': http_password
Michael Kutý099c5342016-09-09 14:44:13 +0200548 })
Michael Kutý099c5342016-09-09 14:44:13 +0200549 return output
550
Ales Komarek07d16552016-09-12 21:39:18 +0200551def account_list(**kwargs):
552 '''
553 List gerrit accounts
554
555 CLI Examples:
556
557 .. code-block:: bash
558
559 salt '*' gerrit.account_list
560
561 '''
562 gerrit_client = _gerrit_http_connection(**kwargs)
563 ret_list = gerrit_client.get('/accounts/?q=*&n=10000')
564 ret = {}
565 for item in ret_list:
566 ret[item['username']] = item
567 return ret
568
569
Ales Komarek2fc39002016-09-14 11:43:56 +0200570def account_get(name, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200571 '''
572 Get gerrit account
573
574 CLI Examples:
575
576 .. code-block:: bash
577
Ales Komarek2fc39002016-09-14 11:43:56 +0200578 salt '*' gerrit.account_get name
Ales Komarek07d16552016-09-12 21:39:18 +0200579
580 '''
581 gerrit_client = _gerrit_http_connection(**kwargs)
Ales Komarek2fc39002016-09-14 11:43:56 +0200582 accounts = account_list(**kwargs)
583 if(name in accounts):
584 ret = accounts.pop(name)
585 else:
586 ret = {'Error': 'Error in retrieving account'}
Ales Komarek07d16552016-09-12 21:39:18 +0200587 return ret
588
589
590def group_list(**kwargs):
591 '''
592 List gerrit groups
593
594 CLI Examples:
595
596 .. code-block:: bash
597
598 salt '*' gerrit.group_list
599
600 '''
601 gerrit_client = _gerrit_http_connection(**kwargs)
602 return gerrit_client.get('/groups/')
603
604
605def group_get(groupname, **kwargs):
606 '''
607 Get gerrit group
608
609 CLI Examples:
610
611 .. code-block:: bash
612
613 salt '*' gerrit.group_get groupname
614
615 '''
616 gerrit_client = _gerrit_http_connection(**kwargs)
Filip Pytloun9c1b92f2017-02-28 19:26:49 +0100617 try:
Ales Komarek07d16552016-09-12 21:39:18 +0200618 item = gerrit_client.get('/groups/%s' % groupname)
619 ret = {item['name']: item}
620 except:
621 ret = {'Error': 'Error in retrieving account'}
622 return ret
623
624
Ales Komarek2fc39002016-09-14 11:43:56 +0200625def group_create(name, description=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200626 '''
627 Create a gerrit group
628
629 :param name: name
630
631 CLI Examples:
632
633 .. code-block:: bash
634
Ales Komarekb0fcc252016-09-14 19:29:37 +0200635 salt '*' gerrit.group_create group-name description
Ales Komarek07d16552016-09-12 21:39:18 +0200636
637 '''
638 gerrit_client = _gerrit_http_connection(**kwargs)
639 ret, changed = _update_group(
Ales Komarek2fc39002016-09-14 11:43:56 +0200640 gerrit_client, **{'name': name, 'description': description})
Ales Komarek07d16552016-09-12 21:39:18 +0200641 return ret
642
Michael Kutý099c5342016-09-09 14:44:13 +0200643
Ales Komarek49a37292016-08-31 16:18:31 +0200644def project_create(name, **kwargs):
645 '''
646 Create a gerrit project
647
648 :param name: new project name
649
650 CLI Examples:
651
652 .. code-block:: bash
653
654 salt '*' gerrit.project_create namespace/nova description='nova project'
Michael Kutý099c5342016-09-09 14:44:13 +0200655
Ales Komarek49a37292016-08-31 16:18:31 +0200656 '''
657 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200658 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200659
660 project = project_get(name, **kwargs)
661
662 if project and not "Error" in project:
663 LOG.debug("Project {0} exists".format(name))
664 return project
665
666 new = gerrit_client.createProject(name)
667 return project_get(name, **kwargs)
668
Michael Kutý099c5342016-09-09 14:44:13 +0200669
Ales Komarek49a37292016-08-31 16:18:31 +0200670def project_get(name, **kwargs):
671 '''
672 Return a specific project
673
674 CLI Examples:
675
676 .. code-block:: bash
677
678 salt '*' gerrit.project_get projectname
679 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200680 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200681 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200682 projects = gerrit_client.listProjects()
683 if not name in projects:
684 return {'Error': 'Error in retrieving project'}
685 ret[name] = {'name': name}
686 return ret
687
688
689def project_list(**connection_args):
690 '''
691 Return a list of available projects
692
693 CLI Example:
694
695 .. code-block:: bash
696
697 salt '*' gerrit.project_list
698 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200699 gerrit_client = _gerrit_ssh_connection(**connection_args)
Ales Komarek49a37292016-08-31 16:18:31 +0200700 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200701 projects = gerrit_client.listProjects()
Ales Komarek49a37292016-08-31 16:18:31 +0200702 for project in projects:
703 ret[project] = {
704 'name': project
705 }
706 return ret
707
708
709def query(change, **kwargs):
710 '''
711 Query gerrit
712
713 :param change: Query content
714
715 CLI Examples:
716
717 .. code-block:: bash
718
719 salt '*' gerrit.query 'status:open project:tools/gerrit limit:2'
Michael Kutý099c5342016-09-09 14:44:13 +0200720
Ales Komarek49a37292016-08-31 16:18:31 +0200721 '''
722 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200723 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200724 msg = gerrit_client.query(change)
725 ret['query'] = msg
726 return ret