|
|
@ -1,7 +1,7 @@
|
|
|
|
#!/usr/bin/python
|
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Copyright (c) 2017 F5 Networks Inc.
|
|
|
|
# Copyright: (c) 2017, F5 Networks Inc.
|
|
|
|
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# 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
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
@ -105,83 +105,91 @@ author:
|
|
|
|
EXAMPLES = r'''
|
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Import and activate ASM policy
|
|
|
|
- name: Import and activate ASM policy
|
|
|
|
bigip_asm_policy:
|
|
|
|
bigip_asm_policy:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
|
|
|
|
name: new_asm_policy
|
|
|
|
name: new_asm_policy
|
|
|
|
file: /root/asm_policy.xml
|
|
|
|
file: /root/asm_policy.xml
|
|
|
|
active: yes
|
|
|
|
active: yes
|
|
|
|
state: present
|
|
|
|
state: present
|
|
|
|
|
|
|
|
provider:
|
|
|
|
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
delegate_to: localhost
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
|
|
|
|
- name: Import ASM policy from template
|
|
|
|
- name: Import ASM policy from template
|
|
|
|
bigip_asm_policy:
|
|
|
|
bigip_asm_policy:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
|
|
|
|
name: new_sharepoint_policy
|
|
|
|
name: new_sharepoint_policy
|
|
|
|
template: SharePoint 2007 (http)
|
|
|
|
template: SharePoint 2007 (http)
|
|
|
|
state: present
|
|
|
|
state: present
|
|
|
|
|
|
|
|
provider:
|
|
|
|
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
delegate_to: localhost
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
|
|
|
|
- name: Create blank ASM policy
|
|
|
|
- name: Create blank ASM policy
|
|
|
|
bigip_asm_policy:
|
|
|
|
bigip_asm_policy:
|
|
|
|
|
|
|
|
name: new_blank_policy
|
|
|
|
|
|
|
|
state: present
|
|
|
|
|
|
|
|
provider:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
user: admin
|
|
|
|
password: secret
|
|
|
|
password: secret
|
|
|
|
name: new_blank_policy
|
|
|
|
|
|
|
|
state: present
|
|
|
|
|
|
|
|
delegate_to: localhost
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
|
|
|
|
- name: Create blank ASM policy and activate
|
|
|
|
- name: Create blank ASM policy and activate
|
|
|
|
bigip_asm_policy:
|
|
|
|
bigip_asm_policy:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
|
|
|
|
name: new_blank_policy
|
|
|
|
name: new_blank_policy
|
|
|
|
active: yes
|
|
|
|
active: yes
|
|
|
|
state: present
|
|
|
|
state: present
|
|
|
|
|
|
|
|
provider:
|
|
|
|
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
delegate_to: localhost
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
|
|
|
|
- name: Activate ASM policy
|
|
|
|
- name: Activate ASM policy
|
|
|
|
bigip_asm_policy:
|
|
|
|
bigip_asm_policy:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
|
|
|
|
name: inactive_policy
|
|
|
|
name: inactive_policy
|
|
|
|
active: yes
|
|
|
|
active: yes
|
|
|
|
state: present
|
|
|
|
state: present
|
|
|
|
|
|
|
|
provider:
|
|
|
|
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
delegate_to: localhost
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
|
|
|
|
- name: Deactivate ASM policy
|
|
|
|
- name: Deactivate ASM policy
|
|
|
|
bigip_asm_policy:
|
|
|
|
bigip_asm_policy:
|
|
|
|
|
|
|
|
name: active_policy
|
|
|
|
|
|
|
|
state: present
|
|
|
|
|
|
|
|
provider:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
server: lb.mydomain.com
|
|
|
|
user: admin
|
|
|
|
user: admin
|
|
|
|
password: secret
|
|
|
|
password: secret
|
|
|
|
name: active_policy
|
|
|
|
|
|
|
|
state: present
|
|
|
|
|
|
|
|
delegate_to: localhost
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
|
|
|
|
- name: Import and activate ASM policy in Role
|
|
|
|
- name: Import and activate ASM policy in Role
|
|
|
|
bigip_asm_policy:
|
|
|
|
bigip_asm_policy:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
|
|
|
|
name: new_asm_policy
|
|
|
|
name: new_asm_policy
|
|
|
|
file: "{{ role_path }}/files/asm_policy.xml"
|
|
|
|
file: "{{ role_path }}/files/asm_policy.xml"
|
|
|
|
active: yes
|
|
|
|
active: yes
|
|
|
|
state: present
|
|
|
|
state: present
|
|
|
|
|
|
|
|
provider:
|
|
|
|
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
delegate_to: localhost
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
|
|
|
|
- name: Import ASM binary policy
|
|
|
|
- name: Import ASM binary policy
|
|
|
|
bigip_asm_policy:
|
|
|
|
bigip_asm_policy:
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
|
|
|
|
name: new_asm_policy
|
|
|
|
name: new_asm_policy
|
|
|
|
file: "/root/asm_policy.plc"
|
|
|
|
file: "/root/asm_policy.plc"
|
|
|
|
active: yes
|
|
|
|
active: yes
|
|
|
|
state: present
|
|
|
|
state: present
|
|
|
|
|
|
|
|
provider:
|
|
|
|
|
|
|
|
server: lb.mydomain.com
|
|
|
|
|
|
|
|
user: admin
|
|
|
|
|
|
|
|
password: secret
|
|
|
|
delegate_to: localhost
|
|
|
|
delegate_to: localhost
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
@ -220,46 +228,52 @@ from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import env_fallback
|
|
|
|
from ansible.module_utils.basic import env_fallback
|
|
|
|
from distutils.version import LooseVersion
|
|
|
|
from distutils.version import LooseVersion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
from library.module_utils.network.f5.bigip import HAS_F5SDK
|
|
|
|
from library.module_utils.network.f5.bigip import F5RestClient
|
|
|
|
from library.module_utils.network.f5.bigip import F5Client
|
|
|
|
|
|
|
|
from library.module_utils.network.f5.common import F5ModuleError
|
|
|
|
from library.module_utils.network.f5.common import F5ModuleError
|
|
|
|
from library.module_utils.network.f5.common import AnsibleF5Parameters
|
|
|
|
from library.module_utils.network.f5.common import AnsibleF5Parameters
|
|
|
|
from library.module_utils.network.f5.common import cleanup_tokens
|
|
|
|
from library.module_utils.network.f5.common import cleanup_tokens
|
|
|
|
from library.module_utils.network.f5.common import fqdn_name
|
|
|
|
from library.module_utils.network.f5.common import fq_name
|
|
|
|
from library.module_utils.network.f5.common import f5_argument_spec
|
|
|
|
from library.module_utils.network.f5.common import f5_argument_spec
|
|
|
|
try:
|
|
|
|
from library.module_utils.network.f5.common import exit_json
|
|
|
|
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
|
|
|
from library.module_utils.network.f5.common import fail_json
|
|
|
|
except ImportError:
|
|
|
|
from library.module_utils.network.f5.icontrol import upload_file
|
|
|
|
HAS_F5SDK = False
|
|
|
|
from library.module_utils.network.f5.icontrol import tmos_version
|
|
|
|
|
|
|
|
from library.module_utils.network.f5.icontrol import module_provisioned
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
from ansible.module_utils.network.f5.bigip import HAS_F5SDK
|
|
|
|
from ansible.module_utils.network.f5.bigip import F5RestClient
|
|
|
|
from ansible.module_utils.network.f5.bigip import F5Client
|
|
|
|
|
|
|
|
from ansible.module_utils.network.f5.common import F5ModuleError
|
|
|
|
from ansible.module_utils.network.f5.common import F5ModuleError
|
|
|
|
from ansible.module_utils.network.f5.common import AnsibleF5Parameters
|
|
|
|
from ansible.module_utils.network.f5.common import AnsibleF5Parameters
|
|
|
|
from ansible.module_utils.network.f5.common import cleanup_tokens
|
|
|
|
from ansible.module_utils.network.f5.common import cleanup_tokens
|
|
|
|
from ansible.module_utils.network.f5.common import fqdn_name
|
|
|
|
from ansible.module_utils.network.f5.common import fq_name
|
|
|
|
from ansible.module_utils.network.f5.common import f5_argument_spec
|
|
|
|
from ansible.module_utils.network.f5.common import f5_argument_spec
|
|
|
|
try:
|
|
|
|
from ansible.module_utils.network.f5.common import exit_json
|
|
|
|
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
|
|
|
|
from ansible.module_utils.network.f5.common import fail_json
|
|
|
|
except ImportError:
|
|
|
|
from ansible.module_utils.network.f5.icontrol import upload_file
|
|
|
|
HAS_F5SDK = False
|
|
|
|
from ansible.module_utils.network.f5.icontrol import tmos_version
|
|
|
|
|
|
|
|
from ansible.module_utils.network.f5.icontrol import module_provisioned
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Parameters(AnsibleF5Parameters):
|
|
|
|
class Parameters(AnsibleF5Parameters):
|
|
|
|
updatables = [
|
|
|
|
updatables = [
|
|
|
|
'active'
|
|
|
|
'active',
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
returnables = [
|
|
|
|
returnables = [
|
|
|
|
'name', 'template', 'file', 'active'
|
|
|
|
'name',
|
|
|
|
|
|
|
|
'template',
|
|
|
|
|
|
|
|
'file',
|
|
|
|
|
|
|
|
'active',
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
api_attributes = [
|
|
|
|
api_attributes = [
|
|
|
|
'name', 'file', 'active'
|
|
|
|
'name',
|
|
|
|
|
|
|
|
'file',
|
|
|
|
|
|
|
|
'active',
|
|
|
|
]
|
|
|
|
]
|
|
|
|
api_map = {
|
|
|
|
api_map = {
|
|
|
|
'filename': 'file'
|
|
|
|
'filename': 'file',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@property
|
|
|
@ -267,18 +281,33 @@ class Parameters(AnsibleF5Parameters):
|
|
|
|
if self._values['template_link'] is not None:
|
|
|
|
if self._values['template_link'] is not None:
|
|
|
|
return self._values['template_link']
|
|
|
|
return self._values['template_link']
|
|
|
|
collection = self._templates_from_device()
|
|
|
|
collection = self._templates_from_device()
|
|
|
|
for resource in collection:
|
|
|
|
for resource in collection['items']:
|
|
|
|
if resource.name == self.template.upper():
|
|
|
|
if resource['name'] == self.template.upper():
|
|
|
|
return dict(link=resource.selfLink)
|
|
|
|
return dict(link=resource['selfLink'])
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@property
|
|
|
|
def full_path(self):
|
|
|
|
def full_path(self):
|
|
|
|
return fqdn_name(self.name)
|
|
|
|
return fq_name(self.name)
|
|
|
|
|
|
|
|
|
|
|
|
def _templates_from_device(self):
|
|
|
|
def _templates_from_device(self):
|
|
|
|
collection = self.client.api.tm.asm.policy_templates_s.get_collection()
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/policy-templates/".format(
|
|
|
|
return collection
|
|
|
|
self.client.provider['server'],
|
|
|
|
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
|
|
|
|
if 'message' in response:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def to_return(self):
|
|
|
|
def to_return(self):
|
|
|
|
result = {}
|
|
|
|
result = {}
|
|
|
@ -463,13 +492,10 @@ class BaseManager(object):
|
|
|
|
result = dict()
|
|
|
|
result = dict()
|
|
|
|
state = self.want.state
|
|
|
|
state = self.want.state
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
if state == "present":
|
|
|
|
if state == "present":
|
|
|
|
changed = self.present()
|
|
|
|
changed = self.present()
|
|
|
|
elif state == "absent":
|
|
|
|
elif state == "absent":
|
|
|
|
changed = self.absent()
|
|
|
|
changed = self.absent()
|
|
|
|
except iControlUnexpectedHTTPError as e:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
changes = self.changes.to_return()
|
|
|
|
changes = self.changes.to_return()
|
|
|
|
result.update(**changes)
|
|
|
|
result.update(**changes)
|
|
|
@ -521,14 +547,25 @@ class BaseManager(object):
|
|
|
|
return self.remove()
|
|
|
|
return self.remove()
|
|
|
|
|
|
|
|
|
|
|
|
def exists(self):
|
|
|
|
def exists(self):
|
|
|
|
policies = self.client.api.tm.asm.policies_s.get_collection()
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/policies/".format(
|
|
|
|
if any(p.name == self.want.name and p.partition == self.want.partition for p in policies):
|
|
|
|
self.client.provider['server'],
|
|
|
|
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if any(p['name'] == self.want.name and p['partition'] == self.want.partition for p in response['items']):
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _file_is_missing(self):
|
|
|
|
def _file_is_missing(self):
|
|
|
|
if self.want.template and self.want.file is None:
|
|
|
|
if self.want.template and self.want.file is None:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.want.template is None and self.want.file is None:
|
|
|
|
|
|
|
|
return False
|
|
|
|
if not os.path.exists(self.want.file):
|
|
|
|
if not os.path.exists(self.want.file):
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return False
|
|
|
@ -571,32 +608,92 @@ class BaseManager(object):
|
|
|
|
|
|
|
|
|
|
|
|
def activate(self):
|
|
|
|
def activate(self):
|
|
|
|
self.have = self.read_current_from_device()
|
|
|
|
self.have = self.read_current_from_device()
|
|
|
|
task = self.apply_on_device()
|
|
|
|
task_id = self.apply_on_device()
|
|
|
|
if self.wait_for_task(task):
|
|
|
|
if self.wait_for_task(task_id, 'apply'):
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
raise F5ModuleError('Apply policy task failed.')
|
|
|
|
raise F5ModuleError('Apply policy task failed.')
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_task(self, task):
|
|
|
|
def wait_for_task(self, task_id, task):
|
|
|
|
|
|
|
|
uri = ''
|
|
|
|
|
|
|
|
if task == 'apply':
|
|
|
|
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/tasks/apply-policy/{2}".format(
|
|
|
|
|
|
|
|
self.client.provider['server'],
|
|
|
|
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
|
|
|
|
task_id
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
elif task == 'import':
|
|
|
|
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/tasks/import-policy/{2}".format(
|
|
|
|
|
|
|
|
self.client.provider['server'],
|
|
|
|
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
|
|
|
|
task_id
|
|
|
|
|
|
|
|
)
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
task.refresh()
|
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
if task.status in ['COMPLETED', 'FAILURE']:
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
|
|
|
|
if 'message' in response:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if response['status'] in ['COMPLETED', 'FAILURE']:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
time.sleep(1)
|
|
|
|
if task.status == 'FAILURE':
|
|
|
|
|
|
|
|
|
|
|
|
if response['status'] == 'FAILURE':
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
if task.status == 'COMPLETED':
|
|
|
|
if response['status'] == 'COMPLETED':
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def update_on_device(self):
|
|
|
|
def _get_policy_id(self):
|
|
|
|
params = self.changes.api_params()
|
|
|
|
|
|
|
|
policies = self.client.api.tm.asm.policies_s.get_collection()
|
|
|
|
|
|
|
|
name = self.want.name
|
|
|
|
name = self.want.name
|
|
|
|
partition = self.want.partition
|
|
|
|
partition = self.want.partition
|
|
|
|
resource = next((p for p in policies if p.name == name and p.partition == partition), None)
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/policies/".format(
|
|
|
|
if resource:
|
|
|
|
self.client.provider['server'],
|
|
|
|
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
policy_id = next(
|
|
|
|
|
|
|
|
(p['id'] for p in response['items'] if p['name'] == name and p['partition'] == partition), None
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not policy_id:
|
|
|
|
|
|
|
|
raise F5ModuleError("The policy was not found")
|
|
|
|
|
|
|
|
return policy_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_on_device(self):
|
|
|
|
|
|
|
|
params = self.changes.api_params()
|
|
|
|
|
|
|
|
policy_id = self._get_policy_id()
|
|
|
|
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/policies/{2}".format(
|
|
|
|
|
|
|
|
self.client.provider['server'],
|
|
|
|
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
|
|
|
|
policy_id
|
|
|
|
|
|
|
|
)
|
|
|
|
if not params['active']:
|
|
|
|
if not params['active']:
|
|
|
|
resource.modify(**params)
|
|
|
|
resp = self.client.api.patch(uri, json=params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
|
|
|
|
if 'message' in response:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
|
|
|
|
|
|
|
|
def create_blank(self):
|
|
|
|
def create_blank(self):
|
|
|
|
self.create_on_device()
|
|
|
|
self.create_on_device()
|
|
|
@ -624,57 +721,180 @@ class BaseManager(object):
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def read_current_from_device(self):
|
|
|
|
def read_current_from_device(self):
|
|
|
|
policies = self.client.api.tm.asm.policies_s.get_collection()
|
|
|
|
policy_id = self._get_policy_id()
|
|
|
|
for policy in policies:
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/policies/{2}".format(
|
|
|
|
if policy.name == self.want.name and policy.partition == self.want.partition:
|
|
|
|
self.client.provider['server'],
|
|
|
|
params = policy.attrs
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
params.update(dict(self_link=policy.selfLink))
|
|
|
|
policy_id
|
|
|
|
return Parameters(params=params)
|
|
|
|
)
|
|
|
|
raise F5ModuleError("The policy was not found")
|
|
|
|
resp = self.client.api.get(uri)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
|
|
|
|
if 'message' in response:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response.update((dict(self_link=response['selfLink'])))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Parameters(params=response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upload_file_to_device(self, content, name):
|
|
|
|
|
|
|
|
url = 'https://{0}:{1}/mgmt/shared/file-transfer/uploads'.format(
|
|
|
|
|
|
|
|
self.client.provider['server'],
|
|
|
|
|
|
|
|
self.client.provider['server_port']
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
upload_file(self.client, url, content, name)
|
|
|
|
|
|
|
|
except F5ModuleError:
|
|
|
|
|
|
|
|
raise F5ModuleError(
|
|
|
|
|
|
|
|
"Failed to upload the file."
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def import_to_device(self):
|
|
|
|
def import_to_device(self):
|
|
|
|
self.client.api.tm.asm.file_transfer.uploads.upload_file(self.want.file)
|
|
|
|
name = os.path.split(self.want.file)[1]
|
|
|
|
|
|
|
|
self.upload_file_to_device(self.want.file, name)
|
|
|
|
time.sleep(2)
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
full_name = fq_name(self.want.partition, self.want.name)
|
|
|
|
|
|
|
|
cmd = 'tmsh load asm policy {0} file /var/config/rest/downloads/{1}'.format(full_name, name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/util/bash/".format(
|
|
|
|
|
|
|
|
self.client.provider['server'],
|
|
|
|
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
args = dict(
|
|
|
|
|
|
|
|
command='run',
|
|
|
|
|
|
|
|
utilCmdArgs='-c "{0}"'.format(cmd)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = self.client.api.post(uri, json=args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
if 'commandResult' in response:
|
|
|
|
|
|
|
|
if 'Unexpected Error' in response['commandResult']:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['commandResult'])
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
|
|
|
|
if 'message' in response:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_temp_policy_from_device(self):
|
|
|
|
name = os.path.split(self.want.file)[1]
|
|
|
|
name = os.path.split(self.want.file)[1]
|
|
|
|
tasks = self.client.api.tm.asm.tasks
|
|
|
|
tpath_name = '/var/config/rest/downloads/{0}'.format(name)
|
|
|
|
result = tasks.import_policy_s.import_policy.create(
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/util/unix-rm/".format(
|
|
|
|
name=self.want.name,
|
|
|
|
self.client.provider['server'],
|
|
|
|
partition=self.want.partition,
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
filename=name
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return result
|
|
|
|
args = dict(
|
|
|
|
|
|
|
|
command='run',
|
|
|
|
|
|
|
|
utilCmdArgs=tpath_name
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = self.client.api.post(uri, json=args)
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
|
|
|
|
if 'message' in response:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
|
|
|
|
|
|
|
|
def apply_on_device(self):
|
|
|
|
def apply_on_device(self):
|
|
|
|
tasks = self.client.api.tm.asm.tasks
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/tasks/apply-policy/".format(
|
|
|
|
result = tasks.apply_policy_s.apply_policy.create(
|
|
|
|
self.client.provider['server'],
|
|
|
|
policyReference={'link': self.have.self_link}
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return result
|
|
|
|
params = dict(policyReference={'link': self.have.self_link})
|
|
|
|
|
|
|
|
resp = self.client.api.post(uri, json=params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] in [400, 403]:
|
|
|
|
|
|
|
|
if 'message' in response:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
|
|
|
|
return response['id']
|
|
|
|
|
|
|
|
|
|
|
|
def create_from_template_on_device(self):
|
|
|
|
def create_from_template_on_device(self):
|
|
|
|
tasks = self.client.api.tm.asm.tasks
|
|
|
|
full_name = fq_name(self.want.partition, self.want.name)
|
|
|
|
result = tasks.import_policy_s.import_policy.create(
|
|
|
|
cmd = 'tmsh create asm policy {0} policy-template {1}'.format(full_name, self.want.template)
|
|
|
|
name=self.want.name,
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/util/bash/".format(
|
|
|
|
partition=self.want.partition,
|
|
|
|
self.client.provider['server'],
|
|
|
|
policyTemplateReference=self.want.template_link
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
time.sleep(2)
|
|
|
|
args = dict(
|
|
|
|
return result
|
|
|
|
command='run',
|
|
|
|
|
|
|
|
utilCmdArgs='-c "{0}"'.format(cmd)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = self.client.api.post(uri, json=args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
if 'commandResult' in response:
|
|
|
|
|
|
|
|
if 'Unexpected Error' in response['commandResult']:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['commandResult'])
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] == 400:
|
|
|
|
|
|
|
|
if 'message' in response:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
|
|
|
|
|
|
|
|
def create_on_device(self):
|
|
|
|
def create_on_device(self):
|
|
|
|
result = self.client.api.tm.asm.policies_s.policy.create(
|
|
|
|
params = self.changes.api_params()
|
|
|
|
name=self.want.name,
|
|
|
|
params['name'] = self.want.name
|
|
|
|
partition=self.want.partition
|
|
|
|
params['partition'] = self.want.partition
|
|
|
|
|
|
|
|
# we need to remove active from params as API will raise an error if the active is set to True,
|
|
|
|
|
|
|
|
# policies can only be activated via apply-policy task endpoint.
|
|
|
|
|
|
|
|
params.pop('active')
|
|
|
|
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/policies/".format(
|
|
|
|
|
|
|
|
self.client.provider['server'],
|
|
|
|
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return result
|
|
|
|
resp = self.client.api.post(uri, json=params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = resp.json()
|
|
|
|
|
|
|
|
except ValueError as ex:
|
|
|
|
|
|
|
|
raise F5ModuleError(str(ex))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'code' in response and response['code'] in [400, 401, 403]:
|
|
|
|
|
|
|
|
if 'message' in response:
|
|
|
|
|
|
|
|
raise F5ModuleError(response['message'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise F5ModuleError(resp.content)
|
|
|
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
return response['selfLink']
|
|
|
|
|
|
|
|
|
|
|
|
def remove_from_device(self):
|
|
|
|
def remove_from_device(self):
|
|
|
|
policies = self.client.api.tm.asm.policies_s.get_collection()
|
|
|
|
policy_id = self._get_policy_id()
|
|
|
|
name = self.want.name
|
|
|
|
uri = "https://{0}:{1}/mgmt/tm/asm/policies/{2}".format(
|
|
|
|
partition = self.want.partition
|
|
|
|
self.client.provider['server'],
|
|
|
|
resource = next((p for p in policies if p.name == name and p.partition == partition), None)
|
|
|
|
self.client.provider['server_port'],
|
|
|
|
if resource:
|
|
|
|
policy_id
|
|
|
|
resource.delete()
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.client.api.delete(uri)
|
|
|
|
|
|
|
|
if response.status in [200, 201]:
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
raise F5ModuleError(response.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ModuleManager(object):
|
|
|
|
class ModuleManager(object):
|
|
|
@ -683,6 +903,10 @@ class ModuleManager(object):
|
|
|
|
self.kwargs = kwargs
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
|
|
|
|
|
|
|
def exec_module(self):
|
|
|
|
def exec_module(self):
|
|
|
|
|
|
|
|
if not module_provisioned(self.client, 'asm'):
|
|
|
|
|
|
|
|
raise F5ModuleError(
|
|
|
|
|
|
|
|
"ASM must be provisioned to use this module."
|
|
|
|
|
|
|
|
)
|
|
|
|
if self.version_is_less_than_13():
|
|
|
|
if self.version_is_less_than_13():
|
|
|
|
manager = self.get_manager('v1')
|
|
|
|
manager = self.get_manager('v1')
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -696,7 +920,7 @@ class ModuleManager(object):
|
|
|
|
return V2Manager(**self.kwargs)
|
|
|
|
return V2Manager(**self.kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def version_is_less_than_13(self):
|
|
|
|
def version_is_less_than_13(self):
|
|
|
|
version = self.client.api.tmos_version
|
|
|
|
version = tmos_version(self.client)
|
|
|
|
if LooseVersion(version) < LooseVersion('13.0.0'):
|
|
|
|
if LooseVersion(version) < LooseVersion('13.0.0'):
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -717,31 +941,6 @@ class V1Manager(BaseManager):
|
|
|
|
def create_from_template(self):
|
|
|
|
def create_from_template(self):
|
|
|
|
self.create_from_template_on_device()
|
|
|
|
self.create_from_template_on_device()
|
|
|
|
|
|
|
|
|
|
|
|
def create_from_template_on_device(self):
|
|
|
|
|
|
|
|
full_name = fqdn_name(self.want.partition, self.want.name)
|
|
|
|
|
|
|
|
cmd = 'tmsh create asm policy {0} policy-template {1}'.format(full_name, self.want.template)
|
|
|
|
|
|
|
|
self.client.api.tm.util.bash.exec_cmd(
|
|
|
|
|
|
|
|
'run',
|
|
|
|
|
|
|
|
utilCmdArgs='-c "{0}"'.format(cmd)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_temp_policy_from_device(self):
|
|
|
|
|
|
|
|
name = os.path.split(self.want.file)[1]
|
|
|
|
|
|
|
|
tpath_name = '/var/config/rest/downloads/{0}'.format(name)
|
|
|
|
|
|
|
|
self.client.api.tm.util.unix_rm.exec_cmd('run', utilCmdArgs=tpath_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def import_to_device(self):
|
|
|
|
|
|
|
|
self.client.api.shared.file_transfer.uploads.upload_file(self.want.file)
|
|
|
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
name = os.path.split(self.want.file)[1]
|
|
|
|
|
|
|
|
full_name = fqdn_name(self.want.partition, self.want.name)
|
|
|
|
|
|
|
|
cmd = 'tmsh load asm policy {0} file /var/config/rest/downloads/{1}'.format(full_name, name)
|
|
|
|
|
|
|
|
self.client.api.tm.util.bash.exec_cmd(
|
|
|
|
|
|
|
|
'run',
|
|
|
|
|
|
|
|
utilCmdArgs='-c "{0}"'.format(cmd)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class V2Manager(BaseManager):
|
|
|
|
class V2Manager(BaseManager):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
@ -751,18 +950,13 @@ class V2Manager(BaseManager):
|
|
|
|
self.want = V2Parameters(params=module.params, client=client)
|
|
|
|
self.want = V2Parameters(params=module.params, client=client)
|
|
|
|
|
|
|
|
|
|
|
|
def create_from_template(self):
|
|
|
|
def create_from_template(self):
|
|
|
|
task = self.create_from_template_on_device()
|
|
|
|
if not self.create_from_template_on_device():
|
|
|
|
if not task:
|
|
|
|
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
if not self.wait_for_task(task):
|
|
|
|
|
|
|
|
raise F5ModuleError('Import policy task failed.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_from_file(self):
|
|
|
|
def create_from_file(self):
|
|
|
|
task = self.import_to_device()
|
|
|
|
if not self.import_to_device():
|
|
|
|
if not task:
|
|
|
|
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
if not self.wait_for_task(task):
|
|
|
|
self.remove_temp_policy_from_device()
|
|
|
|
raise F5ModuleError('Import policy task failed.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ArgumentSpec(object):
|
|
|
|
class ArgumentSpec(object):
|
|
|
@ -840,18 +1034,17 @@ def main():
|
|
|
|
['file', 'template']
|
|
|
|
['file', 'template']
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if not HAS_F5SDK:
|
|
|
|
|
|
|
|
module.fail_json(msg="The python f5-sdk module is required")
|
|
|
|
client = F5RestClient(**module.params)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
client = F5Client(**module.params)
|
|
|
|
|
|
|
|
mm = ModuleManager(module=module, client=client)
|
|
|
|
mm = ModuleManager(module=module, client=client)
|
|
|
|
results = mm.exec_module()
|
|
|
|
results = mm.exec_module()
|
|
|
|
cleanup_tokens(client)
|
|
|
|
cleanup_tokens(client)
|
|
|
|
module.exit_json(**results)
|
|
|
|
exit_json(module, results, client)
|
|
|
|
except F5ModuleError as e:
|
|
|
|
except F5ModuleError as ex:
|
|
|
|
cleanup_tokens(client)
|
|
|
|
cleanup_tokens(client)
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
fail_json(module, ex, client)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|