Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Module for handling gerrit calls. |
| 4 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 5 | :optdepends: - gerritlib/pygerrit Python adapter |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 6 | :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ý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 19 | Examples: |
| 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 Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 30 | ''' |
| 31 | |
| 32 | from __future__ import absolute_import |
| 33 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 34 | import json |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 35 | import logging |
| 36 | import os |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 37 | import urllib |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 38 | import requests.auth |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 39 | |
| 40 | LOG = logging.getLogger(__name__) |
| 41 | |
| 42 | # Import third party libs |
| 43 | HAS_GERRIT = False |
| 44 | try: |
| 45 | from gerritlib import gerrit |
Ales Komarek | 92d0d34 | 2016-09-14 19:32:17 +0200 | [diff] [blame] | 46 | import pygerrit.rest |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 47 | HAS_GERRIT = True |
| 48 | except ImportError: |
| 49 | pass |
| 50 | |
| 51 | |
| 52 | def __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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 64 | # Common functions |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 65 | |
| 66 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 67 | def _get_boolean(gerrit, path): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 68 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 79 | def _get_list(gerrit, path): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 80 | values = gerrit.get(path) |
| 81 | return values |
| 82 | |
| 83 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 84 | def _get_string(gerrit, path): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 85 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 97 | def _set_boolean(gerrit, path, value): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 98 | if value: |
| 99 | gerrit.put(path) |
| 100 | else: |
| 101 | gerrit.delete(path) |
| 102 | |
| 103 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 104 | def _set_string(gerrit, path, value, field_name=None): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 105 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 114 | def _maybe_update_field(gerrit, path, field, gerrit_value, salt_value, |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 115 | type='str', gerrit_api_path=None): |
| 116 | |
| 117 | gerrit_api_path = gerrit_api_path or field |
| 118 | fullpath = path + '/' + gerrit_api_path |
| 119 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 120 | if gerrit_value == salt_value: |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 121 | logging.info("Not updating %s: same value specified: %s", fullpath, |
| 122 | gerrit_value) |
| 123 | value = gerrit_value |
| 124 | changed = False |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 125 | elif salt_value is None: |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 126 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 132 | salt_value) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 133 | if type == 'str': |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 134 | _set_string(gerrit, fullpath, salt_value, field_name=field) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 135 | elif type == 'bool': |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 136 | _set_boolean(gerrit, fullpath, salt_value) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 137 | else: |
| 138 | raise AssertionError("Unknown Ansible parameter type '%s'" % type) |
| 139 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 140 | value = salt_value |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 141 | changed = True |
| 142 | return value, changed |
| 143 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 144 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 145 | def _quote(name): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 146 | return urllib.quote(name, safe="") |
| 147 | |
| 148 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 149 | def _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 | |
| 157 | def _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 | |
| 165 | def _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 | |
| 173 | def _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 | |
| 181 | def _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 | |
| 199 | def _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 Komarek | e59d32e | 2016-09-27 11:19:41 +0200 | [diff] [blame] | 204 | from pygerrit.rest import _decode_response |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 205 | |
| 206 | path = 'accounts/%s/sshkeys' % (account_id) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 207 | |
| 208 | kwargs = { |
| 209 | "data": ssh_public_key |
| 210 | } |
| 211 | kwargs.update(gerrit.kwargs.copy()) |
| 212 | |
| 213 | response = requests.put(gerrit.make_url(path), **kwargs) |
| 214 | |
Ales Komarek | e59d32e | 2016-09-27 11:19:41 +0200 | [diff] [blame] | 215 | return _decode_response(response) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 216 | |
| 217 | |
| 218 | def _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 | |
| 227 | def _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 |
| 245 | except requests.exceptions.HTTPError as e: |
| 246 | if e.response.status_code == 404: |
| 247 | # This is a kludge, it'd be better to work out in advance |
| 248 | # which groups the user is a member of only via membership |
| 249 | # in a different. That's not trivial though with the |
| 250 | # current API Gerrit provides. |
| 251 | logging.info( |
| 252 | "Ignored %s; assuming membership of this group is due " |
| 253 | "to membership of a group that includes it.", e) |
| 254 | else: |
| 255 | raise |
| 256 | |
| 257 | # If the user gave group IDs instead of group names, this will |
| 258 | # needlessly recreate the membership. The only actual issue will be that |
| 259 | # Ansible reports 'changed' when nothing really did change, I think. |
| 260 | # |
| 261 | # We might receive [""] when the user tries to pass in an empty list, so |
| 262 | # handle that. |
| 263 | for new_group in set(salt_groups).difference(gerrit_groups): |
| 264 | if len(new_group) > 0: |
| 265 | _create_group_membership(gerrit, account_id, new_group) |
| 266 | gerrit_groups.append(new_group) |
| 267 | changed = True |
| 268 | |
| 269 | return gerrit_groups, changed |
| 270 | |
| 271 | |
| 272 | def _ensure_only_one_account_email(gerrit, account_id, email): |
| 273 | path = 'accounts/%s' % account_id |
| 274 | email_info_list = _get_list(gerrit, path + '/emails') |
| 275 | |
| 276 | changed = False |
| 277 | found_email = False |
| 278 | for email_info in email_info_list: |
| 279 | existing_email = email_info['email'] |
| 280 | if existing_email == email: |
| 281 | # Since we're deleting all emails except this one, there's no need |
| 282 | # to care whether it's the 'preferred' one. It soon will be! |
| 283 | logging.info("Keeping %s email %s", path, email) |
| 284 | found_email = True |
| 285 | else: |
| 286 | logging.info("Removing %s email %s", path, existing_email) |
| 287 | gerrit.delete(path + '/emails/%s' % _quote(existing_email)) |
| 288 | changed = True |
| 289 | |
| 290 | if len(email) > 0 and not found_email: |
| 291 | _create_account_email(gerrit, account_id, email, |
| 292 | preferred=True, no_confirmation=True) |
| 293 | changed = True |
| 294 | |
| 295 | return email, changed |
| 296 | |
| 297 | |
| 298 | def _ensure_only_one_account_ssh_key(gerrit, account_id, ssh_public_key): |
| 299 | path = 'accounts/%s' % account_id |
| 300 | ssh_key_info_list = _get_list(gerrit, path + '/sshkeys') |
| 301 | |
| 302 | changed = False |
| 303 | found_ssh_key = False |
| 304 | for ssh_key_info in ssh_key_info_list: |
| 305 | if ssh_key_info['ssh_public_key'] == ssh_public_key: |
| 306 | logging.info("Keeping %s SSH key %s", path, ssh_key_info) |
| 307 | found_ssh_key = True |
| 308 | else: |
| 309 | logging.info("Removing %s SSH key %s", path, ssh_key_info) |
| 310 | gerrit.delete(path + '/sshkeys/%i' % ssh_key_info['seq']) |
| 311 | changed = True |
| 312 | |
| 313 | if len(ssh_public_key) > 0 and not found_ssh_key: |
| 314 | _create_account_ssh_key(gerrit, account_id, ssh_public_key) |
| 315 | changed = True |
| 316 | |
| 317 | return ssh_public_key, changed |
| 318 | |
| 319 | |
| 320 | def _update_account(gerrit, username=None, **params): |
| 321 | change = False |
| 322 | |
| 323 | try: |
| 324 | account_info = gerrit.get('/accounts/%s' % _quote(username)) |
| 325 | except requests.exceptions.HTTPError as e: |
| 326 | if e.response.status_code == 404: |
| 327 | logging.info("Account %s not found, creating it.", username) |
| 328 | account_info = _create_account(gerrit, username) |
| 329 | change = True |
| 330 | else: |
| 331 | raise |
| 332 | |
| 333 | logging.debug( |
| 334 | 'Existing account info for account %s: %s', username, |
| 335 | json.dumps(account_info, indent=4)) |
| 336 | |
| 337 | account_id = account_info['_account_id'] |
| 338 | path = 'accounts/%s' % account_id |
| 339 | |
| 340 | output = {} |
| 341 | output['username'] = username |
| 342 | output['id'] = account_id |
| 343 | |
| 344 | fullname, fullname_changed = _maybe_update_field( |
| 345 | gerrit, path, 'name', account_info.get('name'), params.get('fullname')) |
| 346 | output['fullname'] = fullname |
| 347 | change |= fullname_changed |
| 348 | |
| 349 | # Set the value of params that the user did not provide to None. |
| 350 | |
| 351 | if params.get('active') is not None: |
| 352 | active = _get_boolean(gerrit, path + '/active') |
| 353 | active, active_changed = _maybe_update_field( |
| 354 | gerrit, path, 'active', active, params['active'], type='bool') |
| 355 | output['active'] = active |
| 356 | change |= active_changed |
| 357 | |
| 358 | if params.get('email') is not None: |
| 359 | email, emails_changed = _ensure_only_one_account_email( |
| 360 | gerrit, account_id, params['email']) |
| 361 | output['email'] = email |
| 362 | change |= emails_changed |
| 363 | |
| 364 | if params.get('groups') is not None: |
| 365 | groups, groups_changed = _ensure_only_member_of_these_groups( |
| 366 | gerrit, account_info.get('name'), params['groups']) |
| 367 | output['groups'] = groups |
| 368 | change |= groups_changed |
| 369 | |
| 370 | if params.get('http_password') is not None: |
| 371 | http_password = _get_string(gerrit, path + '/password.http') |
| 372 | http_password, http_password_changed = _maybe_update_field( |
| 373 | gerrit, path, 'http_password', http_password, |
| 374 | params.get('http_password'), |
| 375 | gerrit_api_path='password.http') |
| 376 | output['http_password'] = http_password |
| 377 | change |= http_password_changed |
| 378 | |
| 379 | if params.get('ssh_key') is not None: |
| 380 | ssh_key, ssh_keys_changed = _ensure_only_one_account_ssh_key( |
| 381 | gerrit, account_id, params['ssh_key']) |
| 382 | output['ssh_key'] = ssh_key |
| 383 | change |= ssh_keys_changed |
| 384 | |
| 385 | return output, change |
| 386 | |
| 387 | |
| 388 | def _update_group(gerrit, name=None, **params): |
| 389 | change = False |
| 390 | |
| 391 | try: |
| 392 | group_info = gerrit.get('/groups/%s' % _quote(name)) |
| 393 | except requests.exceptions.HTTPError as e: |
| 394 | if e.response.status_code == 404: |
| 395 | logging.info("Group %s not found, creating it.", name) |
| 396 | group_info = _create_group(gerrit, name) |
| 397 | change = True |
| 398 | else: |
| 399 | raise |
| 400 | |
| 401 | logging.debug( |
| 402 | 'Existing info for group %s: %s', name, |
| 403 | json.dumps(group_info, indent=4)) |
| 404 | |
| 405 | output = {group_info['name']: group_info} |
| 406 | |
| 407 | return output, change |
| 408 | |
| 409 | |
| 410 | # Gerrit client connectors |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 411 | |
| 412 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 413 | def _gerrit_ssh_connection(**connection_args): |
| 414 | ''' |
| 415 | Set up gerrit credentials |
| 416 | |
| 417 | Only intended to be used within gerrit-enabled modules |
| 418 | ''' |
| 419 | |
| 420 | prefix = "gerrit" |
| 421 | |
| 422 | # look in connection_args first, then default to config file |
| 423 | def get(key, default=None): |
| 424 | return connection_args.get('connection_' + key, |
| 425 | __salt__['config.get'](prefix, {})).get(key, default) |
| 426 | |
| 427 | host = get('host', 'localhost') |
| 428 | user = get('user', 'admin') |
| 429 | keyfile = get('keyfile', '/var/cache/salt/minion/gerrit_rsa') |
| 430 | |
| 431 | gerrit_client = gerrit.Gerrit(host, user, keyfile=keyfile) |
| 432 | |
| 433 | return gerrit_client |
| 434 | |
| 435 | |
| 436 | def _gerrit_http_connection(**connection_args): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 437 | |
| 438 | prefix = "gerrit" |
| 439 | |
| 440 | # look in connection_args first, then default to config file |
| 441 | def get(key, default=None): |
| 442 | return connection_args.get( |
| 443 | 'connection_' + key, |
| 444 | __salt__['config.get'](prefix, {})).get(key, default) |
| 445 | |
| 446 | host = get('host', 'localhost') |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 447 | http_port = get('http_port', '8082') |
| 448 | protocol = get('protocol', 'http') |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 449 | username = get('user', 'admin') |
| 450 | password = get('password', 'admin') |
| 451 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 452 | url = protocol+"://"+str(host)+':'+str(http_port) |
| 453 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 454 | auth = requests.auth.HTTPDigestAuth( |
| 455 | username, password) |
| 456 | |
| 457 | gerrit = pygerrit.rest.GerritRestAPI( |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 458 | url=url, auth=auth) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 459 | |
| 460 | return gerrit |
| 461 | |
| 462 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 463 | # Salt modules |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 464 | |
| 465 | |
| 466 | def account_create(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 467 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 468 | Create a gerrit account |
| 469 | |
| 470 | :param username: username |
| 471 | :param fullname: fullname |
| 472 | :param email: email |
| 473 | :param active: active |
| 474 | :param groups: array of strings |
| 475 | groups: |
| 476 | - Non-Interactive Users |
| 477 | - Testers |
| 478 | :param ssh_key: public ssh key |
| 479 | :param http_password: http password |
| 480 | |
| 481 | CLI Examples: |
| 482 | |
| 483 | .. code-block:: bash |
| 484 | |
| 485 | salt '*' gerrit.account_create username "full name" "mail@domain.com" |
| 486 | |
| 487 | ''' |
| 488 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 489 | output, changed = _update_account( |
| 490 | gerrit_client, **{ |
| 491 | 'username': username, |
| 492 | 'fullname': fullname, |
| 493 | 'email': email, |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 494 | # 'active': active, |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 495 | 'groups': groups, |
| 496 | 'ssh_key': ssh_key, |
| 497 | 'http_password': http_password |
| 498 | }) |
| 499 | return output |
| 500 | |
| 501 | |
| 502 | def account_update(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs): |
| 503 | ''' |
| 504 | Create a gerrit account |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 505 | |
| 506 | :param username: username |
| 507 | :param fullname: fullname |
| 508 | :param email: email |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 509 | :param active: active |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 510 | :param groups: array of strings |
| 511 | groups: |
| 512 | - Non-Interactive Users |
| 513 | - Testers |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 514 | :param ssh_key: public ssh key |
| 515 | :param http_password: http password |
| 516 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 517 | CLI Examples: |
| 518 | |
| 519 | .. code-block:: bash |
| 520 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 521 | salt '*' gerrit.account_create username "full name" "mail@domain.com" |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 522 | |
| 523 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 524 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 525 | output, changed = _update_account( |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 526 | gerrit_client, **{ |
| 527 | 'username': username, |
| 528 | 'fullname': fullname, |
| 529 | 'email': email, |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 530 | # 'active': active, |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 531 | 'groups': groups, |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 532 | 'ssh_key': ssh_key, |
| 533 | 'http_password': http_password |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 534 | }) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 535 | return output |
| 536 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 537 | def account_list(**kwargs): |
| 538 | ''' |
| 539 | List gerrit accounts |
| 540 | |
| 541 | CLI Examples: |
| 542 | |
| 543 | .. code-block:: bash |
| 544 | |
| 545 | salt '*' gerrit.account_list |
| 546 | |
| 547 | ''' |
| 548 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 549 | ret_list = gerrit_client.get('/accounts/?q=*&n=10000') |
| 550 | ret = {} |
| 551 | for item in ret_list: |
| 552 | ret[item['username']] = item |
| 553 | return ret |
| 554 | |
| 555 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 556 | def account_get(name, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 557 | ''' |
| 558 | Get gerrit account |
| 559 | |
| 560 | CLI Examples: |
| 561 | |
| 562 | .. code-block:: bash |
| 563 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 564 | salt '*' gerrit.account_get name |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 565 | |
| 566 | ''' |
| 567 | gerrit_client = _gerrit_http_connection(**kwargs) |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 568 | accounts = account_list(**kwargs) |
| 569 | if(name in accounts): |
| 570 | ret = accounts.pop(name) |
| 571 | else: |
| 572 | ret = {'Error': 'Error in retrieving account'} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 573 | return ret |
| 574 | |
| 575 | |
| 576 | def group_list(**kwargs): |
| 577 | ''' |
| 578 | List gerrit groups |
| 579 | |
| 580 | CLI Examples: |
| 581 | |
| 582 | .. code-block:: bash |
| 583 | |
| 584 | salt '*' gerrit.group_list |
| 585 | |
| 586 | ''' |
| 587 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 588 | return gerrit_client.get('/groups/') |
| 589 | |
| 590 | |
| 591 | def group_get(groupname, **kwargs): |
| 592 | ''' |
| 593 | Get gerrit group |
| 594 | |
| 595 | CLI Examples: |
| 596 | |
| 597 | .. code-block:: bash |
| 598 | |
| 599 | salt '*' gerrit.group_get groupname |
| 600 | |
| 601 | ''' |
| 602 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 603 | try: |
| 604 | item = gerrit_client.get('/groups/%s' % groupname) |
| 605 | ret = {item['name']: item} |
| 606 | except: |
| 607 | ret = {'Error': 'Error in retrieving account'} |
| 608 | return ret |
| 609 | |
| 610 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 611 | def group_create(name, description=None, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 612 | ''' |
| 613 | Create a gerrit group |
| 614 | |
| 615 | :param name: name |
| 616 | |
| 617 | CLI Examples: |
| 618 | |
| 619 | .. code-block:: bash |
| 620 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 621 | salt '*' gerrit.group_create group-name description |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 622 | |
| 623 | ''' |
| 624 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 625 | ret, changed = _update_group( |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 626 | gerrit_client, **{'name': name, 'description': description}) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 627 | return ret |
| 628 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 629 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 630 | def project_create(name, **kwargs): |
| 631 | ''' |
| 632 | Create a gerrit project |
| 633 | |
| 634 | :param name: new project name |
| 635 | |
| 636 | CLI Examples: |
| 637 | |
| 638 | .. code-block:: bash |
| 639 | |
| 640 | salt '*' gerrit.project_create namespace/nova description='nova project' |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 641 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 642 | ''' |
| 643 | ret = {} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 644 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 645 | |
| 646 | project = project_get(name, **kwargs) |
| 647 | |
| 648 | if project and not "Error" in project: |
| 649 | LOG.debug("Project {0} exists".format(name)) |
| 650 | return project |
| 651 | |
| 652 | new = gerrit_client.createProject(name) |
| 653 | return project_get(name, **kwargs) |
| 654 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 655 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 656 | def project_get(name, **kwargs): |
| 657 | ''' |
| 658 | Return a specific project |
| 659 | |
| 660 | CLI Examples: |
| 661 | |
| 662 | .. code-block:: bash |
| 663 | |
| 664 | salt '*' gerrit.project_get projectname |
| 665 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 666 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 667 | ret = {} |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 668 | projects = gerrit_client.listProjects() |
| 669 | if not name in projects: |
| 670 | return {'Error': 'Error in retrieving project'} |
| 671 | ret[name] = {'name': name} |
| 672 | return ret |
| 673 | |
| 674 | |
| 675 | def project_list(**connection_args): |
| 676 | ''' |
| 677 | Return a list of available projects |
| 678 | |
| 679 | CLI Example: |
| 680 | |
| 681 | .. code-block:: bash |
| 682 | |
| 683 | salt '*' gerrit.project_list |
| 684 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 685 | gerrit_client = _gerrit_ssh_connection(**connection_args) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 686 | ret = {} |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 687 | projects = gerrit_client.listProjects() |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 688 | for project in projects: |
| 689 | ret[project] = { |
| 690 | 'name': project |
| 691 | } |
| 692 | return ret |
| 693 | |
| 694 | |
| 695 | def query(change, **kwargs): |
| 696 | ''' |
| 697 | Query gerrit |
| 698 | |
| 699 | :param change: Query content |
| 700 | |
| 701 | CLI Examples: |
| 702 | |
| 703 | .. code-block:: bash |
| 704 | |
| 705 | salt '*' gerrit.query 'status:open project:tools/gerrit limit:2' |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 706 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 707 | ''' |
| 708 | ret = {} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 709 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 710 | msg = gerrit_client.query(change) |
| 711 | ret['query'] = msg |
| 712 | return ret |