def test_yaml_from_file(create_temp_dir, create_yaml, create_temp_text): newpath = create_temp_dir() fname = create_temp_text(newpath) fullpath = '%s/%s' % (newpath, fname) file_exists = os.path.exists(fullpath) assert True == file_exists yaml_data = yaml_from_file(fullpath) assert not isinstance(yaml_data, dict) yaml_data = [ "runtime: python27", "api_version: 1", "threadsafe: true", ] fname = create_yaml(newpath, '\n'.join(yaml_data)) fullpath = '%s/%s' % (newpath, fname) file_exists = os.path.exists(fullpath) assert True == file_exists yaml_data = yaml_from_file(fullpath) assert yaml_data is not None assert isinstance(yaml_data, dict) yaml_data = yaml_from_file(None) assert yaml_data is None yaml_data = yaml_from_file('/tmp/xyza.yaml') assert yaml_data is None yaml_data = [ "hello: ][", ] fname = create_yaml(newpath, '\n'.join(yaml_data)) fullpath = '%s/%s' % (newpath, fname) yaml_data = yaml_from_file(fullpath) assert yaml_data is None
def is_template_file(self, file_path): """ check for valid template file for parse arm template """ if len(file_path.split(".")) > 0 and file_path.split(".")[-1] == "yaml": json_data = yaml_from_file(file_path, loader=FullLoader) return True if (json_data) else False return False
def is_template_file(self, file_path): """ check for valid template file for parse arm template """ if len(file_path.split(".")) > 0 and file_path.split( ".")[-1] == "yaml": json_data = yaml_from_file(file_path, loader=FullLoader) kube_policy = ["apiVersion", "kind", "metadata", "spec"] return True if json_data and any( elem in json_data for elem in kube_policy) else False return False
def convert_to_json(file_path, node_type): json_data = {} if node_type == 'json': json_data = json_from_file(file_path, escape_chars=['$']) elif node_type == 'terraform': with open(file_path, 'r') as fp: json_data = hcl.load(fp) elif node_type == 'yaml' or node_type == 'yml': json_data = yaml_from_file(file_path) else: logger.error("Snapshot error type:%s and file: %s", node_type, file_path) return json_data
def process_template(self, paths): """ process the files stored at specified paths and returns the template """ template_json = None if paths and isinstance(paths, list): template_file_path = "" deployment_file_path = "" for path in paths: file_path = '%s/%s' % (self.dir_path, path) logger.info("Fetching data : %s ", path) if self.is_template_file(file_path): template_file_path = file_path self.template_file = template_file_path if template_file_path: template_json = yaml_from_file(file_path,loader=FullLoader) return template_json
def yaml_to_json(self, yaml_file): """ takes the yaml file path and converts the returns the converted JSON object """ json_data = yaml_from_file(yaml_file, loader=FullLoader) return json_data