-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitbucket_application_link.py
339 lines (287 loc) · 10.8 KB
/
bitbucket_application_link.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2021, Krzysztof Lewandowski <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r'''
---
module: bitbucket_application_link_info
short_description: Manage application links on Bitbucket Server
description:
- Manage application links on Bitbucket Server.
- One may refer to an application link either by its ID or its name.
- Authentication can be done with I(token) or with I(username) and I(password).
author:
- Krzysztof Lewandowski (@klewan)
version_added: 1.2.0
options:
url:
description:
- Bitbucket Server URL.
type: str
required: false
username:
description:
- Username used for authentication.
- This is only needed when not using I(token).
- Required when I(password) is provided.
type: str
required: false
password:
description:
- Password used for authentication.
- This is only needed when not using I(token).
- Required when I(username) is provided.
type: str
required: false
token:
description:
- Token parameter for authentication.
- This is only needed when not using I(username) and I(password).
type: str
required: false
applink:
description:
- Retrieve application links matching the supplied I(applink) filter.
- This can be '*' which means all application links.
- One may refer to an application link either by its ID or its name.
type: list
required: false
default: [ '*' ]
validate_certs:
description:
- If C(no), SSL certificates will not be validated.
- This should only set to C(no) used on personally controlled sites using self-signed certificates.
type: bool
default: yes
use_proxy:
description:
- If C(no), it will not use a proxy, even if one is defined in an environment variable on the target hosts.
type: bool
default: yes
sleep:
description:
- Number of seconds to sleep between API retries.
type: int
default: 5
retries:
description:
- Number of retries to call Bitbucket API URL before failure.
type: int
default: 3
notes:
- Bitbucket Access Token can be obtained from Bitbucket profile -> Manage Account -> Personal Access Tokens.
- Supports C(check_mode).
'''
EXAMPLES = r'''
- name: Create application link
esp.bitbucket.bitbucket_application_link:
url: 'https://bitbucket.example.com'
username: '{{ bitbucket_username }}'
password: '{{ bitbucket_password }}'
applink:
name: FOO
rpcUrl: https://terraform.example.com/app/my-org
displayUrl: https://terraform.example.com/app/my-org
key: "de00c96434df2b31d3e3e3164391091c"
publicKey: |
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0E6OBvxPXTQlyQ8IBIP7
(...)
Gzr9C9mYr5zh15Rd7ygubYT1rKPTnQEuQMZpki9rsS3cKEYGyIX6nFLJdpZHK7hL
2QIDAQAB
-----END PUBLIC KEY-----
state: present
validate_certs: no
register: _result
- name: Delete application link (supplied by name or ID)
esp.bitbucket.bitbucket_application_link:
url: 'https://bitbucket.example.com'
username: '{{ bitbucket_username }}'
password: '{{ bitbucket_password }}'
applink:
name: FOO
#id: 227dd1d7-f6d6-34a5-b046-5663fb518691
state: absent
validate_certs: no
'''
RETURN = r'''
json:
description: Details of application link.
returned: success
type: dict
sample:
id: "227dd1d7-f6d6-34a5-b046-5663fb518691"
status:
resources-created:
link:
"@href": "https://bitbucket.example.com/rest/applinks/3.0/applicationlink/227dd1d7-f6d6-34a5-b046-5663fb518691"
"@rel": "self"
status-code: "201"
'''
import re
import xmltodict
import json
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.esp.bitbucket.plugins.module_utils.bitbucket import BitbucketHelper
def create_application_link(module, bitbucket, data=None):
"""
Create Application Link
"""
if 'name' not in module.params['applink']:
module.fail_json(msg='`applink.name` is required when the `state` is `present`')
if 'rpcUrl' not in module.params['applink']:
module.fail_json(msg='`applink.rpcUrl` is required when the `state` is `present`')
if 'displayUrl' not in module.params['applink']:
module.fail_json(msg='`applink.displayUrl` is required when the `state` is `present`')
data = {
'id': None,
'name': module.params['applink']['name'],
'rpcUrl': module.params['applink']['rpcUrl'],
'displayUrl': module.params['applink']['displayUrl'],
'typeId': 'generic'
}
info, content = bitbucket.request(
api_url='{url}/rest/applinks/3.0/applicationlink'.format(url=module.params['url']),
method='PUT',
data=data,
)
if info['status'] == 201:
try:
js = json.loads(json.dumps(xmltodict.parse(content['content'])))
if isinstance(js, dict):
m = re.search('applicationlink/(.+?)"', content['content'])
if m:
js['id'] = m.group(1)
ret = js
except Exception as e:
ret = content
return ret
if info['status'] != 201:
module.fail_json(msg='Failed to create an application link: {info}'.format(
info=info,
))
return None
def update_application_link(module, bitbucket, applicationLinkID=None, data=None):
"""
Update Application Link
"""
if 'key' not in module.params['applink']:
module.fail_json(msg='`applink.key` is required when the `state` is `present`')
if 'name' not in module.params['applink']:
module.fail_json(msg='`applink.name` is required when the `state` is `present`')
if 'publicKey' not in module.params['applink']:
module.fail_json(msg='`applink.publicKey` is required when the `state` is `present`')
data = {
'key': module.params['applink']['key'],
'name': module.params['applink']['name'],
'description': module.params['applink'].get('description', None),
'sharedSecret': module.params['applink'].get('sharedSecret', None),
'publicKey': module.params['applink']['publicKey'],
'outgoing': module.params['applink'].get('outgoing', False),
'twoLOAllowed': module.params['applink'].get('twoLOAllowed', False),
'executingTwoLOUser': module.params['applink'].get('executingTwoLOUser', None),
'twoLOImpersonationAllowed': module.params['applink'].get('twoLOImpersonationAllowed', None),
}
info, content = bitbucket.request(
api_url='{url}/rest/applinks-oauth/1.0/applicationlink/{applicationLinkID}/authentication/consumer'.format(
url=module.params['url'],
applicationLinkID=applicationLinkID,
),
method='PUT',
data=data,
)
if info['status'] == 201:
try:
js = json.loads(json.dumps(xmltodict.parse(content['content'])))
if isinstance(js, dict):
m = re.search('applicationlink/(.+?)/', content['content'])
if m:
js['id'] = m.group(1)
ret = js
except Exception as e:
ret = content
return ret
if info['status'] != 201:
module.fail_json(msg='Failed to update an application link: {info}'.format(
info=info,
))
return None
def delete_application_link(module, bitbucket, id=None, fail_when_not_exists=False):
"""
Delete Application Link
"""
url = (BitbucketHelper.BITBUCKET_API_ENDPOINTS['applinks'] + '/{id}').format(
url=module.params['url'],
id=id,
)
info, content = bitbucket.request(
api_url=url,
method='DELETE',
)
if info['status'] == 204:
return content
if info['status'] != 204:
if fail_when_not_exists:
module.fail_json(msg='Failed to delete an application link: {info}'.format(
info=info,
))
else:
return {
'id': id,
'status': info['status'],
'info': info,
'deleted': False,
}
return None
def main():
argument_spec = BitbucketHelper.bitbucket_argument_spec()
argument_spec.update(
state=dict(type='str', choices=['present', 'absent'], default='present'),
applink=dict(type='dict', required=True, no_log=False),
)
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
required_together=[('username', 'password')],
required_one_of=[('username', 'token')],
mutually_exclusive=[('username', 'token')],
)
bitbucket = BitbucketHelper(module)
module.params['return_content'] = True
state = module.params['state']
applink = module.params['applink']
# Seed the result dict in the object
result = dict(
changed=False,
state=state,
json={},
)
if state == 'absent':
result['applink'] = applink
# Retrieve detalis on all Application Links
all_applinks = bitbucket.get_application_links_info()
found = [al for al in all_applinks['json'] if al['id'] == applink.get('id', '_') or al['name'] == applink.get('name', '_')]
if len(found) > 1:
module.fail_json(msg='Found multiple Application Links matching the supplied parameter "%s". Refer to application link either by its ID or name.' % (applink) )
# Delete application link when it exists and state == 'absent'
if state == 'absent':
if len(found) == 1:
result['changed'] = True
if not module.check_mode:
result['json'] = delete_application_link(module, bitbucket, id=found[0]['id'], fail_when_not_exists=False )
# Create or update application link when state == 'present'
else:
if len(found) == 0:
result['changed'] = True
if not module.check_mode:
result['json'] = create_application_link(module, bitbucket, data=applink)
update_application_link(module, bitbucket, applicationLinkID=result['json']['id'], data=applink)
if len(found) == 1:
result['changed'] = True
if not module.check_mode:
result['json'] = update_application_link(module, bitbucket, applicationLinkID=found[0]['id'], data=applink)
module.exit_json(**result)
if __name__ == '__main__':
main()