| test_ssh_client_fip_after_active | Nova 2 | Ensures that an instance can be reached over SSH when the |
| | Neutron 2 | floating IP is assigned after to the VM becoming ACTIVE |
+---------------------------------------+---------------+-----------------------------------------------------------+
+| test_ssh_client_fip_after_init | Nova 2 | Ensures that an instance can have a floating IP assigned |
+| | Neutron 2 | added after initialization |
++---------------------------------------+---------------+-----------------------------------------------------------+
+| test_ssh_client_fip_reverse_engineer | Nova 2 | Ensures that an instance can be reverse engineered and |
+| | Neutron 2 | allows for a floating IP to be added after initialization |
++---------------------------------------+---------------+-----------------------------------------------------------+
| test_ssh_client_fip_second_creator | Nova 2 | Ensures that an instance can be reached over SSH via a |
| | Neutron 2 | second identical creator object |
+---------------------------------------+---------------+-----------------------------------------------------------+
from snaps.config.vm_inst import VmInstanceConfig, FloatingIpConfig
from snaps.openstack.openstack_creator import OpenStackComputeObject
-from snaps.openstack.utils import glance_utils, cinder_utils
+from snaps.openstack.utils import glance_utils, cinder_utils, settings_utils
from snaps.openstack.utils import neutron_utils
from snaps.openstack.utils import nova_utils
from snaps.provisioning import ansible_utils
# Apply floating IPs
for floating_ip_setting in self.instance_settings.floating_ip_settings:
- port = port_dict.get(floating_ip_setting.port_name)
+ self.add_floating_ip(floating_ip_setting)
- if not port:
- raise VmInstanceCreationError(
- 'Cannot find port object with name - ' +
- floating_ip_setting.port_name)
+ def add_floating_ip(self, floating_ip_setting):
+ """
+ Adds a floating IP to a running instance
+ :param floating_ip_setting - the floating IP configuration
+ """
+ port_dict = dict()
+ for key, port in self.__ports:
+ port_dict[key] = port
- # Setup Floating IP only if there is a router with an external
- # gateway
- ext_gateway = self.__ext_gateway_by_router(
- floating_ip_setting.router_name)
- if ext_gateway:
- subnet = neutron_utils.get_subnet(
- self.__neutron,
- subnet_name=floating_ip_setting.subnet_name)
- floating_ip = neutron_utils.create_floating_ip(
- self.__neutron, ext_gateway)
- self.__floating_ip_dict[floating_ip_setting.name] = floating_ip
+ # Apply floating IP
+ port = port_dict.get(floating_ip_setting.port_name)
- logger.info(
- 'Created floating IP %s via router - %s', floating_ip.ip,
- floating_ip_setting.router_name)
- self.__add_floating_ip(floating_ip, port, subnet)
- else:
- raise VmInstanceCreationError(
- 'Unable to add floating IP to port, cannot locate router '
- 'with an external gateway ')
+ if not port:
+ raise VmInstanceCreationError(
+ 'Cannot find port object with name - ' +
+ floating_ip_setting.port_name)
+
+ # Setup Floating IP only if there is a router with an external
+ # gateway
+ ext_gateway = self.__ext_gateway_by_router(
+ floating_ip_setting.router_name)
+ if ext_gateway:
+ subnet = neutron_utils.get_subnet(
+ self.__neutron,
+ subnet_name=floating_ip_setting.subnet_name)
+ floating_ip = neutron_utils.create_floating_ip(
+ self.__neutron, ext_gateway)
+ self.__floating_ip_dict[floating_ip_setting.name] = floating_ip
+
+ logger.info(
+ 'Created floating IP %s via router - %s', floating_ip.ip,
+ floating_ip_setting.router_name)
+ self.__add_floating_ip(floating_ip, port, subnet)
+ else:
+ raise VmInstanceCreationError(
+ 'Unable to add floating IP to port, cannot locate router '
+ 'with an external gateway ')
def __ext_gateway_by_router(self, router_name):
"""
for key, fip in self.__floating_ip_dict.items():
return fip
+ # When cannot be found above
+ if len(self.__floating_ip_dict) > 0:
+ for key, fip in self.__floating_ip_dict.items():
+ return fip
+
def __config_nic(self, nic_name, port, ip):
"""
Although ports/NICs can contain multiple IPs, this code currently only
:param poll_interval: The polling interval in seconds
:return: T/F
"""
- return self.__vm_status_check(STATUS_ACTIVE, block,
- self.instance_settings.vm_boot_timeout,
- poll_interval)
+ if self.__vm_status_check(
+ STATUS_ACTIVE, block, self.instance_settings.vm_boot_timeout,
+ poll_interval):
+ self.__vm = nova_utils.get_server_object_by_id(
+ self._nova, self.__vm.id)
+ return True
+ return False
def __vm_status_check(self, expected_status_code, block, timeout,
poll_interval):
:param fip_name: the name of the floating IP to return
:return: the SSH client or None
"""
- fip = None
if fip_name and self.__floating_ip_dict.get(fip_name):
return self.__floating_ip_dict.get(fip_name)
- if not fip:
+ else:
return self.__get_first_provisioning_floating_ip()
def ssh_client(self, fip_name=None):
self.keypair_settings.private_filepath,
proxy_settings=self._os_creds.proxy_settings)
else:
- logger.warning(
+ FloatingIPAllocationError(
'Cannot return an SSH client. No Floating IP configured')
def add_security_group(self, security_group):
return False
+def generate_creator(os_creds, vm_inst, image_config, keypair_config=None):
+ """
+ Initializes an OpenStackVmInstance object
+ :param os_creds: the OpenStack credentials
+ :param vm_inst: the SNAPS-OO VmInst domain object
+ :param image_config: the associated ImageConfig object
+ :param keypair_config: the associated KeypairConfig object (optional)
+ :return: an initialized OpenStackVmInstance object
+ """
+ nova = nova_utils.nova_client(os_creds)
+ neutron = neutron_utils.neutron_client(os_creds)
+ derived_inst_config = settings_utils.create_vm_inst_config(
+ nova, neutron, vm_inst)
+
+ derived_inst_creator = OpenStackVmInstance(
+ os_creds, derived_inst_config, image_config, keypair_config)
+ derived_inst_creator.initialize()
+ return derived_inst_creator
+
+
class VmInstanceSettings(VmInstanceConfig):
"""
Deprecated, use snaps.config.vm_inst.VmInstanceConfig instead
"""
Exception to be thrown when an VM instance cannot be created
"""
+
+
+class FloatingIPAllocationError(Exception):
+ """
+ Exception to be thrown when an VM instance cannot allocate a floating IP
+ """
VmInstanceConfig, FloatingIpConfig, VmInstanceConfigError,
FloatingIpConfigError)
from snaps.config.volume import VolumeConfig
-from snaps.openstack import create_network, create_router
+from snaps.openstack import create_network, create_router, create_instance
from snaps.openstack.create_flavor import OpenStackFlavor
from snaps.openstack.create_image import OpenStackImage
from snaps.openstack.create_instance import (
from snaps.openstack.tests import openstack_tests, validation_utils
from snaps.openstack.tests.os_source_file_test import (
OSIntegrationTestCase, OSComponentTestCase)
-from snaps.openstack.utils import nova_utils
+from snaps.openstack.utils import nova_utils, settings_utils, neutron_utils
__author__ = 'spisarski'
self.assertTrue(validate_ssh_client(inst_creator))
+ def test_ssh_client_fip_after_init(self):
+ """
+ Tests the ability to assign a floating IP to an already initialized
+ OpenStackVmInstance object. After the floating IP has been allocated
+ and assigned, this test will ensure that it can be accessed via SSH.
+ """
+ port_settings = PortConfig(
+ name=self.port_1_name,
+ network_name=self.pub_net_config.network_settings.name)
+
+ instance_settings = VmInstanceConfig(
+ name=self.vm_inst_name,
+ flavor=self.flavor_creator.flavor_settings.name,
+ port_settings=[port_settings],
+ security_group_names=[self.sec_grp_creator.sec_grp_settings.name])
+
+ inst_creator = OpenStackVmInstance(
+ self.os_creds, instance_settings,
+ self.image_creator.image_settings,
+ keypair_settings=self.keypair_creator.keypair_settings)
+ self.inst_creators.append(inst_creator)
+
+ # block=True will force the create() method to block until the
+ vm_inst = inst_creator.create(block=True)
+ self.assertIsNotNone(vm_inst)
+
+ self.assertTrue(inst_creator.vm_active(block=True))
+ ip = inst_creator.get_port_ip(port_settings.name)
+ self.assertTrue(check_dhcp_lease(inst_creator, ip))
+ self.assertEqual(vm_inst.id, inst_creator.get_vm_inst().id)
+
+ inst_creator.add_floating_ip(FloatingIpConfig(
+ name=self.floating_ip_name, port_name=self.port_1_name,
+ router_name=self.pub_net_config.router_settings.name))
+
+ self.assertTrue(validate_ssh_client(inst_creator))
+
+ def test_ssh_client_fip_reverse_engineer(self):
+ """
+ Tests the ability to assign a floating IP to a reverse engineered
+ OpenStackVmInstance object. After the floating IP has been allocated
+ and assigned, this test will ensure that it can be accessed via SSH.
+ """
+ port_settings = PortConfig(
+ name=self.port_1_name,
+ network_name=self.pub_net_config.network_settings.name)
+
+ instance_settings = VmInstanceConfig(
+ name=self.vm_inst_name,
+ flavor=self.flavor_creator.flavor_settings.name,
+ port_settings=[port_settings],
+ security_group_names=[self.sec_grp_creator.sec_grp_settings.name])
+
+ inst_creator = OpenStackVmInstance(
+ self.os_creds, instance_settings,
+ self.image_creator.image_settings,
+ keypair_settings=self.keypair_creator.keypair_settings)
+ self.inst_creators.append(inst_creator)
+
+ # block=True will force the create() method to block until the
+ vm_inst = inst_creator.create(block=True)
+ self.assertIsNotNone(vm_inst)
+
+ self.assertTrue(inst_creator.vm_active(block=True))
+
+ derived_inst_creator = create_instance.generate_creator(
+ self.os_creds, vm_inst, self.image_creator.image_settings,
+ self.keypair_creator.keypair_settings)
+
+ derived_inst_creator.add_floating_ip(FloatingIpConfig(
+ name=self.floating_ip_name, port_name=self.port_1_name,
+ router_name=self.pub_net_config.router_settings.name))
+ self.inst_creators.append(derived_inst_creator)
+
+ self.assertTrue(validate_ssh_client(
+ derived_inst_creator, fip_name=self.floating_ip_name))
+
def test_ssh_client_fip_second_creator(self):
"""
Tests the ability to access a VM via SSH and a floating IP via a
return False
-def validate_ssh_client(instance_creator):
+def validate_ssh_client(instance_creator, fip_name=None):
"""
Returns True if instance_creator returns an SSH client that is valid
:param instance_creator: the object responsible for creating the VM
instance
+ :param fip_name: the name of the floating IP to use
:return: T/F
"""
ssh_active = instance_creator.vm_ssh_active(block=True)
if ssh_active:
- ssh_client = instance_creator.ssh_client()
+ ssh_client = instance_creator.ssh_client(fip_name=fip_name)
if ssh_client:
try:
out = ssh_client.exec_command('pwd')[1]
def create_security_group_rule(neutron, sec_grp_rule_settings):
"""
- Creates a security group object in OpenStack
+ Creates a security group rule in OpenStack
:param neutron: the client
:param sec_grp_rule_settings: the security group rule settings
:return: a SNAPS-OO SecurityGroupRule domain object
def delete_security_group_rule(neutron, sec_grp_rule):
"""
- Deletes a security group object from OpenStack
+ Deletes a security group rule object from OpenStack
:param neutron: the client
:param sec_grp_rule: the SNAPS SecurityGroupRule object to delete
"""
def get_rules_by_security_group_id(neutron, sec_grp_id):
"""
- Retrieves all of the rules for a given security group
+ Retrieves all of the rules for a given security group by it's ID
:param neutron: the client
:param sec_grp_id: the ID of the associated security group
"""
def get_rule_by_id(neutron, sec_grp, rule_id):
"""
- Deletes a security group object from OpenStack
+ Returns a SecurityGroupRule object from OpenStack
:param neutron: the client
:param sec_grp: the SNAPS SecurityGroup domain object
:param rule_id: the rule's ID
:param ports: a list of tuple 2 where index 0 is the port name and index 1
is the SNAPS-OO Port object
:return: a list of tuple 2 (port_id, SNAPS FloatingIp) objects when ports
- is not None else a list of Port objects
+ is not None else a list of FloatingIp objects
"""
out = list()
fips = neutron.list_floatingips()
def get_network_quotas(neutron, project_id):
"""
- Returns a list of all available keypairs
+ Returns a list of NetworkQuotas objects
:param neutron: the neutron client
:param project_id: the project's ID of the quotas to lookup
:return: an object of type NetworkQuotas or None if not found
def create_security_group_config(neutron, security_group):
"""
- Returns a NetworkConfig object
+ Returns a SecurityGroupConfig object
:param neutron: the neutron client
:param security_group: a SNAPS-OO SecurityGroup domain object
:return:
def create_volume_config(volume):
"""
- Returns a VolumeSettings object
+ Returns a VolumeConfig object
:param volume: a SNAPS-OO Volume object
"""
def create_volume_type_config(volume_type):
"""
- Returns a VolumeTypeSettings object
+ Returns a VolumeTypeConfig object
:param volume_type: a SNAPS-OO VolumeType object
"""
def create_flavor_config(flavor):
"""
- Returns a VolumeSettings object
- :param flavor: a SNAPS-OO Volume object
+ Returns a FlavorConfig object
+ :param flavor: a FlavorConfig object
"""
return FlavorConfig(
name=flavor.name, flavor_id=flavor.id, ram=flavor.ram,
def create_vm_inst_config(nova, neutron, server):
"""
- Returns a NetworkConfig object
+ Returns a VmInstanceConfig object
+ note: if the server instance is not active, the PortSettings objects will
+ not be generated resulting in an invalid configuration
:param nova: the nova client
:param neutron: the neutron client
:param server: a SNAPS-OO VmInst domain object
def __create_port_config(neutron, networks):
"""
- Returns a list of port settings based on the networks parameter
+ Returns a list of PortConfig objects based on the networks parameter
:param neutron: the neutron client
:param networks: a list of tuples where #1 is the SNAPS Network domain
object and #2 is a list of IP addresses