blob: 0dbd71ef1d7c8db567f56e5fbd633abbcf414537 [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
garciadeblas82b591c2021-03-24 09:22:13 +010036 cacert = re.sub(
37 r"\\n",
38 r"\n",
39 cacert,
40 )
David Garcia475a7222020-09-21 16:19:15 +020041 except binascii.Error as e:
42 raise N2VCInvalidCertificate(message="Invalid CA Certificate: {}".format(e))
43
44 return cacert
David Garcia4fee80e2020-05-13 12:18:38 +020045
46
47class N2VCDeploymentStatus(Enum):
48 PENDING = "pending"
49 RUNNING = "running"
50 COMPLETED = "completed"
51 FAILED = "failed"
52 UNKNOWN = "unknown"
53
54
55class Dict(dict):
56 """
57 Dict class that allows to access the keys like attributes
58 """
59
60 def __getattribute__(self, name):
61 if name in self:
62 return self[name]
63
64
65class EntityType(Enum):
66 MACHINE = Machine
67 APPLICATION = Application
68 ACTION = Action
69 UNIT = Unit
70
71 @classmethod
72 def has_value(cls, value):
73 return value in cls._value2member_map_ # pylint: disable=E1101
74
75 @classmethod
76 def get_entity(cls, value):
77 return (
78 cls._value2member_map_[value] # pylint: disable=E1101
79 if value in cls._value2member_map_ # pylint: disable=E1101
80 else None # pylint: disable=E1101
81 )
82
83 @classmethod
84 def get_entity_from_delta(cls, delta_entity: str):
85 """
86 Get Value from delta entity
87
88 :param: delta_entity: Possible values are "machine", "application", "unit", "action"
89 """
90 for v in cls._value2member_map_: # pylint: disable=E1101
91 if v.__name__.lower() == delta_entity:
92 return cls.get_entity(v)
93
94
David Garcia4fee80e2020-05-13 12:18:38 +020095JujuStatusToOSM = {
David Garciac38a6962020-09-16 13:31:33 +020096 "machine": {
David Garcia4fee80e2020-05-13 12:18:38 +020097 "pending": N2VCDeploymentStatus.PENDING,
98 "started": N2VCDeploymentStatus.COMPLETED,
99 },
David Garciac38a6962020-09-16 13:31:33 +0200100 "application": {
David Garcia4fee80e2020-05-13 12:18:38 +0200101 "waiting": N2VCDeploymentStatus.RUNNING,
102 "maintenance": N2VCDeploymentStatus.RUNNING,
103 "blocked": N2VCDeploymentStatus.RUNNING,
104 "error": N2VCDeploymentStatus.FAILED,
105 "active": N2VCDeploymentStatus.COMPLETED,
106 },
David Garciac38a6962020-09-16 13:31:33 +0200107 "action": {
David Garcia2f66c4d2020-06-19 11:40:18 +0200108 "pending": N2VCDeploymentStatus.PENDING,
David Garcia4fee80e2020-05-13 12:18:38 +0200109 "running": N2VCDeploymentStatus.RUNNING,
110 "completed": N2VCDeploymentStatus.COMPLETED,
111 },
David Garciac38a6962020-09-16 13:31:33 +0200112 "unit": {
David Garcia4fee80e2020-05-13 12:18:38 +0200113 "waiting": N2VCDeploymentStatus.RUNNING,
114 "maintenance": N2VCDeploymentStatus.RUNNING,
115 "blocked": N2VCDeploymentStatus.RUNNING,
116 "error": N2VCDeploymentStatus.FAILED,
117 "active": N2VCDeploymentStatus.COMPLETED,
118 },
119}
David Garcia2f66c4d2020-06-19 11:40:18 +0200120
ksaikiranrb816d822021-03-17 12:50:20 +0530121
122def obj_to_yaml(obj: object) -> str:
123 """
124 Converts object to yaml format
125 :return: yaml data
126 """
127 # dump to yaml
128 dump_text = yaml.dump(obj, default_flow_style=False, indent=2)
129 # split lines
130 lines = dump_text.splitlines()
131 # remove !!python/object tags
132 yaml_text = ""
133 for line in lines:
134 index = line.find("!!python/object")
135 if index >= 0:
136 line = line[:index]
137 yaml_text += line + "\n"
138 return yaml_text
139
140
141def obj_to_dict(obj: object) -> dict:
142 """
143 Converts object to dictionary format
144 :return: dict data
145 """
146 # convert obj to yaml
147 yaml_text = obj_to_yaml(obj)
148 # parse to dict
149 return yaml.load(yaml_text, Loader=yaml.Loader)