def test_add_missing_ethernet_networks(self, mock_ethnet_get, mock_uplink_update):
        ethernet_to_add = [
            'eth1',
            'eth2',
        ]
        uplink = {
            'name': 'UplinkName',
            'networkUris': ['/rest/ethernet-networks/d34dcf5e-0d8e-441c-b00d-e1dd6a067188', ]
        }
        uplink_to_update = {
            'networkUris': ['/rest/ethernet-networks/5f14bf27-f839-4e9f-9ec8-9f0e0b413939',
                            '/rest/ethernet-networks/d34dcf5e-0d8e-441c-b00d-e1dd6a067188', ]
        }

        eth1 = self._ethernet_networks
        eth2 = EthernetNetworks(self.connection)
        eth1.data = {'name': 'eth1', 'uri': '/rest/ethernet-networks/d34dcf5e-0d8e-441c-b00d-e1dd6a067188'}
        eth2.data = {'name': 'eth2', 'uri': '/rest/ethernet-networks/5f14bf27-f839-4e9f-9ec8-9f0e0b413939'}
        result_get_enet = [eth1, eth2]

        self._uplink_sets.data = uplink
        mock_ethnet_get.side_effect = result_get_enet

        self._uplink_sets.add_ethernet_networks(ethernet_to_add)
        mock_uplink_update.assert_called_once_with(uplink_to_update)
    def test_add_a_list_of_ethernet_networks(self, mock_ethnet_get,
                                             mock_uplink_update):
        ethernet_to_add = [
            'eth1',
            'eth2',
        ]

        uplink_to_update = {
            'networkUris': [
                '/rest/ethernet-networks/134dcf5e-0d8e-441c-b00d-e1dd6a067188',
                '/rest/ethernet-networks/5f14bf27-f839-4e9f-9ec8-9f0e0b413939',
            ]
        }

        eth1 = self._ethernet_networks
        eth2 = EthernetNetworks(self.connection)
        eth1.data = {
            'name': 'eth1',
            'uri':
            '/rest/ethernet-networks/134dcf5e-0d8e-441c-b00d-e1dd6a067188'
        }
        eth2.data = {
            'name': 'eth2',
            'uri':
            '/rest/ethernet-networks/5f14bf27-f839-4e9f-9ec8-9f0e0b413939'
        }
        result_get_enet = [eth1, eth2]

        mock_ethnet_get.side_effect = result_get_enet

        self._uplink_sets.add_ethernet_networks(ethernet_to_add)
        mock_uplink_update.assert_called_once_with(uplink_to_update)
Пример #3
0
 def __init__(self, con):
     self._connection = con
     self._client = ResourceClient(con, self.URI)
     self._ethernet_network = EthernetNetworks(con)
     self.__default_values = {
         "type": "uplink-setV3",
     }
    def test_get_spt_vlan_network_interface(self):
        spt_obj = ServerProfileTemplate(self.oneview_client,
                                        self.server_profile_template)
        self.oneview_client.server_profile_templates.get_by_id.return_value = \
            spt_obj

        ethernet_obj = EthernetNetworks(self.oneview_client,
                                        self.ethernet_network_mockup)
        self.oneview_client.ethernet_networks.get_by_id.return_value = \
            ethernet_obj

        response = self.client.get(
            "/redfish/v1/CompositionService/ResourceBlocks"
            "/1f0ca9ef-7f81-45e3-9d64-341b46cf87e0"
            "/EthernetInterfaces/1/VLANs/19638712-679d-4232-9743-c7cb6c7bf718")

        result = json.loads(response.data.decode("utf-8"))

        self.assertEqual(status.HTTP_200_OK, response.status_code)
        self.assertEqual("application/json", response.mimetype)
        self.assertEqualMockup(self.expected_vlan_network_interface_spt,
                               result)
        self.oneview_client.server_profile_templates.get_by_id.assert_called_with(
            self.server_profile_template["uri"].split("/")[-1])
        self.oneview_client.ethernet_networks.get_by_id.assert_called_with(
            self.expected_vlan_network_interface_spt["Id"])
Пример #5
0
 def __init__(self, con):
     self._connection = con
     self._client = ResourceClient(con, self.URI)
     self._ethernet_network = EthernetNetworks(con)
     self.__default_values = {
         "type": "uplink-setV3",
     }
    def ethernet_networks(self):
        """
        Gets the EthernetNetworks API client.

        Returns:
            EthernetNetworks:
        """
        return EthernetNetworks(self.__connection)
 def setUp(self):
     self.host = '127.0.0.1'
     self.connection = connection(self.host)
     self._uplink_sets = UplinkSets(self.connection)
     self._uplink_sets.data = {
         'uri': '/rest/ul-inksets/ad28cf21-8b15-4f92-bdcf-51cb2042db32'
     }
     self._ethernet_networks = EthernetNetworks(self.connection)
Пример #8
0
    def ethernet_networks(self):
        """
        Gets the EthernetNetworks API client.

        Returns:
            EthernetNetworks:
        """
        if not self.__ethernet_networks:
            self.__ethernet_networks = EthernetNetworks(self.__connection)
        return self.__ethernet_networks
 def __init__(self, connection, data=None):
     super(UplinkSets, self).__init__(connection, data)
     self._ethernet_networks = EthernetNetworks(connection)
Пример #10
0
 def setUp(self):
     self.host = '127.0.0.1'
     self.connection = connection(self.host)
     self._ethernet_networks = EthernetNetworks(self.connection)
