blob: c869d6ed90299f238743e56424d8febb219036f7 [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
Ales Komarek2fc39002016-09-14 11:43:56 +020025def account_present(name, fullname, 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
28
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:
50 #update account
Ales Komarek07d16552016-09-12 21:39:18 +020051 pass
52 else:
Ales Komarek2fc39002016-09-14 11:43:56 +020053 # Create account
54 __salt__['gerrit.account_create'](name, fullname, email, active, groups, ssh_key, http_password, **kwargs)
55 ret['comment'] = 'Account "{0}" has been added'.format(name)
Ales Komarek07d16552016-09-12 21:39:18 +020056 ret['changes']['Account'] = 'Created'
57 return ret
58
59
Ales Komarek2fc39002016-09-14 11:43:56 +020060def group_present(name, description=None, **kwargs):
Ales Komarek07d16552016-09-12 21:39:18 +020061 '''
62 Ensures that the gerrit group exists
63
64 :param name: group name
65 '''
66 ret = {'name': name,
67 'changes': {},
68 'result': True,
69 'comment': 'Group "{0}" already exists'.format(name)}
70
71 # Check if group is already present
Ales Komarek2fc39002016-09-14 11:43:56 +020072 group = __salt__['gerrit.group_get'](name, **kwargs)
Ales Komarek07d16552016-09-12 21:39:18 +020073
74 if 'Error' not in group:
75 #update group
76 pass
77 else:
78 # Create group
Ales Komarek2fc39002016-09-14 11:43:56 +020079 __salt__['gerrit.group_create'](name, description, **kwargs)
Ales Komarek07d16552016-09-12 21:39:18 +020080 ret['comment'] = 'Group "{0}" has been added'.format(name)
81 ret['changes']['Group'] = 'Created'
82 return ret
Ales Komarek49a37292016-08-31 16:18:31 +020083
84
85def project_present(name, description=None, **kwargs):
86 '''
87 Ensures that the gerrit project exists
88
Ales Komarek07d16552016-09-12 21:39:18 +020089 :param name: project name
Ales Komarek49a37292016-08-31 16:18:31 +020090 :param description: short project description
91 '''
92 ret = {'name': name,
93 'changes': {},
94 'result': True,
95 'comment': 'Project "{0}" already exists'.format(name)}
96
97 # Check if project is already present
98 project = __salt__['gerrit.project_get'](name=name, **kwargs)
99
100 if 'Error' not in project:
101 #update project
102 pass
103 else:
104 # Create project
105 __salt__['gerrit.project_create'](name, **kwargs)
106 ret['comment'] = 'Project "{0}" has been added'.format(name)
107 ret['changes']['Project'] = 'Created'
108 return ret