Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Management of gerrit projects |
| 4 | ============================== |
| 5 | |
| 6 | :depends: - gerritlib Python module |
| 7 | :configuration: See :py:mod:`salt.modules.gerrit` for setup instructions. |
| 8 | |
| 9 | .. code-block:: yaml |
| 10 | |
| 11 | gerrit project: |
| 12 | gerrit.project_present: |
| 13 | - name: gerrit project |
| 14 | |
| 15 | ''' |
| 16 | |
| 17 | |
| 18 | def __virtual__(): |
| 19 | ''' |
| 20 | Only load if the gerrit module is in __salt__ |
| 21 | ''' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 22 | return 'gerrit' if 'gerrit.account_create' in __salt__ else False |
| 23 | |
| 24 | |
Filip Pytloun | cd9cb10 | 2017-03-30 11:10:27 +0200 | [diff] [blame] | 25 | def account_present(name, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 26 | ''' |
| 27 | Ensures that the gerrit account exists |
Filip Pytloun | cd9cb10 | 2017-03-30 11:10:27 +0200 | [diff] [blame] | 28 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 29 | :param name: username |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 30 | :param fullname: fullname |
| 31 | :param email: email |
| 32 | :param active: active |
| 33 | :param groups: array of strings |
| 34 | groups: |
| 35 | - Non-Interactive Users |
| 36 | - Testers |
| 37 | :param ssh_key: public ssh key |
| 38 | :param http_password: http password |
| 39 | |
| 40 | ''' |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 41 | ret = {'name': name, |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 42 | 'changes': {}, |
| 43 | 'result': True, |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 44 | 'comment': 'Account "{0}" already exists'.format(name)} |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 45 | |
| 46 | # Check if account is already present |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 47 | account = __salt__['gerrit.account_get'](name, **kwargs) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 48 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 49 | if 'Error' not in account: |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 50 | # Update account |
| 51 | __salt__['gerrit.account_update'](name, fullname, email, active, groups, ssh_key, http_password, **kwargs) |
| 52 | ret['comment'] = 'Account "{0}" has been updated'.format(name) |
| 53 | ret['changes']['Account'] = 'Updated' |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 54 | else: |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 55 | # Create account |
| 56 | __salt__['gerrit.account_create'](name, fullname, email, active, groups, ssh_key, http_password, **kwargs) |
| 57 | ret['comment'] = 'Account "{0}" has been added'.format(name) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 58 | ret['changes']['Account'] = 'Created' |
| 59 | return ret |
| 60 | |
| 61 | |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 62 | def group_present(name, description=None, **kwargs): |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 63 | ''' |
| 64 | Ensures that the gerrit group exists |
Filip Pytloun | cd9cb10 | 2017-03-30 11:10:27 +0200 | [diff] [blame] | 65 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 66 | :param name: group name |
| 67 | ''' |
| 68 | ret = {'name': name, |
| 69 | 'changes': {}, |
| 70 | 'result': True, |
| 71 | 'comment': 'Group "{0}" already exists'.format(name)} |
| 72 | |
| 73 | # Check if group is already present |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 74 | group = __salt__['gerrit.group_get'](name, **kwargs) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 75 | |
| 76 | if 'Error' not in group: |
Ales Komarek | b0fcc25 | 2016-09-14 19:29:37 +0200 | [diff] [blame] | 77 | # Update group |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 78 | pass |
| 79 | else: |
| 80 | # Create group |
Ales Komarek | 2fc3900 | 2016-09-14 11:43:56 +0200 | [diff] [blame] | 81 | __salt__['gerrit.group_create'](name, description, **kwargs) |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 82 | ret['comment'] = 'Group "{0}" has been added'.format(name) |
| 83 | ret['changes']['Group'] = 'Created' |
| 84 | return ret |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def project_present(name, description=None, **kwargs): |
| 88 | ''' |
| 89 | Ensures that the gerrit project exists |
Filip Pytloun | cd9cb10 | 2017-03-30 11:10:27 +0200 | [diff] [blame] | 90 | |
Ales Komarek | 07d1655 | 2016-09-12 21:39:18 +0200 | [diff] [blame] | 91 | :param name: project name |
Ales Komarek | 49a3729 | 2016-08-31 16:18:31 +0200 | [diff] [blame] | 92 | :param description: short project description |
| 93 | ''' |
| 94 | ret = {'name': name, |
| 95 | 'changes': {}, |
| 96 | 'result': True, |
| 97 | 'comment': 'Project "{0}" already exists'.format(name)} |
| 98 | |
| 99 | # Check if project is already present |
| 100 | project = __salt__['gerrit.project_get'](name=name, **kwargs) |
| 101 | |
| 102 | if 'Error' not in project: |
| 103 | #update project |
| 104 | pass |
| 105 | else: |
| 106 | # Create project |
| 107 | __salt__['gerrit.project_create'](name, **kwargs) |
| 108 | ret['comment'] = 'Project "{0}" has been added'.format(name) |
| 109 | ret['changes']['Project'] = 'Created' |
| 110 | return ret |