Пример #11
0
class EthernetNetworksTest(TestCase):
    def setUp(self):
        self.host = '127.0.0.1'
        self.connection = connection(self.host)
        self._ethernet_networks = EthernetNetworks(self.connection)

    @mock.patch.object(ResourceClient, 'get_all')
    def test_get_all_called_once(self, mock_get_all):
        filter = 'name=TestName'
        sort = 'name:ascending'

        self._ethernet_networks.get_all(2, 500, filter, sort)

        mock_get_all.assert_called_once_with(2, 500, filter=filter, sort=sort)

    @mock.patch.object(ResourceClient, 'create')
    def test_create_should_use_given_values(self, mock_create):
        resource = {
            'vlanId': 10,
            'name': 'OneViewSDK Test Ethernet Network',
            "ethernetNetworkType": "Tagged",
            "purpose": "Management",
            "connectionTemplateUri": None,
            "smartLink": False,
            "type": "ethernet-networkV3",
            "privateNetwork": False
        }
        resource_rest_call = resource.copy()
        mock_create.return_value = {}

        self._ethernet_networks.create(resource, 12)
        mock_create.assert_called_once_with(
            resource_rest_call,
            timeout=12,
            default_values=self._ethernet_networks.DEFAULT_VALUES)

    @mock.patch.object(ResourceClient, 'create')
    def test_create_should_use_default_values(self, mock_create):
        resource = {
            'name': 'OneViewSDK Test Ethernet Network',
        }

        mock_create.return_value = {}

        self._ethernet_networks.create(resource)

        mock_create.assert_called_once_with(
            resource,
            timeout=-1,
            default_values=self._ethernet_networks.DEFAULT_VALUES)

    @mock.patch.object(ResourceClient, 'create')
    @mock.patch.object(ResourceClient, 'get_all')
    def test_create_bulk(self, mock_get_all, mock_create):
        resource = {
            'vlanIdRange': '1-10',
            'purpose': 'General',
            'namePrefix': 'TestNetwork',
            'smartLink': False,
            'privateNetwork': False,
            'bandwidth': {
                'maximumBandwidth': 10000,
                'typicalBandwidth': 2000
            },
            'type': 'bulk-ethernet-network'
        }
        resource_rest_call = resource.copy()
        mock_create.return_value = {}
        mock_get_all.return_value = []

        self._ethernet_networks.create_bulk(resource, 27)

        mock_create.assert_called_once_with(resource_rest_call,
                                            uri='/rest/ethernet-networks/bulk',
                                            timeout=27)
        mock_get_all.assert_called_once_with(
            0,
            -1,
            filter='"\'name\' matches \'TestNetwork\\_%\'"',
            sort='vlanId:ascending')

    @mock.patch.object(ResourceClient, 'update')
    def test_update_should_use_given_values(self, mock_update):
        resource = {
            'name': 'OneViewSDK Test Ethernet Network',
            'smartLink': False,
            'connectionTemplateUri': None,
            'vlanId': None,
            'privateNetwork': True,
            'ethernetNetworkType': 'Untagged',
            'type': 'ethernet-networkV3',
            'purpose': 'General'
        }
        resource_rest_call = resource.copy()
        mock_update.return_value = {}

        self._ethernet_networks.update(resource, timeout=60)
        mock_update.assert_called_once_with(
            resource_rest_call,
            timeout=60,
            default_values=self._ethernet_networks.DEFAULT_VALUES)

    @mock.patch.object(ResourceClient, 'update')
    def test_update_should_use_default_values(self, mock_update):
        resource = {
            'name': 'OneViewSDK Test Ethernet Network',
        }

        mock_update.return_value = {}

        self._ethernet_networks.update(resource)

        mock_update.assert_called_once_with(
            resource,
            timeout=-1,
            default_values=self._ethernet_networks.DEFAULT_VALUES)

    @mock.patch.object(ResourceClient, 'delete')
    def test_delete_called_once(self, mock_delete):
        id = 'ad28cf21-8b15-4f92-bdcf-51cb2042db32'
        self._ethernet_networks.delete(id, force=False, timeout=-1)

        mock_delete.assert_called_once_with(id, force=False, timeout=-1)

    @mock.patch.object(ResourceClient, 'get_by')
    def test_get_by_called_once(self, mock_get_by):
        self._ethernet_networks.get_by('name',
                                       'OneViewSDK Test Ethernet Network')

        mock_get_by.assert_called_once_with(
            'name', 'OneViewSDK Test Ethernet Network')

    @mock.patch.object(ResourceClient, 'get')
    def test_get_called_once(self, mock_get):
        self._ethernet_networks.get('3518be0e-17c1-4189-8f81-83f3724f6155')

        mock_get.assert_called_once_with(
            '3518be0e-17c1-4189-8f81-83f3724f6155')

    @mock.patch.object(ResourceClient, 'get')
    def test_get_with_uri_called_once(self, mock_get):
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155'
        self._ethernet_networks.get(uri)

        mock_get.assert_called_once_with(uri)

    @mock.patch.object(ResourceClient, 'get')
    def test_get_associated_uplink_groups_uri_called_once_with_id(
            self, mock_get):
        self._ethernet_networks.get_associated_uplink_groups(
            '3518be0e-17c1-4189-8f81-83f3724f6155')
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedUplinkGroups'

        mock_get.assert_called_once_with(uri)

    @mock.patch.object(ResourceClient, 'get')
    def test_get_associated_uplink_groups_uri_called_once_with_uri(
            self, mock_get):
        self._ethernet_networks.get_associated_uplink_groups(
            '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155')
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedUplinkGroups'

        mock_get.assert_called_once_with(uri)

    @mock.patch.object(ResourceClient, 'get')
    def test_get_associated_profiles_called_once_with_id(self, mock_get):
        self._ethernet_networks.get_associated_profiles(
            '3518be0e-17c1-4189-8f81-83f3724f6155')
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedProfiles'

        mock_get.assert_called_once_with(uri)

    @mock.patch.object(ResourceClient, 'get')
    def test_get_associated_profiles_called_once_with_uri(self, mock_get):
        self._ethernet_networks.get_associated_profiles(
            '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155')
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedProfiles'

        mock_get.assert_called_once_with(uri)

    def __mock_enet_gel_all(self):
        return [
            {
                'name': 'TestNetwork_1',
                'vlanId': 1
            },
            {
                'name': 'TestNetwork_2',
                'vlanId': 2
            },
            {
                'name': 'TestNetwork_3',
                'vlanId': 3
            },
            {
                'name': 'TestNetwork_4',
                'vlanId': 4
            },
            {
                'name': 'TestNetwork_5',
                'vlanId': 5
            },
            {
                'name': 'TestNetwork_5',
                'vlanId': 6
            },
            {
                'name': 'TestNetwork_7',
                'vlanId': 7
            },
            {
                'name': 'TestNetwork_8',
                'vlanId': 8
            },
            {
                'name': 'TestNetwork_9',
                'vlanId': 9
            },
            {
                'name': 'TestNetwork_10',
                'vlanId': 10
            },
        ]

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_one_range(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_1',
                'vlanId': 1
            },
            {
                'name': 'TestNetwork_2',
                'vlanId': 2
            },
            {
                'name': 'TestNetwork_3',
                'vlanId': 3
            },
            {
                'name': 'TestNetwork_4',
                'vlanId': 4
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '1-4')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_one_value(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_1',
                'vlanId': 1
            },
            {
                'name': 'TestNetwork_2',
                'vlanId': 2
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '2')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_one_value_and_one_range(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_2',
                'vlanId': 2
            },
            {
                'name': 'TestNetwork_9',
                'vlanId': 9
            },
            {
                'name': 'TestNetwork_10',
                'vlanId': 10
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '2, 9-10')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_multiple_values(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_9',
                'vlanId': 9
            },
            {
                'name': 'TestNetwork_10',
                'vlanId': 10
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '9,10')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_multiple_ranges(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_5',
                'vlanId': 6
            },
            {
                'name': 'TestNetwork_7',
                'vlanId': 7
            },
            {
                'name': 'TestNetwork_9',
                'vlanId': 9
            },
            {
                'name': 'TestNetwork_10',
                'vlanId': 10
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '6-7,9-10')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_one_value(self):
        expected_result = [1, 2, 3, 4, 5]
        result = self._ethernet_networks.dissociate_values_or_ranges('5')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_multiple_values(self):
        expected_result = [6, 7, 9]
        result = self._ethernet_networks.dissociate_values_or_ranges('6,7,9')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_one_range(self):
        expected_result = [6, 7]
        result = self._ethernet_networks.dissociate_values_or_ranges('6-7')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_multiple_ranges(self):
        expected_result = [6, 7, 9, 10]
        result = self._ethernet_networks.dissociate_values_or_ranges(
            '6-7,9-10')
        self.assertEqual(result, expected_result)
