1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-09 21:47:17 +00:00

Initial commit

This commit is contained in:
Ansible Core Team 2020-03-09 09:11:07 +00:00
commit aebc1b03fd
4861 changed files with 812621 additions and 0 deletions

View file

@ -0,0 +1,342 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2017 Radware LTD.
#
# This file is part of Ansible
#
# Ansible 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.
#
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'metadata_version': '1.1'}
DOCUMENTATION = '''
module: vdirect_commit
author: Evgeny Fedoruk @ Radware LTD (@evgenyfedoruk)
short_description: Commits pending configuration changes on Radware devices
description:
- Commits pending configuration changes on one or more Radware devices via vDirect server.
- For Alteon ADC device, apply, sync and save actions will be performed by default.
Skipping of an action is possible by explicit parameter specifying.
- For Alteon VX Container device, no sync operation will be performed
since sync action is only relevant for Alteon ADC devices.
- For DefensePro and AppWall devices, a bulk commit action will be performed.
Explicit apply, sync and save actions specifying is not relevant.
notes:
- Requires the Radware vdirect-client Python package on the host. This is as easy as
C(pip install vdirect-client)
options:
vdirect_ip:
description:
- Primary vDirect server IP address, may be set as C(VDIRECT_IP) environment variable.
required: true
vdirect_user:
description:
- vDirect server username, may be set as C(VDIRECT_USER) environment variable.
required: true
vdirect_password:
description:
- vDirect server password, may be set as C(VDIRECT_PASSWORD) environment variable.
required: true
vdirect_secondary_ip:
description:
- Secondary vDirect server IP address, may be set as C(VDIRECT_SECONDARY_IP) environment variable.
vdirect_wait:
description:
- Wait for async operation to complete, may be set as C(VDIRECT_WAIT) environment variable.
type: bool
default: 'yes'
vdirect_https_port:
description:
- vDirect server HTTPS port number, may be set as C(VDIRECT_HTTPS_PORT) environment variable.
default: 2189
vdirect_http_port:
description:
- vDirect server HTTP port number, may be set as C(VDIRECT_HTTP_PORT) environment variable.
default: 2188
vdirect_timeout:
description:
- Amount of time to wait for async operation completion [seconds],
- may be set as C(VDIRECT_TIMEOUT) environment variable.
default: 60
vdirect_use_ssl:
description:
- If C(no), an HTTP connection will be used instead of the default HTTPS connection,
- may be set as C(VDIRECT_HTTPS) or C(VDIRECT_USE_SSL) environment variable.
type: bool
default: 'yes'
validate_certs:
description:
- If C(no), SSL certificates will not be validated,
- may be set as C(VDIRECT_VALIDATE_CERTS) or C(VDIRECT_VERIFY) environment variable.
- This should only set to C(no) used on personally controlled sites using self-signed certificates.
type: bool
default: 'yes'
aliases: [ vdirect_validate_certs ]
devices:
description:
- List of Radware Alteon device names for commit operations.
required: true
apply:
description:
- If C(no), apply action will not be performed. Relevant for ADC devices only.
type: bool
default: 'yes'
save:
description:
- If C(no), save action will not be performed. Relevant for ADC devices only.
type: bool
default: 'yes'
sync:
description:
- If C(no), sync action will not be performed. Relevant for ADC devices only.
type: bool
default: 'yes'
requirements:
- "vdirect-client >= 4.9.0-post4"
'''
EXAMPLES = '''
- name: vdirect_commit
vdirect_commit:
vdirect_ip: 10.10.10.10
vdirect_user: vDirect
vdirect_password: radware
devices: ['dev1', 'dev2']
sync: no
'''
RETURN = '''
result:
description: Message detailing actions result
returned: success
type: str
sample: "Requested actions were successfully performed on all devices."
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback
try:
from vdirect_client import rest_client
HAS_REST_CLIENT = True
except ImportError:
HAS_REST_CLIENT = False
SUCCESS = 'Requested actions were successfully performed on all devices.'
FAILURE = 'Failure occurred while performing requested actions on devices. See details'
ADC_DEVICE_TYPE = 'Adc'
CONTAINER_DEVICE_TYPE = 'Container'
PARTITIONED_CONTAINER_DEVICE_TYPE = 'AlteonPartitioned'
APPWALL_DEVICE_TYPE = 'AppWall'
DP_DEVICE_TYPE = 'DefensePro'
SUCCEEDED = 'succeeded'
FAILED = 'failed'
NOT_PERFORMED = 'not performed'
meta_args = dict(
vdirect_ip=dict(required=True, fallback=(env_fallback, ['VDIRECT_IP'])),
vdirect_user=dict(required=True, fallback=(env_fallback, ['VDIRECT_USER'])),
vdirect_password=dict(
required=True, fallback=(env_fallback, ['VDIRECT_PASSWORD']),
no_log=True, type='str'),
vdirect_secondary_ip=dict(
required=False, fallback=(env_fallback, ['VDIRECT_SECONDARY_IP']),
default=None),
vdirect_use_ssl=dict(
required=False, fallback=(env_fallback, ['VDIRECT_HTTPS', 'VDIRECT_USE_SSL']),
default=True, type='bool'),
vdirect_wait=dict(
required=False, fallback=(env_fallback, ['VDIRECT_WAIT']),
default=True, type='bool'),
vdirect_timeout=dict(
required=False, fallback=(env_fallback, ['VDIRECT_TIMEOUT']),
default=60, type='int'),
validate_certs=dict(
required=False, fallback=(env_fallback, ['VDIRECT_VERIFY', 'VDIRECT_VALIDATE_CERTS']),
default=True, type='bool', aliases=['vdirect_validate_certs']),
vdirect_https_port=dict(
required=False, fallback=(env_fallback, ['VDIRECT_HTTPS_PORT']),
default=2189, type='int'),
vdirect_http_port=dict(
required=False, fallback=(env_fallback, ['VDIRECT_HTTP_PORT']),
default=2188, type='int'),
devices=dict(
required=True, type='list'),
apply=dict(
required=False, default=True, type='bool'),
save=dict(
required=False, default=True, type='bool'),
sync=dict(
required=False, default=True, type='bool'),
)
class CommitException(Exception):
def __init__(self, reason, details):
self.reason = reason
self.details = details
def __str__(self):
return 'Reason: {0}. Details:{1}.'.format(self.reason, self.details)
class MissingDeviceException(CommitException):
def __init__(self, device_name):
super(MissingDeviceException, self).__init__(
'Device missing',
'Device ' + repr(device_name) + ' does not exist')
class VdirectCommit(object):
def __init__(self, params):
self.client = rest_client.RestClient(params['vdirect_ip'],
params['vdirect_user'],
params['vdirect_password'],
wait=params['vdirect_wait'],
secondary_vdirect_ip=params['vdirect_secondary_ip'],
https_port=params['vdirect_https_port'],
http_port=params['vdirect_http_port'],
timeout=params['vdirect_timeout'],
https=params['vdirect_use_ssl'],
verify=params['validate_certs'])
self.devices = params['devices']
self.apply = params['apply']
self.save = params['save']
self.sync = params['sync']
self.devicesMap = {}
def _validate_devices(self):
for device in self.devices:
try:
res = self.client.adc.get(device)
if res[rest_client.RESP_STATUS] == 200:
self.devicesMap.update({device: ADC_DEVICE_TYPE})
continue
res = self.client.container.get(device)
if res[rest_client.RESP_STATUS] == 200:
if res[rest_client.RESP_DATA]['type'] == PARTITIONED_CONTAINER_DEVICE_TYPE:
self.devicesMap.update({device: CONTAINER_DEVICE_TYPE})
continue
res = self.client.appWall.get(device)
if res[rest_client.RESP_STATUS] == 200:
self.devicesMap.update({device: APPWALL_DEVICE_TYPE})
continue
res = self.client.defensePro.get(device)
if res[rest_client.RESP_STATUS] == 200:
self.devicesMap.update({device: DP_DEVICE_TYPE})
continue
except Exception as e:
raise CommitException('Failed to communicate with device ' + device, str(e))
raise MissingDeviceException(device)
def _perform_action_and_update_result(self, device, action, perform, failure_occurred, actions_result):
if not perform or failure_occurred:
actions_result[action] = NOT_PERFORMED
return True
try:
if self.devicesMap[device] == ADC_DEVICE_TYPE:
res = self.client.adc.control_device(device, action)
elif self.devicesMap[device] == CONTAINER_DEVICE_TYPE:
res = self.client.container.control(device, action)
elif self.devicesMap[device] == APPWALL_DEVICE_TYPE:
res = self.client.appWall.control_device(device, action)
elif self.devicesMap[device] == DP_DEVICE_TYPE:
res = self.client.defensePro.control_device(device, action)
if res[rest_client.RESP_STATUS] in [200, 204]:
actions_result[action] = SUCCEEDED
else:
actions_result[action] = FAILED
actions_result['failure_description'] = res[rest_client.RESP_STR]
return False
except Exception as e:
actions_result[action] = FAILED
actions_result['failure_description'] = 'Exception occurred while performing '\
+ action + ' action. Exception: ' + str(e)
return False
return True
def commit(self):
self._validate_devices()
result_to_return = dict()
result_to_return['details'] = list()
for device in self.devices:
failure_occurred = False
device_type = self.devicesMap[device]
actions_result = dict()
actions_result['device_name'] = device
actions_result['device_type'] = device_type
if device_type in [DP_DEVICE_TYPE, APPWALL_DEVICE_TYPE]:
failure_occurred = not self._perform_action_and_update_result(
device, 'commit', True, failure_occurred, actions_result)\
or failure_occurred
else:
failure_occurred = not self._perform_action_and_update_result(
device, 'apply', self.apply, failure_occurred, actions_result)\
or failure_occurred
if device_type != CONTAINER_DEVICE_TYPE:
failure_occurred = not self._perform_action_and_update_result(
device, 'sync', self.sync, failure_occurred, actions_result)\
or failure_occurred
failure_occurred = not self._perform_action_and_update_result(
device, 'save', self.save, failure_occurred, actions_result)\
or failure_occurred
result_to_return['details'].extend([actions_result])
if failure_occurred:
result_to_return['msg'] = FAILURE
if 'msg' not in result_to_return:
result_to_return['msg'] = SUCCESS
return result_to_return
def main():
module = AnsibleModule(argument_spec=meta_args)
if not HAS_REST_CLIENT:
module.fail_json(msg="The python vdirect-client module is required")
try:
vdirect_commit = VdirectCommit(module.params)
result = vdirect_commit.commit()
result = dict(result=result)
module.exit_json(**result)
except Exception as e:
module.fail_json(msg=str(e))
if __name__ == '__main__':
main()

View file

@ -0,0 +1,243 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2017 Radware LTD.
#
# This file is part of Ansible
#
# Ansible 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.
#
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'metadata_version': '1.1'}
DOCUMENTATION = '''
module: vdirect_file
author: Evgeny Fedoruk @ Radware LTD (@evgenyfedoruk)
short_description: Uploads a new or updates an existing runnable file into Radware vDirect server
description:
- Uploads a new or updates an existing configuration template or workflow template into the Radware vDirect server.
All parameters may be set as environment variables.
notes:
- Requires the Radware vdirect-client Python package on the host. This is as easy as
C(pip install vdirect-client)
options:
vdirect_ip:
description:
- Primary vDirect server IP address, may be set as VDIRECT_IP environment variable.
required: true
vdirect_user:
description:
- vDirect server username, may be set as VDIRECT_USER environment variable.
required: true
vdirect_password:
description:
- vDirect server password, may be set as VDIRECT_PASSWORD environment variable.
required: true
vdirect_secondary_ip:
description:
- Secondary vDirect server IP address, may be set as VDIRECT_SECONDARY_IP environment variable.
vdirect_wait:
description:
- Wait for async operation to complete, may be set as VDIRECT_WAIT environment variable.
type: bool
default: 'yes'
vdirect_https_port:
description:
- vDirect server HTTPS port number, may be set as VDIRECT_HTTPS_PORT environment variable.
default: 2189
vdirect_http_port:
description:
- vDirect server HTTP port number, may be set as VDIRECT_HTTP_PORT environment variable.
default: 2188
vdirect_timeout:
description:
- Amount of time to wait for async operation completion [seconds],
- may be set as VDIRECT_TIMEOUT environment variable.
default: 60
vdirect_use_ssl:
description:
- If C(no), an HTTP connection will be used instead of the default HTTPS connection,
- may be set as VDIRECT_HTTPS or VDIRECT_USE_SSL environment variable.
type: bool
default: 'yes'
validate_certs:
description:
- If C(no), SSL certificates will not be validated,
- may be set as VDIRECT_VALIDATE_CERTS or VDIRECT_VERIFY environment variable.
- This should only set to C(no) used on personally controlled sites using self-signed certificates.
type: bool
default: 'yes'
aliases: [ vdirect_validate_certs ]
file_name:
description:
- vDirect runnable file name to be uploaded.
- May be velocity configuration template (.vm) or workflow template zip file (.zip).
required: true
requirements:
- "vdirect-client >= 4.9.0-post4"
'''
EXAMPLES = '''
- name: vdirect_file
vdirect_file:
vdirect_ip: 10.10.10.10
vdirect_user: vDirect
vdirect_password: radware
file_name: /tmp/get_vlans.vm
'''
RETURN = '''
result:
description: Message detailing upload result
returned: success
type: str
sample: "Workflow template created"
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback
import os
import os.path
try:
from vdirect_client import rest_client
HAS_REST_CLIENT = True
except ImportError:
HAS_REST_CLIENT = False
TEMPLATE_EXTENSION = '.vm'
WORKFLOW_EXTENSION = '.zip'
WRONG_EXTENSION_ERROR = 'The file_name parameter must have ' \
'velocity script (.vm) extension or ZIP archive (.zip) extension'
CONFIGURATION_TEMPLATE_CREATED_SUCCESS = 'Configuration template created'
CONFIGURATION_TEMPLATE_UPDATED_SUCCESS = 'Configuration template updated'
WORKFLOW_TEMPLATE_CREATED_SUCCESS = 'Workflow template created'
WORKFLOW_TEMPLATE_UPDATED_SUCCESS = 'Workflow template updated'
meta_args = dict(
vdirect_ip=dict(required=True, fallback=(env_fallback, ['VDIRECT_IP'])),
vdirect_user=dict(required=True, fallback=(env_fallback, ['VDIRECT_USER'])),
vdirect_password=dict(
required=True, fallback=(env_fallback, ['VDIRECT_PASSWORD']),
no_log=True, type='str'),
vdirect_secondary_ip=dict(
required=False, fallback=(env_fallback, ['VDIRECT_SECONDARY_IP']),
default=None),
vdirect_use_ssl=dict(
required=False, fallback=(env_fallback, ['VDIRECT_HTTPS', 'VDIRECT_USE_SSL']),
default=True, type='bool'),
vdirect_wait=dict(
required=False, fallback=(env_fallback, ['VDIRECT_WAIT']),
default=True, type='bool'),
vdirect_timeout=dict(
required=False, fallback=(env_fallback, ['VDIRECT_TIMEOUT']),
default=60, type='int'),
validate_certs=dict(
required=False, fallback=(env_fallback, ['VDIRECT_VERIFY', 'VDIRECT_VALIDATE_CERTS']),
default=True, type='bool', aliases=['vdirect_validate_certs']),
vdirect_https_port=dict(
required=False, fallback=(env_fallback, ['VDIRECT_HTTPS_PORT']),
default=2189, type='int'),
vdirect_http_port=dict(
required=False, fallback=(env_fallback, ['VDIRECT_HTTP_PORT']),
default=2188, type='int'),
file_name=dict(required=True)
)
class FileException(Exception):
def __init__(self, reason, details):
self.reason = reason
self.details = details
def __str__(self):
return 'Reason: {0}. Details:{1}.'.format(self.reason, self.details)
class InvalidSourceException(FileException):
def __init__(self, message):
super(InvalidSourceException, self).__init__(
'Error parsing file', repr(message))
class VdirectFile(object):
def __init__(self, params):
self.client = rest_client.RestClient(params['vdirect_ip'],
params['vdirect_user'],
params['vdirect_password'],
wait=params['vdirect_wait'],
secondary_vdirect_ip=params['vdirect_secondary_ip'],
https_port=params['vdirect_https_port'],
http_port=params['vdirect_http_port'],
timeout=params['vdirect_timeout'],
https=params['vdirect_use_ssl'],
verify=params['validate_certs'])
def upload(self, fqn):
if fqn.endswith(TEMPLATE_EXTENSION):
template_name = os.path.basename(fqn)
template = rest_client.Template(self.client)
runnable_file = open(fqn, 'r')
file_content = runnable_file.read()
result_to_return = CONFIGURATION_TEMPLATE_CREATED_SUCCESS
result = template.create_from_source(file_content, template_name, fail_if_invalid=True)
if result[rest_client.RESP_STATUS] == 409:
result_to_return = CONFIGURATION_TEMPLATE_UPDATED_SUCCESS
result = template.upload_source(file_content, template_name, fail_if_invalid=True)
if result[rest_client.RESP_STATUS] == 400:
raise InvalidSourceException(str(result[rest_client.RESP_STR]))
elif fqn.endswith(WORKFLOW_EXTENSION):
workflow = rest_client.WorkflowTemplate(self.client)
runnable_file = open(fqn, 'rb')
file_content = runnable_file.read()
result_to_return = WORKFLOW_TEMPLATE_CREATED_SUCCESS
result = workflow.create_template_from_archive(file_content, fail_if_invalid=True)
if result[rest_client.RESP_STATUS] == 409:
result_to_return = WORKFLOW_TEMPLATE_UPDATED_SUCCESS
result = workflow.update_archive(file_content, os.path.splitext(os.path.basename(fqn))[0])
if result[rest_client.RESP_STATUS] == 400:
raise InvalidSourceException(str(result[rest_client.RESP_STR]))
else:
result_to_return = WRONG_EXTENSION_ERROR
return result_to_return
def main():
module = AnsibleModule(argument_spec=meta_args)
if not HAS_REST_CLIENT:
module.fail_json(msg="The python vdirect-client module is required")
try:
vdirect_file = VdirectFile(module.params)
result = vdirect_file.upload(module.params['file_name'])
result = dict(result=result)
module.exit_json(**result)
except Exception as e:
module.fail_json(msg=str(e))
if __name__ == '__main__':
main()

