def test_construction_kwargs_2(self): subnet = Subnet( **{ 'name': 'foo', 'id': 'bar', 'project_id': 'proj1', 'network_id': 'foo-bar' }) network = Network( **{ 'name': 'foo', 'id': 'bar', 'project_id': 'proj1', 'type': 'flat', 'admin_state_up': False, 'shared': True, 'external': False, 'subnets': [subnet] }) self.assertEqual('foo', network.name) self.assertEqual('bar', network.id) self.assertEqual('proj1', network.project_id) self.assertEqual('flat', network.type) self.assertFalse(network.admin_state_up) self.assertFalse(network.external) self.assertTrue(network.shared) self.assertEqual([subnet], network.subnets)
def test_construction_kwargs(self): subnet = Subnet( **{ 'name': 'foo', 'id': 'bar', 'project_id': 'proj1', 'cidr': '10.0.0.0/24', 'ip_version': 4, 'gateway_ip': '10.0.0.1', 'enable_dhcp': True, 'dns_nameservers': ['8.8.8.8'], 'host_routes': list(), 'ipv6_ra_mode': 'hello', 'ipv6_address_mode': 'world' }) self.assertEqual('foo', subnet.name) self.assertEqual('bar', subnet.id) self.assertEqual('proj1', subnet.project_id) self.assertEqual('10.0.0.0/24', subnet.cidr) self.assertEqual(4, subnet.ip_version) self.assertEqual('10.0.0.1', subnet.gateway_ip) self.assertTrue(subnet.enable_dhcp) self.assertEqual(1, len(subnet.dns_nameservers)) self.assertEqual('8.8.8.8', subnet.dns_nameservers[0]) self.assertEqual(list(), subnet.host_routes) self.assertEqual('hello', subnet.ipv6_ra_mode) self.assertEqual('world', subnet.ipv6_address_mode)
def get_subnet_by_id(neutron, subnet_id): """ Returns a SNAPS-OO Subnet domain object for a given ID :param neutron: the OpenStack neutron client :param subnet_id: the subnet ID :return: a Subnet object """ os_subnet = neutron.show_subnet(subnet_id) if os_subnet and 'subnet' in os_subnet: return Subnet(**os_subnet['subnet'])
def get_subnets_by_network_id(neutron, network_id): """ Returns a list of SNAPS-OO Subnet domain objects :param neutron: the OpenStack neutron client :param network_id: the subnet's ID :return: a list of Subnet objects """ out = list() os_subnets = neutron.list_subnets(network_id=network_id) for os_subnet in os_subnets['subnets']: out.append(Subnet(**os_subnet)) return out
def create_subnet(neutron, subnet_settings, os_creds, network): """ Creates a network subnet for OpenStack :param neutron: the client :param subnet_settings: A dictionary containing the subnet configuration and is responsible for creating the subnet request JSON body :param os_creds: the OpenStack credentials :param network: the network object :return: a SNAPS-OO Subnet domain object """ if neutron and network and subnet_settings: json_body = { 'subnets': [subnet_settings.dict_for_neutron(os_creds, network=network)] } logger.info('Creating subnet with name ' + subnet_settings.name) subnets = neutron.create_subnet(body=json_body) return Subnet(**subnets['subnets'][0]) else: raise NeutronException('Failed to create subnet')
def get_subnet_by_name(neutron, keystone, subnet_name, project_name=None): """ Returns the first subnet object that fits the query else None including if subnet_settings or subnet_name parameters are None. :param neutron: the Neutron client :param keystone: the Keystone client :param subnet_name: the name of the subnet to retrieve :param project_name: the name of the associated project to the subnet to retrieve :return: a SNAPS-OO Subnet domain object or None """ sub_filter = {'name': subnet_name} subnets = neutron.list_subnets(**sub_filter) for subnet in subnets['subnets']: subnet = Subnet(**subnet) if project_name: project = keystone_utils.get_project_by_id(keystone, subnet.project_id) if project and project.name == project_name: return subnet else: return subnet
def get_subnet(neutron, network, subnet_settings=None, subnet_name=None): """ Returns the first subnet object that fits the query else None including if subnet_settings or subnet_name parameters are None. :param neutron: the client :param network: the associated SNAPS-OO Network domain object :param subnet_settings: the subnet settings of the object to retrieve :param subnet_name: the name of the subnet to retrieve :return: a SNAPS-OO Subnet domain object or None """ sub_filter = {'network_id': network.id} if subnet_settings: sub_filter['name'] = subnet_settings.name sub_filter['cidr'] = subnet_settings.cidr if subnet_settings.gateway_ip: sub_filter['gateway_ip'] = subnet_settings.gateway_ip if subnet_settings.enable_dhcp is not None: sub_filter['enable_dhcp'] = subnet_settings.enable_dhcp if subnet_settings.destination: sub_filter['destination'] = subnet_settings.destination if subnet_settings.nexthop: sub_filter['nexthop'] = subnet_settings.nexthop if subnet_settings.ipv6_ra_mode: sub_filter['ipv6_ra_mode'] = subnet_settings.ipv6_ra_mode if subnet_settings.ipv6_address_mode: sub_filter['ipv6_address_mode'] = subnet_settings.ipv6_address_mode elif subnet_name: sub_filter['name'] = subnet_name else: return None subnets = neutron.list_subnets(**sub_filter) for subnet in subnets['subnets']: return Subnet(**subnet)
def test_construction_named(self): subnet = Subnet( **{ 'name': 'foo', 'id': 'bar', 'project_id': 'proj1', 'network_id': 'foo-bar' }) network = Network(name='foo', id='bar', project_id='proj1', type='flat', admin_state_up=False, shared=True, external=False, subnets=[subnet]) self.assertEqual('foo', network.name) self.assertEqual('bar', network.id) self.assertEqual('proj1', network.project_id) self.assertEqual('flat', network.type) self.assertFalse(network.admin_state_up) self.assertFalse(network.external) self.assertTrue(network.shared) self.assertEqual([subnet], network.subnets)
def test_construction_named(self): subnet = Subnet(name='foo', id='bar', project_id='proj1', cidr='10.0.0.0/24', ip_version=4, gateway_ip='10.0.0.1', enable_dhcp=True, dns_nameservers=['8.8.8.8'], host_routes=list(), ipv6_ra_mode='hello', ipv6_address_mode='world') self.assertEqual('foo', subnet.name) self.assertEqual('bar', subnet.id) self.assertEqual('proj1', subnet.project_id) self.assertEqual('10.0.0.0/24', subnet.cidr) self.assertEqual(4, subnet.ip_version) self.assertEqual('10.0.0.1', subnet.gateway_ip) self.assertTrue(subnet.enable_dhcp) self.assertEqual(1, len(subnet.dns_nameservers)) self.assertEqual('8.8.8.8', subnet.dns_nameservers[0]) self.assertEqual(list(), subnet.host_routes) self.assertEqual('hello', subnet.ipv6_ra_mode) self.assertEqual('world', subnet.ipv6_address_mode)