blob: 8a23a0145169993a528f7164b597437551ef81b8 [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
Filip Pytlounda910752017-08-03 12:13:57 +0200215 requests.post(gerrit.make_url(path), **kwargs)
Ales Komarekb0fcc252016-09-14 19:29:37 +0200216
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)
Ales Komarekb0fcc252016-09-14 19:29:37 +0200221 path = 'groups/%s/members/%s' % (_quote(group_id), account_id)
222 gerrit.put(path, data=json.dumps({}))
223
224
225def _ensure_only_member_of_these_groups(gerrit, account_id, salt_groups):
226 path = 'accounts/%s' % account_id
227 group_info_list = _get_list(gerrit, path + '/groups')
228
229 changed = False
230 gerrit_groups = []
231 for group_info in group_info_list:
232 if group_info['name'] in salt_groups:
233 logging.info("Preserving %s membership of group %s", path,
234 group_info)
235 gerrit_groups.append(group_info['name'])
236 else:
237 logging.info("Removing %s from group %s", path, group_info)
238 membership_path = 'groups/%s/members/%s' % (
239 _quote(group_info['id']), account_id)
240 try:
241 gerrit.delete(membership_path)
242 changed = True
Filip Pytloun9c1b92f2017-02-28 19:26:49 +0100243 except Exception as e:
Filip Pytloun693ebbe2017-03-21 12:55:09 +0100244 try:
245 if e.response.status_code == 404:
246 # This is a kludge, it'd be better to work out in advance
247 # which groups the user is a member of only via membership
248 # in a different. That's not trivial though with the
249 # current API Gerrit provides.
250 logging.info(
251 "Ignored %s; assuming membership of this group is due "
252 "to membership of a group that includes it.", e)
253 else:
254 raise
255 except Exception:
256 raise e
Ales Komarekb0fcc252016-09-14 19:29:37 +0200257
258 # If the user gave group IDs instead of group names, this will
259 # needlessly recreate the membership. The only actual issue will be that
260 # Ansible reports 'changed' when nothing really did change, I think.
261 #
262 # We might receive [""] when the user tries to pass in an empty list, so
263 # handle that.
264 for new_group in set(salt_groups).difference(gerrit_groups):
265 if len(new_group) > 0:
266 _create_group_membership(gerrit, account_id, new_group)
267 gerrit_groups.append(new_group)
268 changed = True
269
270 return gerrit_groups, changed
271
272
273def _ensure_only_one_account_email(gerrit, account_id, email):
274 path = 'accounts/%s' % account_id
275 email_info_list = _get_list(gerrit, path + '/emails')
276
277 changed = False
278 found_email = False
279 for email_info in email_info_list:
280 existing_email = email_info['email']
281 if existing_email == email:
282 # Since we're deleting all emails except this one, there's no need
283 # to care whether it's the 'preferred' one. It soon will be!
284 logging.info("Keeping %s email %s", path, email)
285 found_email = True
286 else:
287 logging.info("Removing %s email %s", path, existing_email)
288 gerrit.delete(path + '/emails/%s' % _quote(existing_email))
289 changed = True
290
291 if len(email) > 0 and not found_email:
292 _create_account_email(gerrit, account_id, email,
293 preferred=True, no_confirmation=True)
294 changed = True
295
296 return email, changed
297
298
299def _ensure_only_one_account_ssh_key(gerrit, account_id, ssh_public_key):
300 path = 'accounts/%s' % account_id
301 ssh_key_info_list = _get_list(gerrit, path + '/sshkeys')
302
303 changed = False
304 found_ssh_key = False
305 for ssh_key_info in ssh_key_info_list:
306 if ssh_key_info['ssh_public_key'] == ssh_public_key:
307 logging.info("Keeping %s SSH key %s", path, ssh_key_info)
308 found_ssh_key = True
309 else:
310 logging.info("Removing %s SSH key %s", path, ssh_key_info)
311 gerrit.delete(path + '/sshkeys/%i' % ssh_key_info['seq'])
312 changed = True
313
314 if len(ssh_public_key) > 0 and not found_ssh_key:
315 _create_account_ssh_key(gerrit, account_id, ssh_public_key)
316 changed = True
317
318 return ssh_public_key, changed
319
320
321def _update_account(gerrit, username=None, **params):
322 change = False
323
324 try:
325 account_info = gerrit.get('/accounts/%s' % _quote(username))
Filip Pytloun9c1b92f2017-02-28 19:26:49 +0100326 except Exception as e:
Filip Pytloun693ebbe2017-03-21 12:55:09 +0100327 try:
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 except Exception:
335 raise e
Ales Komarekb0fcc252016-09-14 19:29:37 +0200336
337 logging.debug(
338 'Existing account info for account %s: %s', username,
339 json.dumps(account_info, indent=4))
340
341 account_id = account_info['_account_id']
342 path = 'accounts/%s' % account_id
343
344 output = {}
345 output['username'] = username
346 output['id'] = account_id
347
348 fullname, fullname_changed = _maybe_update_field(
349 gerrit, path, 'name', account_info.get('name'), params.get('fullname'))
350 output['fullname'] = fullname
351 change |= fullname_changed
352
353 # Set the value of params that the user did not provide to None.
354
355 if params.get('active') is not None:
356 active = _get_boolean(gerrit, path + '/active')
357 active, active_changed = _maybe_update_field(
358 gerrit, path, 'active', active, params['active'], type='bool')
359 output['active'] = active
360 change |= active_changed
361
362 if params.get('email') is not None:
363 email, emails_changed = _ensure_only_one_account_email(
364 gerrit, account_id, params['email'])
365 output['email'] = email
366 change |= emails_changed
367
Filip Pytloun1a521b32017-03-28 12:06:50 +0200368 if params.get('groups'):
Ales Komarekb0fcc252016-09-14 19:29:37 +0200369 groups, groups_changed = _ensure_only_member_of_these_groups(
370 gerrit, account_info.get('name'), params['groups'])
371 output['groups'] = groups
372 change |= groups_changed
373
374 if params.get('http_password') is not None:
375 http_password = _get_string(gerrit, path + '/password.http')
376 http_password, http_password_changed = _maybe_update_field(
377 gerrit, path, 'http_password', http_password,
378 params.get('http_password'),
379 gerrit_api_path='password.http')
380 output['http_password'] = http_password
381 change |= http_password_changed
382
383 if params.get('ssh_key') is not None:
384 ssh_key, ssh_keys_changed = _ensure_only_one_account_ssh_key(
Filip Pytlounda910752017-08-03 12:13:57 +0200385 gerrit, account_id, params['ssh_key'])
Ales Komarekb0fcc252016-09-14 19:29:37 +0200386 output['ssh_key'] = ssh_key
387 change |= ssh_keys_changed
388
389 return output, change
390
391
392def _update_group(gerrit, name=None, **params):
393 change = False
394
395 try:
396 group_info = gerrit.get('/groups/%s' % _quote(name))
Filip Pytloun9c1b92f2017-02-28 19:26:49 +0100397 except Exception as e:
Filip Pytloun693ebbe2017-03-21 12:55:09 +0100398 try:
399 if e.response.status_code == 404:
400 logging.info("Group %s not found, creating it.", name)
401 group_info = _create_group(gerrit, name)
402 change = True
403 else:
404 raise
405 except Exception:
406 raise e
Ales Komarekb0fcc252016-09-14 19:29:37 +0200407
408 logging.debug(
409 'Existing info for group %s: %s', name,
410 json.dumps(group_info, indent=4))
411
412 output = {group_info['name']: group_info}
413
414 return output, change
415
416
417# Gerrit client connectors
Michael Kutý099c5342016-09-09 14:44:13 +0200418
419
Ales Komarek07d16552016-09-12 21:39:18 +0200420def _gerrit_ssh_connection(**connection_args):
421 '''
422 Set up gerrit credentials
423
424 Only intended to be used within gerrit-enabled modules
425 '''
426
427 prefix = "gerrit"
428
429 # look in connection_args first, then default to config file
430 def get(key, default=None):
431 return connection_args.get('connection_' + key,
432 __salt__['config.get'](prefix, {})).get(key, default)
433
434 host = get('host', 'localhost')
435 user = get('user', 'admin')
436 keyfile = get('keyfile', '/var/cache/salt/minion/gerrit_rsa')
437
438 gerrit_client = gerrit.Gerrit(host, user, keyfile=keyfile)
439
440 return gerrit_client
441
442
443def _gerrit_http_connection(**connection_args):
Michael Kutý099c5342016-09-09 14:44:13 +0200444
445 prefix = "gerrit"
446
447 # look in connection_args first, then default to config file
448 def get(key, default=None):
449 return connection_args.get(
450 'connection_' + key,
451 __salt__['config.get'](prefix, {})).get(key, default)
452
453 host = get('host', 'localhost')
Ales Komarek07d16552016-09-12 21:39:18 +0200454 http_port = get('http_port', '8082')
455 protocol = get('protocol', 'http')
Michael Kutý099c5342016-09-09 14:44:13 +0200456 username = get('user', 'admin')
457 password = get('password', 'admin')
Filip Pytlounb5306162017-03-28 09:51:06 +0200458 auth_method = get('auth_method', 'digest')
Michael Kutý099c5342016-09-09 14:44:13 +0200459
Filip Pytlounda910752017-08-03 12:13:57 +0200460 url = protocol + "://" + str(host) + ':' + str(http_port)
Ales Komarek07d16552016-09-12 21:39:18 +0200461
Filip Pytlounb5306162017-03-28 09:51:06 +0200462 if auth_method == 'digest':
463 auth = requests.auth.HTTPDigestAuth(username, password)
464 elif auth_method == 'basic':
465 auth = requests.auth.HTTPBasicAuth(username, password)
466 else:
467 raise Exception("Unknown auth_method %s" % auth_method)
Michael Kutý099c5342016-09-09 14:44:13 +0200468
469 gerrit = pygerrit.rest.GerritRestAPI(
Ales Komarek07d16552016-09-12 21:39:18 +0200470 url=url, auth=auth)
Michael Kutý099c5342016-09-09 14:44:13 +0200471
472 return gerrit
473
474
Ales Komarekb0fcc252016-09-14 19:29:37 +0200475# Salt modules
Ales Komarek07d16552016-09-12 21:39:18 +0200476
477
478def 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 +0200479 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200480 Create a gerrit account
481
482 :param username: username
483 :param fullname: fullname
484 :param email: email
485 :param active: active
486 :param groups: array of strings
487 groups:
488 - Non-Interactive Users
489 - Testers
490 :param ssh_key: public ssh key
491 :param http_password: http password
492
493 CLI Examples:
494
495 .. code-block:: bash
496
497 salt '*' gerrit.account_create username "full name" "mail@domain.com"
498
499 '''
500 gerrit_client = _gerrit_http_connection(**kwargs)
501 output, changed = _update_account(
502 gerrit_client, **{
503 'username': username,
504 'fullname': fullname,
505 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200506# 'active': active,
Ales Komarek07d16552016-09-12 21:39:18 +0200507 'groups': groups,
508 'ssh_key': ssh_key,
509 'http_password': http_password
510 })
511 return output
512
513
514def account_update(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs):
515 '''
516 Create a gerrit account
Michael Kutý099c5342016-09-09 14:44:13 +0200517
518 :param username: username
519 :param fullname: fullname
520 :param email: email
Ales Komarekb0fcc252016-09-14 19:29:37 +0200521 :param active: active
Michael Kutý099c5342016-09-09 14:44:13 +0200522 :param groups: array of strings
523 groups:
524 - Non-Interactive Users
525 - Testers
Ales Komarek07d16552016-09-12 21:39:18 +0200526 :param ssh_key: public ssh key
527 :param http_password: http password
528
Michael Kutý099c5342016-09-09 14:44:13 +0200529 CLI Examples:
530
531 .. code-block:: bash
532
Ales Komarek07d16552016-09-12 21:39:18 +0200533 salt '*' gerrit.account_create username "full name" "mail@domain.com"
Michael Kutý099c5342016-09-09 14:44:13 +0200534
535 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200536 gerrit_client = _gerrit_http_connection(**kwargs)
537 output, changed = _update_account(
Michael Kutý099c5342016-09-09 14:44:13 +0200538 gerrit_client, **{
539 'username': username,
540 'fullname': fullname,
541 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200542# 'active': active,
Michael Kutý099c5342016-09-09 14:44:13 +0200543 'groups': groups,
Ales Komarek07d16552016-09-12 21:39:18 +0200544 'ssh_key': ssh_key,
545 'http_password': http_password
Michael Kutý099c5342016-09-09 14:44:13 +0200546 })
Michael Kutý099c5342016-09-09 14:44:13 +0200547 return output
548
Filip Pytlounda910752017-08-03 12:13:57 +0200549
Ales Komarek07d16552016-09-12 21:39:18 +0200550def account_list(**kwargs):
551 '''
552 List gerrit accounts
553
554 CLI Examples:
555
556 .. code-block:: bash
557
558 salt '*' gerrit.account_list
559
560 '''
561 gerrit_client = _gerrit_http_connection(**kwargs)
562 ret_list = gerrit_client.get('/accounts/?q=*&n=10000')
563 ret = {}
564 for item in ret_list:
565 ret[item['username']] = item
566 return ret
567
568
Ales Komarek2fc39002016-09-14 11:43:56 +0200569def account_get(name, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200570 '''
571 Get gerrit account
572
573 CLI Examples:
574
575 .. code-block:: bash
576
Ales Komarek2fc39002016-09-14 11:43:56 +0200577 salt '*' gerrit.account_get name
Ales Komarek07d16552016-09-12 21:39:18 +0200578
579 '''
580 gerrit_client = _gerrit_http_connection(**kwargs)
Filip Pytloun1a521b32017-03-28 12:06:50 +0200581 try:
582 ret = gerrit_client.get('/accounts/%s' % name)
583 except Exception:
Ales Komarek2fc39002016-09-14 11:43:56 +0200584 ret = {'Error': 'Error in retrieving account'}
Ales Komarek07d16552016-09-12 21:39:18 +0200585 return ret
586
587
588def group_list(**kwargs):
589 '''
590 List gerrit groups
591
592 CLI Examples:
593
594 .. code-block:: bash
595
596 salt '*' gerrit.group_list
597
598 '''
599 gerrit_client = _gerrit_http_connection(**kwargs)
600 return gerrit_client.get('/groups/')
601
602
603def group_get(groupname, **kwargs):
604 '''
605 Get gerrit group
606
607 CLI Examples:
608
609 .. code-block:: bash
610
611 salt '*' gerrit.group_get groupname
612
613 '''
614 gerrit_client = _gerrit_http_connection(**kwargs)
Filip Pytloun9c1b92f2017-02-28 19:26:49 +0100615 try:
Ales Komarek07d16552016-09-12 21:39:18 +0200616 item = gerrit_client.get('/groups/%s' % groupname)
617 ret = {item['name']: item}
618 except:
619 ret = {'Error': 'Error in retrieving account'}
620 return ret
621
622
Ales Komarek2fc39002016-09-14 11:43:56 +0200623def group_create(name, description=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200624 '''
625 Create a gerrit group
626
627 :param name: name
628
629 CLI Examples:
630
631 .. code-block:: bash
632
Ales Komarekb0fcc252016-09-14 19:29:37 +0200633 salt '*' gerrit.group_create group-name description
Ales Komarek07d16552016-09-12 21:39:18 +0200634
635 '''
636 gerrit_client = _gerrit_http_connection(**kwargs)
637 ret, changed = _update_group(
Ales Komarek2fc39002016-09-14 11:43:56 +0200638 gerrit_client, **{'name': name, 'description': description})
Ales Komarek07d16552016-09-12 21:39:18 +0200639 return ret
640
Michael Kutý099c5342016-09-09 14:44:13 +0200641
Ales Komarek49a37292016-08-31 16:18:31 +0200642def project_create(name, **kwargs):
643 '''
644 Create a gerrit project
645
646 :param name: new project name
647
648 CLI Examples:
649
650 .. code-block:: bash
651
652 salt '*' gerrit.project_create namespace/nova description='nova project'
Michael Kutý099c5342016-09-09 14:44:13 +0200653
Ales Komarek49a37292016-08-31 16:18:31 +0200654 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200655 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200656
657 project = project_get(name, **kwargs)
658
Filip Pytlounda910752017-08-03 12:13:57 +0200659 if project and "Error" not in project:
Ales Komarek49a37292016-08-31 16:18:31 +0200660 LOG.debug("Project {0} exists".format(name))
661 return project
662
Filip Pytlounda910752017-08-03 12:13:57 +0200663 gerrit_client.createProject(name)
Ales Komarek49a37292016-08-31 16:18:31 +0200664 return project_get(name, **kwargs)
665
Michael Kutý099c5342016-09-09 14:44:13 +0200666
Ales Komarek49a37292016-08-31 16:18:31 +0200667def project_get(name, **kwargs):
668 '''
669 Return a specific project
670
671 CLI Examples:
672
673 .. code-block:: bash
674
675 salt '*' gerrit.project_get projectname
676 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200677 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200678 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200679 projects = gerrit_client.listProjects()
Filip Pytlounda910752017-08-03 12:13:57 +0200680 if name not in projects:
Ales Komarek49a37292016-08-31 16:18:31 +0200681 return {'Error': 'Error in retrieving project'}
682 ret[name] = {'name': name}
683 return ret
684
685
686def project_list(**connection_args):
687 '''
688 Return a list of available projects
689
690 CLI Example:
691
692 .. code-block:: bash
693
694 salt '*' gerrit.project_list
695 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200696 gerrit_client = _gerrit_ssh_connection(**connection_args)
Ales Komarek49a37292016-08-31 16:18:31 +0200697 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200698 projects = gerrit_client.listProjects()
Ales Komarek49a37292016-08-31 16:18:31 +0200699 for project in projects:
700 ret[project] = {
701 'name': project
702 }
703 return ret
704
705
706def query(change, **kwargs):
707 '''
708 Query gerrit
709
710 :param change: Query content
711
712 CLI Examples:
713
714 .. code-block:: bash
715
716 salt '*' gerrit.query 'status:open project:tools/gerrit limit:2'
Michael Kutý099c5342016-09-09 14:44:13 +0200717
Ales Komarek49a37292016-08-31 16:18:31 +0200718 '''
719 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200720 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200721 msg = gerrit_client.query(change)
722 ret['query'] = msg
723 return ret