blob: 98f5bb15ac6918d09d6cfc61202f5edc784c5986 [file] [log] [blame]
tiernof800c5c2020-06-30 13:24:17 +00001# -*- coding: utf-8 -*-
2
3##
4# Copyright 2020 Telefonica S.A.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17##
18
tiernof800c5c2020-06-30 13:24:17 +000019import yaml
tiernof800c5c2020-06-30 13:24:17 +000020from osm_lcm.lcm_utils import LcmException
tiernob996d942020-07-03 14:52:28 +000021from jinja2 import Template, TemplateError, TemplateNotFound, TemplateSyntaxError
tiernof800c5c2020-06-30 13:24:17 +000022
23__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
24
tiernof800c5c2020-06-30 13:24:17 +000025
bravof73bac502021-05-11 07:38:47 -040026def parse_job(job_data: str, variables: dict) -> dict:
27 try:
28 template = Template(job_data)
29 job_parsed = template.render(variables or {})
30 return yaml.safe_load(job_parsed)
31 except (TemplateError, TemplateNotFound, TemplateSyntaxError) as e:
32 # TODO yaml exceptions
aticig15db6142022-01-24 12:51:26 +030033 raise LcmException(
34 "Error parsing Jinja2 to prometheus job. job_data={}, variables={}. Error={}".format(
35 job_data, variables, e
36 )
37 )