def pre_protect(self, cnxt, workload_action_excution):
        for db_action in self.actions:
            # instantiate action class
            action_obj = action.load(db_action, context=cnxt)

            # invoke protect method on action class passing resource id
            action_obj.pre_protect(cnxt, workload_action_excution.id, db_action.resource.id)
    def pre_protect(self, cnxt, workload_action_excution):
        for db_action in self.actions:
            # instantiate action class
            action_obj = action.load(db_action, context=cnxt)

            # invoke protect method on action class passing resource id
            action_obj.pre_protect(cnxt, workload_action_excution.id,
                                   db_action.resource.id)
    def protect(self, cnxt):
        # Define container_name, Swift create if container does not exists
        container_name = get_policy_execution_container_name(
            cnxt.tenant, self.name)
        swift_conn = Clients(cnxt).swift()
        # container = swift.swift_create_container(request, name)
        headers = {'X-Container-Meta-dr_state': 'processing'}
        swift_conn.put_container(container_name, headers)
        LOG.debug("put_container %s " % (container_name))

        # Create a global container for the tenant in case it does not exists
        if action.Action.is_using_global_container:
            swift_conn.put_container(
                get_global_container_name(cnxt.tenant, self.name),
                {'X-Container-Meta-dr_state': 'ready'})

        # create workload_policy_execution instance
        protect_execution = {
            'workload_policy_id': self.id,
            'status': 'Creating',
        }
        workload_action_excution =\
            api.workload_policy_excution_create(cnxt, protect_execution)
        # TODO(load dynamic)
        template_generator = heat_flame_template.HeatFlameTemplate(cnxt)

        try:
            if self.consistent_protect:
                self.pre_protect(cnxt, workload_action_excution)

            # iterate on actions
            protect_statuses = []
            metadata = {}
            metadata["workload_policy_name"] = self.name
            policy_status = True
            LOG.debug("workload_policy protect, policy_status = %s" %
                      (policy_status))
            metadata["actions"] = []
            for db_action in self.actions:
                try:
                    # instantiate action class
                    action_obj = action.load(db_action, context=cnxt)

                    if action_obj.is_using_global_container():
                        action_container_name =\
                            get_global_container_name(cnxt.tenant, self.name)
                    else:
                        action_container_name = container_name

                    # invoke protect method on action class passing resource id
                    status, protect_data =\
                        action_obj.protect(cnxt, workload_action_excution.id,
                                           db_action.resource.id,
                                           action_container_name)

                    LOG.debug("workload_policy protect, status = %s" %
                              (status))
                    policy_status = policy_status and (status == "Protected")
                    LOG.debug("workload_policy protect, policy_status = %s" %
                              (policy_status))

                    action_obj.generate_template(cnxt, template_generator)

                    # save result of invocation in relation
                    # to workload_policy_execution
                    protect_statuses.\
                        append({"action id": db_action.action.id,
                                "action_name": db_action.action.name,
                                "resource_id": db_action.resource.id,
                                "resource_name": db_action.resource.name,
                                "status": status})

                    db_action.execution_data = protect_data
                    metadata["actions"].append(db_action)
                    LOG.debug("action metadata %s " % (metadata))
                except Exception, e:
                    policy_status = False
                    exc = sys.exc_info()
                    LOG.error(traceback.format_exception(*exc))
                    LOG.error("resource %s could not be protected using action"
                              "%s. Verify that the resource is a valid "
                              " resource" %
                              (db_action.resource.id, db_action.action.name))
        except Exception, e:
            policy_status = False
            LOG.debug(e)
            LOG.error("Workload could not be protected")
    def protect(self, cnxt):
        # Define container_name, Swift create if container does not exists
        container_name = get_policy_execution_container_name(cnxt.tenant, self.name)
        swift_conn = Clients(cnxt).swift()
        # container = swift.swift_create_container(request, name)
        headers = {"X-Container-Meta-dr_state": "processing"}
        swift_conn.put_container(container_name, headers)
        LOG.debug("put_container %s " % (container_name))

        # Create a global container for the tenant in case it does not exists
        if action.Action.is_using_global_container:
            swift_conn.put_container(
                get_global_container_name(cnxt.tenant, self.name), {"X-Container-Meta-dr_state": "ready"}
            )

        # create workload_policy_execution instance
        protect_execution = {"workload_policy_id": self.id, "status": "Creating"}
        workload_action_excution = api.workload_policy_excution_create(cnxt, protect_execution)
        # TODO(load dynamic)
        template_generator = heat_flame_template.HeatFlameTemplate(cnxt)

        try:
            if self.consistent_protect:
                self.pre_protect(cnxt, workload_action_excution)

            # iterate on actions
            protect_statuses = []
            metadata = {}
            metadata["workload_policy_name"] = self.name
            policy_status = True
            LOG.debug("workload_policy protect, policy_status = %s" % (policy_status))
            metadata["actions"] = []
            for db_action in self.actions:
                try:
                    # instantiate action class
                    action_obj = action.load(db_action, context=cnxt)

                    if action_obj.is_using_global_container():
                        action_container_name = get_global_container_name(cnxt.tenant, self.name)
                    else:
                        action_container_name = container_name

                    # invoke protect method on action class passing resource id
                    status, protect_data = action_obj.protect(
                        cnxt, workload_action_excution.id, db_action.resource.id, action_container_name
                    )

                    LOG.debug("workload_policy protect, status = %s" % (status))
                    policy_status = policy_status and (status == "Protected")
                    LOG.debug("workload_policy protect, policy_status = %s" % (policy_status))

                    action_obj.generate_template(cnxt, template_generator)

                    # save result of invocation in relation
                    # to workload_policy_execution
                    protect_statuses.append(
                        {
                            "action id": db_action.action.id,
                            "action_name": db_action.action.name,
                            "resource_id": db_action.resource.id,
                            "resource_name": db_action.resource.name,
                            "status": status,
                        }
                    )

                    db_action.execution_data = protect_data
                    metadata["actions"].append(db_action)
                    LOG.debug("action metadata %s " % (metadata))
                except Exception, e:
                    policy_status = False
                    exc = sys.exc_info()
                    LOG.error(traceback.format_exception(*exc))
                    LOG.error(
                        "resource %s could not be protected using action"
                        "%s. Verify that the resource is a valid "
                        " resource" % (db_action.resource.id, db_action.action.name)
                    )
        except Exception, e:
            policy_status = False
            LOG.debug(e)
            LOG.error("Workload could not be protected")