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