mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
Adds bigip_iapp_template module (#25630)
This module can be used to upload and manage TCL iApps on a BIG-IP. iApps can be added, removed and updated in place as needed. iApp files should be provided to the module via Ansible lookups. Unit tests are provided. Integration tests can be found here https://github.com/F5Networks/f5-ansible/blob/devel/test/integration/bigip_iapp_template.yaml#L23 https://github.com/F5Networks/f5-ansible/tree/devel/test/integration/targets/bigip_iapp_template/tasks
This commit is contained in:
25
test/units/modules/network/f5/fixtures/basic-iapp.tmpl
Normal file
25
test/units/modules/network/f5/fixtures/basic-iapp.tmpl
Normal file
@@ -0,0 +1,25 @@
|
||||
sys application template good_templ {
|
||||
actions {
|
||||
definition {
|
||||
html-help {
|
||||
# HTML Help for the template
|
||||
}
|
||||
implementation {
|
||||
# TMSH implementation code
|
||||
}
|
||||
macro {
|
||||
# TMSH macro code
|
||||
}
|
||||
presentation {
|
||||
# APL presentation language
|
||||
}
|
||||
role-acl { admin manager resource-admin }
|
||||
run-as none
|
||||
}
|
||||
}
|
||||
description "My basic template"
|
||||
partition Common
|
||||
requires-modules { ltm }
|
||||
ignore-verification true
|
||||
requires-bigip-version-min 11.6.0
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
cli admin-partitions {
|
||||
update-partition Common
|
||||
}
|
||||
|
||||
sys application template foo.iapp {
|
||||
|
||||
actions {
|
||||
definition {
|
||||
|
||||
implementation {
|
||||
set cfg { ltm virtual forwarding-repro {
|
||||
destination 0.0.0.0:any
|
||||
description "something 1"
|
||||
mask any
|
||||
profiles {
|
||||
fastL4 { }
|
||||
}
|
||||
source 1.1.1.1/32
|
||||
translate-address disabled
|
||||
translate-port disabled
|
||||
vlans { __forwarding_vlans__ }
|
||||
vlans-enabled
|
||||
} }
|
||||
|
||||
|
||||
if {![info exists {::var__forwarding_vlans}] || (${::var__forwarding_vlans} == "")} {
|
||||
set {::var__forwarding_vlans} "{}"
|
||||
puts "Info: assigning empty string to variable {::var__forwarding_vlans}"
|
||||
}
|
||||
|
||||
|
||||
set cfg [string map "__forwarding_vlans__ ${::var__forwarding_vlans} __app_service__ $tmsh::app_name.app/$tmsh::app_name " $cfg]
|
||||
set fileId [open /var/tmp/demo.repro.cfg "w"]
|
||||
puts -nonewline $fileId $cfg
|
||||
close $fileId
|
||||
|
||||
|
||||
tmsh::load sys config merge file /var/tmp/demo.repro.cfg
|
||||
}
|
||||
|
||||
presentation {
|
||||
|
||||
include "/Common/f5.apl_common"
|
||||
section var {
|
||||
string forwarding_vlans display "xxlarge"
|
||||
}
|
||||
|
||||
text {
|
||||
var "General variables"
|
||||
var.forwarding_vlans "__var__forwarding_vlans__"
|
||||
}
|
||||
}
|
||||
role-acl { admin manager resource-admin }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"kind": "tm:sys:application:template:templatestate",
|
||||
"name": "good_templ",
|
||||
"partition": "Common",
|
||||
"fullPath": "/Common/good_templ",
|
||||
"generation": 410,
|
||||
"selfLink": "https://localhost/mgmt/tm/sys/application/template/~Common~good_templ?ver=13.0.0",
|
||||
"description": "My basic template",
|
||||
"ignoreVerification": "false",
|
||||
"requiresBigipVersionMin": "11.6.0",
|
||||
"requiresModules": [
|
||||
"ltm"
|
||||
],
|
||||
"tmplChecksum": "90c46acee5ca08e300da0bcdb9130745",
|
||||
"totalSigningStatus": "checksum",
|
||||
"verificationStatus": "checksum-verified",
|
||||
"actionsReference": {
|
||||
"link": "https://localhost/mgmt/tm/sys/application/template/~Common~good_templ/actions?ver=13.0.0",
|
||||
"isSubcollection": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"kind": "tm:sys:application:template:templatestate",
|
||||
"name": "good_templ",
|
||||
"partition": "Common",
|
||||
"fullPath": "/Common/good_templ",
|
||||
"generation": 410,
|
||||
"selfLink": "https://localhost/mgmt/tm/sys/application/template/~Common~good_templ?ver=13.0.0",
|
||||
"description": "My basic foo bar",
|
||||
"ignoreVerification": "false",
|
||||
"requiresBigipVersionMin": "12.0.0",
|
||||
"requiresModules": [
|
||||
"ltm"
|
||||
],
|
||||
"tmplChecksum": "eee01710dbe330d380d1a4fa30eeabdb",
|
||||
"totalSigningStatus": "checksum",
|
||||
"verificationStatus": "checksum-verified",
|
||||
"actionsReference": {
|
||||
"link": "https://localhost/mgmt/tm/sys/application/template/~Common~good_templ/actions?ver=13.0.0",
|
||||
"isSubcollection": true
|
||||
}
|
||||
}
|
||||
191
test/units/modules/network/f5/test_bigip_iapp_template.py
Normal file
191
test/units/modules/network/f5/test_bigip_iapp_template.py
Normal file
@@ -0,0 +1,191 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright 2017 F5 Networks Inc.
|
||||
#
|
||||
# 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
|
||||
|
||||
import sys
|
||||
|
||||
if sys.version_info < (2, 7):
|
||||
from nose.plugins.skip import SkipTest
|
||||
raise SkipTest("F5 Ansible modules require Python >= 2.7")
|
||||
|
||||
import os
|
||||
import json
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.module_utils import basic
|
||||
from ansible.compat.tests.mock import patch, Mock
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from ansible.module_utils.f5_utils import AnsibleF5Client
|
||||
|
||||
try:
|
||||
from library.bigip_iapp_template import Parameters
|
||||
from library.bigip_iapp_template import ModuleManager
|
||||
from library.bigip_iapp_template import ArgumentSpec
|
||||
except ImportError:
|
||||
from ansible.modules.network.f5.bigip_iapp_template import Parameters
|
||||
from ansible.modules.network.f5.bigip_iapp_template import ArgumentSpec
|
||||
from ansible.modules.network.f5.bigip_iapp_template import ModuleManager
|
||||
|
||||
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
|
||||
fixture_data = {}
|
||||
|
||||
|
||||
def set_module_args(args):
|
||||
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
|
||||
basic._ANSIBLE_ARGS = to_bytes(args)
|
||||
|
||||
|
||||
def load_fixture(name):
|
||||
path = os.path.join(fixture_path, name)
|
||||
|
||||
if path in fixture_data:
|
||||
return fixture_data[path]
|
||||
|
||||
with open(path) as f:
|
||||
data = f.read()
|
||||
|
||||
try:
|
||||
data = json.loads(data)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
fixture_data[path] = data
|
||||
return data
|
||||
|
||||
|
||||
class TestParameters(unittest.TestCase):
|
||||
def test_module_parameters(self):
|
||||
iapp = load_fixture('create_iapp_template.iapp')
|
||||
args = dict(
|
||||
content=iapp
|
||||
)
|
||||
p = Parameters(args)
|
||||
assert p.name == 'foo.iapp'
|
||||
|
||||
|
||||
@patch('ansible.module_utils.f5_utils.AnsibleF5Client._get_mgmt_root',
|
||||
return_value=True)
|
||||
class TestManager(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.spec = ArgumentSpec()
|
||||
|
||||
def test_create_iapp_template(self, *args):
|
||||
# Configure the arguments that would be sent to the Ansible module
|
||||
set_module_args(dict(
|
||||
content=load_fixture('basic-iapp.tmpl'),
|
||||
password='passsword',
|
||||
server='localhost',
|
||||
user='admin'
|
||||
))
|
||||
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
mm = ModuleManager(client)
|
||||
|
||||
# Override methods to force specific logic in the module to happen
|
||||
mm.exists = Mock(side_effect=[False, True])
|
||||
mm.create_on_device = Mock(return_value=True)
|
||||
|
||||
results = mm.exec_module()
|
||||
|
||||
assert results['changed'] is True
|
||||
|
||||
def test_update_iapp_template(self, *args):
|
||||
# Configure the arguments that would be sent to the Ansible module
|
||||
set_module_args(dict(
|
||||
content=load_fixture('basic-iapp.tmpl'),
|
||||
password='passsword',
|
||||
server='localhost',
|
||||
user='admin'
|
||||
))
|
||||
|
||||
current1 = Parameters(load_fixture('load_sys_application_template_w_new_checksum.json'))
|
||||
current2 = Parameters(load_fixture('load_sys_application_template_w_old_checksum.json'))
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
mm = ModuleManager(client)
|
||||
|
||||
# Override methods to force specific logic in the module to happen
|
||||
mm.exists = Mock(side_effect=[True, True])
|
||||
mm.create_on_device = Mock(return_value=True)
|
||||
mm.read_current_from_device = Mock(return_value=current1)
|
||||
mm.template_in_use = Mock(return_value=False)
|
||||
mm._get_temporary_template = Mock(return_value=current2)
|
||||
mm._remove_iapp_checksum = Mock(return_value=None)
|
||||
mm._generate_template_checksum_on_device = Mock(return_value=None)
|
||||
|
||||
results = mm.exec_module()
|
||||
|
||||
assert results['changed'] is True
|
||||
|
||||
def test_delete_iapp_template(self, *args):
|
||||
set_module_args(dict(
|
||||
content=load_fixture('basic-iapp.tmpl'),
|
||||
password='passsword',
|
||||
server='localhost',
|
||||
user='admin',
|
||||
state='absent'
|
||||
))
|
||||
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
mm = ModuleManager(client)
|
||||
|
||||
# Override methods to force specific logic in the module to happen
|
||||
mm.exists = Mock(side_effect=[True, False])
|
||||
mm.remove_from_device = Mock(return_value=True)
|
||||
|
||||
results = mm.exec_module()
|
||||
|
||||
assert results['changed'] is True
|
||||
|
||||
def test_delete_iapp_template_idempotent(self, *args):
|
||||
set_module_args(dict(
|
||||
content=load_fixture('basic-iapp.tmpl'),
|
||||
password='passsword',
|
||||
server='localhost',
|
||||
user='admin',
|
||||
state='absent'
|
||||
))
|
||||
|
||||
client = AnsibleF5Client(
|
||||
argument_spec=self.spec.argument_spec,
|
||||
supports_check_mode=self.spec.supports_check_mode,
|
||||
f5_product_name=self.spec.f5_product_name
|
||||
)
|
||||
mm = ModuleManager(client)
|
||||
|
||||
# Override methods to force specific logic in the module to happen
|
||||
mm.exists = Mock(side_effect=[False, False])
|
||||
|
||||
results = mm.exec_module()
|
||||
|
||||
assert results['changed'] is False
|
||||
Reference in New Issue
Block a user