Exemplo n.º 1
0
class InterfaceTestCase(BaseFeatureCase):

    @mock.patch('pyhpecw7.comware.HPCOM7')
    @mock.patch.object(Interface, '_get_iface_index')
    @mock.patch.object(Interface, '_is_ethernet_is_routed')
    def setUp(self, mock_is_eth, mock_get_index, mock_device):
        self.device = mock_device

        mock_get_index.return_value = IFACE_INDEX

        mock_is_eth.return_value = True, False
        self.eth_iface = Interface(self.device, ETH_IFACE)

        mock_is_eth.return_value = False, True
        self.lo_iface = Interface(self.device, LOGICAL_IFACE)

    def test_init(self):
        self.assertTrue(self.eth_iface.is_ethernet)
        self.assertFalse(self.eth_iface.is_routed)

        self.assertFalse(self.lo_iface.is_ethernet)
        self.assertTrue(self.lo_iface.is_routed)

        self.assertEqual(self.eth_iface.iface_index, IFACE_INDEX)
        self.assertEqual(self.lo_iface.iface_index, IFACE_INDEX)

        self.assertEqual(self.eth_iface.iface_type, 'FortyGigE')
        self.assertEqual(self.lo_iface.iface_type, 'LoopBack')

    def test_get_index(self):
        expected_get, get_reply = self.xml_get_and_reply('interface_index')
        self.device.get.return_value = get_reply

        expected = '9'
        result = self.eth_iface._get_iface_index()

        self.assertEqual(result, expected)
        self.assert_get_request(expected_get)

    def test_is_eth_is_routed(self):
        expected_get, get_reply = self.xml_get_and_reply('interface_is_eth')
        self.device.get.return_value = get_reply

        expected = True, True
        result = self.eth_iface._is_ethernet_is_routed()

        self.assertEqual(result, expected)
        self.assert_get_request(expected_get)

    def test_get_defaults(self):
        eth_defaults = self.eth_iface.get_default_config()
        lo_defaults = self.lo_iface.get_default_config()

        eth_expected = dict(description=ETH_IFACE + ' Interface', admin='up', speed='auto', duplex='auto', type='bridged')
        lo_expected = dict(description=LOGICAL_IFACE + ' Interface', admin='up', type='routed')

        self.assertEqual(eth_defaults, eth_expected)
        self.assertEqual(lo_defaults, lo_expected)

    def test_param_check(self):
        with self.assertRaises(InterfaceParamsError):
            self.lo_iface.param_check(speed='1000')

        self.eth_iface.iface_exists = False
        with self.assertRaises(InterfaceAbsentError):
            self.eth_iface.param_check(admin='up')

        self.eth_iface.iface_type = None
        with self.assertRaises(InterfaceTypeError):
            self.eth_iface.param_check(admin='up')

    def test_get_config(self):
        expected_get, get_reply = self.xml_get_and_reply('interface')
        self.device.get.return_value = get_reply

        expected = {'admin': 'up', 'duplex': 'auto', 'speed': 'auto', 'description': 'FortyGigE1/0/3 Interface', 'type': 'routed'}

        iface = self.eth_iface.get_config()

        self.assertEqual(iface, expected)
        self.assert_get_request(expected_get)

    def test_logical_iface(self):
        expected = self.read_action_xml('interface_logical')

        self.lo_iface._logical_iface()
        self.assert_action_request(expected)

        self.lo_iface._logical_iface(stage=True)
        self.assert_stage_request(expected, 'action')

    def test_logical_iface_remove(self):
        expected = self.read_action_xml('interface_logical_remove')
        self.lo_iface._logical_iface(remove=True)
        self.assert_action_request(expected)

    def test_build_config_default(self):
        expected = self.read_action_xml('interface_default')

        self.eth_iface._build_config('default', admin='up', duplex='half')
        self.assert_action_request(expected)

        self.eth_iface._build_config('default', stage=True, admin='up', duplex='half')
        self.assert_stage_request(expected, 'action')

    def test_build_config_present(self):
        expected = self.read_config_xml('interface')

        self.eth_iface._build_config('present', admin='up', duplex='half')
        self.assert_config_request(expected)

        self.eth_iface._build_config('present', stage=True, admin='up', duplex='half')
        self.assert_stage_request(expected, 'edit_config')

    def test_build_config_remove(self):
        expected = self.read_action_xml('interface_default')

        self.eth_iface._build_config('absent', admin='up', duplex='half')
        self.assert_action_request(expected)

        self.eth_iface._build_config('absent', stage=True, admin='up', duplex='half')
        self.assert_stage_request(expected, 'action')

    @mock.patch.object(Interface, '_logical_iface')
    def test_remove(self, mock_logical):
        self.lo_iface.remove_logical()
        mock_logical.assert_called_with(remove=True, stage=False)

        self.lo_iface.remove_logical(stage=True)
        mock_logical.assert_called_with(remove=True, stage=True)

    @mock.patch.object(Interface, '_logical_iface')
    def test_create(self, mock_logical):
        self.lo_iface.create_logical()
        mock_logical.assert_called_with(stage=False)

        self.lo_iface.create_logical(stage=True)
        mock_logical.assert_called_with(stage=True)

    @mock.patch.object(Interface, '_build_config')
    def test_build(self, mock_build):
        self.eth_iface.build(admin='up')
        mock_build.assert_called_with(state='present', stage=False, admin='up')

        self.eth_iface.build(admin='up', stage=True)
        mock_build.assert_called_with(state='present', stage=True, admin='up')

    @mock.patch.object(Interface, '_build_config')
    def test_default(self, mock_build):
        self.eth_iface.default()
        mock_build.assert_called_with(state='default', stage=False)

        self.eth_iface.default(stage=True)
        mock_build.assert_called_with(state='default', stage=True)
