| tierno | f800c5c | 2020-06-30 13:24:17 +0000 | [diff] [blame] | 1 | # -*- 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 | |
| tierno | f800c5c | 2020-06-30 13:24:17 +0000 | [diff] [blame] | 19 | import yaml |
| tierno | f800c5c | 2020-06-30 13:24:17 +0000 | [diff] [blame] | 20 | from osm_lcm.lcm_utils import LcmException |
| tierno | b996d94 | 2020-07-03 14:52:28 +0000 | [diff] [blame] | 21 | from jinja2 import Template, TemplateError, TemplateNotFound, TemplateSyntaxError |
| tierno | f800c5c | 2020-06-30 13:24:17 +0000 | [diff] [blame] | 22 | |
| 23 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 24 | |
| tierno | f800c5c | 2020-06-30 13:24:17 +0000 | [diff] [blame] | 25 | |
| bravof | 73bac50 | 2021-05-11 07:38:47 -0400 | [diff] [blame] | 26 | def 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 |
| aticig | 15db614 | 2022-01-24 12:51:26 +0300 | [diff] [blame] | 33 | raise LcmException( |
| 34 | "Error parsing Jinja2 to prometheus job. job_data={}, variables={}. Error={}".format( |
| 35 | job_data, variables, e |
| 36 | ) |
| 37 | ) |