def environment(layer, config, environment_name=None, stage=None, layer_name=None, resource_name=None, output_name=None): """Gets the physical ID of a resource in another environment. :param layer: The Layer object of the layer declaring the reference. :param config: An object holding humilis configuration options. :param layer_name: The name of the layer that contains the target resource. :param resource_name: The logical name of the target resource. :param output_name: The name of the layer output :returns: The physical ID of the resource. """ if not (resource_name or output_name) or (resource_name and output_name): raise ValueError( "Exactly one of these two parameters should be provider: either" "'resource_name' or 'output_name'") if resource_name: stack_name = utils.get_cf_name(environment_name, layer_name, stage=stage) return _get_stack_resource(layer, config, stack_name, resource_name) else: return output(layer, config, layer_name=layer_name, stage=stage, environment_name=environment_name, output_name=output_name)
def output(layer, config, layer_name=None, output_name=None, environment_name=None, stage=None): """Gets the value of an output produced by an already deployed layer. :param layer: The Layer object for the layer declaring the reference. :param config: An object holding humilis configuration options. :param layer_name: The logical name of the layer that produced the output. :param output_name: The logical name of the output parameter. """ if not environment_name or not stage: environment_name = layer.env_name stage = layer.env_stage stack_name = utils.get_cf_name(environment_name, layer_name, stage=stage) cf = Cloudformation(config) try: output = cf.get_stack_output(stack_name, output_name) except AttributeError: msg = "No output '{}' in CF stack '{}'".format(output_name, stack_name) ref = "output/{}/{}/{}/{}".format(environment_name, layer_name, stage, output_name) raise ReferenceError(ref, msg) if len(output) < 1: all_stack_outputs = [x['OutputKey'] for x in cf.stack_outputs[stack_name]] msg = ("{} output does not exist for stack {} " "(with outputs {}).").format(output_name, stack_name, all_stack_outputs) ref = "output ({}/{})".format(layer_name, output_name) raise ReferenceError(ref, msg, logger=layer.logger) return output[0]
def test_create_layer_object(test_environment, test_vpc_layer): layer = test_vpc_layer assert layer.name == 'vpc' assert layer.cf_name == utils.get_cf_name( layer.env_name, layer.name, stage=layer.env_stage) assert len(layer.yaml_params) == 3 assert layer.yaml_params['vpc_cidr']['value'] == '10.0.0.4/16' assert layer.tags.get('humilis:layer') == layer.name assert layer.tags.get('humilis:environment') == test_environment.name
def environment(layer, config, environment_name=None, stage=None, layer_name=None, resource_name=None): """Gets the physical ID of a resource in another environment. :param layer: The Layer object of the layer declaring the reference. :param config: An object holding humilis configuration options. :param layer_name: The name of the layer that contains the target resource. :param resource_name: The logical name of the target resource. :returns: The physical ID of the resource. """ stack_name = utils.get_cf_name(environment_name, layer_name, stage=stage) return _get_stack_resource(config, stack_name, resource_name)
def layer(layer, config, layer_name=None, resource_name=None): """Gets the physical ID of a resource in an already deployed layer. :param layer: The Layer object for the layer declaring the reference. :param config: An object holding humilis configuration options. :param layer_name: The name of the layer that contains the target resource. :param resource_name: The logical name of the target resource. :returns: The physical ID of the resource. """ stack_name = utils.get_cf_name(layer.env_name, layer_name, stage=layer.env_stage) return _get_stack_resource(config, stack_name, resource_name)
def output(layer, config, layer_name=None, output_name=None): """Gets the value of an output produced by an already deployed layer. :param layer: The Layer object for the layer declaring the reference. :param config: An object holding humilis configuration options. :param layer_name: The logical name of the layer that produced the output. :param output_name: The logical name of the output parameter. """ stack_name = utils.get_cf_name(layer.env_name, layer_name, stage=layer.env_stage) cf = Cloudformation(config) output = cf.get_stack_output(stack_name, output_name) if len(output) < 1: all_stack_outputs = [x['OutputKey'] for x in cf.stack_outputs[stack_name]] msg = ("{} output does not exist for stack {} " "(with outputs {}).").format(output_name, stack_name, all_stack_outputs) ref = "output ({}/{})".format(layer_name, output_name) raise ReferenceError(ref, msg, logger=layer.logger) return output[0]
def cf_name(self): """The name of the CF stack associated to this layer.""" return get_cf_name(self.env_name, self.name, stage=self.env_stage)
def __init__(self, environment, name, layer_type=None, logger=None, loader=None, **user_params): self.__environment_repr = repr(environment) self.cf = environment.cf if logger is None: self.logger = logging.getLogger(__name__) # To prevent warnings self.logger.addHandler(logging.NullHandler()) else: self.logger = logger self.name = name self.env_name = environment.name self.env_stage = environment.stage self.env_basedir = environment.basedir self.depends_on = [] self.section = {} self.type = layer_type if layer_type is not None: basedir = config.layers.get(layer_type) if not basedir: msg = ("The plugin providing the layer type '{}' is not " "installed in this system. Please install it and " "try again.").format(layer_type) raise MissingPluginError(msg) else: basedir = None if basedir is None: basedir = os.path.join(self.env_basedir, 'layers', self.name) self.basedir = basedir if loader is None: loader = DirTreeBackedObject(basedir, self.logger) self.loader = loader self.children = [] # These param set will be sent to the template compiler and will be # populated once the layers this layer depend on have been created. self.params = {} # the parameters that will be used to compile meta.yaml self.meta = {} meta_params = {p[0]: p[1] for p in itertools.chain(self.loader_params.items(), user_params.items())} self.meta = self.loader.load_section('meta', params=meta_params) for dep in self.meta.get('dependencies', []): dep_cf_name = get_cf_name(self.env_name, dep, stage=self.env_stage) self.depends_on.append(dep_cf_name) self.sns_topic_arn = environment.sns_topic_arn self.tags = { 'humilis:environment': self.env_name, 'humilis:layer': self.name, 'humilis:stage': self.env_stage, 'humilis:created': str(datetime.datetime.now())} for tagname, tagvalue in environment.tags.items(): self.tags[tagname] = tagvalue for tagname, tagvalue in self.meta.get('tags', {}).items(): self.tags[tagname] = tagvalue self.yaml_params = self.meta.get('parameters', {}) for k, v in self.yaml_params.items(): # Set 1 as default priority for all parameters v['priority'] = v.get('priority', 1) # User params override what is in the layer definition file self.user_params = user_params for pname, pvalue in user_params.items(): if pname in self.yaml_params: self.yaml_params[pname]['value'] = pvalue self.__ec2 = None self.__s3 = None