2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2019, Evgeniy Krysanov <evgeniy.krysanov@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
|
|
|
module: bitbucket_pipeline_variable
|
|
|
|
short_description: Manages Bitbucket pipeline variables
|
|
|
|
description:
|
|
|
|
- Manages Bitbucket pipeline variables.
|
|
|
|
author:
|
|
|
|
- Evgeniy Krysanov (@catcombo)
|
2021-10-31 19:09:25 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.bitbucket
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
repository:
|
|
|
|
description:
|
|
|
|
- The repository name.
|
|
|
|
type: str
|
|
|
|
required: true
|
2021-10-31 19:09:25 +01:00
|
|
|
workspace:
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The repository owner.
|
2021-10-31 19:09:25 +01:00
|
|
|
- Alias I(username) has been deprecated and will become an alias of I(user) in community.general 6.0.0.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: str
|
|
|
|
required: true
|
2021-10-31 19:09:25 +01:00
|
|
|
aliases: [ username ]
|
2020-03-09 10:11:07 +01:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The pipeline variable name.
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
value:
|
|
|
|
description:
|
|
|
|
- The pipeline variable value.
|
|
|
|
type: str
|
|
|
|
secured:
|
|
|
|
description:
|
|
|
|
- Whether to encrypt the variable value.
|
|
|
|
type: bool
|
2022-08-24 19:59:56 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicates desired state of the variable.
|
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
choices: [ absent, present ]
|
|
|
|
notes:
|
|
|
|
- Check mode is supported.
|
|
|
|
- For secured values return parameter C(changed) is always C(True).
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Create or update pipeline variables from the list
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.bitbucket_pipeline_variable:
|
2020-03-09 10:11:07 +01:00
|
|
|
repository: 'bitbucket-repo'
|
2021-10-31 19:09:25 +01:00
|
|
|
workspace: bitbucket_workspace
|
2020-03-09 10:11:07 +01:00
|
|
|
name: '{{ item.name }}'
|
|
|
|
value: '{{ item.value }}'
|
|
|
|
secured: '{{ item.secured }}'
|
|
|
|
state: present
|
|
|
|
with_items:
|
2022-09-06 20:42:17 +02:00
|
|
|
- { name: AWS_ACCESS_KEY, value: ABCD1234, secured: false }
|
|
|
|
- { name: AWS_SECRET, value: qwe789poi123vbn0, secured: true }
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: Remove pipeline variable
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.bitbucket_pipeline_variable:
|
2020-03-09 10:11:07 +01:00
|
|
|
repository: bitbucket-repo
|
2021-10-31 19:09:25 +01:00
|
|
|
workspace: bitbucket_workspace
|
2020-03-09 10:11:07 +01:00
|
|
|
name: AWS_ACCESS_KEY
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r''' # '''
|
|
|
|
|
2021-01-14 15:27:15 +01:00
|
|
|
from ansible.module_utils.basic import AnsibleModule, _load_params
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.source_control.bitbucket import BitbucketHelper
|
|
|
|
|
|
|
|
error_messages = {
|
|
|
|
'required_value': '`value` is required when the `state` is `present`',
|
|
|
|
}
|
|
|
|
|
|
|
|
BITBUCKET_API_ENDPOINTS = {
|
2021-10-31 19:09:25 +01:00
|
|
|
'pipeline-variable-list': '%s/2.0/repositories/{workspace}/{repo_slug}/pipelines_config/variables/' % BitbucketHelper.BITBUCKET_API_URL,
|
|
|
|
'pipeline-variable-detail': '%s/2.0/repositories/{workspace}/{repo_slug}/pipelines_config/variables/{variable_uuid}' % BitbucketHelper.BITBUCKET_API_URL,
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_existing_pipeline_variable(module, bitbucket):
|
|
|
|
"""
|
|
|
|
Search for a pipeline variable
|
|
|
|
|
|
|
|
:param module: instance of the :class:`AnsibleModule`
|
|
|
|
:param bitbucket: instance of the :class:`BitbucketHelper`
|
|
|
|
:return: existing variable or None if not found
|
|
|
|
:rtype: dict or None
|
|
|
|
|
|
|
|
Return example::
|
|
|
|
|
|
|
|
{
|
|
|
|
'name': 'AWS_ACCESS_OBKEY_ID',
|
|
|
|
'value': 'x7HU80-a2',
|
|
|
|
'type': 'pipeline_variable',
|
|
|
|
'secured': False,
|
|
|
|
'uuid': '{9ddb0507-439a-495a-99f3-5464f15128127}'
|
|
|
|
}
|
|
|
|
|
|
|
|
The `value` key in dict is absent in case of secured variable.
|
|
|
|
"""
|
2020-12-17 07:57:07 +01:00
|
|
|
variables_base_url = BITBUCKET_API_ENDPOINTS['pipeline-variable-list'].format(
|
2021-10-31 19:09:25 +01:00
|
|
|
workspace=module.params['workspace'],
|
2020-12-17 07:57:07 +01:00
|
|
|
repo_slug=module.params['repository'],
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
# Look through the all response pages in search of variable we need
|
2020-12-17 07:57:07 +01:00
|
|
|
page = 1
|
|
|
|
while True:
|
|
|
|
next_url = "%s?page=%s" % (variables_base_url, page)
|
2020-03-09 10:11:07 +01:00
|
|
|
info, content = bitbucket.request(
|
2020-12-17 07:57:07 +01:00
|
|
|
api_url=next_url,
|
2020-03-09 10:11:07 +01:00
|
|
|
method='GET',
|
|
|
|
)
|
|
|
|
|
|
|
|
if info['status'] == 404:
|
2021-10-31 19:09:25 +01:00
|
|
|
module.fail_json(msg='Invalid `repository` or `workspace`.')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if info['status'] != 200:
|
|
|
|
module.fail_json(msg='Failed to retrieve the list of pipeline variables: {0}'.format(info))
|
|
|
|
|
2020-12-17 07:57:07 +01:00
|
|
|
# We are at the end of list
|
|
|
|
if 'pagelen' in content and content['pagelen'] == 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
page += 1
|
2020-03-09 10:11:07 +01:00
|
|
|
var = next(filter(lambda v: v['key'] == module.params['name'], content['values']), None)
|
|
|
|
|
|
|
|
if var is not None:
|
|
|
|
var['name'] = var.pop('key')
|
|
|
|
return var
|
|
|
|
|
|
|
|
|
|
|
|
def create_pipeline_variable(module, bitbucket):
|
|
|
|
info, content = bitbucket.request(
|
|
|
|
api_url=BITBUCKET_API_ENDPOINTS['pipeline-variable-list'].format(
|
2021-10-31 19:09:25 +01:00
|
|
|
workspace=module.params['workspace'],
|
2020-03-09 10:11:07 +01:00
|
|
|
repo_slug=module.params['repository'],
|
|
|
|
),
|
|
|
|
method='POST',
|
|
|
|
data={
|
|
|
|
'key': module.params['name'],
|
|
|
|
'value': module.params['value'],
|
|
|
|
'secured': module.params['secured'],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if info['status'] != 201:
|
|
|
|
module.fail_json(msg='Failed to create pipeline variable `{name}`: {info}'.format(
|
|
|
|
name=module.params['name'],
|
|
|
|
info=info,
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
def update_pipeline_variable(module, bitbucket, variable_uuid):
|
|
|
|
info, content = bitbucket.request(
|
|
|
|
api_url=BITBUCKET_API_ENDPOINTS['pipeline-variable-detail'].format(
|
2021-10-31 19:09:25 +01:00
|
|
|
workspace=module.params['workspace'],
|
2020-03-09 10:11:07 +01:00
|
|
|
repo_slug=module.params['repository'],
|
|
|
|
variable_uuid=variable_uuid,
|
|
|
|
),
|
|
|
|
method='PUT',
|
|
|
|
data={
|
|
|
|
'value': module.params['value'],
|
|
|
|
'secured': module.params['secured'],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if info['status'] != 200:
|
|
|
|
module.fail_json(msg='Failed to update pipeline variable `{name}`: {info}'.format(
|
|
|
|
name=module.params['name'],
|
|
|
|
info=info,
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
def delete_pipeline_variable(module, bitbucket, variable_uuid):
|
|
|
|
info, content = bitbucket.request(
|
|
|
|
api_url=BITBUCKET_API_ENDPOINTS['pipeline-variable-detail'].format(
|
2021-10-31 19:09:25 +01:00
|
|
|
workspace=module.params['workspace'],
|
2020-03-09 10:11:07 +01:00
|
|
|
repo_slug=module.params['repository'],
|
|
|
|
variable_uuid=variable_uuid,
|
|
|
|
),
|
|
|
|
method='DELETE',
|
|
|
|
)
|
|
|
|
|
|
|
|
if info['status'] != 204:
|
|
|
|
module.fail_json(msg='Failed to delete pipeline variable `{name}`: {info}'.format(
|
|
|
|
name=module.params['name'],
|
|
|
|
info=info,
|
|
|
|
))
|
|
|
|
|
|
|
|
|
2021-01-14 15:27:15 +01:00
|
|
|
class BitBucketPipelineVariable(AnsibleModule):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
params = _load_params() or {}
|
|
|
|
if params.get('secured'):
|
|
|
|
kwargs['argument_spec']['value'].update({'no_log': True})
|
|
|
|
super(BitBucketPipelineVariable, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def main():
|
|
|
|
argument_spec = BitbucketHelper.bitbucket_argument_spec()
|
|
|
|
argument_spec.update(
|
|
|
|
repository=dict(type='str', required=True),
|
2021-10-31 19:09:25 +01:00
|
|
|
workspace=dict(
|
|
|
|
type='str', aliases=['username'], required=True,
|
|
|
|
deprecated_aliases=[dict(name='username', version='6.0.0', collection_name='community.general')],
|
|
|
|
),
|
2020-03-09 10:11:07 +01:00
|
|
|
name=dict(type='str', required=True),
|
|
|
|
value=dict(type='str'),
|
|
|
|
secured=dict(type='bool', default=False),
|
|
|
|
state=dict(type='str', choices=['present', 'absent'], required=True),
|
|
|
|
)
|
2021-01-14 15:27:15 +01:00
|
|
|
module = BitBucketPipelineVariable(
|
2020-03-09 10:11:07 +01:00
|
|
|
argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True,
|
2021-10-31 19:09:25 +01:00
|
|
|
required_one_of=BitbucketHelper.bitbucket_required_one_of(),
|
|
|
|
required_together=BitbucketHelper.bitbucket_required_together(),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
bitbucket = BitbucketHelper(module)
|
|
|
|
|
|
|
|
value = module.params['value']
|
|
|
|
state = module.params['state']
|
|
|
|
secured = module.params['secured']
|
|
|
|
|
|
|
|
# Check parameters
|
|
|
|
if (value is None) and (state == 'present'):
|
|
|
|
module.fail_json(msg=error_messages['required_value'])
|
|
|
|
|
|
|
|
# Retrieve access token for authorized API requests
|
|
|
|
bitbucket.fetch_access_token()
|
|
|
|
|
|
|
|
# Retrieve existing pipeline variable (if any)
|
|
|
|
existing_variable = get_existing_pipeline_variable(module, bitbucket)
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
# Create new variable in case it doesn't exists
|
|
|
|
if not existing_variable and (state == 'present'):
|
|
|
|
if not module.check_mode:
|
|
|
|
create_pipeline_variable(module, bitbucket)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
# Update variable if it is secured or the old value does not match the new one
|
|
|
|
elif existing_variable and (state == 'present'):
|
|
|
|
if (existing_variable['secured'] != secured) or (existing_variable.get('value') != value):
|
|
|
|
if not module.check_mode:
|
|
|
|
update_pipeline_variable(module, bitbucket, existing_variable['uuid'])
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
# Delete variable
|
|
|
|
elif existing_variable and (state == 'absent'):
|
|
|
|
if not module.check_mode:
|
|
|
|
delete_pipeline_variable(module, bitbucket, existing_variable['uuid'])
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|