def main():
    if not HAS_F5SDK:
        raise F5ModuleError("The python f5-sdk module is required")

    spec = ArgumentSpec()

    client = AnsibleF5Client(argument_spec=spec.argument_spec,
                             supports_check_mode=spec.supports_check_mode,
                             f5_product_name=spec.f5_product_name)

    try:
        mm = ModuleManager(client)
        results = mm.exec_module()
        client.module.exit_json(**results)
    except F5ModuleError as e:
        client.module.fail_json(msg=str(e))
Example #2
0
 def test_invalid_unknown_params(self, *args):
     set_module_args(
         dict(name='test-route',
              password='******',
              server='localhost',
              user='******',
              state='present',
              foo="bar"))
     with patch(
             'ansible.module_utils.f5_utils.AnsibleModule.fail_json') as mo:
         mo.return_value = True
         AnsibleF5Client(argument_spec=self.spec.argument_spec,
                         mutually_exclusive=self.spec.mutually_exclusive,
                         supports_check_mode=self.spec.supports_check_mode,
                         f5_product_name=self.spec.f5_product_name)
         assert mo.call_count == 1
Example #3
0
def main():
    spec = ArgumentSpec()

    client = AnsibleF5Client(argument_spec=spec.argument_spec,
                             supports_check_mode=spec.supports_check_mode,
                             f5_product_name=spec.f5_product_name)

    if client.module.params['transport'] != 'cli' and not HAS_F5SDK:
        raise F5ModuleError(
            "The python f5-sdk module is required to use the rest api")

    try:
        mm = ModuleManager(client)
        results = mm.exec_module()
        client.module.exit_json(**results)
    except (FailedConditionsError, AttributeError) as e:
        client.module.fail_json(msg=str(e))
