Second patch for volume support.
[snaps.git] / snaps / openstack / utils / heat_utils.py
index a91a21c..6910bfe 100644 (file)
@@ -17,12 +17,13 @@ import logging
 import yaml
 from heatclient.client import Client
 from heatclient.common.template_format import yaml_loader
+from novaclient.exceptions import NotFound
 from oslo_serialization import jsonutils
 
 from snaps import file_utils
-from snaps.domain.stack import Stack
+from snaps.domain.stack import Stack, Resource, Output
 
-from snaps.openstack.utils import keystone_utils
+from snaps.openstack.utils import keystone_utils, neutron_utils, nova_utils
 
 __author__ = 'spisarski'
 
@@ -86,17 +87,6 @@ def get_stack_status(heat_cli, stack_id):
     return heat_cli.stacks.get(stack_id).stack_status
 
 
-def get_stack_outputs(heat_cli, stack_id):
-    """
-    Returns a domain Stack object for a given ID
-    :param heat_cli: the OpenStack heat client
-    :param stack_id: the ID of the heat stack to retrieve
-    :return: the Stack domain object else None
-    """
-    stack = heat_cli.stacks.get(stack_id)
-    return stack.outputs
-
-
 def create_stack(heat_cli, stack_settings):
     """
     Executes an Ansible playbook to the given host
@@ -130,6 +120,102 @@ def delete_stack(heat_cli, stack):
     heat_cli.stacks.delete(stack.id)
 
 
+def __get_os_resources(heat_cli, stack):
+    """
+    Returns all of the OpenStack resource objects for a given stack
+    :param heat_cli: the OpenStack heat client
+    :param stack: the SNAPS-OO Stack domain object
+    :return: a list
+    """
+    return heat_cli.resources.list(stack.id)
+
+
+def get_resources(heat_cli, stack):
+    """
+    Returns all of the OpenStack resource objects for a given stack
+    :param heat_cli: the OpenStack heat client
+    :param stack: the SNAPS-OO Stack domain object
+    :return: a list
+    """
+    os_resources = __get_os_resources(heat_cli, stack)
+
+    if os_resources:
+        out = list()
+        for os_resource in os_resources:
+            out.append(Resource(resource_type=os_resource.resource_type,
+                                resource_id=os_resource.physical_resource_id))
+        return out
+
+
+def get_outputs(heat_cli, stack):
+    """
+    Returns all of the SNAPS-OO Output domain objects for the defined outputs
+    for given stack
+    :param heat_cli: the OpenStack heat client
+    :param stack: the SNAPS-OO Stack domain object
+    :return: a list
+    """
+    out = list()
+
+    os_stack = heat_cli.stacks.get(stack.id)
+
+    outputs = None
+    if os_stack:
+        outputs = os_stack.outputs
+
+    if outputs:
+        for output in outputs:
+            out.append(Output(**output))
+
+    return out
+
+
+def get_stack_networks(heat_cli, neutron, stack):
+    """
+    Returns an instance of NetworkSettings for each network owned 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 NetworkSettings
+    """
+
+    out = list()
+    resources = get_resources(heat_cli, stack)
+    for resource in resources:
+        if resource.type == 'OS::Neutron::Net':
+            network = neutron_utils.get_network_by_id(
+                neutron, resource.id)
+            if network:
+                out.append(network)
+
+    return out
+
+
+def get_stack_servers(heat_cli, nova, stack):
+    """
+    Returns an instance of NetworkSettings for each network owned by this 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 NetworkSettings
+    """
+
+    out = list()
+    resources = get_resources(heat_cli, stack)
+    for resource in resources:
+        if resource.type == 'OS::Nova::Server':
+            try:
+                server = nova_utils.get_server_object_by_id(
+                    nova, resource.id)
+                if server:
+                    out.append(server)
+            except NotFound:
+                logger.warn(
+                    'VmInst 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