blob: 47969dac7016c3fdac122fce88f0996558e8d5ac [file] [log] [blame]
Ales Komarek49a37292016-08-31 16:18:31 +02001# -*- coding: utf-8 -*-
2'''
3Management 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
18def __virtual__():
19 '''
20 Only load if the gerrit module is in __salt__
21 '''
Ales Komarek07d16552016-09-12 21:39:18 +020022 return 'gerrit' if 'gerrit.account_create' in __salt__ else False
23
24
Filip Pytlouncd9cb102017-03-30 11:10:27 +020025def account_present(name, fullname=None, email=None, active=None, groups=[], ssh_key=None, http_password=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +020026 '''
27 Ensures that the gerrit account exists
Filip Pytlouncd9cb102017-03-30 11:10:27 +020028
Ales Komarek2fc39002016-09-14 11:43:56 +020029 :param name: username
Ales Komarek07d16552016-09-12 21:39:18 +020030 :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 Komarek2fc39002016-09-14 11:43:56 +020041 ret = {'name': name,
Ales Komarek07d16552016-09-12 21:39:18 +020042 'changes': {},
43 'result': True,
Ales Komarek2fc39002016-09-14 11:43:56 +020044 'comment': 'Account "{0}" already exists'.format(name)}
Ales Komarek07d16552016-09-12 21:39:18 +020045
46 # Check if account is already present
Ales Komarek2fc39002016-09-14 11:43:56 +020047 account = __salt__['gerrit.account_get'](name, **kwargs)
Ales Komarek07d16552016-09-12 21:39:18 +020048
Ales Komarek2fc39002016-09-14 11:43:56 +020049 if 'Error' not in account:
Ales Komarekb0fcc252016-09-14 19:29:37 +020050 # 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 Komarek07d16552016-09-12 21:39:18 +020054 else:
Ales Komarek2fc39002016-09-14 11:43:56 +020055 # 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 Komarek07d16552016-09-12 21:39:18 +020058 ret['changes']['Account'] = 'Created'
59 return ret
60
61
Ales Komarek2fc39002016-09-14 11:43:56 +020062def group_present(name, description=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +020063 '''
64 Ensures that the gerrit group exists
Filip Pytlouncd9cb102017-03-30 11:10:27 +020065
Ales Komarek07d16552016-09-12 21:39:18 +020066 :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 Komarek2fc39002016-09-14 11:43:56 +020074 group = __salt__['gerrit.group_get'](name, **kwargs)
Ales Komarek07d16552016-09-12 21:39:18 +020075
76 if 'Error' not in group:
Ales Komarekb0fcc252016-09-14 19:29:37 +020077 # Update group
Ales Komarek07d16552016-09-12 21:39:18 +020078 pass
79 else:
80 # Create group
Ales Komarek2fc39002016-09-14 11:43:56 +020081 __salt__['gerrit.group_create'](name, description, **kwargs)
Ales Komarek07d16552016-09-12 21:39:18 +020082 ret['comment'] = 'Group "{0}" has been added'.format(name)
83 ret['changes']['Group'] = 'Created'
84 return ret
Ales Komarek49a37292016-08-31 16:18:31 +020085
86
87def project_present(name, description=None, **kwargs):
88 '''
89 Ensures that the gerrit project exists
Filip Pytlouncd9cb102017-03-30 11:10:27 +020090
Ales Komarek07d16552016-09-12 21:39:18 +020091 :param name: project name
Ales Komarek49a37292016-08-31 16:18:31 +020092 :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