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: |
Filip Pytloun | 512baf4 | 2017-03-09 22:14:00 +0100 | [diff] [blame] | 74 | raise Exception( |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 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) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 87 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 88 | 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ý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 97 | return value |
| 98 | |
| 99 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 100 | def _set_boolean(gerrit, path, value): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 101 | if value: |
| 102 | gerrit.put(path) |
| 103 | else: |
| 104 | gerrit.delete(path) |
| 105 | |
| 106 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 107 | def _set_string(gerrit, path, value, field_name=None): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 108 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 117 | def _maybe_update_field(gerrit, path, field, gerrit_value, salt_value, |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 118 | type='str', gerrit_api_path=None): |
| 119 | |
| 120 | gerrit_api_path = gerrit_api_path or field |
| 121 | fullpath = path + '/' + gerrit_api_path |
| 122 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 123 | if gerrit_value == salt_value: |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 124 | logging.info("Not updating %s: same value specified: %s", fullpath, |
| 125 | gerrit_value) |
| 126 | value = gerrit_value |
| 127 | changed = False |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 128 | elif salt_value is None: |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 129 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 135 | salt_value) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 136 | if type == 'str': |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 137 | _set_string(gerrit, fullpath, salt_value, field_name=field) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 138 | elif type == 'bool': |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 139 | _set_boolean(gerrit, fullpath, salt_value) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 140 | else: |
| 141 | raise AssertionError("Unknown Ansible parameter type '%s'" % type) |
| 142 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 143 | value = salt_value |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 144 | changed = True |
| 145 | return value, changed |
| 146 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 147 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 148 | def _quote(name): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 149 | return urllib.quote(name, safe="") |
| 150 | |
| 151 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 152 | def _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 | |
| 160 | def _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 | |
| 168 | def _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 | |
| 176 | def _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 | |
| 184 | def _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 | |
| 202 | def _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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 207 | |
| 208 | path = 'accounts/%s/sshkeys' % (account_id) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 209 | |
| 210 | kwargs = { |
| 211 | "data": ssh_public_key |
| 212 | } |
| 213 | kwargs.update(gerrit.kwargs.copy()) |
| 214 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame^] | 215 | requests.post(gerrit.make_url(path), **kwargs) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 216 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 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) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 221 | path = 'groups/%s/members/%s' % (_quote(group_id), account_id) |
| 222 | gerrit.put(path, data=json.dumps({})) |
| 223 | |
| 224 | |
| 225 | def _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 Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 243 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 244 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 257 | |
| 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 | |
| 273 | def _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 | |
| 299 | def _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 | |
| 321 | def _update_account(gerrit, username=None, **params): |
| 322 | change = False |
| 323 | |
| 324 | try: |
| 325 | account_info = gerrit.get('/accounts/%s' % _quote(username)) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 326 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 327 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 336 | |
| 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 Pytloun | 1a521b3 | 2017-03-28 12:06:50 +0200 | [diff] [blame] | 368 | if params.get('groups'): |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 369 | 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 Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame^] | 385 | gerrit, account_id, params['ssh_key']) |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 386 | output['ssh_key'] = ssh_key |
| 387 | change |= ssh_keys_changed |
| 388 | |
| 389 | return output, change |
| 390 | |
| 391 | |
| 392 | def _update_group(gerrit, name=None, **params): |
| 393 | change = False |
| 394 | |
| 395 | try: |
| 396 | group_info = gerrit.get('/groups/%s' % _quote(name)) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 397 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 398 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 407 | |
| 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ý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 418 | |
| 419 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 420 | def _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 | |
| 443 | def _gerrit_http_connection(**connection_args): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 444 | |
| 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 Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 454 | http_port = get('http_port', '8082') |
| 455 | protocol = get('protocol', 'http') |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 456 | username = get('user', 'admin') |
| 457 | password = get('password', 'admin') |
Filip Pytloun | b530616 | 2017-03-28 09:51:06 +0200 | [diff] [blame] | 458 | auth_method = get('auth_method', 'digest') |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 459 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame^] | 460 | url = protocol + "://" + str(host) + ':' + str(http_port) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 461 | |
Filip Pytloun | b530616 | 2017-03-28 09:51:06 +0200 | [diff] [blame] | 462 | 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ý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 468 | |
| 469 | gerrit = pygerrit.rest.GerritRestAPI( |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 470 | url=url, auth=auth) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 471 | |
| 472 | return gerrit |
| 473 | |
| 474 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 475 | # Salt modules |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 476 | |
| 477 | |
| 478 | 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] | 479 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 480 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 506 | # 'active': active, |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 507 | 'groups': groups, |
| 508 | 'ssh_key': ssh_key, |
| 509 | 'http_password': http_password |
| 510 | }) |
| 511 | return output |
| 512 | |
| 513 | |
| 514 | def 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ý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 517 | |
| 518 | :param username: username |
| 519 | :param fullname: fullname |
| 520 | :param email: email |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 521 | :param active: active |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 522 | :param groups: array of strings |
| 523 | groups: |
| 524 | - Non-Interactive Users |
| 525 | - Testers |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 526 | :param ssh_key: public ssh key |
| 527 | :param http_password: http password |
| 528 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 529 | CLI Examples: |
| 530 | |
| 531 | .. code-block:: bash |
| 532 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 533 | salt '*' gerrit.account_create username "full name" "mail@domain.com" |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 534 | |
| 535 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 536 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 537 | output, changed = _update_account( |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 538 | gerrit_client, **{ |
| 539 | 'username': username, |
| 540 | 'fullname': fullname, |
| 541 | 'email': email, |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 542 | # 'active': active, |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 543 | 'groups': groups, |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 544 | 'ssh_key': ssh_key, |
| 545 | 'http_password': http_password |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 546 | }) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 547 | return output |
| 548 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame^] | 549 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 550 | def 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 Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 569 | def account_get(name, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 570 | ''' |
| 571 | Get gerrit account |
| 572 | |
| 573 | CLI Examples: |
| 574 | |
| 575 | .. code-block:: bash |
| 576 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 577 | salt '*' gerrit.account_get name |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 578 | |
| 579 | ''' |
| 580 | gerrit_client = _gerrit_http_connection(**kwargs) |
Filip Pytloun | 1a521b3 | 2017-03-28 12:06:50 +0200 | [diff] [blame] | 581 | try: |
| 582 | ret = gerrit_client.get('/accounts/%s' % name) |
| 583 | except Exception: |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 584 | ret = {'Error': 'Error in retrieving account'} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 585 | return ret |
| 586 | |
| 587 | |
| 588 | def 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 | |
| 603 | def 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 Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 615 | try: |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 616 | 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 Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 623 | def group_create(name, description=None, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 624 | ''' |
| 625 | Create a gerrit group |
| 626 | |
| 627 | :param name: name |
| 628 | |
| 629 | CLI Examples: |
| 630 | |
| 631 | .. code-block:: bash |
| 632 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 633 | salt '*' gerrit.group_create group-name description |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 634 | |
| 635 | ''' |
| 636 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 637 | ret, changed = _update_group( |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 638 | gerrit_client, **{'name': name, 'description': description}) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 639 | return ret |
| 640 | |
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 | def 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ý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 653 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 654 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 655 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 656 | |
| 657 | project = project_get(name, **kwargs) |
| 658 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame^] | 659 | if project and "Error" not in project: |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 660 | LOG.debug("Project {0} exists".format(name)) |
| 661 | return project |
| 662 | |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame^] | 663 | gerrit_client.createProject(name) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 664 | return project_get(name, **kwargs) |
| 665 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 666 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 667 | def 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 Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 677 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 678 | ret = {} |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 679 | projects = gerrit_client.listProjects() |
Filip Pytloun | da91075 | 2017-08-03 12:13:57 +0200 | [diff] [blame^] | 680 | if name not in projects: |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 681 | return {'Error': 'Error in retrieving project'} |
| 682 | ret[name] = {'name': name} |
| 683 | return ret |
| 684 | |
| 685 | |
| 686 | def 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 Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 696 | gerrit_client = _gerrit_ssh_connection(**connection_args) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 697 | ret = {} |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 698 | projects = gerrit_client.listProjects() |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 699 | for project in projects: |
| 700 | ret[project] = { |
| 701 | 'name': project |
| 702 | } |
| 703 | return ret |
| 704 | |
| 705 | |
| 706 | def 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ý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 717 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 718 | ''' |
| 719 | ret = {} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 720 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 721 | msg = gerrit_client.query(change) |
| 722 | ret['query'] = msg |
| 723 | return ret |