def BuildConfig(config=None, template=None, composite_type=None, properties=None): """Takes the path to a config and returns a processed config. Args: config: Path to the yaml config file. template: Path to the template config file. composite_type: name of the composite type config. properties: Dictionary of properties, only used if the file is a template or composite type. Returns: A tuple of base_path, config_contents, and a list of import objects. Raises: ArgumentError: If using the properties flag for a config file instead of a template or composite type. """ config_obj = _BuildImportObject(config=config, template=template, composite_type=composite_type, properties=properties) if composite_type: return config_obj if config: if config_obj.IsTemplate(): raise exceptions.ArgumentError( 'Creating deployments from templates with the --config ' 'flag is not supported. Please use the --template flag.') elif properties: raise exceptions.ArgumentError( 'The properties flag should only be used ' 'when using a template (--template) or composite type ' '(--composite-type) as your config file.') else: return config_obj if template: if not config_obj.IsTemplate(): raise exceptions.ArgumentError( 'The --template flag should only be used ' 'when using a template as your config file.') # Otherwise we should build the config from scratch. base_name = config_obj.GetBaseName() # Build the single template resource. custom_resource = {'type': base_name, 'name': _SanitizeBaseName(base_name)} # Attach properties if we were given any. if properties: custom_resource['properties'] = properties # Add the import and single resource together into a config file. custom_dict = { 'imports': [ { 'path': base_name }, ], 'resources': [ custom_resource, ] } custom_outputs = [] # Import the schema file and attach the outputs to config if there is any schema_path = config_obj.GetFullPath() + '.schema' schema_name = config_obj.GetName() + '.schema' schema_object = _BuildFileImportObject(schema_path, schema_name) if schema_object.Exists(): schema_content = schema_object.GetContent() config_name = custom_resource['name'] yaml_schema = yaml.load(schema_content, file_hint=schema_path) if yaml_schema and OUTPUTS in yaml_schema: for output_name in yaml_schema[OUTPUTS].keys(): custom_outputs.append({ 'name': output_name, 'value': '$(ref.' + config_name + '.' + output_name + ')' }) if custom_outputs: custom_dict['outputs'] = custom_outputs custom_content = yaml.dump(custom_dict) # Override the template_object with it's new config_content return config_obj.SetContent(custom_content)
def Filter(self, unused_tool_context, args): if not args.deployment: raise exceptions.ArgumentError('argument --deployment is required')
def BuildConfig(config=None, template=None, composite_type=None, properties=None): """Takes the path to a config and returns a processed config. Args: config: Path to the yaml config file. template: Path to the template config file. composite_type: name of the composite type config. properties: Dictionary of properties, only used if the file is a template or composite type. Returns: A tuple of base_path, config_contents, and a list of import objects. Raises: ArgumentError: If using the properties flag for a config file instead of a template or composite type. """ config_obj = _BuildImportObject(config=config, template=template, composite_type=composite_type, properties=properties) if composite_type: return config_obj if config: if config_obj.IsTemplate(): # TODO(b/62844648): when support for passing templates with the config # flag is completely removed, simply change the warning to an exception log.warn( 'Creating deployments from templates with the \'--config\' ' 'flag has been deprecated. Support for this will be ' 'removed 2017/11/08. Please use \'--template\'instead.') elif properties: raise exceptions.ArgumentError( 'The properties flag should only be used ' 'when using a template or composite type as your config file.') else: return config_obj if template: if not config_obj.IsTemplate(): raise exceptions.ArgumentError( 'The template flag should only be used ' 'when using a template as your config file.') # Otherwise we should build the config from scratch. base_name = config_obj.GetBaseName() # Build the single template resource. custom_resource = {'type': base_name, 'name': _SanitizeBaseName(base_name)} # Attach properties if we were given any. if properties: custom_resource['properties'] = properties # Add the import and single resource together into a config file. custom_dict = { 'imports': [ { 'path': base_name }, ], 'resources': [ custom_resource, ] } custom_outputs = [] # Import the schema file and attach the outputs to config if there is any schema_path = config_obj.GetFullPath() + '.schema' schema_name = config_obj.GetName() + '.schema' schema_object = _BuildFileImportObject(schema_path, schema_name) if schema_object.Exists(): schema_content = schema_object.GetContent() config_name = custom_resource['name'] try: yaml_schema = yaml.safe_load(schema_content) if yaml_schema and OUTPUTS in yaml_schema: for output_name in yaml_schema[OUTPUTS].keys(): custom_outputs.append({ 'name': output_name, 'value': '$(ref.' + config_name + '.' + output_name + ')' }) except yaml.YAMLError as e: raise exceptions.ConfigError('Invalid schema file %s. %s' % (schema_path, str(e))) if custom_outputs: custom_dict['outputs'] = custom_outputs # Dump using default_flow_style=False to use spacing instead of '{ }' custom_content = yaml.dump(custom_dict, default_flow_style=False) # Override the template_object with it's new config_content return config_obj.SetContent(custom_content)
def _BuildConfig(full_path, properties): """Takes the argument from the --config flag, and returns a processed config. Args: full_path: Path to the config yaml file, with an optional list of imports. properties: Dictionary of properties, only used if the file is a template. Returns: A tuple of base_path, config_contents, and a list of import objects. Raises: ArgumentError: If using the properties flag for a config file instead of a template. """ config_obj = _BuildImportObject(full_path) if not config_obj.IsTemplate(): if properties: raise exceptions.ArgumentError( 'The properties flag should only be used ' 'when passing in a template as your config file.') return config_obj # Otherwise we should build the config from scratch. base_name = config_obj.GetBaseName() # Build the single template resource. custom_resource = {'type': base_name, 'name': _SanitizeBaseName(base_name)} # Attach properties if we were given any. if properties: custom_resource['properties'] = properties # Add the import and single resource together into a config file. custom_dict = { 'imports': [ { 'path': base_name }, ], 'resources': [ custom_resource, ] } custom_outputs = [] # Import the schema file and attach the outputs to config if there is any schema_path = config_obj.GetFullPath() + '.schema' schema_name = config_obj.GetName() + '.schema' schema_object = _BuildImportObject(schema_path, schema_name) if schema_object.Exists(): schema_content = schema_object.GetContent() config_name = custom_resource['name'] try: yaml_schema = yaml.safe_load(schema_content) if yaml_schema and OUTPUTS in yaml_schema: for output_name in yaml_schema[OUTPUTS].keys(): custom_outputs.append({ 'name': output_name, 'value': '$(ref.' + config_name + '.' + output_name + ')' }) except yaml.YAMLError as e: raise exceptions.ConfigError('Invalid schema file %s. %s' % (schema_path, str(e))) if custom_outputs: custom_dict['outputs'] = custom_outputs # Dump using default_flow_style=False to use spacing instead of '{ }' custom_content = yaml.dump(custom_dict, default_flow_style=False) # Override the template_object with it's new config_content return config_obj.SetContent(custom_content)