Example #4
0
    def test_update_user_shell_to_bash_mutliple_roles(self, *args):
        set_module_args(dict(
            username_credential='someuser',
            password='******',
            server='localhost',
            user='******',
            shell='bash'
        ))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name
        )

        # Configure the parameters that would be returned by querying the
        # remote device
        access = [
            {'name': 'Common', 'role': 'operator'},
            {'name': 'all', 'role': 'guest'}
        ]
        current = Parameters(
            dict(
                user='******',
                shell='tmsh',
                partition_access=access
            )
        )

        # Override methods to force specific logic in the module to happen
        mm = ModuleManager(client)
        mm.is_version_less_than_13 = lambda: True
        mm.exit_json = lambda x: False

        upm = UnparitionedManager(client)
        upm.exists = lambda: True
        upm.update_on_device = lambda: True
        upm.read_current_from_device = lambda: current

        msg = "Shell access is only available to 'admin' or " \
              "'resource-admin' roles"

        with pytest.raises(F5ModuleError) as ex:
            upm.exec_module()
        assert str(ex.value) == msg
    def test_invalid_filter_empty_value_raises(self, *args):
        set_module_args(
            dict(server='localhost',
                 password='******',
                 user='******',
                 filter='name:'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        with pytest.raises(F5ModuleError) as err:
            msg = '"name:" is not a valid filter format. Filters must have key:value format'
            mm = ModuleManager(client)
            mm.exec_module()

        assert str(err.value) == msg
Example #6
0
    def test_run_single_modification_command(self, *args):
        set_module_args(
            dict(commands=["tmsh create ltm virtual foo"],
                 server='localhost',
                 user='******',
                 password='******'))

        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)

        results = mm.exec_module()

        assert results['changed'] is True
        self.assertEqual(self.run_commands.call_count, 0)
        self.assertEqual(self.execute_on_device.call_count, 1)
    def test_invalid_include_raises(self, *args):
        set_module_args(
            dict(server='localhost',
                 password='******',
                 user='******',
                 include=['bar', 'foo']))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        with pytest.raises(F5ModuleError) as err:
            msg = 'Include parameter may only be specified as one or more of the following: all, volume, image, hotfix'
            mm = ModuleManager(client)
            mm.exec_module()

        assert str(err.value) == msg
    def test_invalid_filter_raises(self, *args):
        set_module_args(
            dict(server='localhost',
                 password='******',
                 user='******',
                 filter='foo:bar'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        with pytest.raises(F5ModuleError) as err:
            msg = '"foo" is not a supported filter. Supported key values are: name, build, version, status, active'
            mm = ModuleManager(client)
            mm.exec_module()

        assert str(err.value) == msg
    def test_create(self, *args):
        set_module_args(
            dict(name='foo',
                 server='localhost',
                 password='******',
                 user='******'))

        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)
        mm.create_on_device = lambda: True
        mm.exists = lambda: False

        results = mm.exec_module()

        assert results['changed'] is True
 def test_cli_command(self, *args):
     set_module_args(dict(
         commands=[
             "show sys version"
         ],
         server='localhost',
         user='******',
         password='******',
         transport='cli'
     ))
     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)
     results = mm.exec_module()
     self.assertEqual(self.run_commands.call_count, 1)
     self.assertEqual(self.execute_on_device.call_count, 0)
    def test_run_single_command(self, *args):
        set_module_args(
            dict(commands=["tmsh show sys version"],
                 server='localhost',
                 user='******',
                 password='******'))

        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.execute_on_device = Mock(return_value='foo')

        results = mm.exec_module()

        assert results['changed'] is True
Example #12
0
    def test_modify_profiles(self, *args):
        set_module_args(
            dict(name="my-virtual-server",
                 partition="Common",
                 password="******",
                 profiles=['http', 'clientssl'],
                 server="localhost",
                 state="present",
                 user="******",
                 validate_certs="no"))

        # Configure the parameters that would be returned by querying the
        # remote device
        current = VirtualServerApiParameters(
            load_fixture('load_ltm_virtual_2.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)

        # Override methods to force specific logic in the module to happen
        vsm = VirtualServerManager(client)
        vsm.exists = Mock(return_value=True)
        vsm.read_current_from_device = Mock(return_value=current)
        vsm.update_on_device = Mock(return_value=True)

        mm = ModuleManager(client)
        mm.get_manager = Mock(return_value=vsm)

        results = mm.exec_module()

        assert results['changed'] is True
        assert len(results['profiles']) == 2
        assert 'name' in results['profiles'][0]
        assert 'context' in results['profiles'][0]
        assert results['profiles'][0]['name'] == 'http'
        assert results['profiles'][0]['context'] == 'all'
        assert 'name' in results['profiles'][1]
        assert 'context' in results['profiles'][1]
        assert results['profiles'][1]['name'] == 'clientssl'
        assert results['profiles'][1]['context'] == 'clientside'
Example #13
0
    def test_create_gtm_irule_src(self, *args):
        set_module_args(
            dict(name='foo',
                 module='gtm',
                 src='{0}/create_ltm_irule.tcl'.format(fixture_path),
                 partition='Common',
                 server='localhost',
                 password='******',
                 user='******'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name,
            mutually_exclusive=self.spec.mutually_exclusive,
        )

        if PY3:
            builtins_name = 'builtins'
        else:
            builtins_name = '__builtin__'

        with patch(builtins_name + '.open',
                   mock_open(read_data='this is my content'),
                   create=True):
            # Override methods in the specific type of manager
            tm = GtmManager(client)
            tm.exists = Mock(side_effect=[False, True])
            tm.create_on_device = Mock(return_value=True)

            # Override methods to force specific logic in the module to happen
            mm = ModuleManager(client)
            mm.get_manager = Mock(return_value=tm)

            results = mm.exec_module()

        assert results['changed'] is True
        assert results['content'] == 'this is my content'
        assert results['module'] == 'gtm'
        assert results['src'] == '{0}/create_ltm_irule.tcl'.format(
            fixture_path)
        assert len(results.keys()) == 4
Example #14
0
    def test_delete_device_group(self, *args):
        set_module_args(
            dict(name="foo-group",
                 state="absent",
                 server='localhost',
                 user='******',
                 password='******'))

        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
Example #15
0
    def test_run_single_command(self, *args):
        set_module_args(
            dict(commands=["tmsh show sys version"],
                 server='localhost',
                 user='******',
                 password='******'))

        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)
        mm._run_commands = Mock(return_value=[])
        mm.execute_on_device = Mock(return_value=[])

        results = mm.exec_module()

        assert results['changed'] is False
        assert mm._run_commands.call_count == 0
        assert mm.execute_on_device.call_count == 1
Example #16
0
    def test_create_remote_syslog(self, *args):
        set_module_args(
            dict(remote_host='10.10.10.10',
                 server='localhost',
                 password='******',
                 user='******'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        # Override methods in the specific type of manager
        mm = ModuleManager(client)
        mm.exists = Mock(side_effect=[False, True])
        mm.update_on_device = Mock(return_value=True)

        results = mm.exec_module()

        assert results['changed'] is True
    def test_get_image_and_hotfixes_no_filter(self, *args):
        set_module_args(
            dict(server='localhost',
                 password='******',
                 user='******',
                 include=['image', 'hotfix']))

        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)
        mm.exit_json = Mock(return_value=False)
        results = mm.exec_module()

        assert results['changed'] is True
        assert len(results['images']) == 8
        assert len(results['hotfixes']) == 5
        assert 'volumes' not in results.keys()
    def test_create_datacenter(self, *args):
        set_module_args(
            dict(state='present',
                 password='******',
                 server='localhost',
                 user='******',
                 name='foo'))

        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_delete_iapp_template_idempotent(self, *args):
        set_module_args(
            dict(content=load_fixture('basic-iapp.tmpl'),
                 password='******',
                 server='localhost',
                 user='******',
                 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
Example #20
0
def main():
    if not HAS_F5SDK:
        raise F5ModuleError("The python f5-sdk module is required")

    spec = ArgumentSpec()

    client = AnsibleF5Client(
        argument_spec=spec.argument_spec,
        supports_check_mode=spec.supports_check_mode,
        f5_product_name=spec.f5_product_name,
        mutually_exclusive=[['tagged_interfaces', 'untagged_interfaces']])

    try:
        mm = ModuleManager(client)
        results = mm.exec_module()
        cleanup_tokens(client)
        client.module.exit_json(**results)
    except F5ModuleError as e:
        cleanup_tokens(client)
        client.module.fail_json(msg=str(e))
    def test_create_policy_rule_idempotent_check(self, *args):
        set_module_args(dict(
            name="rule1",
            state='present',
            policy='policy1',
            actions=[
                dict(
                    type='forward',
                    pool='baz'
                )
            ],
            conditions=[
                dict(
                    type='http_uri',
                    path_begins_with_any=['/ABC']
                )
            ],
            password='******',
            server='localhost',
            user='******'
        ))

        current = ApiParameters(load_fixture('load_ltm_policy_draft_rule_http-uri_forward.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
        )

        # Override methods to force specific logic in the module to happen
        mm = ModuleManager(client)
        mm.exists = Mock(return_value=True)
        mm.read_current_from_device = Mock(return_value=current)
        mm.draft_exists = Mock(return_value=False)
        mm.update_on_device = Mock(return_value=True)
        mm._create_existing_policy_draft_on_device = Mock(return_value=True)
        mm.publish_on_device = Mock(return_value=True)

        results = mm.exec_module()

        assert results['changed'] is True
    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='******',
                 server='localhost',
                 user='******'))

        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
Example #23
0
    def test_create_remote_syslog_idempotent(self, *args):
        set_module_args(
            dict(remote_host='10.10.10.10',
                 server='localhost',
                 password='******',
                 user='******'))

        current = Parameters(load_fixture('load_tm_sys_syslog.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)

        # Override methods in the specific type of manager
        mm = ModuleManager(client)
        mm.exists = Mock(return_value=True)
        mm.read_current_from_device = Mock(return_value=current)

        results = mm.exec_module()

        assert results['changed'] is False
Example #24
0
    def test_module_mutual_exclusion(self, *args):
        set_module_args(
            dict(content='foo',
                 module='ltm',
                 name='foo',
                 state='present',
                 src='/path/to/irules/foo.tcl',
                 partition='Common',
                 server='localhost',
                 password='******',
                 user='******'))

        with patch('ansible.module_utils.basic.AnsibleModule.fail_json',
                   unsafe=True) as mo:
            AnsibleF5Client(
                argument_spec=self.spec.argument_spec,
                supports_check_mode=self.spec.supports_check_mode,
                f5_product_name=self.spec.f5_product_name,
                mutually_exclusive=self.spec.mutually_exclusive,
            )
            mo.assert_called_once()
Example #25
0
    def test_update_user_shell_to_bash(self, *args):
        set_module_args(dict(
            username_credential='someuser',
            password='******',
            server='localhost',
            user='******',
            shell='bash'
        ))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name
        )

        # Configure the parameters that would be returned by querying the
        # remote device
        access = [{'name': 'all', 'role': 'admin'}]
        current = Parameters(
            dict(
                user='******',
                shell='tmsh',
                partition_access=access
            )
        )

        # Override methods to force specific logic in the module to happen
        mm = ModuleManager(client)
        mm.is_version_less_than_13 = lambda: True
        mm.exit_json = lambda x: False

        upm = UnparitionedManager(client)
        upm.exists = lambda: True
        upm.update_on_device = lambda: True
        upm.read_current_from_device = lambda: current

        results = upm.exec_module()

        assert results['changed'] is True
        assert results['shell'] == 'bash'
Example #26
0
    def test_get_typed_pool_facts(self, *args):
        set_module_args(dict(
            include='pool',
            password='******',
            server='localhost',
            user='******'
        ))

        fixture1 = load_fixture('load_gtm_pool_a_collection.json')
        fixture2 = load_fixture('load_gtm_pool_a_example_stats.json')
        collection = [FakeARecord(attrs=x) for x in fixture1['items']]
        stats = Stats(FakeStatResource(fixture2['entries']))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name
        )

        # Override methods in the specific type of manager
        tfm = TypedPoolFactManager(client)
        tfm.read_collection_from_device = Mock(return_value=collection)
        tfm.read_stats_from_device = Mock(return_value=stats.stat)

        tm = PoolFactManager(client)
        tm.version_is_less_than_12 = Mock(return_value=False)
        tm.get_manager = Mock(return_value=tfm)

        # Override methods to force specific logic in the module to happen
        mm = ModuleManager(client)
        mm.get_manager = Mock(return_value=tm)
        mm.gtm_provisioned = Mock(return_value=True)

        results = mm.exec_module()

        assert results['changed'] is True
        assert 'pool' in results
        assert len(results['pool']) > 0
        assert 'load_balancing_mode' in results['pool'][0]
    def test_import_key_no_key_passphrase(self, *args):
        set_module_args(
            dict(name='foo',
                 content=load_fixture('cert1.key'),
                 state='present',
                 password='******',
                 server='localhost',
                 user='******'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        # Override methods in the specific type of manager
        cm = ModuleManager(client)
        cm.exists = Mock(side_effect=[False, True])
        cm.create_on_device = Mock(return_value=True)

        results = cm.exec_module()

        assert results['changed'] is True
Example #28
0
    def test_create_device_trust_idempotent(self, *args):
        set_module_args(
            dict(peer_server='10.10.10.10',
                 peer_hostname='foo.bar.baz',
                 peer_user='******',
                 peer_password='******',
                 server='localhost',
                 password='******',
                 user='******'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name)

        # Override methods in the specific type of manager
        mm = ModuleManager(client)
        mm.exists = Mock(return_value=True)

        results = mm.exec_module()

        assert results['changed'] is False
Example #29
0
    def test_delete(self, *args):
        set_module_args(
            dict(name='test-route',
                 password='******',
                 server='localhost',
                 user='******',
                 state='absent'))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            mutually_exclusive=self.spec.mutually_exclusive,
            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
        assert 'description' not in results
Example #30
0
    def test_update_user_shell_to_none_shell_attribute_missing(self, *args):
        set_module_args(dict(
            username_credential='someuser',
            password='******',
            server='localhost',
            user='******',
            shell='none'
        ))

        client = AnsibleF5Client(
            argument_spec=self.spec.argument_spec,
            supports_check_mode=self.spec.supports_check_mode,
            f5_product_name=self.spec.f5_product_name
        )

        # Configure the parameters that would be returned by querying the
        # remote device
        access = [{'name': 'Common', 'role': 'guest'}]
        current = Parameters(
            dict(
                user='******',
                partition_access=access
            )
        )

        # Override methods to force specific logic in the module to happen
        mm = ModuleManager(client)
        mm.is_version_less_than_13 = Mock(return_value=True)

        upm = UnparitionedManager(client)
        upm.exists = Mock(return_value=True)
        upm.update_on_device = Mock(return_value=True)
        upm.read_current_from_device = Mock(return_value=current)

        results = upm.exec_module()

        assert results['changed'] is False
        assert not hasattr(results, 'shell')