def _get_by_ref_or_id(self, ref_or_id, exclude_fields=None, include_fields=None): """ Retrieve resource object by an id of a reference. Note: This method throws StackStormDBObjectNotFoundError exception if the object is not found in the database. """ if exclude_fields and include_fields: msg = ('exclude_fields and include_fields arguments are mutually exclusive. ' 'You need to provide either one or another, but not both.') raise ValueError(msg) if ResourceReference.is_resource_reference(ref_or_id): # references always contain a dot and id's can't contain it is_reference = True else: is_reference = False if is_reference: resource_db = self._get_by_ref(resource_ref=ref_or_id, exclude_fields=exclude_fields, include_fields=include_fields) else: resource_db = self._get_by_id(resource_id=ref_or_id, exclude_fields=exclude_fields, include_fields=include_fields) if not resource_db: msg = 'Resource with a reference or id "%s" not found' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) return resource_db
def _get_by_ref_or_id(self, ref_or_id, exclude_fields=None): """ Retrieve resource object by an id of a reference. Note: This method throws StackStormDBObjectNotFoundError exception if the object is not found in the database. """ if ResourceReference.is_resource_reference(ref_or_id): # references always contain a dot and id's can't contain it is_reference = True else: is_reference = False if is_reference: resource_db = self._get_by_ref(resource_ref=ref_or_id, exclude_fields=exclude_fields) else: resource_db = self._get_by_id(resource_id=ref_or_id, exclude_fields=exclude_fields) if not resource_db: msg = 'Resource with a reference or id "%s" not found' % ( ref_or_id) raise StackStormDBObjectNotFoundError(msg) return resource_db
def _transform_action(name, spec): action_key, input_key = None, None for spec_type, spec_meta in six.iteritems(SPEC_TYPES): if spec_meta['action_key'] in spec: action_key = spec_meta['action_key'] input_key = spec_meta['input_key'] break if not action_key: return if spec[action_key] == 'st2.callback': raise WorkflowDefinitionException('st2.callback is deprecated.') # Convert parameters that are inline (i.e. action: some_action var1={$.value1} var2={$.value2}) # and split it to action name and input dict as illustrated below. # # action: some_action # input: # var1: <% $.value1 %> # var2: <% $.value2 %> # # This step to separate the action name and the input parameters is required # to wrap them with the st2.action proxy. # # action: st2.action # input: # ref: some_action # parameters: # var1: <% $.value1 %> # var2: <% $.value2 %> _eval_inline_params(spec, action_key, input_key) transformed = (spec[action_key] == 'st2.action') action_ref = spec[input_key]['ref'] if transformed else spec[action_key] action = None # Identify if action is a registered StackStorm action. if action_ref and ResourceReference.is_resource_reference(action_ref): action = action_utils.get_action_by_ref(ref=action_ref) # If action is a registered StackStorm action, then wrap the # action with the st2 proxy and validate the action input. if action: if not transformed: spec[action_key] = 'st2.action' action_input = spec.get(input_key) spec[input_key] = {'ref': action_ref} if action_input: spec[input_key]['parameters'] = action_input action_input = spec.get(input_key, {}) action_params = action_input.get('parameters', {}) _validate_action_parameters(name, action, action_params)
def _get_by_ref_or_id(self, ref_or_id, exclude_fields=None, include_fields=None): """ Retrieve resource object by an id of a reference. Note: This method throws StackStormDBObjectNotFoundError exception if the object is not found in the database. """ if exclude_fields and include_fields: msg = ( "exclude_fields and include_fields arguments are mutually exclusive. " "You need to provide either one or another, but not both.") raise ValueError(msg) if ResourceReference.is_resource_reference(ref_or_id): # references always contain a dot and id's can't contain it is_reference = True else: is_reference = False if is_reference: resource_db = self._get_by_ref( resource_ref=ref_or_id, exclude_fields=exclude_fields, include_fields=include_fields, ) else: resource_db = self._get_by_id( resource_id=ref_or_id, exclude_fields=exclude_fields, include_fields=include_fields, ) if not resource_db: msg = 'Resource with a reference or id "%s" not found' % ( ref_or_id) raise StackStormDBObjectNotFoundError(msg) return resource_db
def _transform_action(spec, action_key, input_key): if action_key not in spec or spec.get(action_key) == 'st2.action': return if spec.get(action_key) == 'st2.callback': raise Exception('st2.callback is deprecated.') # Convert parameters that are inline (i.e. action: some_action var1={$.value1} var2={$.value2}) # and split it to action name and input dict as illustrated below. # # action: some_action # input: # var1: <% $.value1 %> # var2: <% $.value2 %> # # This step to separate the action name and the input parameters is required # to wrap them with the st2.action proxy. # # action: st2.action # input: # ref: some_action # parameters: # var1: <% $.value1 %> # var2: <% $.value2 %> _eval_inline_params(spec, action_key, input_key) action_ref = spec.get(action_key) if action_ref and ResourceReference.is_resource_reference(action_ref): ref = ResourceReference.from_string_reference(ref=action_ref) actions = Action.query(name=ref.name, pack=ref.pack) action = actions.first() if actions else None else: action = None if action: spec[action_key] = 'st2.action' action_input = spec.get(input_key) spec[input_key] = {'ref': action_ref} if action_input: spec[input_key]['parameters'] = action_input
def _get_by_ref_or_id(self, ref_or_id): """ Retrieve resource object by an id of a reference. """ if ResourceReference.is_resource_reference(ref_or_id): # references always contain a dot and id's can't contain it is_reference = True else: is_reference = False if is_reference: resource_db = self._get_by_ref(resource_ref=ref_or_id) else: resource_db = self._get_by_id(resource_id=ref_or_id) if not resource_db: msg = 'Resource with a reference of id "%s" not found' % (ref_or_id) raise Exception(msg) return resource_db
def _get_by_ref_or_id(self, ref_or_id): """ Retrieve resource object by an id of a reference. """ if ResourceReference.is_resource_reference(ref_or_id): # references always contain a dot and id's can't contain it is_reference = True else: is_reference = False if is_reference: resource_db = self._get_by_ref(resource_ref=ref_or_id) else: resource_db = self._get_by_id(resource_id=ref_or_id) if not resource_db: msg = 'Resource with a reference of id "%s" not found' % (ref_or_id) raise Exception(msg) return resource_db
def _transform_action(spec, action_key, input_key): if action_key not in spec or spec.get(action_key) == 'st2.action': return if spec.get(action_key) == 'st2.callback': raise Exception('st2.callback is deprecated.') # Convert parameters that are inline (i.e. action: some_action var1={$.value1} var2={$.value2}) # and split it to action name and input dict as illustrated below. # # action: some_action # input: # var1: $.value1 # var2: $.value2 # # This step to separate the action name and the input parameters is required # to wrap them with the st2.action proxy. # # action: st2.action # input: # ref: some_action # parameters: # var1: $.value1 # var2: $.value2 _eval_inline_params(spec, action_key, input_key) action_ref = spec.get(action_key) if ResourceReference.is_resource_reference(action_ref): ref = ResourceReference.from_string_reference(ref=action_ref) actions = Action.query(name=ref.name, pack=ref.pack) action = actions.first() if actions else None else: action = None if action: spec[action_key] = 'st2.action' spec[input_key] = {'ref': action_ref, 'parameters': spec[input_key]}
def _get_by_ref_or_id(self, ref_or_id, exclude_fields=None): """ Retrieve resource object by an id of a reference. Note: This method throws StackStormDBObjectNotFoundError exception if the object is not found in the database. """ if ResourceReference.is_resource_reference(ref_or_id): # references always contain a dot and id's can't contain it is_reference = True else: is_reference = False if is_reference: resource_db = self._get_by_ref(resource_ref=ref_or_id, exclude_fields=exclude_fields) else: resource_db = self._get_by_id(resource_id=ref_or_id, exclude_fields=exclude_fields) if not resource_db: msg = 'Resource with a reference or id "%s" not found' % (ref_or_id) raise StackStormDBObjectNotFoundError(msg) return resource_db
def test_is_resource_reference(self): self.assertTrue(ResourceReference.is_resource_reference('foo.bar')) self.assertTrue(ResourceReference.is_resource_reference('foo.bar.ponies')) self.assertFalse(ResourceReference.is_resource_reference('foo'))
def test_is_resource_reference(self): self.assertTrue(ResourceReference.is_resource_reference("foo.bar")) self.assertTrue( ResourceReference.is_resource_reference("foo.bar.ponies")) self.assertFalse(ResourceReference.is_resource_reference("foo"))