View file

@ -0,0 +1,340 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2017 Radware LTD.
#
# This file is part of Ansible
#
# Ansible 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.
#
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'metadata_version': '1.1'}
DOCUMENTATION = '''
module: vdirect_runnable
author: Evgeny Fedoruk @ Radware LTD (@evgenyfedoruk)
short_description: Runs templates and workflow actions in Radware vDirect server
description:
- Runs configuration templates, creates workflows and runs workflow actions in Radware vDirect server.
notes:
- Requires the Radware vdirect-client Python package on the host. This is as easy as
C(pip install vdirect-client)
options:
vdirect_ip:
description:
- Primary vDirect server IP address, may be set as C(VDIRECT_IP) environment variable.
required: true
vdirect_user:
description:
- vDirect server username, may be set as C(VDIRECT_USER) environment variable.
required: true
vdirect_password:
description:
- vDirect server password, may be set as C(VDIRECT_PASSWORD) environment variable.
required: true
vdirect_secondary_ip:
description:
- Secondary vDirect server IP address, may be set as C(VDIRECT_SECONDARY_IP) environment variable.
vdirect_wait:
description:
- Wait for async operation to complete, may be set as C(VDIRECT_WAIT) environment variable.
type: bool
default: 'yes'
vdirect_https_port:
description:
- vDirect server HTTPS port number, may be set as C(VDIRECT_HTTPS_PORT) environment variable.
default: 2189
vdirect_http_port:
description:
- vDirect server HTTP port number, may be set as C(VDIRECT_HTTP_PORT) environment variable.
default: 2188
vdirect_timeout:
description:
- Amount of time to wait for async operation completion [seconds],
- may be set as C(VDIRECT_TIMEOUT) environment variable.
default: 60
vdirect_use_ssl:
description:
- If C(no), an HTTP connection will be used instead of the default HTTPS connection,
- may be set as C(VDIRECT_HTTPS) or C(VDIRECT_USE_SSL) environment variable.
type: bool
default: 'yes'
validate_certs:
description:
- If C(no), SSL certificates will not be validated,
- may be set as C(VDIRECT_VALIDATE_CERTS) or C(VDIRECT_VERIFY) environment variable.
- This should only set to C(no) used on personally controlled sites using self-signed certificates.
type: bool
default: 'yes'
aliases: [ vdirect_validate_certs ]
runnable_type:
description:
- vDirect runnable type.
required: true
choices: ['ConfigurationTemplate', 'Workflow', 'WorkflowTemplate', 'Plugin']
runnable_name:
description:
- vDirect runnable name to run.
- May be configuration template name, workflow template name or workflow instance name.
required: true
action_name:
description:
- Workflow action name to run.
- Required if I(runnable_type=Workflow).
parameters:
description:
- Action parameters dictionary. In case of C(ConfigurationTemplate) runnable type,
- the device connection details should always be passed as a parameter.
requirements:
- "vdirect-client >= 4.9.0-post4"
'''
EXAMPLES = '''
- name: vdirect_runnable
vdirect_runnable:
vdirect_ip: 10.10.10.10
vdirect_user: vDirect
vdirect_password: radware
runnable_type: ConfigurationTemplate
runnable_name: get_vlans
parameters: {'vlans_needed':1,'adc':[{'type':'Adc','name':'adc-1'}]}
'''
RETURN = '''
result:
description: Message detailing run result
returned: success
type: str
sample: "Workflow action run completed."
'''
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import env_fallback
try:
from vdirect_client import rest_client
HAS_REST_CLIENT = True
except ImportError:
HAS_REST_CLIENT = False
CONFIGURATION_TEMPLATE_RUNNABLE_TYPE = 'ConfigurationTemplate'
WORKFLOW_TEMPLATE_RUNNABLE_TYPE = 'WorkflowTemplate'
WORKFLOW_RUNNABLE_TYPE = 'Workflow'
PLUGIN_RUNNABLE_TYPE = 'Plugin'
TEMPLATE_SUCCESS = 'Configuration template run completed.'
WORKFLOW_CREATION_SUCCESS = 'Workflow created.'
WORKFLOW_ACTION_SUCCESS = 'Workflow action run completed.'
PLUGIN_ACTION_SUCCESS = 'Plugin action run completed.'
meta_args = dict(
vdirect_ip=dict(required=True, fallback=(env_fallback, ['VDIRECT_IP'])),
vdirect_user=dict(required=True, fallback=(env_fallback, ['VDIRECT_USER'])),
vdirect_password=dict(
required=True, fallback=(env_fallback, ['VDIRECT_PASSWORD']),
no_log=True, type='str'),
vdirect_secondary_ip=dict(
required=False, fallback=(env_fallback, ['VDIRECT_SECONDARY_IP']),
default=None),
vdirect_use_ssl=dict(
required=False, fallback=(env_fallback, ['VDIRECT_HTTPS', 'VDIRECT_USE_SSL']),
default=True, type='bool'),
vdirect_wait=dict(
required=False, fallback=(env_fallback, ['VDIRECT_WAIT']),
default=True, type='bool'),
vdirect_timeout=dict(
required=False, fallback=(env_fallback, ['VDIRECT_TIMEOUT']),
default=60, type='int'),
validate_certs=dict(
required=False, fallback=(env_fallback, ['VDIRECT_VERIFY', 'VDIRECT_VALIDATE_CERTS']),
default=True, type='bool', aliases=['vdirect_validate_certs']),
vdirect_https_port=dict(
required=False, fallback=(env_fallback, ['VDIRECT_HTTPS_PORT']),
default=2189, type='int'),
vdirect_http_port=dict(
required=False, fallback=(env_fallback, ['VDIRECT_HTTP_PORT']),
default=2188, type='int'),
runnable_type=dict(
required=True,
choices=[CONFIGURATION_TEMPLATE_RUNNABLE_TYPE, WORKFLOW_TEMPLATE_RUNNABLE_TYPE, WORKFLOW_RUNNABLE_TYPE, PLUGIN_RUNNABLE_TYPE]),
runnable_name=dict(required=True),
action_name=dict(required=False, default=None),
parameters=dict(required=False, type='dict', default={})
)
class RunnableException(Exception):
def __init__(self, reason, details):
self.reason = reason
self.details = details
def __str__(self):
return 'Reason: {0}. Details:{1}.'.format(self.reason, self.details)
class WrongActionNameException(RunnableException):
def __init__(self, action, available_actions):
super(WrongActionNameException, self).__init__('Wrong action name ' + repr(action),
'Available actions are: ' + repr(available_actions))
class MissingActionParametersException(RunnableException):
def __init__(self, required_parameters):
super(MissingActionParametersException, self).__init__(
'Action parameters missing',
'Required parameters are: ' + repr(required_parameters))
class MissingRunnableException(RunnableException):
def __init__(self, name):
super(MissingRunnableException, self).__init__(
'Runnable missing',
'Runnable ' + name + ' is missing')
class VdirectRunnable(object):
CREATE_WORKFLOW_ACTION = 'createWorkflow'
RUN_ACTION = 'run'
def __init__(self, params):
self.client = rest_client.RestClient(params['vdirect_ip'],
params['vdirect_user'],
params['vdirect_password'],
wait=params['vdirect_wait'],
secondary_vdirect_ip=params['vdirect_secondary_ip'],
https_port=params['vdirect_https_port'],
http_port=params['vdirect_http_port'],
timeout=params['vdirect_timeout'],
strict_http_results=True,
https=params['vdirect_use_ssl'],
verify=params['validate_certs'])
self.params = params
self.type = self.params['runnable_type']
self.name = self.params['runnable_name']
if self.type == WORKFLOW_TEMPLATE_RUNNABLE_TYPE:
self.action_name = VdirectRunnable.CREATE_WORKFLOW_ACTION
elif self.type == CONFIGURATION_TEMPLATE_RUNNABLE_TYPE:
self.action_name = VdirectRunnable.RUN_ACTION
else:
self.action_name = self.params['action_name']
if 'parameters' in self.params and self.params['parameters']:
self.action_params = self.params['parameters']
else:
self.action_params = {}
def _validate_runnable_exists(self):
if self.type == WORKFLOW_RUNNABLE_TYPE:
res = self.client.runnable.get_runnable_objects(self.type)
runnable_names = res[rest_client.RESP_DATA]['names']
if self.name not in runnable_names:
raise MissingRunnableException(self.name)
else:
try:
self.client.catalog.get_catalog_item(self.type, self.name)
except rest_client.RestClientException:
raise MissingRunnableException(self.name)
def _validate_action_name(self):
if self.type in [WORKFLOW_RUNNABLE_TYPE, PLUGIN_RUNNABLE_TYPE]:
res = self.client.runnable.get_available_actions(self.type, self.name)
available_actions = res[rest_client.RESP_DATA]['names']
if self.action_name not in available_actions:
raise WrongActionNameException(self.action_name, available_actions)
def _validate_required_action_params(self):
action_params_names = [n for n in self.action_params]
res = self.client.runnable.get_action_info(self.type, self.name, self.action_name)
if 'parameters' in res[rest_client.RESP_DATA]:
action_params_spec = res[rest_client.RESP_DATA]['parameters']
else:
action_params_spec = []
required_action_params_dict = [{'name': p['name'], 'type': p['type']} for p in action_params_spec
if p['type'] == 'alteon' or
p['type'] == 'defensePro' or
p['type'] == 'appWall' or
p['type'] == 'alteon[]' or
p['type'] == 'defensePro[]' or
p['type'] == 'appWall[]' or
p['direction'] != 'out']
required_action_params_names = [n['name'] for n in required_action_params_dict]
if set(required_action_params_names) & set(action_params_names) != set(required_action_params_names):
raise MissingActionParametersException(required_action_params_dict)
def run(self):
self._validate_runnable_exists()
self._validate_action_name()
self._validate_required_action_params()
data = self.action_params
result = self.client.runnable.run(data, self.type, self.name, self.action_name)
result_to_return = {'msg': ''}
if result[rest_client.RESP_STATUS] == 200:
if result[rest_client.RESP_DATA]['success']:
if self.type == WORKFLOW_TEMPLATE_RUNNABLE_TYPE:
result_to_return['msg'] = WORKFLOW_CREATION_SUCCESS
elif self.type == CONFIGURATION_TEMPLATE_RUNNABLE_TYPE:
result_to_return['msg'] = TEMPLATE_SUCCESS
elif self.type == PLUGIN_RUNNABLE_TYPE:
result_to_return['msg'] = PLUGIN_ACTION_SUCCESS
else:
result_to_return['msg'] = WORKFLOW_ACTION_SUCCESS
result_to_return['output'] = result[rest_client.RESP_DATA]
else:
if 'exception' in result[rest_client.RESP_DATA]:
raise RunnableException(result[rest_client.RESP_DATA]['exception']['message'],
result[rest_client.RESP_STR])
else:
raise RunnableException('The status returned ' + str(result[rest_client.RESP_DATA]['status']),
result[rest_client.RESP_STR])
else:
raise RunnableException(result[rest_client.RESP_REASON],
result[rest_client.RESP_STR])
return result_to_return
def main():
module = AnsibleModule(argument_spec=meta_args,
required_if=[['runnable_type', WORKFLOW_RUNNABLE_TYPE, ['action_name']],
['runnable_type', PLUGIN_RUNNABLE_TYPE, ['action_name']]])
if not HAS_REST_CLIENT:
module.fail_json(msg="The python vdirect-client module is required")
try:
vdirect_runnable = VdirectRunnable(module.params)
result = vdirect_runnable.run()
result = dict(result=result)
module.exit_json(**result)
except Exception as e:
module.fail_json(msg=str(e))
if __name__ == '__main__':
main()