示例#1
0
文件: resregistry.py 项目: ooici/pyon
    def set_lifecycle_state(self, resource_id='', target_lcstate=''):
        """Sets the lifecycle state (if possible) to the target state. Supports compound states"""
        if not target_lcstate:
            raise BadRequest("Bad life-cycle state %s" % target_lcstate)
        if target_lcstate.startswith(LCS.DELETED):
            self.lcs_delete(resource_id)
        if target_lcstate.startswith(LCS.RETIRED):
            self.execute_lifecycle_transition(resource_id, LCE.RETIRE)

        res_obj = self.read(resource_id)
        old_lcstate = res_obj.lcstate
        old_availability = res_obj.availability

        restype = res_obj.type_
        restype_workflow = get_restype_lcsm(restype)
        if not restype_workflow:
            raise BadRequest("Resource id=%s type=%s has no lifecycle" % (resource_id, restype))

        if '_' in target_lcstate:    # Support compound
            target_lcs, target_av = lcsplit(target_lcstate)
            if target_lcs not in LCS:
                raise BadRequest("Unknown life-cycle state %s" % target_lcs)
            if target_av and target_av not in AS:
                raise BadRequest("Unknown life-cycle availability %s" % target_av)
        elif target_lcstate in LCS:
            target_lcs, target_av = target_lcstate, res_obj.availability
        elif target_lcstate in AS:
            target_lcs, target_av = res_obj.lcstate, target_lcstate
        else:
            raise BadRequest("Unknown life-cycle state %s" % target_lcstate)

        # Check that target state is allowed
        lcs_successors = restype_workflow.get_lcstate_successors(old_lcstate)
        av_successors = restype_workflow.get_availability_successors(old_availability)
        found_lcs, found_av = target_lcs in lcs_successors.values(), target_av in av_successors.values()
        if not found_lcs and not found_av:
            raise BadRequest("Target state %s not reachable for resource in state %s_%s" % (
                target_lcstate, old_lcstate, old_availability))

        res_obj.lcstate = target_lcs
        res_obj.availability = target_av
        res_obj.ts_updated = get_ion_ts()

        updres = self.rr_store.update(res_obj)
        log.debug("set_lifecycle_state(res_id=%s, target=%s). Change %s_%s to %s_%s", resource_id, target_lcstate,
                  old_lcstate, old_availability, res_obj.lcstate, res_obj.availability)

        if self.container.has_capability(self.container.CCAP.EVENT_PUBLISHER):
            self.event_pub.publish_event(event_type="ResourceLifecycleEvent",
                                     origin=res_obj._id, origin_type=res_obj.type_,
                                     sub_type="%s.%s" % (res_obj.lcstate, res_obj.availability),
                                     lcstate=res_obj.lcstate, availability=res_obj.availability,
                                     lcstate_before=old_lcstate, availability_before=old_availability)
示例#2
0
    def set_lifecycle_state(self, resource_id='', target_lcstate=''):
        """Sets the lifecycle state (if possible) to the target state. Supports compound states"""
        if not target_lcstate:
            raise BadRequest("Bad life-cycle state %s" % target_lcstate)
        if target_lcstate.startswith('RETIRED'):
            return self.retire(resource_id)

        res_obj = self.read(resource_id)
        old_target = target_lcstate
        old_state = res_obj.lcstate
        old_availability = res_obj.availability
        old_lcs = lcstate(old_state, old_availability)
        restype = res_obj._get_type()
        restype_workflow = get_restype_lcsm(restype)
        if not restype_workflow:
            raise BadRequest("Resource id=%s type=%s has no lifecycle" % (resource_id, restype))

        if '_' in target_lcstate:    # Support compound
            target_lcmat, target_lcav = lcsplit(target_lcstate)
            if target_lcmat not in LCS:
                raise BadRequest("Unknown life-cycle state %s" % target_lcmat)
            if target_lcav and target_lcav not in AS:
                raise BadRequest("Unknown life-cycle availability %s" % target_lcav)
        elif target_lcstate in LCS:
            target_lcmat, target_lcav = target_lcstate, res_obj.availability
            target_lcstate = lcstate(target_lcmat, target_lcav)
        elif target_lcstate in AS:
            target_lcmat, target_lcav = res_obj.lcstate, target_lcstate
            target_lcstate = lcstate(target_lcmat, target_lcav)
        else:
            raise BadRequest("Unknown life-cycle state %s" % target_lcstate)

        # Check that target state is allowed
        if not target_lcstate in restype_workflow.get_successors(old_lcs).values():
            raise BadRequest("Target state %s not reachable for resource in state %s" % (target_lcstate, old_lcs))

        res_obj.lcstate = target_lcmat
        res_obj.availability = target_lcav

        res_obj.ts_updated = get_ion_ts()

        updres = self.rr_store.update(res_obj)
        log.debug("set_lifecycle_state(res_id=%s, target=%s). Change %s_%s to %s_%s", resource_id, old_target,
                  old_state, old_availability, res_obj.lcstate, res_obj.availability)

        if self.container.has_capability(self.container.CCAP.EVENT_PUBLISHER):
            self.event_pub.publish_event(event_type="ResourceLifecycleEvent",
                                     origin=res_obj._id, origin_type=res_obj.type_,
                                     sub_type="%s.%s" % (res_obj.lcstate, res_obj.availability),
                                     lcstate=res_obj.lcstate, availability=res_obj.availability,
                                     lcstate_before=old_state, availability_before=old_availability)
示例#3
0
    def execute_lifecycle_transition(self, resource_id='', transition_event=''):
        if transition_event == LCE.RETIRE:
            return self.retire(resource_id)

        res_obj = self.read(resource_id)

        old_state = res_obj.lcstate
        old_availability = res_obj.availability
        old_lcs = lcstate(old_state, old_availability)

        restype = res_obj._get_type()
        restype_workflow = get_restype_lcsm(restype)
        if not restype_workflow:
            raise BadRequest("Resource id=%s type=%s has no lifecycle" % (resource_id, restype))

        new_state = restype_workflow.get_successor(old_lcs, transition_event)
        if not new_state:
            raise BadRequest("Resource id=%s, type=%s, lcstate=%s has no transition for event %s" % (
                resource_id, restype, old_lcs, transition_event))

        lcmat, lcav = lcsplit(new_state)
        res_obj.lcstate = lcmat
        res_obj.availability = lcav

        res_obj.ts_updated = get_ion_ts()
        self.rr_store.update(res_obj)
        log.debug("execute_lifecycle_transition(res_id=%s, event=%s). Change %s_%s to %s_%s", resource_id, transition_event,
                  old_state, old_availability, res_obj.lcstate, res_obj.availability)

        if self.container.has_capability(self.container.CCAP.EVENT_PUBLISHER):
            self.event_pub.publish_event(event_type="ResourceLifecycleEvent",
                                     origin=res_obj._id, origin_type=res_obj.type_,
                                     sub_type="%s.%s" % (res_obj.lcstate, res_obj.availability),
                                     lcstate=res_obj.lcstate, availability=res_obj.availability,
                                     lcstate_before=old_state, availability_before=old_availability,
                                     transition_event=transition_event)

        return lcstate(res_obj.lcstate, res_obj.availability)