def get_hosts(container_holder, name, locator): """ A TOSCA orchestrator will interpret this keyword to refer to the all nodes that "host" the node using this reference (i.e., as identified by its HostedOn relationship). Specifically, TOSCA orchestrators that encounter this keyword when evaluating the get_attribute or ``get_property`` functions SHALL search each node along the "HostedOn" relationship chain starting at the immediate node that hosts the node where the function was evaluated (and then that node's host node, and so forth) until a match is found or the "HostedOn" relationship chain ends. """ container = container_holder.container if (not isinstance(container, Node)) and (not isinstance( container, NodeTemplate)): raise InvalidValueError( 'function "{0}" refers to "HOST" but it is not contained in ' 'a node: {1}'.format(name, full_type_name(container)), locator=locator) if not isinstance(container, Node): # NodeTemplate does not have "host"; we'll wait until instantiation raise CannotEvaluateFunctionException() host = container.host if host is None: # We might have a host later raise CannotEvaluateFunctionException() return [host]
def __evaluate__(self, container_holder): final = True string_with_tokens, final = evaluate(self.string_with_tokens, final, container_holder) string_of_token_chars, final = evaluate(self.string_of_token_chars, final, container_holder) if string_of_token_chars: regex = '[' + ''.join(re.escape(c) for c in string_of_token_chars) + ']' split = re.split(regex, string_with_tokens) if self.substring_index < len(split): return Evaluation(split[self.substring_index], final) raise CannotEvaluateFunctionException()
def __evaluate__(self, container_holder): service = container_holder.service if service is None: raise CannotEvaluateFunctionException() value = service.inputs.get(self.input_property_name) if value is not None: value = value.value return Evaluation(value, False) # We never return final evaluations! raise InvalidValueError( 'function "get_input" argument is not a valid input name: {0}' .format(safe_repr(self.input_property_name)), locator=self.locator)
def __evaluate__(self, container_holder): modelable_entities = get_modelable_entities(container_holder, 'get_property', self.locator, self.modelable_entity_name) req_or_cap_name = self.nested_property_name_or_index[0] for modelable_entity in modelable_entities: properties = None # First argument refers to a requirement template? if hasattr(modelable_entity, 'requirement_templates') \ and modelable_entity.requirement_templates \ and (req_or_cap_name in [v.name for v in modelable_entity.requirement_templates]): for requirement in modelable_entity.requirement_templates: if requirement.name == req_or_cap_name: # TODO raise CannotEvaluateFunctionException() # First argument refers to a capability? elif hasattr(modelable_entity, 'capabilities') \ and modelable_entity.capabilities \ and (req_or_cap_name in modelable_entity.capabilities): properties = modelable_entity.capabilities[ req_or_cap_name].properties nested_property_name_or_index = self.nested_property_name_or_index[ 1:] # First argument refers to a capability template? elif hasattr(modelable_entity, 'capability_templates') \ and modelable_entity.capability_templates \ and (req_or_cap_name in modelable_entity.capability_templates): properties = modelable_entity.capability_templates[ req_or_cap_name].properties nested_property_name_or_index = self.nested_property_name_or_index[ 1:] else: properties = modelable_entity.properties nested_property_name_or_index = self.nested_property_name_or_index evaluation = get_modelable_entity_parameter( properties, nested_property_name_or_index) if evaluation is not None: return evaluation raise InvalidValueError( u'function "get_property" could not find "{0}" in modelable entity "{1}"' .format(u'.'.join(self.nested_property_name_or_index), self.modelable_entity_name), locator=self.locator)
def get_target(container_holder, name, locator): """ A TOSCA orchestrator will interpret this keyword as the Node Template instance that is at the target end of the relationship that contains the referencing function. """ container = container_holder.container if (not isinstance(container, Relationship)) and \ (not isinstance(container, RelationshipTemplate)): raise InvalidValueError('function "{0}" refers to "TARGET" but it is not contained in ' 'a relationship: {1}'.format(name, full_type_name(container)), locator=locator) if not isinstance(container, RelationshipTemplate): # RelationshipTemplate does not have "target_node"; we'll wait until instantiation raise CannotEvaluateFunctionException() return [container.target_node]
def get_modelable_entities(container_holder, name, locator, modelable_entity_name): """ The following keywords MAY be used in some TOSCA function in place of a TOSCA Node or Relationship Template name. """ if modelable_entity_name == 'SELF': return get_self(container_holder, name, locator) elif modelable_entity_name == 'HOST': return get_hosts(container_holder, name, locator) elif modelable_entity_name == 'SOURCE': return get_source(container_holder, name, locator) elif modelable_entity_name == 'TARGET': return get_target(container_holder, name, locator) elif isinstance(modelable_entity_name, basestring): modelable_entities = [] service = container_holder.service if service is not None: for node in service.nodes.itervalues(): if node.node_template.name == modelable_entity_name: modelable_entities.append(node) else: service_template = container_holder.service_template if service_template is not None: for node_template in service_template.node_templates.itervalues( ): if node_template.name == modelable_entity_name: modelable_entities.append(node_template) if not modelable_entities: raise CannotEvaluateFunctionException() return modelable_entities raise InvalidValueError( 'function "{0}" could not find modelable entity "{1}"'.format( name, modelable_entity_name), locator=locator)