blob: f0146a00417cbb9603f609812692c2372eeb9935 [file] [log] [blame]
David Garcia4fee80e2020-05-13 12:18:38 +02001# Copyright 2020 Canonical Ltd.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
David Garcia475a7222020-09-21 16:19:15 +020015import base64
16import re
17import binascii
ksaikiranrb816d822021-03-17 12:50:20 +053018import yaml
David Garcia4fee80e2020-05-13 12:18:38 +020019from enum import Enum
20from juju.machine import Machine
21from juju.application import Application
22from juju.action import Action
23from juju.unit import Unit
David Garcia475a7222020-09-21 16:19:15 +020024from n2vc.exceptions import N2VCInvalidCertificate
25
26
27def base64_to_cacert(b64string):
28 """Convert the base64-encoded string containing the VCA CACERT.
29
30 The input string....
31
32 """
33 try:
34 cacert = base64.b64decode(b64string).decode("utf-8")
35
36 cacert = re.sub(r"\\n", r"\n", cacert,)
37 except binascii.Error as e:
38 raise N2VCInvalidCertificate(message="Invalid CA Certificate: {}".format(e))
39
40 return cacert
David Garcia4fee80e2020-05-13 12:18:38 +020041
42
43class N2VCDeploymentStatus(Enum):
44 PENDING = "pending"
45 RUNNING = "running"
46 COMPLETED = "completed"
47 FAILED = "failed"
48 UNKNOWN = "unknown"
49
50
51class Dict(dict):
52 """
53 Dict class that allows to access the keys like attributes
54 """
55
56 def __getattribute__(self, name):
57 if name in self:
58 return self[name]
59
60
61class EntityType(Enum):
62 MACHINE = Machine
63 APPLICATION = Application
64 ACTION = Action
65 UNIT = Unit
66
67 @classmethod
68 def has_value(cls, value):
69 return value in cls._value2member_map_ # pylint: disable=E1101
70
71 @classmethod
72 def get_entity(cls, value):
73 return (
74 cls._value2member_map_[value] # pylint: disable=E1101
75 if value in cls._value2member_map_ # pylint: disable=E1101
76 else None # pylint: disable=E1101
77 )
78
79 @classmethod
80 def get_entity_from_delta(cls, delta_entity: str):
81 """
82 Get Value from delta entity
83
84 :param: delta_entity: Possible values are "machine", "application", "unit", "action"
85 """
86 for v in cls._value2member_map_: # pylint: disable=E1101
87 if v.__name__.lower() == delta_entity:
88 return cls.get_entity(v)
89
90
David Garcia4fee80e2020-05-13 12:18:38 +020091JujuStatusToOSM = {
David Garciac38a6962020-09-16 13:31:33 +020092 "machine": {
David Garcia4fee80e2020-05-13 12:18:38 +020093 "pending": N2VCDeploymentStatus.PENDING,
94 "started": N2VCDeploymentStatus.COMPLETED,
95 },
David Garciac38a6962020-09-16 13:31:33 +020096 "application": {
David Garcia4fee80e2020-05-13 12:18:38 +020097 "waiting": N2VCDeploymentStatus.RUNNING,
98 "maintenance": N2VCDeploymentStatus.RUNNING,
99 "blocked": N2VCDeploymentStatus.RUNNING,
100 "error": N2VCDeploymentStatus.FAILED,
101 "active": N2VCDeploymentStatus.COMPLETED,
102 },
David Garciac38a6962020-09-16 13:31:33 +0200103 "action": {
David Garcia2f66c4d2020-06-19 11:40:18 +0200104 "pending": N2VCDeploymentStatus.PENDING,
David Garcia4fee80e2020-05-13 12:18:38 +0200105 "running": N2VCDeploymentStatus.RUNNING,
106 "completed": N2VCDeploymentStatus.COMPLETED,
107 },
David Garciac38a6962020-09-16 13:31:33 +0200108 "unit": {
David Garcia4fee80e2020-05-13 12:18:38 +0200109 "waiting": N2VCDeploymentStatus.RUNNING,
110 "maintenance": N2VCDeploymentStatus.RUNNING,
111 "blocked": N2VCDeploymentStatus.RUNNING,
112 "error": N2VCDeploymentStatus.FAILED,
113 "active": N2VCDeploymentStatus.COMPLETED,
114 },
115}
David Garcia2f66c4d2020-06-19 11:40:18 +0200116
ksaikiranrb816d822021-03-17 12:50:20 +0530117
118def obj_to_yaml(obj: object) -> str:
119 """
120 Converts object to yaml format
121 :return: yaml data
122 """
123 # dump to yaml
124 dump_text = yaml.dump(obj, default_flow_style=False, indent=2)
125 # split lines
126 lines = dump_text.splitlines()
127 # remove !!python/object tags
128 yaml_text = ""
129 for line in lines:
130 index = line.find("!!python/object")
131 if index >= 0:
132 line = line[:index]
133 yaml_text += line + "\n"
134 return yaml_text
135
136
137def obj_to_dict(obj: object) -> dict:
138 """
139 Converts object to dictionary format
140 :return: dict data
141 """
142 # convert obj to yaml
143 yaml_text = obj_to_yaml(obj)
144 # parse to dict
145 return yaml.load(yaml_text, Loader=yaml.Loader)