Exemplo n.º 2
0
def main():
    module = AnsibleModule(
        argument_spec=dict(name=dict(required=True),
                           admin=dict(choices=['up', 'down']),
                           description=dict(),
                           type=dict(choices=['bridged', 'routed']),
                           duplex=dict(choices=['auto', 'full']),
                           speed=dict(type='str'),
                           state=dict(choices=['present', 'absent', 'default'],
                                      default='present'),
                           hostname=dict(required=True),
                           username=dict(required=True),
                           password=dict(required=True),
                           port=dict(type='int', default=830)),
        supports_check_mode=True)

    if not HAS_PYHP:
        safe_fail(module,
                  msg='There was a problem loading from the pyhpecw7 ' +
                  'module.',
                  error=str(ie))

    filtered_keys = ('state', 'hostname', 'username', 'password', 'port',
                     'CHECKMODE', 'name')

    hostname = socket.gethostbyname(module.params['hostname'])
    username = module.params['username']
    password = module.params['password']
    port = module.params['port']

    device = HPCOM7(host=hostname,
                    username=username,
                    password=password,
                    port=port)

    name = module.params['name']
    state = module.params['state']
    changed = False

    if state == 'present':
        if module.params.get('type'):
            if module.params.get('admin') or module.params.get('description')\
                    or module.params.get('duplex') or module.params.get('speed'):
                module.fail_json(msg='The type parameter is incompatible with:'
                                 '\nadmin, description, duplex, speed.'
                                 '\nPlease configure type first by itself,'
                                 '\nthen run again.')

    proposed = dict((k, v) for k, v in module.params.iteritems()
                    if v is not None and k not in filtered_keys)

    try:
        device.open()
    except ConnectionError as e:
        safe_fail(module,
                  device,
                  msg=str(e),
                  descr='Error opening connection to device.')

    try:
        interface = Interface(device, name)
    except PYHPError as e:
        safe_fail(module,
                  device,
                  descr='There was problem recognizing that interface.',
                  msg=str(e))

    try:
        interface.param_check(**proposed)
    except PYHPError as e:
        safe_fail(module,
                  device,
                  descr='There was problem with the supplied parameters.',
                  msg=str(e))

    try:
        existing = interface.get_config()
    except PYHPError as e:
        safe_fail(module,
                  device,
                  msg=str(e),
                  descr='Error getting existing config.')

    if state == 'present':
        delta = dict(
            set(proposed.iteritems()).difference(existing.iteritems()))
        if delta or not existing:
            original = existing
            if not interface.iface_exists:
                try:
                    interface.create_logical()
                    interface.update()
                    changed = True
                    existing = interface.get_config()
                except PYHPError as e:
                    safe_fail(module,
                              device,
                              msg='Exception message ' + str(e),
                              descr='There was a problem creating' +
                              ' the logical interface.')
                delta = dict(
                    set(proposed.iteritems()).difference(existing.iteritems()))

            if delta:
                interface.build(stage=True, **delta)
    elif state == 'default':
        defaults = interface.get_default_config()
        delta = dict(
            set(existing.iteritems()).difference(defaults.iteritems()))
        if delta:
            interface.default(stage=True)
    elif state == 'absent':
        if interface.iface_exists:
            if interface.is_ethernet:
                defaults = interface.get_default_config()
                delta = dict(
                    set(existing.iteritems()).difference(defaults.iteritems()))
                if delta:
                    try:
                        interface.default(stage=True)
                    except InterfaceError as e:
                        safe_fail(module,
                                  device,
                                  msg=str(e),
                                  descr='Error getting default configuration.')
            else:
                try:
                    interface.remove_logical(stage=True)
                except InterfaceError as e:
                    safe_fail(module,
                              device,
                              msg=str(e),
                              descr='Error removing logical interface.')

    commands = None
    end_state = existing

    if device.staged:
        commands = device.staged_to_string()
        if module.check_mode:
            safe_exit(module, device, changed=True, commands=commands)
        else:
            try:
                device.execute_staged()
                end_state = interface.get_config()
            except PYHPError as e:
                safe_fail(module,
                          device,
                          msg=str(e),
                          descr='Error on device execution.')
            changed = True

    results = {}
    results['proposed'] = proposed
    results['existing'] = existing
    results['state'] = state
    results['commands'] = commands
    results['changed'] = changed
    results['end_state'] = end_state

    safe_exit(module, device, **results)