Пример #12
0
 def __init__(self, connection, data=None):
     super(UplinkSets, self).__init__(connection, data)
     self._ethernet_networks = EthernetNetworks(connection)
Пример #13
0
class UplinkSets(Resource):
    """
    Uplink Sets API client.

    """
    URI = '/rest/uplink-sets'

    DEFAULT_VALUES = {
        '200': {
            "type": "uplink-setV3"
        },
        '300': {
            "type": "uplink-setV300"
        },
        '500': {
            "type": "uplink-setV300"
        },
        '600': {
            "type": "uplink-setV4"
        },
        '800': {
            "type": "uplink-setV4"
        }
    }

    def __init__(self, connection, data=None):
        super(UplinkSets, self).__init__(connection, data)
        self._ethernet_networks = EthernetNetworks(connection)

    @ensure_resource_client
    def get_ethernet_networks(self):
        """
        Gets a list of associated ethernet networks of an uplink set.

        Args:
            id_or_uri: Can be either the uplink set id or the uplink set uri.

        Returns:
            list: Associated ethernet networks.
        """
        network_uris = self.data.get('networkUris')
        networks = []
        if network_uris:
            for uri in network_uris:
                networks.append(self._ethernet_networks.get_by_uri(uri))
        return networks

    @ensure_resource_client
    def add_ethernet_networks(self, ethernet_names):
        """
        Adds existing ethernet networks to an uplink set.

        Args:
            id_or_uri:
                Can be either the uplink set id or the uplink set uri.
            ethernet_name:
                Could be either one or more ethernet network names.
        Returns:
            dict: The updated uplink set.
        """
        self.__set_ethernet_uris(ethernet_names, operation="add")
        return self.data

    @ensure_resource_client
    def remove_ethernet_networks(self, ethernet_names):
        """
        Remove existing ethernet networks of an uplink set.

        Args:
            id_or_uri:
                Can be either the uplink set id or the uplink set uri.
            ethernet_id_or_uris:
                Could be either one or more ethernet network id or ethernet network uri.

        Returns:
            dict: The updated uplink set.
        """
        self.__set_ethernet_uris(ethernet_names, operation="remove")
        return self.data

    def __set_ethernet_uris(self, ethernet_names, operation="add"):
        """Updates network uris."""
        if not isinstance(ethernet_names, list):
            ethernet_names = [ethernet_names]

        associated_enets = self.data.get('networkUris', [])
        ethernet_uris = []

        for i, enet in enumerate(ethernet_names):
            enet_exists = self._ethernet_networks.get_by_name(enet)
            if enet_exists:
                ethernet_uris.append(enet_exists.data['uri'])
            else:
                raise HPOneViewResourceNotFound(
                    "Ethernet: {} does not exist".foramt(enet))

        if operation == "remove":
            enets_to_update = sorted(
                list(set(associated_enets) - set(ethernet_uris)))
        elif operation == "add":
            enets_to_update = sorted(
                list(set(associated_enets).union(set(ethernet_uris))))
        else:
            raise ValueError(
                "Value {} is not supported as operation. The supported values are: ['add', 'remove']"
            )

        if set(enets_to_update) != set(associated_enets):
            updated_network = {'networkUris': enets_to_update}
            self.update(updated_network)
