Fixed logger name.
[snaps.git] / snaps / openstack / create_instance.py
index 99ab87c..97f04f3 100644 (file)
@@ -190,12 +190,12 @@ class OpenStackVmInstance:
         :return: the external network name or None
         """
         router = neutron_utils.get_router_by_name(self.__neutron, router_name)
-        if router and router['router'].get('external_gateway_info'):
+        if router and router.external_gateway_info:
             network = neutron_utils.get_network_by_id(
                 self.__neutron,
-                router['router']['external_gateway_info']['network_id'])
+                router.external_gateway_info['network_id'])
             if network:
-                return network['network']['name']
+                return network.name
         return None
 
     def clean(self):
@@ -263,27 +263,16 @@ class OpenStackVmInstance:
         ports = list()
 
         for port_setting in port_settings:
-            # First check to see if network already has this port
-            # TODO/FIXME - this could potentially cause problems if another
-            # port with the same name exists
-            # VM has the same network/port name pair
-            found = False
-
-            # TODO/FIXME - should we not be iterating on ports for the specific
-            # network in question as unique port names
-            # seem to only be important by network
-            existing_ports = self.__neutron.list_ports()['ports']
-            for existing_port in existing_ports:
-                if existing_port['name'] == port_setting.name:
-                    ports.append((port_setting.name, {'port': existing_port}))
-                    found = True
-                    break
-
-            if not found and not cleanup:
-                ports.append((port_setting.name,
-                              neutron_utils.create_port(self.__neutron,
-                                                        self.__os_creds,
-                                                        port_setting)))
+            port = neutron_utils.get_port_by_name(self.__neutron,
+                                                  port_setting.name)
+            if port:
+                ports.append((port_setting.name, {'port': port}))
+            elif not cleanup:
+                # Exception will be raised when port with same name already
+                # exists
+                ports.append(
+                    (port_setting.name, neutron_utils.create_port(
+                        self.__neutron, self.__os_creds, port_setting)))
 
         return ports
 
@@ -298,13 +287,13 @@ class OpenStackVmInstance:
         if subnet:
             # Take IP of subnet if there is one configured on which to place
             # the floating IP
-            for fixed_ip in port['port']['fixed_ips']:
+            for fixed_ip in port.fixed_ips:
                 if fixed_ip['subnet_id'] == subnet['subnet']['id']:
                     ip = fixed_ip['ip_address']
                     break
         else:
             # Simply take the first
-            ip = port['port']['fixed_ips'][0]['ip_address']
+            ip = port.ips[0]['ip_address']
 
         if ip:
             count = timeout / poll_interval
@@ -345,12 +334,12 @@ class OpenStackVmInstance:
         """
         return self.__vm
 
-    def get_os_vm_server_obj(self):
+    def get_console_output(self):
         """
-        Returns the OpenStack server object
-        :return: the server object
+        Returns the vm console object for parsing logs
+        :return: the console output object
         """
-        return nova_utils.get_latest_server_os_object(self.__nova, self.__vm)
+        return nova_utils.get_server_console_output(self.__nova, self.__vm)
 
     def get_port_ip(self, port_name, subnet_name=None):
         """
@@ -363,7 +352,6 @@ class OpenStackVmInstance:
         """
         port = self.get_port_by_name(port_name)
         if port:
-            port_dict = port['port']
             if subnet_name:
                 subnet = neutron_utils.get_subnet_by_name(self.__neutron,
                                                           subnet_name)
@@ -372,13 +360,12 @@ class OpenStackVmInstance:
                                    'not be located with name - %s',
                                    subnet_name)
                     return None
-                for fixed_ip in port_dict['fixed_ips']:
-                    if fixed_ip['subnet_id'] == subnet['subnet']['id']:
+                for fixed_ip in port.ips:
+                    if fixed_ip['subnet_id'] == subnet.id:
                         return fixed_ip['ip_address']
             else:
-                fixed_ips = port_dict['fixed_ips']
-                if fixed_ips and len(fixed_ips) > 0:
-                    return fixed_ips[0]['ip_address']
+                if port.ips and len(port.ips) > 0:
+                    return port.ips[0]['ip_address']
         return None
 
     def get_port_mac(self, port_name):
@@ -392,8 +379,7 @@ class OpenStackVmInstance:
         """
         port = self.get_port_by_name(port_name)
         if port:
-            port_dict = port['port']
-            return port_dict['mac_address']
+            return port.mac_address
         return None
 
     def get_port_by_name(self, port_name):
@@ -408,11 +394,18 @@ class OpenStackVmInstance:
         logger.warning('Cannot find port with name - ' + port_name)
         return None
 
+    def get_vm_info(self):
+        """
+        Returns a dictionary of a VMs info as returned by OpenStack
+        :return: a dict()
+        """
+        return nova_utils.get_server_info(self.__nova, self.__vm)
+
     def config_nics(self):
         """
         Responsible for configuring NICs on RPM systems where the instance has
         more than one configured port
-        :return: None
+        :return: the value returned by ansible_utils.apply_ansible_playbook()
         """
         if len(self.__ports) > 1 and len(self.__floating_ips) > 0:
             if self.vm_active(block=True) and self.vm_ssh_active(block=True):
@@ -420,11 +413,12 @@ class OpenStackVmInstance:
                     port_index = self.__ports.index((key, port))
                     if port_index > 0:
                         nic_name = 'eth' + repr(port_index)
-                        self.__config_nic(
+                        retval = self.__config_nic(
                             nic_name, port,
                             self.__get_first_provisioning_floating_ip().ip)
                         logger.info('Configured NIC - %s on VM - %s',
                                     nic_name, self.instance_settings.name)
+                        return retval
 
     def __get_first_provisioning_floating_ip(self):
         """
@@ -450,7 +444,7 @@ class OpenStackVmInstance:
         :param ip: The IP on which to apply the playbook.
         :return: the return value from ansible
         """
-        port_ip = port['port']['fixed_ips'][0]['ip_address']
+        port_ip = port.ips[0]['ip_address']
         variables = {
             'floating_ip': ip,
             'nic_name': nic_name,
@@ -568,18 +562,17 @@ class OpenStackVmInstance:
         if not self.__vm:
             return False
 
-        instance = nova_utils.get_latest_server_os_object(
-            self.__nova, self.__vm)
-        if not instance:
+        status = nova_utils.get_server_status(self.__nova, self.__vm)
+        if not status:
             logger.warning('Cannot find instance with id - ' + self.__vm.id)
             return False
 
-        if instance.status == 'ERROR':
+        if status == 'ERROR':
             raise Exception('Instance had an error during deployment')
         logger.debug(
             'Instance status [%s] is - %s', self.instance_settings.name,
-            instance.status)
-        return instance.status == expected_status_code
+            status)
+        return status == expected_status_code
 
     def vm_ssh_active(self, block=False, poll_interval=POLL_INTERVAL):
         """