def test_create_network_router_new_user_to_admin_project(self): """ Tests the creation of an OpenStack network and router with the admin user to the new project. """ # Create Network/Subnet where the project names have been changed new_project_name = self.os_creds.project_name self.net_config.network_settings.project_name = new_project_name self.net_config.network_settings.subnet_settings[0].project_name = \ new_project_name self.net_creator = OpenStackNetwork( self.admin_os_creds, self.net_config.network_settings) self.net_creator.create() retrieved_net = neutron_utils.get_network( self.neutron, self.keystone, network_settings=self.net_config.network_settings, project_name=self.os_creds.project_name) self.assertEqual(self.net_creator.get_network().id, retrieved_net.id) # Create Router self.net_config.router_settings.project_name = new_project_name self.router_creator = create_router.OpenStackRouter( self.admin_os_creds, self.net_config.router_settings) self.router_creator.create() retrieved_router = neutron_utils.get_router( self.neutron, self.keystone, router_settings=self.router_creator.router_settings, project_name=self.os_creds.project_name) self.assertEqual( self.router_creator.get_router().id, retrieved_router.id)
def __create_floatingip_config(neutron, keystone, port_settings, project_name): """ Returns a list of FloatingIpConfig objects as they pertain to an existing deployed server instance :param neutron: the neutron client :param keystone: the keystone client :param port_settings: list of SNAPS-OO PortConfig objects :return: a list of FloatingIpConfig objects or an empty list if no floating IPs have been created """ base_fip_name = 'fip-' fip_ctr = 1 out = list() fip_ports = list() for port_setting in port_settings: setting_port = neutron_utils.get_port(neutron, keystone, port_setting, project_name=project_name) if setting_port: network = neutron_utils.get_network( neutron, keystone, network_name=port_setting.network_name) network_ports = neutron_utils.get_ports(neutron, network) if network_ports: for setting_port in network_ports: if port_setting.mac_address == setting_port.mac_address: fip_ports.append((port_setting.name, setting_port)) break floating_ips = neutron_utils.get_port_floating_ips(neutron, fip_ports) for port_id, floating_ip in floating_ips: router = neutron_utils.get_router_by_id(neutron, floating_ip.router_id) setting_port = neutron_utils.get_port_by_id(neutron, floating_ip.port_id) kwargs = dict() kwargs['name'] = base_fip_name + str(fip_ctr) kwargs['port_name'] = setting_port.name kwargs['port_id'] = setting_port.id kwargs['router_name'] = router.name if setting_port: for ip_dict in setting_port.ips: if ('ip_address' in ip_dict and 'subnet_id' in ip_dict and ip_dict['ip_address'] == floating_ip.fixed_ip_address): subnet = neutron_utils.get_subnet_by_id( neutron, ip_dict['subnet_id']) if subnet: kwargs['subnet_name'] = subnet.name out.append(FloatingIpConfig(**kwargs)) fip_ctr += 1 return out
def __map_os_server_obj_to_vm_inst(neutron, keystone, os_server, project_name=None): """ Returns a VmInst object for an OpenStack Server object :param neutron: the Neutron client :param keystone: the Keystone client :param os_server: the OpenStack server object :param project_name: the associated project name :return: an equivalent SNAPS-OO VmInst domain object """ sec_grp_names = list() # VM must be active for 'security_groups' attr to be initialized if hasattr(os_server, 'security_groups'): for sec_group in os_server.security_groups: if sec_group.get('name'): sec_grp_names.append(sec_group.get('name')) out_ports = list() if len(os_server.networks) > 0: for net_name, ips in os_server.networks.items(): network = neutron_utils.get_network(neutron, keystone, network_name=net_name, project_name=project_name) if network: ports = neutron_utils.get_ports(neutron, network, ips) for port in ports: out_ports.append(port) else: raise NovaException( 'Unable to locate network in project {} with ' 'name {}'.format(project_name, net_name)) volumes = None if hasattr(os_server, 'os-extended-volumes:volumes_attached'): volumes = getattr(os_server, 'os-extended-volumes:volumes_attached') return VmInst( name=os_server.name, inst_id=os_server.id, image_id=os_server.image['id'], flavor_id=os_server.flavor['id'], ports=out_ports, keypair_name=os_server.key_name, sec_grp_names=sec_grp_names, volume_ids=volumes, compute_host=os_server._info.get('OS-EXT-SRV-ATTR:host'), availability_zone=os_server._info.get('OS-EXT-AZ:availability_zone'))
def dict_for_neutron(self, neutron, os_creds): """ Returns a dictionary object representing this object. This is meant to be converted into JSON designed for use by the Neutron API TODO - expand automated testing to exercise all parameters :param neutron: The neutron client to retrieve external network information if necessary :param os_creds: The OpenStack credentials for retrieving the keystone client for looking up the project ID when the self.project_name is not None :return: the dictionary object """ out = dict() ext_gw = dict() session = keystone_utils.keystone_session(os_creds) keystone = keystone_utils.keystone_client(os_creds, session) try: if self.name: out['name'] = self.name if self.project_name: project = keystone_utils.get_project( keystone=keystone, project_name=self.project_name) if project: out['tenant_id'] = project.id else: raise RouterConfigError( 'Could not find project ID for project named - ' + self.project_name) if self.admin_state_up is not None: out['admin_state_up'] = self.admin_state_up if self.external_gateway: ext_net = neutron_utils.get_network( neutron, keystone, network_name=self.external_gateway) if ext_net: ext_gw['network_id'] = ext_net.id out['external_gateway_info'] = ext_gw else: raise RouterConfigError( 'Could not find the external network named - ' + self.external_gateway) finally: keystone_utils.close_session(session) return {'router': out}
def test_create_router_with_ext_port(self): """ Test creation of a router with a port to an external network as an 'admin' user. """ port_settings = [ create_network.PortConfig(name=self.guid + '-port1', network_name=self.ext_net_name) ] router_settings = RouterConfig(name=self.guid + '-pub-router', port_settings=port_settings) self.router_creator = create_router.OpenStackRouter( self.admin_os_creds, router_settings) self.router_creator.create() admin_neutron = neutron_utils.neutron_client(self.admin_os_creds, self.admin_os_session) admin_keystone = keystone_utils.keystone_client( self.admin_os_creds, self.admin_os_session) router = neutron_utils.get_router( admin_neutron, admin_keystone, router_settings=router_settings, project_name=self.admin_os_creds.project_name) self.assertIsNotNone(router) self.assertEquals(router, self.router_creator.get_router()) ext_net = neutron_utils.get_network(admin_neutron, admin_keystone, network_name=self.ext_net_name) self.assertIsNotNone(ext_net) self.assertIsNotNone(router.port_subnets) id_found = False for port, subnets in router.port_subnets: self.assertIsNotNone(subnets) self.assertIsNotNone(port) if ext_net.id == port.network_id: id_found = True for subnet in subnets: self.assertIsNotNone(subnet) self.assertEqual(ext_net.id, subnet.network_id) self.assertTrue(id_found)
def test_create_network_router_admin_user_to_new_project(self): """ Tests the creation of an OpenStack network to the the current using the credentials to the admin project. """ # Create Network/Subnet where the project names have been changed project_name = self.os_creds.project_name config = self.net_config.network_settings config.project_name = project_name config.subnet_settings[0].project_name = project_name self.net_creator = OpenStackNetwork(self.admin_os_creds, config) self.net_creator.create() retrieved_net = neutron_utils.get_network( self.neutron, self.keystone, network_name=self.net_config.network_settings.name, project_name=self.os_creds.project_name) self.assertEqual(self.net_creator.get_network().id, retrieved_net.id) # Initialize with actual credentials config.project_name = None config.subnet_settings[0].project_name = None proj_net_creator = OpenStackNetwork(self.os_creds, config) proj_net = proj_net_creator.create() self.assertEqual(retrieved_net, proj_net) # Create Router self.net_config.router_settings.project_name = project_name self.router_creator = create_router.OpenStackRouter( self.admin_os_creds, self.net_config.router_settings) self.router_creator.create() retrieved_router = neutron_utils.get_router( self.neutron, self.keystone, router_settings=self.router_creator.router_settings, project_name=self.os_creds.project_name) self.assertEqual( self.router_creator.get_router().id, retrieved_router.id)
def test_create_router_with_stack(self): """ Tests the creation of an OpenStack router with Heat and the retrieval of the Router Domain objects from heat_utils#get_stack_routers(). """ self.stack = heat_utils.create_stack(self.heat_client, self.stack_settings) # Wait until stack deployment has completed end_time = time.time() + stack_config.STACK_COMPLETE_TIMEOUT is_active = False while time.time() < end_time: status = heat_utils.get_stack_status(self.heat_client, self.stack.id) if status == stack_config.STATUS_CREATE_COMPLETE: is_active = True break elif status == stack_config.STATUS_CREATE_FAILED: is_active = False break time.sleep(3) self.assertTrue(is_active) routers = heat_utils.get_stack_routers(self.heat_client, self.neutron, self.stack) self.assertEqual(1, len(routers)) router = routers[0] self.assertEqual(self.router_name, router.name) keystone = keystone_utils.keystone_client(self.os_creds, self.os_session) ext_net = neutron_utils.get_network(self.neutron, keystone, network_name=self.ext_net_name) self.assertEqual(ext_net.id, router.external_network_id)
def __get_internal_subnet(self, sub_config): """ returns the Subnet domain object from the subnet configurator :param sub_config: :return: """ if isinstance(sub_config, dict): sub_dict = sub_config['subnet'] network = neutron_utils.get_network( self._neutron, self._keystone, network_name=sub_dict['network_name'], project_name=sub_dict['project_name']) if network: return neutron_utils.get_subnet( self._neutron, network, subnet_name=sub_dict['subnet_name']) else: return neutron_utils.get_subnet_by_name( self._neutron, self._keystone, subnet_name=sub_config, project_name=self._os_creds.project_name)
def test_create_delete_network(self): """ Tests the creation of an OpenStack network, it's deletion, then cleanup """ # Create Network self.net_creator = OpenStackNetwork( self.os_creds, self.net_config.network_settings) self.net_creator.create() # Validate network was created self.assertTrue(neutron_utils_tests.validate_network( self.neutron, self.keystone, self.net_creator.network_settings.name, True, self.os_creds.project_name, mtu=999)) neutron_utils.delete_network( self.neutron, self.net_creator.get_network()) self.assertIsNone(neutron_utils.get_network( self.neutron, self.keystone, network_settings=self.net_creator.network_settings, project_name=self.os_creds.project_name)) # This shall not throw an exception here self.net_creator.clean()
def dict_for_neutron(self, neutron, os_creds): """ Returns a dictionary object representing this object. This is meant to be converted into JSON designed for use by the Neutron API TODO - expand automated testing to exercise all parameters :param neutron: the Neutron client :param os_creds: the OpenStack credentials :return: the dictionary object """ out = dict() session = keystone_utils.keystone_session(os_creds) keystone = keystone_utils.keystone_client(os_creds, session) project_name = os_creds.project_name if self.project_name: project_name = project_name try: network = neutron_utils.get_network(neutron, keystone, network_name=self.network_name) if network and not (network.shared or network.external): network = neutron_utils.get_network( neutron, keystone, network_name=self.network_name, project_name=project_name) finally: if session: keystone_utils.close_session(session) if not network: raise PortConfigError('Cannot locate network with name - ' + self.network_name + ' in project - ' + str(project_name)) out['network_id'] = network.id if self.admin_state_up is not None: out['admin_state_up'] = self.admin_state_up if self.name: out['name'] = self.name if self.project_name: project = keystone_utils.get_project( keystone=keystone, project_name=self.project_name) project_id = None if project: project_id = project.id if project_id: out['tenant_id'] = project_id else: raise PortConfigError( 'Could not find project ID for project named - ' + self.project_name) if self.mac_address: out['mac_address'] = self.mac_address fixed_ips = self.__get_fixed_ips(neutron, network) if fixed_ips and len(fixed_ips) > 0: out['fixed_ips'] = fixed_ips if self.security_groups: sec_grp_ids = list() for sec_grp_name in self.security_groups: sec_grp = neutron_utils.get_security_group( neutron, keystone, sec_grp_name=sec_grp_name, project_name=self.project_name) if sec_grp: sec_grp_ids.append(sec_grp.id) out['security_groups'] = sec_grp_ids if self.port_security_enabled is not None: out['port_security_enabled'] = self.port_security_enabled if self.allowed_address_pairs and len(self.allowed_address_pairs) > 0: out['allowed_address_pairs'] = self.allowed_address_pairs if self.opt_value: out['opt_value'] = self.opt_value if self.opt_name: out['opt_name'] = self.opt_name if self.device_owner: out['device_owner'] = self.device_owner if self.device_id: out['device_id'] = self.device_id if self.extra_dhcp_opts: out['extra_dhcp_opts'] = self.extra_dhcp_opts return {'port': out}