예제 #1
0
class PortChannelTestCase(BaseFeatureCase):

    @mock.patch('pyhpecw7.comware.HPCOM7')
    def setUp(self,  mock_device):
        self.device = mock_device
        self.rpc = Portchannel(self.device, R_GROUP_ID, 'routed')
        self.bpc = Portchannel(self.device, B_GROUP_ID, 'bridged')

    def test_init(self):
        self.assertEqual(self.rpc.pc_type, 'routed')
        self.assertEqual(self.bpc.pc_type, 'bridged')


    def test_get_portchannels(self):
        expected_get, get_reply = self.xml_get_and_reply('portchannel_list')
        self.device.get.return_value = get_reply

        expected = ['3', '100']

        result = self.bpc.get_portchannels()

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

    @mock.patch.object(Portchannel, 'get_interface_from_index')
    @mock.patch.object(Portchannel, 'get_lacp_mode_by_name')
    def test_get_config(self, mock_lacp_mode, mock_iface_from_index):
        mock_lacp_mode.return_value = 'active'

        iface_list = ['FortyGigE1/0/2', 'FortyGigE1/0/1']
        def mock_iface_gen(self):
            return iface_list.pop()

        mock_iface_from_index.side_effect = mock_iface_gen

        expected_get, get_reply = self.xml_get_and_reply('portchannel')
        self.device.get.return_value = get_reply

        expected = {
            'min_ports': None,
            'max_ports': None,
            'lacp_modes_by_interface': [
                {
                    'interface': 'FortyGigE1/0/1',
                    'lacp_mode': 'active'
                },
                {
                    'interface': 'FortyGigE1/0/2',
                    'lacp_mode': 'active'
                }
            ],
            'nc_groupid': '100',
            'mode': 'static',
            'members': [
                'FortyGigE1/0/1',
                'FortyGigE1/0/2'
            ],
            'lacp_edge': 'disabled',
            'pc_index': '33736',
            'groupid': '100'
        }

        result = self.bpc.get_config()

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

    @mock.patch.object(Portchannel, 'get_interface_from_index')
    def test_all_members(self, mock_iface_from_index):
        iface_list = ['FortyGigE1/0/2', 'FortyGigE1/0/1']
        def mock_iface_gen(self):
            return iface_list.pop()

        mock_iface_from_index.side_effect = mock_iface_gen

        expected_get, get_reply = self.xml_get_and_reply('portchannel_all_members')
        self.device.get.return_value = get_reply

        expected = ['FortyGigE1/0/1', 'FortyGigE1/0/2']
        result = self.bpc.get_all_members()

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

    @mock.patch.object(Portchannel, 'get_interface_from_index')
    def test_all_members_ifindex(self, mock_iface_from_index):
        iface_list = ['FortyGigE1/0/2', 'FortyGigE1/0/1']
        def mock_iface_gen(self):
            return iface_list.pop()

        mock_iface_from_index.side_effect = mock_iface_gen

        expected_get, get_reply = self.xml_get_and_reply('portchannel_all_members')
        self.device.get.return_value = get_reply

        expected = ['1', '5']
        result = self.bpc.get_all_members(list_type='ifindex')

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

    @mock.patch.object(Portchannel, 'get_interface_from_index')
    def test_all_members_asdict(self, mock_iface_from_index):
        iface_list = ['FortyGigE1/0/2', 'FortyGigE1/0/1']
        def mock_iface_gen(self):
            return iface_list.pop()

        mock_iface_from_index.side_effect = mock_iface_gen

        expected_get, get_reply = self.xml_get_and_reply('portchannel_all_members')
        self.device.get.return_value = get_reply

        expected = {'FortyGigE1/0/2': '100', 'FortyGigE1/0/1': '100'}
        result = self.bpc.get_all_members(asdict=True)

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

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_lacp_mode_by_name_passive(self, mock_index_from_iface):
        mock_index_from_iface.return_value = '41'

        expected_get, get_reply = self.xml_get_and_reply('portchannel_lacp_mode_by_name_passive')
        self.device.get.return_value = get_reply

        expected = 'passive'
        result = self.bpc.get_lacp_mode_by_name('FortyGigE1/0/11')

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

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_lacp_mode_by_name_active(self, mock_index_from_iface):
        mock_index_from_iface.return_value = '1'

        expected_get, get_reply = self.xml_get_and_reply('portchannel_lacp_mode_by_name_active')
        self.device.get.return_value = get_reply

        expected = 'active'
        result = self.bpc.get_lacp_mode_by_name('FortyGigE1/0/1')

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

    def test_interface_from_index(self):
        expected_get, get_reply = self.xml_get_and_reply('portchannel_interface_from_index')
        self.device.get.return_value = get_reply

        expected = 'FortyGigE1/0/11'
        result = self.bpc.get_interface_from_index('41')

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

    @mock.patch('pyhpecw7.features.portchannel.Interface')
    def test_index_from_Interface(self, mock_iface):
        mock_iface.return_value.iface_index = '96'

        result = self.bpc.get_index_from_interface('FortyGigE1/0/1')
        self.assertEqual(result, '96')

        # check local caching
        mock_iface.return_value.iface_index = '1111'

        result = self.bpc.get_index_from_interface('FortyGigE1/0/1')
        self.assertEqual(result, '96')

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_add_lagg_member(self, mock_index_from_iface):
        mock_index_from_iface.return_value = '1'
        expected = self.read_config_xml('portchannel_lagg_member')
        result = self.bpc._add_lagg_member('FortyGigE1/0/1', lacp='active')

        self.assert_elements_equal(result, expected)

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_build_config(self, mock_index_from_iface):
        index_list = ['1', '5']
        def mock_index_gen(interface):
            return index_list.pop(0)

        mock_index_from_iface.side_effect = mock_index_gen

        expected_call = self.read_config_xml('portchannel_basic')
        self.bpc._build_config('present', members=['FortyGigE1/0/1', 'FortyGigE1/0/2'])

        self.assert_config_request(expected_call)

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_build_config_stage(self, mock_index_from_iface):
        index_list = ['1', '5']
        def mock_index_gen(interface):
            return index_list.pop(0)

        mock_index_from_iface.side_effect = mock_index_gen

        expected_call = self.read_config_xml('portchannel_basic')
        self.bpc._build_config('present', stage=True, members=['FortyGigE1/0/1', 'FortyGigE1/0/2'])

        self.assert_stage_request(expected_call, 'edit_config')

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_build_config_remove_members(self, mock_index_from_iface):
        index_list = ['1', '5', '9']
        def mock_index_gen(interface):
            return index_list.pop(0)

        mock_index_from_iface.side_effect = mock_index_gen

        self.bpc.members_to_remove = ['FortyGigE1/0/3']

        expected_call = self.read_config_xml('portchannel_basic_remove_members')
        self.bpc._build_config('present', members=['FortyGigE1/0/1', 'FortyGigE1/0/2'])

        self.assert_config_request(expected_call)

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_build_config_change_lacp(self, mock_index_from_iface):
        index_list = ['1', '5', '9']
        def mock_index_gen(interface):
            return index_list.pop(0)

        mock_index_from_iface.side_effect = mock_index_gen

        self.bpc.desired_lacp_mode = 'bridged'

        expected_call = self.read_config_xml('portchannel_change_lacp')
        self.bpc._build_config('present', members=['FortyGigE1/0/1', 'FortyGigE1/0/2'], lacp_to_change=['FortyGigE1/0/1'])

        self.assert_config_request(expected_call)

    def test_build_config_no_members(self):
        expected_call = self.read_config_xml('portchannel_no_members')
        self.bpc._build_config('present')

        self.assert_config_request(expected_call)

    def test_build_config_remove(self):
        expected_call = self.read_config_xml('portchannel_remove')
        self.bpc._build_config('absent')

        self.assert_config_request(expected_call)

    def test_build_config_min_max(self):
        expected_xml_call = self.read_config_xml('portchannel_no_members')
        expected_cli_call = ['interface Bridge-Aggregation 100', 'link-aggregation selected-port minimum 2', 'link-aggregation selected-port maximum 8']

        self.bpc._build_config('present', min_ports='2', max_ports='8')

        self.assert_config_request(expected_xml_call)
        self.bpc.device.cli_config.assert_called_with(expected_cli_call)

    @mock.patch.object(Portchannel, '_build_config')
    def test_build(self, mock_build_config):
        kwargs = dict(members=['FortyGigE1/0/1', 'FortyGigE1/0/2'])
        self.bpc.build(**kwargs)
        mock_build_config.assert_called_with(state='present', stage=False, **kwargs)

    @mock.patch.object(Portchannel, '_build_config')
    def test_remove(self, mock_build_config):
        self.bpc.remove()
        mock_build_config.assert_called_with(state='absent', stage=False)

    @mock.patch('pyhpecw7.features.portchannel.Interface')
    @mock.patch.object(Portchannel, 'get_all_members')
    def test_param_check(self, mock_get_all_members, mock_iface):
        mock_iface.return_value.get_config.return_value = {'type': 'bridged'}
        mock_get_all_members.return_value = {'FortyGigE1/0/1': B_GROUP_ID}
        self.bpc.param_check(members=['FortyGigE1/0/1'])

        mock_iface.return_value.get_config.return_value = {'type': 'routed'}
        with self.assertRaises(InvalidPortType):
            self.bpc.param_check(members=['FortyGigE1/0/1'])

        mock_iface.return_value.get_config.return_value = {'type': 'bridged'}
        mock_get_all_members.return_value = {'FortyGigE1/0/1': '300'}
        with self.assertRaises(AggregationGroupError):
            self.bpc.param_check(members=['FortyGigE1/0/1'])
