blob: bd21b2e6d252a1461b80d79349e8f631ed866925 [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')
460
Ales Komarek07d16552016-09-12 21:39:18 +0200461 url = protocol+"://"+str(host)+':'+str(http_port)
462
Michael Kutý099c5342016-09-09 14:44:13 +0200463 auth = requests.auth.HTTPDigestAuth(
464 username, password)
465
466 gerrit = pygerrit.rest.GerritRestAPI(
Ales Komarek07d16552016-09-12 21:39:18 +0200467 url=url, auth=auth)
Michael Kutý099c5342016-09-09 14:44:13 +0200468
469 return gerrit
470
471
Ales Komarekb0fcc252016-09-14 19:29:37 +0200472# Salt modules
Ales Komarek07d16552016-09-12 21:39:18 +0200473
474
475def 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 +0200476 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200477 Create a gerrit account
478
479 :param username: username
480 :param fullname: fullname
481 :param email: email
482 :param active: active
483 :param groups: array of strings
484 groups:
485 - Non-Interactive Users
486 - Testers
487 :param ssh_key: public ssh key
488 :param http_password: http password
489
490 CLI Examples:
491
492 .. code-block:: bash
493
494 salt '*' gerrit.account_create username "full name" "mail@domain.com"
495
496 '''
497 gerrit_client = _gerrit_http_connection(**kwargs)
498 output, changed = _update_account(
499 gerrit_client, **{
500 'username': username,
501 'fullname': fullname,
502 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200503# 'active': active,
Ales Komarek07d16552016-09-12 21:39:18 +0200504 '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
Ales Komarekb0fcc252016-09-14 19:29:37 +0200518 :param active: active
Michael Kutý099c5342016-09-09 14:44:13 +0200519 :param groups: array of strings
520 groups:
521 - Non-Interactive Users
522 - Testers
Ales Komarek07d16552016-09-12 21:39:18 +0200523 :param ssh_key: public ssh key
524 :param http_password: http password
525
Michael Kutý099c5342016-09-09 14:44:13 +0200526 CLI Examples:
527
528 .. code-block:: bash
529
Ales Komarek07d16552016-09-12 21:39:18 +0200530 salt '*' gerrit.account_create username "full name" "mail@domain.com"
Michael Kutý099c5342016-09-09 14:44:13 +0200531
532 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200533 gerrit_client = _gerrit_http_connection(**kwargs)
534 output, changed = _update_account(
Michael Kutý099c5342016-09-09 14:44:13 +0200535 gerrit_client, **{
536 'username': username,
537 'fullname': fullname,
538 'email': email,
Ales Komarekb0fcc252016-09-14 19:29:37 +0200539# 'active': active,
Michael Kutý099c5342016-09-09 14:44:13 +0200540 'groups': groups,
Ales Komarek07d16552016-09-12 21:39:18 +0200541 'ssh_key': ssh_key,
542 'http_password': http_password
Michael Kutý099c5342016-09-09 14:44:13 +0200543 })
Michael Kutý099c5342016-09-09 14:44:13 +0200544 return output
545
Ales Komarek07d16552016-09-12 21:39:18 +0200546def account_list(**kwargs):
547 '''
548 List gerrit accounts
549
550 CLI Examples:
551
552 .. code-block:: bash
553
554 salt '*' gerrit.account_list
555
556 '''
557 gerrit_client = _gerrit_http_connection(**kwargs)
558 ret_list = gerrit_client.get('/accounts/?q=*&n=10000')
559 ret = {}
560 for item in ret_list:
561 ret[item['username']] = item
562 return ret
563
564
Ales Komarek2fc39002016-09-14 11:43:56 +0200565def account_get(name, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200566 '''
567 Get gerrit account
568
569 CLI Examples:
570
571 .. code-block:: bash
572
Ales Komarek2fc39002016-09-14 11:43:56 +0200573 salt '*' gerrit.account_get name
Ales Komarek07d16552016-09-12 21:39:18 +0200574
575 '''
576 gerrit_client = _gerrit_http_connection(**kwargs)
Ales Komarek2fc39002016-09-14 11:43:56 +0200577 accounts = account_list(**kwargs)
578 if(name in accounts):
579 ret = accounts.pop(name)
580 else:
581 ret = {'Error': 'Error in retrieving account'}
Ales Komarek07d16552016-09-12 21:39:18 +0200582 return ret
583
584
585def group_list(**kwargs):
586 '''
587 List gerrit groups
588
589 CLI Examples:
590
591 .. code-block:: bash
592
593 salt '*' gerrit.group_list
594
595 '''
596 gerrit_client = _gerrit_http_connection(**kwargs)
597 return gerrit_client.get('/groups/')
598
599
600def group_get(groupname, **kwargs):
601 '''
602 Get gerrit group
603
604 CLI Examples:
605
606 .. code-block:: bash
607
608 salt '*' gerrit.group_get groupname
609
610 '''
611 gerrit_client = _gerrit_http_connection(**kwargs)
Filip Pytloun9c1b92f2017-02-28 19:26:49 +0100612 try:
Ales Komarek07d16552016-09-12 21:39:18 +0200613 item = gerrit_client.get('/groups/%s' % groupname)
614 ret = {item['name']: item}
615 except:
616 ret = {'Error': 'Error in retrieving account'}
617 return ret
618
619
Ales Komarek2fc39002016-09-14 11:43:56 +0200620def group_create(name, description=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +0200621 '''
622 Create a gerrit group
623
624 :param name: name
625
626 CLI Examples:
627
628 .. code-block:: bash
629
Ales Komarekb0fcc252016-09-14 19:29:37 +0200630 salt '*' gerrit.group_create group-name description
Ales Komarek07d16552016-09-12 21:39:18 +0200631
632 '''
633 gerrit_client = _gerrit_http_connection(**kwargs)
634 ret, changed = _update_group(
Ales Komarek2fc39002016-09-14 11:43:56 +0200635 gerrit_client, **{'name': name, 'description': description})
Ales Komarek07d16552016-09-12 21:39:18 +0200636 return ret
637
Michael Kutý099c5342016-09-09 14:44:13 +0200638
Ales Komarek49a37292016-08-31 16:18:31 +0200639def project_create(name, **kwargs):
640 '''
641 Create a gerrit project
642
643 :param name: new project name
644
645 CLI Examples:
646
647 .. code-block:: bash
648
649 salt '*' gerrit.project_create namespace/nova description='nova project'
Michael Kutý099c5342016-09-09 14:44:13 +0200650
Ales Komarek49a37292016-08-31 16:18:31 +0200651 '''
652 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200653 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200654
655 project = project_get(name, **kwargs)
656
657 if project and not "Error" in project:
658 LOG.debug("Project {0} exists".format(name))
659 return project
660
661 new = gerrit_client.createProject(name)
662 return project_get(name, **kwargs)
663
Michael Kutý099c5342016-09-09 14:44:13 +0200664
Ales Komarek49a37292016-08-31 16:18:31 +0200665def project_get(name, **kwargs):
666 '''
667 Return a specific project
668
669 CLI Examples:
670
671 .. code-block:: bash
672
673 salt '*' gerrit.project_get projectname
674 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200675 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200676 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200677 projects = gerrit_client.listProjects()
678 if not name in projects:
679 return {'Error': 'Error in retrieving project'}
680 ret[name] = {'name': name}
681 return ret
682
683
684def project_list(**connection_args):
685 '''
686 Return a list of available projects
687
688 CLI Example:
689
690 .. code-block:: bash
691
692 salt '*' gerrit.project_list
693 '''
Ales Komarek07d16552016-09-12 21:39:18 +0200694 gerrit_client = _gerrit_ssh_connection(**connection_args)
Ales Komarek49a37292016-08-31 16:18:31 +0200695 ret = {}
Ales Komarek49a37292016-08-31 16:18:31 +0200696 projects = gerrit_client.listProjects()
Ales Komarek49a37292016-08-31 16:18:31 +0200697 for project in projects:
698 ret[project] = {
699 'name': project
700 }
701 return ret
702
703
704def query(change, **kwargs):
705 '''
706 Query gerrit
707
708 :param change: Query content
709
710 CLI Examples:
711
712 .. code-block:: bash
713
714 salt '*' gerrit.query 'status:open project:tools/gerrit limit:2'
Michael Kutý099c5342016-09-09 14:44:13 +0200715
Ales Komarek49a37292016-08-31 16:18:31 +0200716 '''
717 ret = {}
Ales Komarek07d16552016-09-12 21:39:18 +0200718 gerrit_client = _gerrit_ssh_connection(**kwargs)
Ales Komarek49a37292016-08-31 16:18:31 +0200719 msg = gerrit_client.query(change)
720 ret['query'] = msg
721 return ret