Exemplo n.º 3
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            admin=dict(choices=["up", "down"]),
            description=dict(),
            type=dict(choices=["bridged", "routed"]),
            duplex=dict(choices=["auto", "full"]),
            speed=dict(type="str"),
            state=dict(choices=["present", "absent", "default"], default="present"),
            hostname=dict(required=True),
            username=dict(required=True),
            password=dict(required=True),
            port=dict(type="int", default=830),
        ),
        supports_check_mode=True,
    )

    if not HAS_PYHP:
        safe_fail(module, msg="There was a problem loading from the pyhpecw7 " + "module.", error=str(ie))

    filtered_keys = ("state", "hostname", "username", "password", "port", "CHECKMODE", "name")

    hostname = socket.gethostbyname(module.params["hostname"])
    username = module.params["username"]
    password = module.params["password"]
    port = module.params["port"]

    device = HPCOM7(host=hostname, username=username, password=password, port=port)

    name = module.params["name"]
    state = module.params["state"]
    changed = False

    if state == "present":
        if module.params.get("type"):
            if (
                module.params.get("admin")
                or module.params.get("description")
                or module.params.get("duplex")
                or module.params.get("speed")
            ):
                module.fail_json(
                    msg="The type parameter is incompatible with:"
                    "\nadmin, description, duplex, speed."
                    "\nPlease configure type first by itself,"
                    "\nthen run again."
                )

    proposed = dict((k, v) for k, v in module.params.iteritems() if v is not None and k not in filtered_keys)

    try:
        device.open()
    except ConnectionError as e:
        safe_fail(module, device, msg=str(e), descr="Error opening connection to device.")

    try:
        interface = Interface(device, name)
    except PYHPError as e:
        safe_fail(module, device, descr="There was problem recognizing that interface.", msg=str(e))

    try:
        interface.param_check(**proposed)
    except PYHPError as e:
        safe_fail(module, device, descr="There was problem with the supplied parameters.", msg=str(e))

    try:
        existing = interface.get_config()
    except PYHPError as e:
        safe_fail(module, device, msg=str(e), descr="Error getting existing config.")

    if state == "present":
        delta = dict(set(proposed.iteritems()).difference(existing.iteritems()))
        if delta or not existing:
            original = existing
            if not interface.iface_exists:
                try:
                    interface.create_logical()
                    interface.update()
                    changed = True
                    existing = interface.get_config()
                except PYHPError as e:
                    safe_fail(
                        module,
                        device,
                        msg="Exception message " + str(e),
                        descr="There was a problem creating" + " the logical interface.",
                    )
                delta = dict(set(proposed.iteritems()).difference(existing.iteritems()))

            if delta:
                interface.build(stage=True, **delta)
    elif state == "default":
        defaults = interface.get_default_config()
        delta = dict(set(existing.iteritems()).difference(defaults.iteritems()))
        if delta:
            interface.default(stage=True)
    elif state == "absent":
        if interface.iface_exists:
            if interface.is_ethernet:
                defaults = interface.get_default_config()
                delta = dict(set(existing.iteritems()).difference(defaults.iteritems()))
                if delta:
                    try:
                        interface.default(stage=True)
                    except InterfaceError as e:
                        safe_fail(module, device, msg=str(e), descr="Error getting default configuration.")
            else:
                try:
                    interface.remove_logical(stage=True)
                except InterfaceError as e:
                    safe_fail(module, device, msg=str(e), descr="Error removing logical interface.")

    commands = None
    end_state = existing

    if device.staged:
        commands = device.staged_to_string()
        if module.check_mode:
            safe_exit(module, device, changed=True, commands=commands)
        else:
            try:
                device.execute_staged()
                end_state = interface.get_config()
            except PYHPError as e:
                safe_fail(module, device, msg=str(e), descr="Error on device execution.")
            changed = True

    results = {}
    results["proposed"] = proposed
    results["existing"] = existing
    results["state"] = state
    results["commands"] = commands
    results["changed"] = changed
    results["end_state"] = end_state

    safe_exit(module, device, **results)