Пример #14
0
class UplinkSets(object):
    URI = '/rest/uplink-sets'

    def __init__(self, con):
        self._connection = con
        self._client = ResourceClient(con, self.URI)
        self._ethernet_network = EthernetNetworks(con)
        self.__default_values = {
            "type": "uplink-setV3",
        }

    def get_all(self, start=0, count=-1, filter='', sort=''):
        """
        Gets a paginated list of uplink sets based on optional sorting and filtering and is constrained by start and
        count parameters.

        Filters can be used in the URL to control the number of uplink sets that are returned.
        With no filters specified, the API returns all uplink sets.

        Args:
            start:
                The first item to return, using 0-based indexing.
                If not specified, the default is 0 - start with the first available item.
            count:
                The number of resources to return. A count of -1 requests all items.
                The actual number of items in the response might differ from the requested
                count if the sum of start and count exceeds the total number of items.
            filter (list or str):
                A general filter/query string to narrow the list of items returned. The
                default is no filter; all resources are returned.
            sort:
                The sort order of the returned data set. By default, the sort order is based
                on create time with the oldest entry first.

        Returns:
            list: A list of uplink sets.
        """
        return self._client.get_all(start, count, filter=filter, sort=sort)

    def get(self, id_or_uri):
        """
        Gets an uplink set with the specified ID.

        Args:
            id_or_uri: Can be either the uplink set id or the uplink set uri.

        Returns:
            dict: The uplink set.
        """
        return self._client.get(id_or_uri)

    def get_by(self, field, value):
        """
        Gets all uplink sets that match the filter.

        The search is case-insensitive.

        Args:
            field: Field name to filter.
            value: Value to filter.

        Returns:
            list: Uplink sets

        """
        return self._client.get_by(field, value)

    def create(self, resource, timeout=-1):
        """
        Creates an uplink set.

        Args:
            resource (dict): Object to create.
            timeout:
                Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation in
                OneView, just stops waiting for its completion.

        Returns:
            dict: Created resource.
        """
        data = self.__default_values.copy()
        data.update(resource)
        return self._client.create(data, timeout=timeout)

    def update(self, resource, timeout=-1):
        """
        Updates an uplink set.

        Args:
            resource (dict): Resource to update.
            timeout:
                Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation in
                OneView, just stops waiting for its completion.

        Returns:
            dict: Updated resource.
        """
        data = self.__default_values.copy()
        data.update(resource)
        return self._client.update(data, timeout=timeout)

    def delete(self, resource, force=False, timeout=-1):
        """
        Deletes an uplink set. If the uplink set was carrying a Fibre Channel (FC) network, any connections which are
        deployed and using the FC network will be placed into a 'Failed' state.

        Args:
            resource: Resource to delete or the resource ID.
            force:
                 If set to true, the operation completes despite any problems with
                 network connectivity or errors on the resource itself. The default is false.
            timeout:
                Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation in
                OneView, just stops waiting for its completion.

            Returns:
                bool: True if successfully deleted.
        """
        return self._client.delete(resource, force=force, timeout=timeout)

    def get_ethernet_networks(self, id_or_uri):
        """
        Gets a list of associated ethernet networks of an uplink set.

        Args:
            id_or_uri: Can be either the uplink set id or the uplink set uri.

        Returns:
            list: Associated ethernet networks.
        """
        uplink = self.get(id_or_uri)
        network_uris = uplink.get('networkUris')
        networks = []
        if network_uris:
            for uri in network_uris:
                networks.append(self._ethernet_network.get(uri))
        return networks

    def add_ethernet_networks(self, id_or_uri, ethernet_id_or_uris):
        """
        Adds existing ethernet networks to an uplink set.

        Args:
            id_or_uri:
                Can be either the uplink set id or the uplink set uri.
            ethernet_id_or_uris:
                Could be either one or more ethernet network id or ethernet network uri.

        Returns:
            dict: The updated uplink set.
        """
        return self.__set_ethernet_uris(id_or_uri,
                                        ethernet_id_or_uris,
                                        operation="add")

    def remove_ethernet_networks(self, id_or_uri, ethernet_id_or_uris):
        """
        Remove existing ethernet networks of an uplink set.

        Args:
            id_or_uri:
                Can be either the uplink set id or the uplink set uri.
            ethernet_id_or_uris:
                Could be either one or more ethernet network id or ethernet network uri.

        Returns:
            dict: The updated uplink set.
        """
        return self.__set_ethernet_uris(id_or_uri,
                                        ethernet_id_or_uris,
                                        operation="remove")

    def __set_ethernet_uris(self,
                            id_or_uri,
                            ethernet_id_or_uris,
                            operation="add"):
        if not isinstance(ethernet_id_or_uris, list):
            ethernet_id_or_uris = [ethernet_id_or_uris]

        uplink = self.get(id_or_uri)

        associated_enets = uplink.get('networkUris', [])

        for i, enet in enumerate(ethernet_id_or_uris):
            ethernet_id_or_uris[
                i] = enet if '/' in enet else self._ethernet_network.URI + '/' + enet

        if operation == "remove":
            enets_to_update = sorted(
                list(set(associated_enets) - set(ethernet_id_or_uris)))
        elif operation == "add":
            enets_to_update = sorted(
                list(set(associated_enets).union(set(ethernet_id_or_uris))))
        else:
            raise ValueError(
                "Value {} is not supported as operation. The supported values are: ['add', 'remove']"
            )

        if set(enets_to_update) != set(associated_enets):
            uplink['networkUris'] = enets_to_update
            return self.update(uplink)
        else:
            return uplink
Пример #15
0
class UplinkSets(Resource):
    """
    Uplink Sets API client.

    """
    URI = '/rest/uplink-sets'

    DEFAULT_VALUES = {
        '200': {"type": "uplink-setV3"},
        '300': {"type": "uplink-setV300"},
        '500': {"type": "uplink-setV300"},
        '600': {"type": "uplink-setV4"},
        '800': {"type": "uplink-setV4"}
    }

    def __init__(self, connection, data=None):
        super(UplinkSets, self).__init__(connection, data)
        self._ethernet_networks = EthernetNetworks(connection)

    @ensure_resource_client
    def get_ethernet_networks(self):
        """
        Gets a list of associated ethernet networks of an uplink set.

        Args:
            id_or_uri: Can be either the uplink set id or the uplink set uri.

        Returns:
            list: Associated ethernet networks.
        """
        network_uris = self.data.get('networkUris')
        networks = []
        if network_uris:
            for uri in network_uris:
                networks.append(self._ethernet_networks.get_by_uri(uri))
        return networks

    @ensure_resource_client
    def add_ethernet_networks(self, ethernet_names):
        """
        Adds existing ethernet networks to an uplink set.

        Args:
            id_or_uri:
                Can be either the uplink set id or the uplink set uri.
            ethernet_name:
                Could be either one or more ethernet network names.
        Returns:
            dict: The updated uplink set.
        """
        self.__set_ethernet_uris(ethernet_names, operation="add")
        return self.data

    @ensure_resource_client
    def remove_ethernet_networks(self, ethernet_names):
        """
        Remove existing ethernet networks of an uplink set.

        Args:
            id_or_uri:
                Can be either the uplink set id or the uplink set uri.
            ethernet_id_or_uris:
                Could be either one or more ethernet network id or ethernet network uri.

        Returns:
            dict: The updated uplink set.
        """
        self.__set_ethernet_uris(ethernet_names, operation="remove")
        return self.data

    def __set_ethernet_uris(self, ethernet_names, operation="add"):
        """Updates network uris."""
        if not isinstance(ethernet_names, list):
            ethernet_names = [ethernet_names]

        associated_enets = self.data.get('networkUris', [])
        ethernet_uris = []

        for i, enet in enumerate(ethernet_names):
            enet_exists = self._ethernet_networks.get_by_name(enet)
            if enet_exists:
                ethernet_uris.append(enet_exists.data['uri'])
            else:
                raise HPOneViewResourceNotFound("Ethernet: {} does not exist".foramt(enet))

        if operation == "remove":
            enets_to_update = sorted(list(set(associated_enets) - set(ethernet_uris)))
        elif operation == "add":
            enets_to_update = sorted(list(set(associated_enets).union(set(ethernet_uris))))
        else:
            raise ValueError("Value {} is not supported as operation. The supported values are: ['add', 'remove']")

        if set(enets_to_update) != set(associated_enets):
            updated_network = {'networkUris': enets_to_update}
            self.update(updated_network)
