Refactoring of ImageSettings to extend ImageConfig
[snaps.git] / snaps / openstack / utils / heat_utils.py
index f2d4efd..e319229 100644 (file)
@@ -23,8 +23,8 @@ from oslo_serialization import jsonutils
 from snaps import file_utils
 from snaps.domain.stack import Stack, Resource, Output
 
-from snaps.openstack.utils import keystone_utils, neutron_utils, nova_utils, \
-    cinder_utils
+from snaps.openstack.utils import (
+    keystone_utils, neutron_utils, nova_utils, cinder_utils)
 
 __author__ = 'spisarski'
 
@@ -37,7 +37,7 @@ def heat_client(os_creds):
     :param os_creds: the OpenStack credentials
     :return: the client
     """
-    logger.debug('Retrieving Nova Client')
+    logger.debug('Retrieving Heat Client')
     return Client(os_creds.heat_api_version,
                   session=keystone_utils.keystone_session(os_creds),
                   region_name=os_creds.region_name)
@@ -157,8 +157,11 @@ def get_resources(heat_cli, stack, res_type=None):
             if ((res_type and os_resource.resource_type == res_type)
                     or not res_type):
                 out.append(Resource(
+                    name=os_resource.resource_name,
                     resource_type=os_resource.resource_type,
-                    resource_id=os_resource.physical_resource_id))
+                    resource_id=os_resource.physical_resource_id,
+                    status=os_resource.resource_status,
+                    status_reason=os_resource.resource_status_reason))
         return out
 
 
@@ -197,14 +200,52 @@ def get_stack_networks(heat_cli, neutron, stack):
     out = list()
     resources = get_resources(heat_cli, stack, 'OS::Neutron::Net')
     for resource in resources:
-        network = neutron_utils.get_network_by_id(
-            neutron, resource.id)
+        network = neutron_utils.get_network_by_id(neutron, resource.id)
         if network:
             out.append(network)
 
     return out
 
 
+def get_stack_routers(heat_cli, neutron, stack):
+    """
+    Returns a list of Network domain objects deployed by this stack
+    :param heat_cli: the OpenStack heat client object
+    :param neutron: the OpenStack neutron client object
+    :param stack: the SNAPS-OO Stack domain object
+    :return: a list of Network objects
+    """
+
+    out = list()
+    resources = get_resources(heat_cli, stack, 'OS::Neutron::Router')
+    for resource in resources:
+        router = neutron_utils.get_router_by_id(neutron, resource.id)
+        if router:
+            out.append(router)
+
+    return out
+
+
+def get_stack_security_groups(heat_cli, neutron, stack):
+    """
+    Returns a list of SecurityGroup domain objects deployed by this stack
+    :param heat_cli: the OpenStack heat client object
+    :param neutron: the OpenStack neutron client object
+    :param stack: the SNAPS-OO Stack domain object
+    :return: a list of SecurityGroup objects
+    """
+
+    out = list()
+    resources = get_resources(heat_cli, stack, 'OS::Neutron::SecurityGroup')
+    for resource in resources:
+        security_group = neutron_utils.get_security_group_by_id(
+            neutron, resource.id)
+        if security_group:
+            out.append(security_group)
+
+    return out
+
+
 def get_stack_servers(heat_cli, nova, stack):
     """
     Returns a list of VMInst domain objects associated with a Stack
@@ -227,9 +268,31 @@ def get_stack_servers(heat_cli, nova, stack):
     return out
 
 
+def get_stack_keypairs(heat_cli, nova, stack):
+    """
+    Returns a list of Keypair domain objects associated with a Stack
+    :param heat_cli: the OpenStack heat client object
+    :param nova: the OpenStack nova client object
+    :param stack: the SNAPS-OO Stack domain object
+    :return: a list of VMInst domain objects
+    """
+
+    out = list()
+    resources = get_resources(heat_cli, stack, 'OS::Nova::KeyPair')
+    for resource in resources:
+        try:
+            keypair = nova_utils.get_keypair_by_id(nova, resource.id)
+            if keypair:
+                out.append(keypair)
+        except NotFound:
+            logger.warn('Keypair cannot be located with ID %s', resource.id)
+
+    return out
+
+
 def get_stack_volumes(heat_cli, cinder, stack):
     """
-    Returns an instance of NetworkSettings for each network owned by this stack
+    Returns an instance of Volume domain objects created by this stack
     :param heat_cli: the OpenStack heat client object
     :param cinder: the OpenStack cinder client object
     :param stack: the SNAPS-OO Stack domain object
@@ -251,7 +314,7 @@ def get_stack_volumes(heat_cli, cinder, stack):
 
 def get_stack_volume_types(heat_cli, cinder, stack):
     """
-    Returns an instance of NetworkSettings for each network owned by this stack
+    Returns an instance of VolumeType domain objects created by this stack
     :param heat_cli: the OpenStack heat client object
     :param cinder: the OpenStack cinder client object
     :param stack: the SNAPS-OO Stack domain object
@@ -271,6 +334,29 @@ def get_stack_volume_types(heat_cli, cinder, stack):
     return out
 
 
+def get_stack_flavors(heat_cli, nova, stack):
+    """
+    Returns an instance of Flavor SNAPS domain object for each flavor created
+    by this stack
+    :param heat_cli: the OpenStack heat client object
+    :param nova: the OpenStack cinder client object
+    :param stack: the SNAPS-OO Stack domain object
+    :return: a list of Volume domain objects
+    """
+
+    out = list()
+    resources = get_resources(heat_cli, stack, 'OS::Nova::Flavor')
+    for resource in resources:
+        try:
+            flavor = nova_utils.get_flavor_by_id(nova, resource.id)
+            if flavor:
+                out.append(flavor)
+        except NotFound:
+            logger.warn('Flavor cannot be located with ID %s', resource.id)
+
+    return out
+
+
 def parse_heat_template_str(tmpl_str):
     """
     Takes a heat template string, performs some simple validation and returns a