Module for uploading templates into vDirect server (#27570)
* Module for uploading templates into vDirect server Module for uploading configuration and workflow templates into Radware vDirect server * Module for uploading templates into vDirect server Module for uploading configuration and workflow templates into Radware vDirect server
This commit is contained in:
parent
c3b2a48972
commit
71313ce04d
6 changed files with 471 additions and 0 deletions
0
lib/ansible/modules/network/radware/__init__.py
Normal file
0
lib/ansible/modules/network/radware/__init__.py
Normal file
236
lib/ansible/modules/network/radware/vdirect_file.py
Normal file
236
lib/ansible/modules/network/radware/vdirect_file.py
Normal file
|
@ -0,0 +1,236 @@
|
|||
#!/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)
|
||||
version_added: "2.4"
|
||||
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
|
||||
default: None
|
||||
vdirect_password:
|
||||
description:
|
||||
- vDirect server password, may be set as VDIRECT_PASSWORD environment variable.
|
||||
required: true
|
||||
default: None
|
||||
vdirect_secondary_ip:
|
||||
description:
|
||||
- Secondary vDirect server IP address, may be set as VDIRECT_SECONDARY_IP environment variable.
|
||||
required: false
|
||||
default: None
|
||||
vdirect_wait:
|
||||
description:
|
||||
- Wait for async operation to complete, may be set as VDIRECT_WAIT environment variable.
|
||||
required: false
|
||||
type: bool
|
||||
default: 'yes'
|
||||
vdirect_https_port:
|
||||
description:
|
||||
- vDirect server HTTPS port number, may be set as VDIRECT_HTTPS_PORT environment variable.
|
||||
required: false
|
||||
default: 2189
|
||||
vdirect_http_port:
|
||||
description:
|
||||
- vDirect server HTTP port number, may be set as VDIRECT_HTTP_PORT environment variable.
|
||||
required: false
|
||||
default: 2188
|
||||
vdirect_timeout:
|
||||
description:
|
||||
- Amount of time to wait for async operation completion [seconds],
|
||||
- may be set as VDIRECT_TIMEOUT environment variable.
|
||||
required: false
|
||||
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.
|
||||
required: false
|
||||
type: bool
|
||||
default: 'yes'
|
||||
vdirect_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.
|
||||
required: false
|
||||
type: bool
|
||||
default: 'yes'
|
||||
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.1.1"
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: vdirect_file
|
||||
vdirect_file:
|
||||
vdirect_primary_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: string
|
||||
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']),
|
||||
default=None),
|
||||
vdirect_user=dict(
|
||||
required=True, fallback=(env_fallback, ['VDIRECT_USER']),
|
||||
default=None),
|
||||
vdirect_password=dict(
|
||||
required=True, fallback=(env_fallback, ['VDIRECT_PASSWORD']),
|
||||
default=None, 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'),
|
||||
vdirect_validate_certs=dict(
|
||||
required=False, fallback=(env_fallback, ['VDIRECT_VERIFY', 'VDIRECT_VALIDATE_CERTS']),
|
||||
default=True, type='bool'),
|
||||
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, default=None)
|
||||
)
|
||||
|
||||
|
||||
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['vdirect_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 = template.create_from_source(file_content, template_name, fail_if_invalid=True)
|
||||
if result[rest_client.RESP_STATUS] == 409:
|
||||
template.upload_source(file_content, template_name, fail_if_invalid=True)
|
||||
result = CONFIGURATION_TEMPLATE_UPDATED_SUCCESS
|
||||
else:
|
||||
result = CONFIGURATION_TEMPLATE_CREATED_SUCCESS
|
||||
elif fqn.endswith(WORKFLOW_EXTENSION):
|
||||
workflow = rest_client.WorkflowTemplate(self.client)
|
||||
|
||||
runnable_file = open(fqn, 'rb')
|
||||
file_content = runnable_file.read()
|
||||
result = workflow.create_template_from_archive(file_content, fail_if_invalid=True)
|
||||
if result[rest_client.RESP_STATUS] == 409:
|
||||
workflow.update_archive(file_content, os.path.splitext(os.path.basename(fqn))[0])
|
||||
result = WORKFLOW_TEMPLATE_UPDATED_SUCCESS
|
||||
else:
|
||||
result = WORKFLOW_TEMPLATE_CREATED_SUCCESS
|
||||
else:
|
||||
result = WRONG_EXTENSION_ERROR
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
if not HAS_REST_CLIENT:
|
||||
raise ImportError("The python vdirect-client module is required")
|
||||
|
||||
module = AnsibleModule(argument_spec=meta_args)
|
||||
|
||||
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()
|
0
test/units/modules/network/radware/__init__.py
Normal file
0
test/units/modules/network/radware/__init__.py
Normal file
26
test/units/modules/network/radware/ct.vm
Normal file
26
test/units/modules/network/radware/ct.vm
Normal file
|
@ -0,0 +1,26 @@
|
|||
##-----------------------------------------------------------------------------
|
||||
## COPYRIGHT 2017, Radware Ltd. All Rights Reserved
|
||||
## THE USE, COPY OR INSTALLATION OF THIS/THESE FILE/FILES IS SUBJECT
|
||||
## TO THE RADWARE "END USER LICENSE AGREEMENT" A COPY OF WHICH
|
||||
## IS PROVIDED WITH THE PACKAGE THAT INCLUDES THIS FILE/FILES AND
|
||||
## CAN ALSO BE ACCESSED AT http://www.radware.com/Resources/eula.html
|
||||
##-----------------------------------------------------------------------------
|
||||
|
||||
#property('description', 'Ansible Test mock')
|
||||
|
||||
#param($p1, 'int', 'in')
|
||||
#param($p2, 'int[]', 'out')
|
||||
|
||||
#set($p2 = [])
|
||||
#set($start = 2)
|
||||
#set($end = 1024)
|
||||
#set($range = [$start..$end])
|
||||
|
||||
#foreach($i in $range)
|
||||
#set($j = $adc.readBean('MOCK', $i))
|
||||
#if ($adc.isEmpty($j))
|
||||
#set($dummy = $p2.add($i))
|
||||
#if ($p2.size() == $p1)
|
||||
#break
|
||||
#end
|
||||
#end
|
209
test/units/modules/network/radware/test_vdirect_file.py
Normal file
209
test/units/modules/network/radware/test_vdirect_file.py
Normal file
|
@ -0,0 +1,209 @@
|
|||
# -*- 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/>.
|
||||
|
||||
import os
|
||||
from mock import patch, MagicMock
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch
|
||||
|
||||
RESP_STATUS = 0
|
||||
RESP_REASON = 1
|
||||
RESP_STR = 2
|
||||
RESP_DATA = 3
|
||||
|
||||
NONE_PARAMS = {'vdirect_ip': None, 'vdirect_user': None, 'vdirect_password': None,
|
||||
'vdirect_wait': None, 'vdirect_secondary_ip': None,
|
||||
'vdirect_https_port': None, 'vdirect_http_port': None,
|
||||
'vdirect_timeout': None, 'vdirect_use_ssl': None, 'vdirect_validate_certs': None}
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.RestClient')
|
||||
class RestClient ():
|
||||
def __init__(self, vdirect_ip=None, vdirect_user=None, vdirect_password=None, wait=None,
|
||||
secondary_vdirect_ip=None, https_port=None, http_port=None,
|
||||
timeout=None, https=None, strict_http_results=None,
|
||||
verify=None):
|
||||
pass
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.Template')
|
||||
class Template ():
|
||||
create_from_source_result = None
|
||||
upload_source_result = None
|
||||
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@classmethod
|
||||
def set_create_from_source_result(cls, result):
|
||||
Template.create_from_source_result = result
|
||||
|
||||
@classmethod
|
||||
def set_upload_source_result(cls, result):
|
||||
Template.upload_source_result = result
|
||||
|
||||
def create_from_source(self, data, name=None, tenant=None, fail_if_invalid=False):
|
||||
return Template.create_from_source_result
|
||||
|
||||
def upload_source(self, data, name=None, tenant=None, fail_if_invalid=False):
|
||||
return Template.upload_source_result
|
||||
|
||||
|
||||
@patch('vdirect_client.rest_client.WorkflowTemplate')
|
||||
class WorkflowTemplate ():
|
||||
create_template_from_archive_result = None
|
||||
update_archive_result = None
|
||||
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
@classmethod
|
||||
def set_create_template_from_archive_result(cls, result):
|
||||
WorkflowTemplate.create_template_from_archive_result = result
|
||||
|
||||
@classmethod
|
||||
def set_update_archive_result(cls, result):
|
||||
WorkflowTemplate.update_archive_result = result
|
||||
|
||||
def create_template_from_archive(self, data, validate=False, fail_if_invalid=False, tenant=None):
|
||||
return WorkflowTemplate.create_template_from_archive_result
|
||||
|
||||
def update_archive(self, data, workflow_template_name):
|
||||
return WorkflowTemplate.update_archive_result
|
||||
|
||||
|
||||
class TestManager(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_missing_parameter(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible.modules.network.radware import vdirect_file
|
||||
|
||||
try:
|
||||
params = NONE_PARAMS.copy()
|
||||
del params['vdirect_ip']
|
||||
vdirect_file.VdirectFile(params)
|
||||
self.assertFalse("KeyError was not thrown for missing parameter")
|
||||
except KeyError:
|
||||
assert True
|
||||
|
||||
def test_wrong_file_extension(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible.modules.network.radware import vdirect_file
|
||||
|
||||
module_mock.RESP_STATUS = 0
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
result = file.upload("file.??")
|
||||
assert result == vdirect_file.WRONG_EXTENSION_ERROR
|
||||
|
||||
def test_missing_file(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible.modules.network.radware import vdirect_file
|
||||
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
try:
|
||||
file.upload("missing_file.vm")
|
||||
self.assertFalse("IOException was not thrown for missing file")
|
||||
except IOError:
|
||||
assert True
|
||||
|
||||
def test_template_upload_create_success(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible.modules.network.radware import vdirect_file
|
||||
vdirect_file.rest_client.RESP_STATUS = 0
|
||||
vdirect_file.rest_client.Template = Template
|
||||
|
||||
Template.set_create_from_source_result([400])
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
result = file.upload(os.path.join(path, "ct.vm"))
|
||||
self.assertEqual(result, vdirect_file.CONFIGURATION_TEMPLATE_CREATED_SUCCESS,
|
||||
'Unexpected result received:' + repr(result))
|
||||
|
||||
def test_template_upload_update_success(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible.modules.network.radware import vdirect_file
|
||||
vdirect_file.rest_client.RESP_STATUS = 0
|
||||
vdirect_file.rest_client.Template = Template
|
||||
|
||||
Template.set_create_from_source_result([409])
|
||||
Template.set_upload_source_result([400])
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
result = file.upload(os.path.join(path, "ct.vm"))
|
||||
self.assertEqual(result, vdirect_file.CONFIGURATION_TEMPLATE_UPDATED_SUCCESS,
|
||||
'Unexpected result received:' + repr(result))
|
||||
|
||||
def test_workflow_upload_create_success(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible.modules.network.radware import vdirect_file
|
||||
vdirect_file.rest_client.RESP_STATUS = 0
|
||||
vdirect_file.rest_client.WorkflowTemplate = WorkflowTemplate
|
||||
|
||||
WorkflowTemplate.set_create_template_from_archive_result([400])
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
result = file.upload(os.path.join(path, "wt.zip"))
|
||||
self.assertEqual(result, vdirect_file.WORKFLOW_TEMPLATE_CREATED_SUCCESS,
|
||||
'Unexpected result received:' + repr(result))
|
||||
|
||||
def test_workflow_upload_update_success(self, *args):
|
||||
module_mock = MagicMock()
|
||||
with patch.dict('sys.modules', **{
|
||||
'vdirect_client': module_mock,
|
||||
'vdirect_client.rest_client': module_mock,
|
||||
}):
|
||||
from ansible.modules.network.radware import vdirect_file
|
||||
vdirect_file.rest_client.RESP_STATUS = 0
|
||||
vdirect_file.rest_client.WorkflowTemplate = WorkflowTemplate
|
||||
|
||||
WorkflowTemplate.set_create_template_from_archive_result([409])
|
||||
WorkflowTemplate.set_update_archive_result([400])
|
||||
file = vdirect_file.VdirectFile(NONE_PARAMS)
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
result = file.upload(os.path.join(path, "wt.zip"))
|
||||
self.assertEqual(result, vdirect_file.WORKFLOW_TEMPLATE_UPDATED_SUCCESS,
|
||||
'Unexpected result received:' + repr(result))
|
0
test/units/modules/network/radware/wt.zip
Normal file
0
test/units/modules/network/radware/wt.zip
Normal file
Loading…
Reference in a new issue