Пример #16
0
class UplinkSets(object):
    URI = '/rest/uplink-sets'

    def __init__(self, con):
        self._connection = con
        self._client = ResourceClient(con, self.URI)
        self._ethernet_network = EthernetNetworks(con)
        self.__default_values = {
            "type": "uplink-setV3",
        }

    def get_all(self, start=0, count=-1, filter='', sort=''):
        """
        Gets a paginated list of uplink sets based on optional sorting and filtering, and constrained by start and
        count parameters.
        Filters can be used in the URL to control the number of uplink sets that are returned.
        With no filters specified, the API returns all uplink sets.

        Args:
            start:
                The first item to return, using 0-based indexing.
                If not specified, the default is 0 - start with the first available item.
            count:
                The number of resources to return. A count of -1 requests all the items.
                The actual number of items in the response may differ from the requested
                count if the sum of start and count exceed the total number of items.
            filter:
                A general filter/query string to narrow the list of items returned. The
                default is no filter - all resources are returned.
            sort:
                The sort order of the returned data set. By default, the sort order is based
                on create time, with the oldest entry first.

        Returns:
            list: A list of uplink sets.
        """
        return self._client.get_all(start, count, filter=filter, sort=sort)

    def get(self, id_or_uri):
        """
        Gets an uplink set with the specified ID
        Args:
            id_or_uri:
                Could be either the uplink set id or the uplink set uri

        Returns:
            dict: The uplink set
        """
        return self._client.get(id_or_uri)

    def get_by(self, field, value):
        """
        Get all uplink sets that match the filter
        The search is case insensitive

        Args:
            field: field name to filter
            value: value to filter

        Returns:
            list: Uplink sets

        """
        return self._client.get_by(field, value)

    def create(self, resource, timeout=-1):
        """
        Creates an uplink set.

        Args:
            resource: dict object to create
            timeout:
                Timeout in seconds. Wait task completion by default. The timeout does not abort the operation in
                OneView, just stops waiting for its completion.

        Returns:
            dict: Created resource.

        """
        data = self.__default_values.copy()
        data.update(resource)
        return self._client.create(data, timeout=timeout)

    def update(self, resource, timeout=-1):
        """
        Updates an uplink set.

        Args:
            resource (dict): Resource to update
            timeout:
                Timeout in seconds. Wait task completion by default. The timeout does not abort the operation in
                OneView, just stops waiting for its completion.

        Returns:
            dict: Updated resource.

        """
        data = self.__default_values.copy()
        data.update(resource)
        return self._client.update(data, timeout=timeout)

    def delete(self, resource, force=False, timeout=-1):
        """
        Deletes an uplink set. If the uplink set was carrying a Fibre Channel (FC) network, any connections which are
        deployed and using the FC network will be placed into a 'Failed' state.

        Args:
            resource: Resource to delete or the resource ID
            force:
                 If set to true the operation completes despite any problems with
                 network connectivity or errors on the resource itself. The default is false.
            timeout:
                Timeout in seconds. Wait task completion by default. The timeout does not abort the operation in
                OneView, just stops waiting for its completion.

            Returns:
                bool: True if successfully deleted

        """
        return self._client.delete(resource, force=force, timeout=timeout)

    def get_ethernet_networks(self, id_or_uri):
        """
        Gets a list of associated ethernet networks of an uplink set
        Args:
            id_or_uri:
                Could be either the uplink set id or the uplink set uri

        Returns:
            list: Associated ethernet networks
        """
        uplink = self.get(id_or_uri)
        network_uris = uplink.get('networkUris')
        networks = []
        if network_uris:
            for uri in network_uris:
                networks.append(self._ethernet_network.get(uri))
        return networks

    def add_ethernet_networks(self, id_or_uri, ethernet_id_or_uris):
        """
        Adds existing ethernet networks to an uplink set
        Args:
            id_or_uri:
                Could be either the uplink set id or the uplink set uri
            ethernet_id_or_uris:
                Could be either one or more ethernet network id or ethernet network uri

        Returns:
            dict: The updated uplink set
        """
        return self.__set_ethernet_uris(id_or_uri, ethernet_id_or_uris, operation="add")

    def remove_ethernet_networks(self, id_or_uri, ethernet_id_or_uris):
        """
        Remove existing ethernet networks of an uplink set
        Args:
            id_or_uri:
                Could be either the uplink set id or the uplink set uri
            ethernet_id_or_uris:
                Could be either one or more ethernet network id or ethernet network uri

        Returns:
            dict: The updated uplink set
        """
        return self.__set_ethernet_uris(id_or_uri, ethernet_id_or_uris, operation="remove")

    def __set_ethernet_uris(self, id_or_uri, ethernet_id_or_uris, operation="add"):
        if not isinstance(ethernet_id_or_uris, list):
            ethernet_id_or_uris = [ethernet_id_or_uris]

        uplink = self.get(id_or_uri)

        associated_enets = uplink.get('networkUris', [])

        for i, enet in enumerate(ethernet_id_or_uris):
            ethernet_id_or_uris[i] = enet if '/' in enet else self._ethernet_network.URI + '/' + enet

        if operation == "remove":
            enets_to_update = sorted(list(set(associated_enets) - set(ethernet_id_or_uris)))
        elif operation == "add":
            enets_to_update = sorted(list(set(associated_enets).union(set(ethernet_id_or_uris))))
        else:
            raise ValueError("Value {} is not supported as operation. The supported values are: ['add', 'remove']")

        if set(enets_to_update) != set(associated_enets):
            uplink['networkUris'] = enets_to_update
            return self.update(uplink)
        else:
            return uplink
