/
gerrit_project.in.py
188 lines (145 loc) · 6.13 KB
/
gerrit_project.in.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Copyright (C) 2015 Codethink Limited
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Prepend the contents of common.py to this file to create the final
# self-contained module.
import logging
from ansible.module_utils.basic import *
DOCUMENTATION = '''
---
module: gerrit_project
author: Sam Thursfield
short_description: Manage projects in an instance of Gerrit Code Review
'''
EXAMPLES = '''
- gerrit_project:
name: Morph
description: Baserock build tool
state: active
gerrit_url: http://gerrit.example.com:8080/
gerrit_admin_username: dicky
gerrit_admin_password: b0sst0nes
'''
# https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html
PROJECT_ARGUMENTS = dict(
name = dict(required=True),
description = dict(),
# You might expect there to be an 'absent' state, but there's actually no
# way to delete projects out of the box with Gerrit. There is a
# delete-project plugin that allows it.
state = dict(default='active',
choices=['active', 'hidden', 'read_only'])
)
def create_project(gerrit, name=None):
# It's possible to pass a ProjectInput structure to configure the
# project now, but to reduce the amount of code here we leave it to the
# update_project() function.
project_info = gerrit.put('/projects/%s' % quote(name))
project_config_info = gerrit.get('/projects/%s/config' % quote(name))
return project_config_info
def remove_project(gerrit, name=None):
try:
project_info = gerrit.get('/projects/%s' % quote(name))
raise AnsibleGerritError(
"Cannot remove project %s: deleting projects is not supported by "
"Gerrit. (Although you could use the delete-projects plugin, if "
"you really want to get rid of it)." % name)
except requests.exceptions.HTTPError as e:
if e.response.status_code == 404:
logging.info("Project %s not found.", name)
changed = False
else:
raise e
return changed
def update_project(gerrit, name=None, **params):
change = False
try:
config_info = gerrit.get('/projects/%s/config' % quote(name))
if 'state' not in config_info:
# State is only returned if it is READ_ONLY or HIDDEN
config_info['state'] = 'ACTIVE'
except requests.exceptions.HTTPError as e:
if e.response.status_code == 404:
logging.info("Project %s not found, creating it.", name)
config_info = create_project(gerrit, name)
change = True
else:
raise
logging.debug(
'Existing config info for project %s: %s', name,
json.dumps(config_info, indent=4))
# We provide a value for all fields of the ConfigInput structure. Most of
# them are optional and will be unchanged if we don't provide a value, but
# 'description' needs a value or the project's description will be removed.
# To err on the side of safety, we provide values for all the fields we
# can.
config_input = {}
# This is going to be ugly, needs to be split up into functions.
for field, spec in PROJECT_ARGUMENTS.iteritems():
if field != 'name':
if params.get(field) is None:
# User didn't specify a value.
if field in config_info:
# Keep the current value.
current_value = value_from_config_info(
field, spec, config_info[field])
config_input[field] = current_value
else:
logging.warning(
"Ignoring field %s that is missing from config_info",
field)
else:
# User specified a new value.
if field in config_info:
old_value = value_from_config_info(
field, spec, config_info[field])
else:
old_value = None
new_value = value_from_param(
field, spec, params[field])
if old_value != new_value:
change = True
if new_value is not None:
config_info[field] = new_value
config_input[field] = new_value
if change:
logging.debug(
'Config input for project %s: %s', name,
json.dumps(config_input, indent=4))
headers = {'content-type': 'application/json'}
gerrit.put('/projects/%s/config' % quote(name), data=json.dumps(config_input),
headers=headers)
return config_info, change
def main():
logging.basicConfig(filename='/tmp/ansible-gerrit-debug.log',
level=logging.DEBUG)
argument_spec = dict()
argument_spec.update(PROJECT_ARGUMENTS)
argument_spec.update(GERRIT_COMMON_ARGUMENTS)
module = AnsibleModule(argument_spec)
logging.debug('Module parameters: %s', json.dumps(module.params, indent=4))
try:
gerrit = gerrit_connection(**module.params)
if module.params['state'] == 'absent':
changed = remove_project(gerrit, **module.params)
module.exit_json(changed=changed)
else:
project_config_info, changed = update_project(
gerrit, **module.params)
module.exit_json(changed=changed,
project_config_info=project_config_info)
except (AnsibleGerritError, requests.exceptions.RequestException) as e:
logging.error('%r', e)
module.fail_json(msg=str(e))
main()