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 | 1a521b3 | 2017-03-28 12:06:50 +0200 | [diff] [blame^] | 215 | response = 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) |
| 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 |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 245 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 246 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 259 | |
| 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 | |
| 275 | def _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 | |
| 301 | def _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 | |
| 323 | def _update_account(gerrit, username=None, **params): |
| 324 | change = False |
| 325 | |
| 326 | try: |
| 327 | account_info = gerrit.get('/accounts/%s' % _quote(username)) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 328 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 329 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 338 | |
| 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 | |
Filip Pytloun | 1a521b3 | 2017-03-28 12:06:50 +0200 | [diff] [blame^] | 370 | if params.get('groups'): |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 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 | |
| 394 | def _update_group(gerrit, name=None, **params): |
| 395 | change = False |
| 396 | |
| 397 | try: |
| 398 | group_info = gerrit.get('/groups/%s' % _quote(name)) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 399 | except Exception as e: |
Filip Pytloun | 693ebbe | 2017-03-21 12:55:09 +0100 | [diff] [blame] | 400 | 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 Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 409 | |
| 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ý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 420 | |
| 421 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 422 | def _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 | |
| 445 | def _gerrit_http_connection(**connection_args): |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 446 | |
| 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 Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 456 | http_port = get('http_port', '8082') |
| 457 | protocol = get('protocol', 'http') |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 458 | username = get('user', 'admin') |
| 459 | password = get('password', 'admin') |
Filip Pytloun | b530616 | 2017-03-28 09:51:06 +0200 | [diff] [blame] | 460 | auth_method = get('auth_method', 'digest') |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 461 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 462 | url = protocol+"://"+str(host)+':'+str(http_port) |
| 463 | |
Filip Pytloun | b530616 | 2017-03-28 09:51:06 +0200 | [diff] [blame] | 464 | if auth_method == 'digest': |
| 465 | auth = requests.auth.HTTPDigestAuth(username, password) |
| 466 | elif auth_method == 'basic': |
| 467 | auth = requests.auth.HTTPBasicAuth(username, password) |
| 468 | else: |
| 469 | raise Exception("Unknown auth_method %s" % auth_method) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 470 | |
| 471 | gerrit = pygerrit.rest.GerritRestAPI( |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 472 | url=url, auth=auth) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 473 | |
| 474 | return gerrit |
| 475 | |
| 476 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 477 | # Salt modules |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 478 | |
| 479 | |
| 480 | 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] | 481 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 482 | Create a gerrit account |
| 483 | |
| 484 | :param username: username |
| 485 | :param fullname: fullname |
| 486 | :param email: email |
| 487 | :param active: active |
| 488 | :param groups: array of strings |
| 489 | groups: |
| 490 | - Non-Interactive Users |
| 491 | - Testers |
| 492 | :param ssh_key: public ssh key |
| 493 | :param http_password: http password |
| 494 | |
| 495 | CLI Examples: |
| 496 | |
| 497 | .. code-block:: bash |
| 498 | |
| 499 | salt '*' gerrit.account_create username "full name" "mail@domain.com" |
| 500 | |
| 501 | ''' |
| 502 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 503 | output, changed = _update_account( |
| 504 | gerrit_client, **{ |
| 505 | 'username': username, |
| 506 | 'fullname': fullname, |
| 507 | 'email': email, |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 508 | # 'active': active, |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 509 | 'groups': groups, |
| 510 | 'ssh_key': ssh_key, |
| 511 | 'http_password': http_password |
| 512 | }) |
| 513 | return output |
| 514 | |
| 515 | |
| 516 | def account_update(username, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs): |
| 517 | ''' |
| 518 | Create a gerrit account |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 519 | |
| 520 | :param username: username |
| 521 | :param fullname: fullname |
| 522 | :param email: email |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 523 | :param active: active |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 524 | :param groups: array of strings |
| 525 | groups: |
| 526 | - Non-Interactive Users |
| 527 | - Testers |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 528 | :param ssh_key: public ssh key |
| 529 | :param http_password: http password |
| 530 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 531 | CLI Examples: |
| 532 | |
| 533 | .. code-block:: bash |
| 534 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 535 | salt '*' gerrit.account_create username "full name" "mail@domain.com" |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 536 | |
| 537 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 538 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 539 | output, changed = _update_account( |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 540 | gerrit_client, **{ |
| 541 | 'username': username, |
| 542 | 'fullname': fullname, |
| 543 | 'email': email, |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 544 | # 'active': active, |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 545 | 'groups': groups, |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 546 | 'ssh_key': ssh_key, |
| 547 | 'http_password': http_password |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 548 | }) |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 549 | return output |
| 550 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 551 | def account_list(**kwargs): |
| 552 | ''' |
| 553 | List gerrit accounts |
| 554 | |
| 555 | CLI Examples: |
| 556 | |
| 557 | .. code-block:: bash |
| 558 | |
| 559 | salt '*' gerrit.account_list |
| 560 | |
| 561 | ''' |
| 562 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 563 | ret_list = gerrit_client.get('/accounts/?q=*&n=10000') |
| 564 | ret = {} |
| 565 | for item in ret_list: |
| 566 | ret[item['username']] = item |
| 567 | return ret |
| 568 | |
| 569 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 570 | def account_get(name, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 571 | ''' |
| 572 | Get gerrit account |
| 573 | |
| 574 | CLI Examples: |
| 575 | |
| 576 | .. code-block:: bash |
| 577 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 578 | salt '*' gerrit.account_get name |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 579 | |
| 580 | ''' |
| 581 | gerrit_client = _gerrit_http_connection(**kwargs) |
Filip Pytloun | 1a521b3 | 2017-03-28 12:06:50 +0200 | [diff] [blame^] | 582 | try: |
| 583 | ret = gerrit_client.get('/accounts/%s' % name) |
| 584 | except Exception: |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 585 | ret = {'Error': 'Error in retrieving account'} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 586 | return ret |
| 587 | |
| 588 | |
| 589 | def group_list(**kwargs): |
| 590 | ''' |
| 591 | List gerrit groups |
| 592 | |
| 593 | CLI Examples: |
| 594 | |
| 595 | .. code-block:: bash |
| 596 | |
| 597 | salt '*' gerrit.group_list |
| 598 | |
| 599 | ''' |
| 600 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 601 | return gerrit_client.get('/groups/') |
| 602 | |
| 603 | |
| 604 | def group_get(groupname, **kwargs): |
| 605 | ''' |
| 606 | Get gerrit group |
| 607 | |
| 608 | CLI Examples: |
| 609 | |
| 610 | .. code-block:: bash |
| 611 | |
| 612 | salt '*' gerrit.group_get groupname |
| 613 | |
| 614 | ''' |
| 615 | gerrit_client = _gerrit_http_connection(**kwargs) |
Filip Pytloun | 9c1b92f | 2017-02-28 19:26:49 +0100 | [diff] [blame] | 616 | try: |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 617 | item = gerrit_client.get('/groups/%s' % groupname) |
| 618 | ret = {item['name']: item} |
| 619 | except: |
| 620 | ret = {'Error': 'Error in retrieving account'} |
| 621 | return ret |
| 622 | |
| 623 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 624 | def group_create(name, description=None, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 625 | ''' |
| 626 | Create a gerrit group |
| 627 | |
| 628 | :param name: name |
| 629 | |
| 630 | CLI Examples: |
| 631 | |
| 632 | .. code-block:: bash |
| 633 | |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 634 | salt '*' gerrit.group_create group-name description |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 635 | |
| 636 | ''' |
| 637 | gerrit_client = _gerrit_http_connection(**kwargs) |
| 638 | ret, changed = _update_group( |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 639 | gerrit_client, **{'name': name, 'description': description}) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 640 | return ret |
| 641 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 642 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 643 | def project_create(name, **kwargs): |
| 644 | ''' |
| 645 | Create a gerrit project |
| 646 | |
| 647 | :param name: new project name |
| 648 | |
| 649 | CLI Examples: |
| 650 | |
| 651 | .. code-block:: bash |
| 652 | |
| 653 | salt '*' gerrit.project_create namespace/nova description='nova project' |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 654 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 655 | ''' |
| 656 | ret = {} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 657 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 658 | |
| 659 | project = project_get(name, **kwargs) |
| 660 | |
| 661 | if project and not "Error" in project: |
| 662 | LOG.debug("Project {0} exists".format(name)) |
| 663 | return project |
| 664 | |
| 665 | new = gerrit_client.createProject(name) |
| 666 | return project_get(name, **kwargs) |
| 667 | |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 668 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 669 | def project_get(name, **kwargs): |
| 670 | ''' |
| 671 | Return a specific project |
| 672 | |
| 673 | CLI Examples: |
| 674 | |
| 675 | .. code-block:: bash |
| 676 | |
| 677 | salt '*' gerrit.project_get projectname |
| 678 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 679 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 680 | ret = {} |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 681 | projects = gerrit_client.listProjects() |
| 682 | if not name in projects: |
| 683 | return {'Error': 'Error in retrieving project'} |
| 684 | ret[name] = {'name': name} |
| 685 | return ret |
| 686 | |
| 687 | |
| 688 | def project_list(**connection_args): |
| 689 | ''' |
| 690 | Return a list of available projects |
| 691 | |
| 692 | CLI Example: |
| 693 | |
| 694 | .. code-block:: bash |
| 695 | |
| 696 | salt '*' gerrit.project_list |
| 697 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 698 | gerrit_client = _gerrit_ssh_connection(**connection_args) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 699 | ret = {} |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 700 | projects = gerrit_client.listProjects() |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 701 | for project in projects: |
| 702 | ret[project] = { |
| 703 | 'name': project |
| 704 | } |
| 705 | return ret |
| 706 | |
| 707 | |
| 708 | def query(change, **kwargs): |
| 709 | ''' |
| 710 | Query gerrit |
| 711 | |
| 712 | :param change: Query content |
| 713 | |
| 714 | CLI Examples: |
| 715 | |
| 716 | .. code-block:: bash |
| 717 | |
| 718 | salt '*' gerrit.query 'status:open project:tools/gerrit limit:2' |
Michael Kutý | 099c534 | 2016-09-09 14:44:13 +0200 | [diff] [blame] | 719 | |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 720 | ''' |
| 721 | ret = {} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 722 | gerrit_client = _gerrit_ssh_connection(**kwargs) |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 723 | msg = gerrit_client.query(change) |
| 724 | ret['query'] = msg |
| 725 | return ret |