Пример #17
0
class EthernetNetworksTest(TestCase):
    def setUp(self):
        self.host = '127.0.0.1'
        self.connection = connection(self.host)
        self._ethernet_networks = EthernetNetworks(self.connection)

    @mock.patch.object(ResourceHelper, 'get_all')
    def test_get_all_called_once(self, mock_get_all):
        filter = 'name=TestName'
        sort = 'name:ascending'

        self._ethernet_networks.get_all(2, 500, filter, sort)

        mock_get_all.assert_called_once_with(count=500,
                                             filter=filter,
                                             sort=sort,
                                             start=2)

    @mock.patch.object(ResourceHelper, 'create')
    def test_create_should_use_given_values(self, mock_create):
        resource = {
            'vlanId': 10,
            'name': 'OneViewSDK Test Ethernet Network',
            "ethernetNetworkType": "Tagged",
            "purpose": "Management",
            "connectionTemplateUri": None,
            "smartLink": False,
            "privateNetwork": False
        }
        resource_rest_call = resource.copy()
        resource_rest_call['type'] = 'ethernet-networkV4'
        mock_create.return_value = {}

        self._ethernet_networks.create(resource, timeout=12)
        mock_create.assert_called_once_with(resource_rest_call, None, 12, None,
                                            False)

    @mock.patch.object(ResourceHelper, 'create')
    @mock.patch.object(Resource, 'get_all')
    def test_create_bulk(self, mock_get_all, mock_create):
        resource = {
            'vlanIdRange': '1-10',
            'purpose': 'General',
            'namePrefix': 'TestNetwork',
            'smartLink': False,
            'privateNetwork': False,
            'bandwidth': {
                'maximumBandwidth': 10000,
                'typicalBandwidth': 2000
            }
        }
        resource_rest_call = resource.copy()
        resource_rest_call['type'] = 'bulk-ethernet-networkV1'

        mock_create.return_value = {}
        mock_get_all.return_value = []

        self._ethernet_networks.create_bulk(resource, 27)

        mock_create.assert_called_once_with(resource_rest_call,
                                            uri='/rest/ethernet-networks/bulk',
                                            timeout=27)
        mock_get_all.assert_called_once_with(
            filter='"\'name\' matches \'TestNetwork\\_%\'"',
            sort='vlanId:ascending')

    @mock.patch.object(Resource, 'update')
    def test_update_should_use_given_values(self, mock_update):
        resource = {
            'name': 'OneViewSDK Test Ethernet Network',
            'smartLink': False,
            'connectionTemplateUri': None,
            'vlanId': None,
            'privateNetwork': True,
            'ethernetNetworkType': 'Untagged',
            'purpose': 'General'
        }
        resource_rest_call = resource.copy()
        mock_update.return_value = {}

        self._ethernet_networks.update(resource, timeout=60)
        mock_update.assert_called_once_with(resource_rest_call, timeout=60)

    @mock.patch.object(Resource, 'update')
    def test_update_should_use_default_values(self, mock_update):
        resource = {
            'name': 'OneViewSDK Test Ethernet Network',
        }

        mock_update.return_value = {}

        self._ethernet_networks.update(resource)

        mock_update.assert_called_once_with(resource)

    @mock.patch.object(Resource, 'delete')
    def test_delete_called_once(self, mock_delete):
        id = 'ad28cf21-8b15-4f92-bdcf-51cb2042db32'
        self._ethernet_networks.delete(id, force=False, timeout=-1)

        mock_delete.assert_called_once_with(id, force=False, timeout=-1)

    @mock.patch.object(Resource, 'get_by')
    def test_get_by_called_once(self, mock_get_by):
        self._ethernet_networks.get_by('name',
                                       'OneViewSDK Test Ethernet Network')

        mock_get_by.assert_called_once_with(
            'name', 'OneViewSDK Test Ethernet Network')

    @mock.patch.object(ResourceHelper, 'do_get')
    def test_get_associated_uplink_groups_uri_called_once_with_uri(
            self, mock_get):
        self._ethernet_networks.data = {
            "name": "name",
            "uri":
            "/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155"
        }
        self._ethernet_networks.get_associated_uplink_groups()

        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedUplinkGroups'

        mock_get.assert_called_once_with(uri)

    @mock.patch.object(ResourceHelper, 'do_get')
    def test_get_associated_profiles_called_once_with_uri(self, mock_get):
        self._ethernet_networks.data = {
            "name": "name",
            "uri":
            "/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155"
        }
        self._ethernet_networks.get_associated_profiles()

        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedProfiles'

        mock_get.assert_called_once_with(uri)

    def __mock_enet_gel_all(self):
        return [
            {
                'name': 'TestNetwork_1',
                'vlanId': 1
            },
            {
                'name': 'TestNetwork_2',
                'vlanId': 2
            },
            {
                'name': 'TestNetwork_3',
                'vlanId': 3
            },
            {
                'name': 'TestNetwork_4',
                'vlanId': 4
            },
            {
                'name': 'TestNetwork_5',
                'vlanId': 5
            },
            {
                'name': 'TestNetwork_5',
                'vlanId': 6
            },
            {
                'name': 'TestNetwork_7',
                'vlanId': 7
            },
            {
                'name': 'TestNetwork_8',
                'vlanId': 8
            },
            {
                'name': 'TestNetwork_9',
                'vlanId': 9
            },
            {
                'name': 'TestNetwork_10',
                'vlanId': 10
            },
        ]

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_one_range(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_1',
                'vlanId': 1
            },
            {
                'name': 'TestNetwork_2',
                'vlanId': 2
            },
            {
                'name': 'TestNetwork_3',
                'vlanId': 3
            },
            {
                'name': 'TestNetwork_4',
                'vlanId': 4
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '1-4')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_one_value(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_1',
                'vlanId': 1
            },
            {
                'name': 'TestNetwork_2',
                'vlanId': 2
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '2')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_one_value_and_one_range(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_2',
                'vlanId': 2
            },
            {
                'name': 'TestNetwork_9',
                'vlanId': 9
            },
            {
                'name': 'TestNetwork_10',
                'vlanId': 10
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '2, 9-10')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_multiple_values(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_9',
                'vlanId': 9
            },
            {
                'name': 'TestNetwork_10',
                'vlanId': 10
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '9,10')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_multiple_ranges(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {
                'name': 'TestNetwork_5',
                'vlanId': 6
            },
            {
                'name': 'TestNetwork_7',
                'vlanId': 7
            },
            {
                'name': 'TestNetwork_9',
                'vlanId': 9
            },
            {
                'name': 'TestNetwork_10',
                'vlanId': 10
            },
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '6-7,9-10')
        self.assertEqual(result, expected_result)

    @mock.patch.object(ResourcePatchMixin, 'patch_request')
    @mock.patch.object(Resource, 'get_by')
    def test_patch_should_use_user_defined_values(self, mock_get_by,
                                                  mock_patch):
        mock_patch.return_value = {}
        self._ethernet_networks.data = {
            "name": "test name",
            "uri": "/rest/test"
        }
        self._ethernet_networks.patch('replace', '/scopeUris',
                                      ['/rest/fake/scope123'], 1)
        mock_patch.assert_called_once_with('/rest/test',
                                           body=[{
                                               u'path':
                                               '/scopeUris',
                                               u'value':
                                               ['/rest/fake/scope123'],
                                               u'op':
                                               'replace'
                                           }],
                                           custom_headers=1,
                                           timeout=-1)

    def test_dissociate_values_or_ranges_with_one_value(self):
        expected_result = [1, 2, 3, 4, 5]
        result = self._ethernet_networks.dissociate_values_or_ranges('5')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_multiple_values(self):
        expected_result = [6, 7, 9]
        result = self._ethernet_networks.dissociate_values_or_ranges('6,7,9')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_one_range(self):
        expected_result = [6, 7]
        result = self._ethernet_networks.dissociate_values_or_ranges('6-7')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_multiple_ranges(self):
        expected_result = [6, 7, 9, 10]
        result = self._ethernet_networks.dissociate_values_or_ranges(
            '6-7,9-10')
        self.assertEqual(result, expected_result)

    @mock.patch.object(ResourceHelper, 'create')
    @mock.patch.object(Resource, 'get_all')
    def test_delete_bulk(self, mock_get_all, mock_create):
        resource = {
            "networkUris": [
                "/rest/ethernet-networks/e2f0031b-52bd-4223-9ac1-d91cb519d548",
                "/rest/ethernet-networks/f2f0031b-52bd-4223-9ac1-d91cb519d549",
                "/rest/ethernet-networks/02f0031b-52bd-4223-9ac1-d91cb519d54a"
            ]
        }
        resource_rest_call = resource.copy()

        mock_create.return_value = {}
        mock_get_all.return_value = []

        self._ethernet_networks.delete_bulk(resource, 27)

        mock_create.assert_called_once_with(
            resource_rest_call,
            uri='/rest/ethernet-networks/bulk-delete',
            timeout=27)