예제 #2
0
class PortChannelTestCase(BaseFeatureCase):

    @mock.patch('pyhpecw7.comware.HPCOM7')
    def setUp(self,  mock_device):
        self.device = mock_device
        self.rpc = Portchannel(self.device, R_GROUP_ID, 'routed')
        self.bpc = Portchannel(self.device, B_GROUP_ID, 'bridged')

    def test_init(self):
        self.assertEqual(self.rpc.pc_type, 'routed')
        self.assertEqual(self.bpc.pc_type, 'bridged')


    def test_get_portchannels(self):
        expected_get, get_reply = self.xml_get_and_reply('portchannel_list')
        self.device.get.return_value = get_reply

        expected = ['3', '100']

        result = self.bpc.get_portchannels()

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

    @mock.patch.object(Portchannel, 'get_interface_from_index')
    @mock.patch.object(Portchannel, 'get_lacp_mode_by_name')
    def test_get_config(self, mock_lacp_mode, mock_iface_from_index):
        mock_lacp_mode.return_value = 'active'

        iface_list = ['FortyGigE1/0/2', 'FortyGigE1/0/1']
        def mock_iface_gen(self):
            return iface_list.pop()

        mock_iface_from_index.side_effect = mock_iface_gen

        expected_get, get_reply = self.xml_get_and_reply('portchannel')
        self.device.get.return_value = get_reply

        expected = {
            'min_ports': None,
            'max_ports': None,
            'lacp_modes_by_interface': [
                {
                    'interface': 'FortyGigE1/0/1',
                    'lacp_mode': 'active'
                },
                {
                    'interface': 'FortyGigE1/0/2',
                    'lacp_mode': 'active'
                }
            ],
            'nc_groupid': '100',
            'mode': 'static',
            'members': [
                'FortyGigE1/0/1',
                'FortyGigE1/0/2'
            ],
            'lacp_edge': 'disabled',
            'pc_index': '33736',
            'groupid': '100'
        }

        result = self.bpc.get_config()

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

    @mock.patch.object(Portchannel, 'get_interface_from_index')
    def test_all_members(self, mock_iface_from_index):
        iface_list = ['FortyGigE1/0/2', 'FortyGigE1/0/1']
        def mock_iface_gen(self):
            return iface_list.pop()

        mock_iface_from_index.side_effect = mock_iface_gen

        expected_get, get_reply = self.xml_get_and_reply('portchannel_all_members')
        self.device.get.return_value = get_reply

        expected = ['FortyGigE1/0/1', 'FortyGigE1/0/2']
        result = self.bpc.get_all_members()

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

    @mock.patch.object(Portchannel, 'get_interface_from_index')
    def test_all_members_ifindex(self, mock_iface_from_index):
        iface_list = ['FortyGigE1/0/2', 'FortyGigE1/0/1']
        def mock_iface_gen(self):
            return iface_list.pop()

        mock_iface_from_index.side_effect = mock_iface_gen

        expected_get, get_reply = self.xml_get_and_reply('portchannel_all_members')
        self.device.get.return_value = get_reply

        expected = ['1', '5']
        result = self.bpc.get_all_members(list_type='ifindex')

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

    @mock.patch.object(Portchannel, 'get_interface_from_index')
    def test_all_members_asdict(self, mock_iface_from_index):
        iface_list = ['FortyGigE1/0/2', 'FortyGigE1/0/1']
        def mock_iface_gen(self):
            return iface_list.pop()

        mock_iface_from_index.side_effect = mock_iface_gen

        expected_get, get_reply = self.xml_get_and_reply('portchannel_all_members')
        self.device.get.return_value = get_reply

        expected = {'FortyGigE1/0/2': '100', 'FortyGigE1/0/1': '100'}
        result = self.bpc.get_all_members(asdict=True)

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

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_lacp_mode_by_name_passive(self, mock_index_from_iface):
        mock_index_from_iface.return_value = '41'

        expected_get, get_reply = self.xml_get_and_reply('portchannel_lacp_mode_by_name_passive')
        self.device.get.return_value = get_reply

        expected = 'passive'
        result = self.bpc.get_lacp_mode_by_name('FortyGigE1/0/11')

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

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_lacp_mode_by_name_active(self, mock_index_from_iface):
        mock_index_from_iface.return_value = '1'

        expected_get, get_reply = self.xml_get_and_reply('portchannel_lacp_mode_by_name_active')
        self.device.get.return_value = get_reply

        expected = 'active'
        result = self.bpc.get_lacp_mode_by_name('FortyGigE1/0/1')

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

    def test_interface_from_index(self):
        expected_get, get_reply = self.xml_get_and_reply('portchannel_interface_from_index')
        self.device.get.return_value = get_reply

        expected = 'FortyGigE1/0/11'
        result = self.bpc.get_interface_from_index('41')

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

    @mock.patch('pyhpecw7.features.portchannel.Interface')
    def test_index_from_Interface(self, mock_iface):
        mock_iface.return_value.iface_index = '96'

        result = self.bpc.get_index_from_interface('FortyGigE1/0/1')
        self.assertEqual(result, '96')

        # check local caching
        mock_iface.return_value.iface_index = '1111'

        result = self.bpc.get_index_from_interface('FortyGigE1/0/1')
        self.assertEqual(result, '96')

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_add_lagg_member(self, mock_index_from_iface):
        mock_index_from_iface.return_value = '1'
        expected = self.read_config_xml('portchannel_lagg_member')
        result = self.bpc._add_lagg_member('FortyGigE1/0/1', lacp='active')

        self.assert_elements_equal(result, expected)

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_build_config(self, mock_index_from_iface):
        index_list = ['1', '5']
        def mock_index_gen(interface):
            return index_list.pop(0)

        mock_index_from_iface.side_effect = mock_index_gen

        expected_call = self.read_config_xml('portchannel_basic')
        self.bpc._build_config('present', members=['FortyGigE1/0/1', 'FortyGigE1/0/2'])

        self.assert_config_request(expected_call)

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_build_config_stage(self, mock_index_from_iface):
        index_list = ['1', '5']
        def mock_index_gen(interface):
            return index_list.pop(0)

        mock_index_from_iface.side_effect = mock_index_gen

        expected_call = self.read_config_xml('portchannel_basic')
        self.bpc._build_config('present', stage=True, members=['FortyGigE1/0/1', 'FortyGigE1/0/2'])

        self.assert_stage_request(expected_call, 'edit_config')

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_build_config_remove_members(self, mock_index_from_iface):
        index_list = ['1', '5', '9']
        def mock_index_gen(interface):
            return index_list.pop(0)

        mock_index_from_iface.side_effect = mock_index_gen

        self.bpc.members_to_remove = ['FortyGigE1/0/3']

        expected_call = self.read_config_xml('portchannel_basic_remove_members')
        self.bpc._build_config('present', members=['FortyGigE1/0/1', 'FortyGigE1/0/2'])

        self.assert_config_request(expected_call)

    @mock.patch.object(Portchannel, 'get_index_from_interface')
    def test_build_config_change_lacp(self, mock_index_from_iface):
        index_list = ['1', '5', '9']
        def mock_index_gen(interface):
            return index_list.pop(0)

        mock_index_from_iface.side_effect = mock_index_gen

        self.bpc.desired_lacp_mode = 'bridged'

        expected_call = self.read_config_xml('portchannel_change_lacp')
        self.bpc._build_config('present', members=['FortyGigE1/0/1', 'FortyGigE1/0/2'], lacp_to_change=['FortyGigE1/0/1'])

        self.assert_config_request(expected_call)

    def test_build_config_no_members(self):
        expected_call = self.read_config_xml('portchannel_no_members')
        self.bpc._build_config('present')

        self.assert_config_request(expected_call)

    def test_build_config_remove(self):
        expected_call = self.read_config_xml('portchannel_remove')
        self.bpc._build_config('absent')

        self.assert_config_request(expected_call)

    def test_build_config_min_max(self):
        expected_xml_call = self.read_config_xml('portchannel_no_members')
        expected_cli_call = ['interface Bridge-Aggregation 100', 'link-aggregation selected-port minimum 2', 'link-aggregation selected-port maximum 8']

        self.bpc._build_config('present', min_ports='2', max_ports='8')

        self.assert_config_request(expected_xml_call)
        self.bpc.device.cli_config.assert_called_with(expected_cli_call)

    @mock.patch.object(Portchannel, '_build_config')
    def test_build(self, mock_build_config):
        kwargs = dict(members=['FortyGigE1/0/1', 'FortyGigE1/0/2'])
        self.bpc.build(**kwargs)
        mock_build_config.assert_called_with(state='present', stage=False, **kwargs)

    @mock.patch.object(Portchannel, '_build_config')
    def test_remove(self, mock_build_config):
        self.bpc.remove()
        mock_build_config.assert_called_with(state='absent', stage=False)

    @mock.patch('pyhpecw7.features.portchannel.Interface')
    @mock.patch.object(Portchannel, 'get_all_members')
    def test_param_check(self, mock_get_all_members, mock_iface):
        mock_iface.return_value.get_config.return_value = {'type': 'bridged'}
        mock_get_all_members.return_value = {'FortyGigE1/0/1': B_GROUP_ID}
        self.bpc.param_check(members=['FortyGigE1/0/1'])

        mock_iface.return_value.get_config.return_value = {'type': 'routed'}
        with self.assertRaises(InvalidPortType):
            self.bpc.param_check(members=['FortyGigE1/0/1'])

        mock_iface.return_value.get_config.return_value = {'type': 'bridged'}
        mock_get_all_members.return_value = {'FortyGigE1/0/1': '300'}
        with self.assertRaises(AggregationGroupError):
            self.bpc.param_check(members=['FortyGigE1/0/1'])
