From f831a4b3d80bcbfd9710dc8346d3c1b9ee44dede Mon Sep 17 00:00:00 2001 From: abregman Date: Wed, 15 Aug 2018 12:45:02 +0300 Subject: [PATCH] Add common stack methods * Creating and removing stacks * Waiting for resource status to be complete * Loading Heat template Change-Id: I3dbafb03d5ca671865335e7320c67f07fa5f9aaa --- tobiko/common/stack.py | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tobiko/common/stack.py diff --git a/tobiko/common/stack.py b/tobiko/common/stack.py new file mode 100644 index 000000000..997b4f7e2 --- /dev/null +++ b/tobiko/common/stack.py @@ -0,0 +1,64 @@ +# Copyright 2018 Red Hat +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +import os +import time + +from heatclient.common import template_utils +import yaml + + +class StackManager(object): + """Manages Heat stacks.""" + + def __init__(self, client_manager, templates_dir): + self.client = client_manager.get_heat_client() + self.templates_dir = templates_dir + + def load_template(self, template_path): + """Loads template from a given file.""" + _files, template = template_utils.get_template_contents(template_path) + return yaml.safe_dump(template) + + def create_stack(self, stack_name, template_name, parameters): + """Creates stack based on passed parameters.""" + template = self.load_template(os.path.join(self.templates_dir, + template_name)) + + stack = self.client.stacks.create(stack_name=stack_name, + template=template, + parameters=parameters) + + print(stack) + return stack + + def delete_stack(self, sid): + """Deletes stack.""" + self.client.stacks.delete(sid) + + def get_stack(self, stack_name): + """Returns stack ID.""" + return self.client.stacks.get(stack_name) + + def wait_for_status_complete(self, stack_id, resource_name): + """Verifies resource reached complete status. + + If it didn't, the method will wait until resource reaches + complete status or when timeout reached. + """ + res = self.client.resources.get(stack_id, resource_name) + print(res.resource_status) + while (res.resource_status != 'CREATE_COMPLETE'): + print(res.resource_status) + time.sleep(5) + res = self.client.resources.get(stack_id, resource_name)