Пример #18
0
 def ethernet_networks(self):
     if not self.__ethernet_networks:
         self.__ethernet_networks = EthernetNetworks(self.__connection)
     return self.__ethernet_networks
 def setUp(self):
     self.host = '127.0.0.1'
     self.connection = connection(self.host)
     self._ethernet_networks = EthernetNetworks(self.connection)
class EthernetNetworksTest(TestCase):
    def setUp(self):
        self.host = '127.0.0.1'
        self.connection = connection(self.host)
        self._ethernet_networks = EthernetNetworks(self.connection)

    @mock.patch.object(ResourceClient, 'get_all')
    def test_get_all_called_once(self, mock_get_all):
        filter = 'name=TestName'
        sort = 'name:ascending'

        self._ethernet_networks.get_all(2, 500, filter, sort)

        mock_get_all.assert_called_once_with(2, 500, filter=filter, sort=sort)

    @mock.patch.object(ResourceClient, 'create')
    def test_create_should_use_given_values(self, mock_create):
        resource = {
            'vlanId': 10,
            'name': 'OneViewSDK Test Ethernet Network',
            "ethernetNetworkType": "Tagged",
            "purpose": "Management",
            "connectionTemplateUri": None,
            "smartLink": False,
            "type": "ethernet-networkV3",
            "privateNetwork": False
        }
        resource_rest_call = resource.copy()
        mock_create.return_value = {}

        self._ethernet_networks.create(resource, 12)
        mock_create.assert_called_once_with(resource_rest_call, timeout=12)

    @mock.patch.object(ResourceClient, 'create')
    def test_create_should_use_default_values(self, mock_create):
        resource = {
            'name': 'OneViewSDK Test Ethernet Network',
        }
        resource_with_default_values = {
            'name': 'OneViewSDK Test Ethernet Network',
            'ethernetNetworkType': 'Tagged',
            'type': 'ethernet-networkV3'
        }
        mock_create.return_value = {}

        self._ethernet_networks.create(resource)

        mock_create.assert_called_once_with(
            resource_with_default_values, timeout=-1)

    @mock.patch.object(ResourceClient, 'create')
    @mock.patch.object(ResourceClient, 'get_all')
    def test_create_bulk(self, mock_get_all, mock_create):
        resource = {
            'vlanIdRange': '1-10',
            'purpose': 'General',
            'namePrefix': 'TestNetwork',
            'smartLink': False,
            'privateNetwork': False,
            'bandwidth': {
                'maximumBandwidth': 10000,
                'typicalBandwidth': 2000
            },
            'type': 'bulk-ethernet-network'
        }
        resource_rest_call = resource.copy()
        mock_create.return_value = {}
        mock_get_all.return_value = []

        self._ethernet_networks.create_bulk(resource, 27)

        mock_create.assert_called_once_with(
            resource_rest_call, uri='/rest/ethernet-networks/bulk', timeout=27)
        mock_get_all.assert_called_once_with(
            0, -1, filter='"\'name\' matches \'TestNetwork\\_%\'"', sort='vlanId:ascending')

    @mock.patch.object(ResourceClient, 'update')
    def test_update_should_use_given_values(self, mock_update):
        resource = {
            'name': 'OneViewSDK Test Ethernet Network',
            'smartLink': False,
            'connectionTemplateUri': None,
            'vlanId': None,
            'privateNetwork': True,
            'ethernetNetworkType': 'Untagged',
            'type': 'ethernet-networkV3',
            'purpose': 'General'
        }
        resource_rest_call = resource.copy()
        mock_update.return_value = {}

        self._ethernet_networks.update(resource, timeout=60)
        mock_update.assert_called_once_with(resource_rest_call, timeout=60)

    @mock.patch.object(ResourceClient, 'update')
    def test_update_should_use_default_values(self, mock_update):
        resource = {
            'name': 'OneViewSDK Test Ethernet Network',
        }
        resource_with_default_values = {
            'type': 'ethernet-networkV3',
            'name': 'OneViewSDK Test Ethernet Network',
            'ethernetNetworkType': 'Tagged',
        }
        mock_update.return_value = {}

        self._ethernet_networks.update(resource)

        mock_update.assert_called_once_with(
            resource_with_default_values, timeout=-1)

    @mock.patch.object(ResourceClient, 'delete')
    def test_delete_called_once(self, mock_delete):
        id = 'ad28cf21-8b15-4f92-bdcf-51cb2042db32'
        self._ethernet_networks.delete(id, force=False, timeout=-1)

        mock_delete.assert_called_once_with(id, force=False, timeout=-1)

    @mock.patch.object(ResourceClient, 'get_by')
    def test_get_by_called_once(self, mock_get_by):
        self._ethernet_networks.get_by(
            'name', 'OneViewSDK Test Ethernet Network')

        mock_get_by.assert_called_once_with(
            'name', 'OneViewSDK Test Ethernet Network')

    @mock.patch.object(ResourceClient, 'get')
    def test_get_called_once(self, mock_get):
        self._ethernet_networks.get('3518be0e-17c1-4189-8f81-83f3724f6155')

        mock_get.assert_called_once_with(
            '3518be0e-17c1-4189-8f81-83f3724f6155')

    @mock.patch.object(ResourceClient, 'get')
    def test_get_with_uri_called_once(self, mock_get):
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155'
        self._ethernet_networks.get(uri)

        mock_get.assert_called_once_with(uri)

    @mock.patch.object(ResourceClient, 'get')
    def test_get_associated_uplink_groups_uri_called_once_with_id(self, mock_get):
        self._ethernet_networks.get_associated_uplink_groups(
            '3518be0e-17c1-4189-8f81-83f3724f6155')
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedUplinkGroups'

        mock_get.assert_called_once_with(uri)

    @mock.patch.object(ResourceClient, 'get')
    def test_get_associated_uplink_groups_uri_called_once_with_uri(self, mock_get):
        self._ethernet_networks.get_associated_uplink_groups(
            '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155')
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedUplinkGroups'

        mock_get.assert_called_once_with(uri)

    @mock.patch.object(ResourceClient, 'get')
    def test_get_associated_profiles_called_once_with_id(self, mock_get):
        self._ethernet_networks.get_associated_profiles(
            '3518be0e-17c1-4189-8f81-83f3724f6155')
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedProfiles'

        mock_get.assert_called_once_with(uri)

    @mock.patch.object(ResourceClient, 'get')
    def test_get_associated_profiles_called_once_with_uri(self, mock_get):
        self._ethernet_networks.get_associated_profiles(
            '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155')
        uri = '/rest/ethernet-networks/3518be0e-17c1-4189-8f81-83f3724f6155/associatedProfiles'

        mock_get.assert_called_once_with(uri)

    def __mock_enet_gel_all(self):
        return [
            {'name': 'TestNetwork_1', 'vlanId': 1},
            {'name': 'TestNetwork_2', 'vlanId': 2},
            {'name': 'TestNetwork_3', 'vlanId': 3},
            {'name': 'TestNetwork_4', 'vlanId': 4},
            {'name': 'TestNetwork_5', 'vlanId': 5},
            {'name': 'TestNetwork_5', 'vlanId': 6},
            {'name': 'TestNetwork_7', 'vlanId': 7},
            {'name': 'TestNetwork_8', 'vlanId': 8},
            {'name': 'TestNetwork_9', 'vlanId': 9},
            {'name': 'TestNetwork_10', 'vlanId': 10},
        ]

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_one_range(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {'name': 'TestNetwork_1', 'vlanId': 1},
            {'name': 'TestNetwork_2', 'vlanId': 2},
            {'name': 'TestNetwork_3', 'vlanId': 3},
            {'name': 'TestNetwork_4', 'vlanId': 4},
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '1-4')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_one_value(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {'name': 'TestNetwork_1', 'vlanId': 1},
            {'name': 'TestNetwork_2', 'vlanId': 2},
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '2')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_one_value_and_one_range(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {'name': 'TestNetwork_2', 'vlanId': 2},
            {'name': 'TestNetwork_9', 'vlanId': 9},
            {'name': 'TestNetwork_10', 'vlanId': 10},
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '2, 9-10')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_multiple_values(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {'name': 'TestNetwork_9', 'vlanId': 9},
            {'name': 'TestNetwork_10', 'vlanId': 10},
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '9,10')
        self.assertEqual(result, expected_result)

    @mock.patch.object(EthernetNetworks, 'get_all')
    def test_get_bulk_with_multiple_ranges(self, mock_get_all):
        mock_get_all.return_value = self.__mock_enet_gel_all()

        expected_result = [
            {'name': 'TestNetwork_5', 'vlanId': 6},
            {'name': 'TestNetwork_7', 'vlanId': 7},
            {'name': 'TestNetwork_9', 'vlanId': 9},
            {'name': 'TestNetwork_10', 'vlanId': 10},
        ]

        result = self._ethernet_networks.get_range('TestNetwork', '6-7,9-10')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_one_value(self):
        expected_result = [1, 2, 3, 4, 5]
        result = self._ethernet_networks.dissociate_values_or_ranges('5')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_multiple_values(self):
        expected_result = [6, 7, 9]
        result = self._ethernet_networks.dissociate_values_or_ranges('6,7,9')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_one_range(self):
        expected_result = [6, 7]
        result = self._ethernet_networks.dissociate_values_or_ranges('6-7')
        self.assertEqual(result, expected_result)

    def test_dissociate_values_or_ranges_with_multiple_ranges(self):
        expected_result = [6, 7, 9, 10]
        result = self._ethernet_networks.dissociate_values_or_ranges('6-7,9-10')
        self.assertEqual(result, expected_result)
Пример #21
0
 def __init__(self, con):
     self._connection = con
     self._client = ResourceClient(con, self.URI)
     self._ethernet_network = EthernetNetworks(con)
Пример #22
0
 def __init__(self, con):
     self._connection = con
     self._client = ResourceClient(con, self.URI)
     self._ethernet_network = EthernetNetworks(con)