예제 #3
0
 def setUp(self,  mock_device):
     self.device = mock_device
     self.rpc = Portchannel(self.device, R_GROUP_ID, 'routed')
     self.bpc = Portchannel(self.device, B_GROUP_ID, 'bridged')
예제 #4
0
 def setUp(self,  mock_device):
     self.device = mock_device
     self.rpc = Portchannel(self.device, R_GROUP_ID, 'routed')
     self.bpc = Portchannel(self.device, B_GROUP_ID, 'bridged')
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            link_type=dict(required=True,
                           choices=['access', 'trunk']),
            pvid=dict(type='str'),
            permitted_vlans=dict(type='str'),
            state=dict(choices=['present', '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')

    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('link_type') == 'access':
            if module.params.get('permitted_vlans'):
                safe_fail(module,
                          msg='Access interfaces don\'t take'
                          + ' permitted vlan lists.')

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

    # Make sure vlan exists
    pvid = module.params.get('pvid')
    if pvid and state != 'default':
        try:
            vlan = Vlan(device, pvid)
            if not vlan.get_config():
                safe_fail(module, device,
                          msg='Vlan {0} does not exist,'.format(pvid)
                          + ' Use vlan module to create it.')
        except PYHPError as e:
            module.fail_json(msg=str(e),
                             descr='Error initializing Vlan object'
                             + ' or getting current vlan config.')

    # Make sure port is not part of port channel
    try:
        portchannel = Portchannel(device, '99', 'bridged')
        pc_list = portchannel.get_all_members()
    except PYHPError as e:
        module.fail_json(msg=str(e),
                         descr='Error getting port channel information.')
    if name in pc_list:
        safe_fail(module, device,
                  msg='{0} is currently part of a port channel.'.format(name)
                  + ' Changes should be made to the port channel interface.')

    try:
        switchport = Switchport(device, name)
    except PYHPError as e:
        safe_fail(module, device, msg=str(e),
                  descr='Error initialzing Switchport object.')

    # Make sure interface exists and is ethernet
    if not switchport.interface.iface_exists:
        safe_fail(module, device,
                  msg='{0} doesn\'t exist on the device.'.format(name))

    # Make sure interface is in bridged mode
    try:
        if_info = switchport.interface.get_config()
    except PYHPError as e:
        safe_fail(module, device, msg=str(e),
                  descr='Error getting current interface config.')

    if if_info.get('type') != 'bridged':
        safe_fail(module, device, msg='{0} is not in bridged mode.'.format(name)
                  + ' Please use the interface module to change that.')

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

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

    if state == 'present':
        delta = dict(set(proposed.items()) - set(existing.items()))
        if delta:
            delta['link_type'] = proposed.get('link_type')
            pvid = proposed.get('pvid')
            if pvid:
                delta['pvid'] = pvid

            switchport.build(stage=True, **delta)
    elif state == 'default':
        defaults = switchport.get_default()
        delta = dict(set(existing.items()) - set(defaults.items()))
        if delta:
            switchport.default(stage=True)

    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 = switchport.get_config()
            except PYHPError as e:
                safe_fail(module, device, msg=str(e),
                          descr='Error during command 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)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            link_type=dict(required=True,
                           choices=['access', 'trunk']),
            pvid=dict(type='str'),
            permitted_vlans=dict(type='str'),
            state=dict(choices=['present', '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')

    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('link_type') == 'access':
            if module.params.get('permitted_vlans'):
                safe_fail(module,
                          msg='Access interfaces don\'t take'
                          + ' permitted vlan lists.')

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

    # Make sure vlan exists
    pvid = module.params.get('pvid')
    if pvid and state != 'default':
        try:
            vlan = Vlan(device, pvid)
            if not vlan.get_config():
                safe_fail(module, device,
                          msg='Vlan {0} does not exist,'.format(pvid)
                          + ' Use vlan module to create it.')
        except PYHPError as e:
            module.fail_json(msg=str(e),
                             descr='Error initializing Vlan object'
                             + ' or getting current vlan config.')

    # Make sure port is not part of port channel
    try:
        portchannel = Portchannel(device, '99', 'bridged')
        pc_list = portchannel.get_all_members()
    except PYHPError as e:
        module.fail_json(msg=str(e),
                         descr='Error getting port channel information.')
    if name in pc_list:
        safe_fail(module, device,
                  msg='{0} is currently part of a port channel.'.format(name)
                  + ' Changes should be made to the port channel interface.')

    try:
        switchport = Switchport(device, name)
    except PYHPError as e:
        safe_fail(module, device, msg=str(e),
                  descr='Error initialzing Switchport object.')

    # Make sure interface exists and is ethernet
    if not switchport.interface.iface_exists:
        safe_fail(module, device,
                  msg='{0} doesn\'t exist on the device.'.format(name))

    # Make sure interface is in bridged mode
    try:
        if_info = switchport.interface.get_config()
    except PYHPError as e:
        safe_fail(module, device, msg=str(e),
                  descr='Error getting current interface config.')

    if if_info.get('type') != 'bridged':
        safe_fail(module, device, msg='{0} is not in bridged mode.'.format(name)
                  + ' Please use the interface module to change that.')

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

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

    if state == 'present':
        delta = dict(set(proposed.iteritems()).difference(
            existing.iteritems()))
        if delta:
            delta['link_type'] = proposed.get('link_type')
            pvid = proposed.get('pvid')
            if pvid:
                delta['pvid'] = pvid

            switchport.build(stage=True, **delta)
    elif state == 'default':
        defaults = switchport.get_default()
        delta = dict(set(existing.iteritems()).difference(
            defaults.iteritems()))
        if delta:
            switchport.default(stage=True)

    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 = switchport.get_config()
            except PYHPError as e:
                safe_fail(module, device, msg=str(e),
                          descr='Error during command 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)
예제 #7
0
def main():
    module = AnsibleModule(argument_spec=dict(
        group=dict(required=True, type='str'),
        members=dict(required=False, type='list'),
        mode=dict(required=False, choices=['static', 'dynamic']),
        type=dict(required=True, choices=['bridged', 'routed']),
        lacp_mode=dict(required=False, choices=['active', 'passive']),
        lacp_edge=dict(required=False, choices=['enabled', 'disabled']),
        min_ports=dict(required=False, type='str'),
        max_ports=dict(required=False, type='str'),
        state=dict(choices=['present', 'absent'], default='present'),
        port=dict(default=830, type='int'),
        hostname=dict(required=True),
        username=dict(required=True),
        password=dict(required=True),
    ),
                           supports_check_mode=True)
    if not HAS_PYHP:
        module.fail_json(msg='There was a problem loading from the pyhpecw7 ' +
                         'module.',
                         error=str(ie))

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

    device_args = dict(host=hostname,
                       username=username,
                       password=password,
                       port=port)

    device = HPCOM7(**device_args)

    groupid = module.params['group']
    members = module.params['members']
    lacp_mode = module.params['lacp_mode']
    mode = module.params['mode']
    lacp_edge = module.params['lacp_edge']
    min_ports = module.params['min_ports']
    max_ports = module.params['max_ports']

    pc_type = module.params['type']

    state = module.params['state']

    if members:
        if not isinstance(members, list):
            module.fail_json(
                msg='members param must be a list.  YAML format ' +
                '\nmust be used within the playbook')
    if state == 'present' and not members:
        module.fail_json(msg='members param required when state=present')

    if mode == 'static' and lacp_mode:
        lacp_mode = None
        lacp_edge = None

    changed = False

    args = dict(groupid=groupid,
                lacp_edge=lacp_edge,
                mode=mode,
                min_ports=min_ports,
                max_ports=max_ports,
                lacp_mode=lacp_mode)

    proposed = dict((k, v) for k, v in args.iteritems() if v is not None)

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

    try:
        portchannel = Portchannel(device, groupid, pc_type)
        portchannel.param_check(members=members, **proposed)
        existing = portchannel.get_config()
    except InvalidPortType as ipte:
        safe_fail(module, device, msg=str(ipte))
    except AggregationGroupError as age:
        safe_fail(module, device, msg=str(age))
    except PYHPError as e:
        safe_fail(module, device, msg=str(e))

    if 'members' in existing.keys():
        existing_members = existing.pop('members')
    else:
        existing_members = []

    if state == 'present':
        delta = get_delta(existing, proposed, existing_members, members,
                          lacp_mode, portchannel)
        if delta:
            # delta['groupid'] = groupid
            portchannel.build(stage=True, **delta)
    elif state == 'absent':
        if existing:
            portchannel.remove(stage=True)

    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 = portchannel.get_config()
            except PYHPError as e:
                safe_fail(module,
                          device,
                          msg=str(e),
                          descr='error during execution')
            changed = True

    proposed['members'] = members
    proposed['type'] = pc_type

    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)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            group=dict(required=True, type='str'),
            members=dict(required=False),
            mode=dict(required=False, choices=['static', 'dynamic']),
            type=dict(required=False, choices=['bridged', 'routed']),
            lacp_mode=dict(required=False, choices=['active', 'passive']),
            lacp_edge=dict(required=False, choices=['enabled', 'disabled']),
            min_ports=dict(required=False, type='str'),
            max_ports=dict(required=False, type='str'),
            state=dict(choices=['present', 'absent'], default='present'),
            port=dict(default=830, type='int'),
            hostname=dict(required=True),
            username=dict(required=True),
            password=dict(required=True),
        ),
        supports_check_mode=True
    )
    if not HAS_PYHP:
        module.fail_json(msg='There was a problem loading from the pyhpecw7 '
                         + 'module.', error=str(ie))

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

    device_args = dict(host=hostname, username=username,
                       password=password, port=port)

    device = HPCOM7(**device_args)

    groupid = module.params['group']
    members = module.params['members']
    lacp_mode = module.params['lacp_mode']
    mode = module.params['mode']
    lacp_edge = module.params['lacp_edge']
    min_ports = module.params['min_ports']
    max_ports = module.params['max_ports']

    pc_type = module.params['type']

    state = module.params['state']

    if members:
        if not isinstance(members, list):
            module.fail_json(msg='members param must be a list.  YAML format '
                             + '\nmust be used within the playbook')
    if state == 'present' and not members:
        module.fail_json(msg='members param required when state=present')

    if mode == 'static' and lacp_mode:
        lacp_mode = None
        lacp_edge = None

    changed = False

    args = dict(groupid=groupid, lacp_edge=lacp_edge, mode=mode,
                min_ports=min_ports, max_ports=max_ports, lacp_mode=lacp_mode)

    proposed = dict((k, v) for k, v in args.iteritems() if v is not None)

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

    try:
        portchannel = Portchannel(device, groupid, pc_type)
        portchannel.param_check(members=members, **proposed)
        existing = portchannel.get_config()
    except InvalidPortType as ipte:
        safe_fail(module, device, msg=str(ipte))
    except AggregationGroupError as age:
        safe_fail(module, device, msg=str(age))
    except PYHPError as e:
        safe_fail(module, device, msg=str(e))

    if 'members' in existing.keys():
        existing_members = existing.pop('members')
    else:
        existing_members = []

    if state == 'present':
        delta = get_delta(existing, proposed, existing_members,
                          members, lacp_mode, portchannel)
        if delta:
            # delta['groupid'] = groupid
            portchannel.build(stage=True, **delta)
    elif state == 'absent':
        if existing:
            portchannel.remove(stage=True)

    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 = portchannel.get_config()
            except PYHPError as e:
                safe_fail(module, device, msg=str(e),
                          descr='error during execution')
            changed = True

    proposed['members'] = members
    proposed['type'] = pc_type

    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)