Example #1
0
    def post(self, tenant_id):
        """
        Create and deploy a new stack.

        :param tenant_id:
        :return: 409, if the stack name was already used.
            400, if the heat template could not be parsed properly.
            500, if any exception occurred while creation.
            200, if everything worked out.
        """
        logging.debug("API CALL: %s POST" % str(self.__class__.__name__))

        try:
            stack_dict = json.loads(request.data)
            for stack in self.api.compute.stacks.values():
                if stack.stack_name == stack_dict['stack_name']:
                    return [], 409
            stack = Stack()
            stack.stack_name = stack_dict['stack_name']
            reader = HeatParser(self.api.compute)

            if isinstance(stack_dict['template'], str) or isinstance(stack_dict['template'], unicode):
                stack_dict['template'] = json.loads(stack_dict['template'])
            if not reader.parse_input(stack_dict['template'], stack, self.api.compute.dc.label):
                self.api.compute.clean_broken_stack(stack)
                return 'Could not create stack.', 400

            stack.creation_time = str(datetime.now())
            stack.status = "CREATE_COMPLETE"

            return_dict = {"stack": {"id": stack.id,
                                     "links": [
                                         {
                                             "href": "http://%s:%s/v1/%s/stacks/%s"
                                                     % (self.api.ip, self.api.port, tenant_id, stack.id),
                                             "rel": "self"
                                         }]}}

            self.api.compute.add_stack(stack)
            self.api.compute.deploy_stack(stack.id)
            return Response(json.dumps(return_dict), status=200, mimetype="application/json")

        except Exception as ex:
            logging.exception("Heat: Create Stack exception.")
            return ex.message, 500
Example #2
0
    def put(self, tenant_id, stack_name_or_id, stack_id=None):
        """
        Updates an existing stack with a new heat template.

        :param tenant_id:
        :param stack_name_or_id: Specifies the stack, which should be updated.
        :param stack_id:
        :return: 404, if the requested stack could not be found.
            400, if the stack creation (because of errors in the heat template) or the stack update failed.
            500, if any exception occurred while updating.
            202, if everything worked out.
        """
        logging.debug("Heat: Update Stack")
        try:
            old_stack = None
            if stack_name_or_id in self.api.compute.stacks:
                old_stack = self.api.compute.stacks[stack_name_or_id]
            else:
                for tmp_stack in self.api.compute.stacks.values():
                    if tmp_stack.stack_name == stack_name_or_id:
                        old_stack = tmp_stack
            if old_stack is None:
                return 'Could not resolve Stack - ID', 404

            stack_dict = json.loads(request.data)

            stack = Stack()
            stack.stack_name = old_stack.stack_name
            stack.id = old_stack.id
            stack.creation_time = old_stack.creation_time
            stack.update_time = str(datetime.now())
            stack.status = "UPDATE_COMPLETE"

            reader = HeatParser(self.api.compute)
            if isinstance(stack_dict['template'], str) or isinstance(stack_dict['template'], unicode):
                stack_dict['template'] = json.loads(stack_dict['template'])
            if not reader.parse_input(stack_dict['template'], stack, self.api.compute.dc.label):
                return 'Could not create stack.', 400

            if not self.api.compute.update_stack(old_stack.id, stack):
                return 'Could not update stack.', 400

            return Response(status=202, mimetype="application/json")

        except Exception as ex:
            logging.exception("Heat: Update Stack exception")
            return ex.message, 500