blob: 5ac0e2c6d68581598c6e2e8f8376362ec0b7c7a5 [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
Pedro Escaleira4809ea92022-03-21 17:54:45 +000019import string
20import secrets
David Garcia4fee80e2020-05-13 12:18:38 +020021from enum import Enum
22from juju.machine import Machine
23from juju.application import Application
24from juju.action import Action
25from juju.unit import Unit
David Garcia475a7222020-09-21 16:19:15 +020026from n2vc.exceptions import N2VCInvalidCertificate
27
28
29def base64_to_cacert(b64string):
30 """Convert the base64-encoded string containing the VCA CACERT.
31
32 The input string....
33
34 """
35 try:
36 cacert = base64.b64decode(b64string).decode("utf-8")
37
garciadeblas82b591c2021-03-24 09:22:13 +010038 cacert = re.sub(
39 r"\\n",
40 r"\n",
41 cacert,
42 )
David Garcia475a7222020-09-21 16:19:15 +020043 except binascii.Error as e:
44 raise N2VCInvalidCertificate(message="Invalid CA Certificate: {}".format(e))
45
46 return cacert
David Garcia4fee80e2020-05-13 12:18:38 +020047
48
49class N2VCDeploymentStatus(Enum):
50 PENDING = "pending"
51 RUNNING = "running"
52 COMPLETED = "completed"
53 FAILED = "failed"
54 UNKNOWN = "unknown"
55
56
57class Dict(dict):
58 """
59 Dict class that allows to access the keys like attributes
60 """
61
62 def __getattribute__(self, name):
63 if name in self:
64 return self[name]
65
66
67class EntityType(Enum):
68 MACHINE = Machine
69 APPLICATION = Application
70 ACTION = Action
71 UNIT = Unit
72
73 @classmethod
74 def has_value(cls, value):
75 return value in cls._value2member_map_ # pylint: disable=E1101
76
77 @classmethod
78 def get_entity(cls, value):
79 return (
80 cls._value2member_map_[value] # pylint: disable=E1101
81 if value in cls._value2member_map_ # pylint: disable=E1101
82 else None # pylint: disable=E1101
83 )
84
85 @classmethod
86 def get_entity_from_delta(cls, delta_entity: str):
87 """
88 Get Value from delta entity
89
90 :param: delta_entity: Possible values are "machine", "application", "unit", "action"
91 """
92 for v in cls._value2member_map_: # pylint: disable=E1101
93 if v.__name__.lower() == delta_entity:
94 return cls.get_entity(v)
95
96
David Garcia4fee80e2020-05-13 12:18:38 +020097JujuStatusToOSM = {
David Garciac38a6962020-09-16 13:31:33 +020098 "machine": {
David Garcia4fee80e2020-05-13 12:18:38 +020099 "pending": N2VCDeploymentStatus.PENDING,
100 "started": N2VCDeploymentStatus.COMPLETED,
101 },
David Garciac38a6962020-09-16 13:31:33 +0200102 "application": {
David Garcia4fee80e2020-05-13 12:18:38 +0200103 "waiting": N2VCDeploymentStatus.RUNNING,
104 "maintenance": N2VCDeploymentStatus.RUNNING,
105 "blocked": N2VCDeploymentStatus.RUNNING,
106 "error": N2VCDeploymentStatus.FAILED,
107 "active": N2VCDeploymentStatus.COMPLETED,
108 },
David Garciac38a6962020-09-16 13:31:33 +0200109 "action": {
David Garcia2f66c4d2020-06-19 11:40:18 +0200110 "pending": N2VCDeploymentStatus.PENDING,
David Garcia4fee80e2020-05-13 12:18:38 +0200111 "running": N2VCDeploymentStatus.RUNNING,
112 "completed": N2VCDeploymentStatus.COMPLETED,
113 },
David Garciac38a6962020-09-16 13:31:33 +0200114 "unit": {
David Garcia4fee80e2020-05-13 12:18:38 +0200115 "waiting": N2VCDeploymentStatus.RUNNING,
116 "maintenance": N2VCDeploymentStatus.RUNNING,
117 "blocked": N2VCDeploymentStatus.RUNNING,
118 "error": N2VCDeploymentStatus.FAILED,
119 "active": N2VCDeploymentStatus.COMPLETED,
120 },
121}
David Garcia2f66c4d2020-06-19 11:40:18 +0200122
ksaikiranrb816d822021-03-17 12:50:20 +0530123
124def obj_to_yaml(obj: object) -> str:
125 """
126 Converts object to yaml format
127 :return: yaml data
128 """
129 # dump to yaml
130 dump_text = yaml.dump(obj, default_flow_style=False, indent=2)
131 # split lines
132 lines = dump_text.splitlines()
133 # remove !!python/object tags
134 yaml_text = ""
135 for line in lines:
136 index = line.find("!!python/object")
137 if index >= 0:
138 line = line[:index]
139 yaml_text += line + "\n"
140 return yaml_text
141
142
143def obj_to_dict(obj: object) -> dict:
144 """
145 Converts object to dictionary format
146 :return: dict data
147 """
148 # convert obj to yaml
149 yaml_text = obj_to_yaml(obj)
150 # parse to dict
151 return yaml.load(yaml_text, Loader=yaml.Loader)
Pedro Escaleira4809ea92022-03-21 17:54:45 +0000152
153
154def generate_random_alfanum_string(size: int) -> str:
155 """
156 Generate random alfa-numeric string with a size given by argument
157 :param size:
158 :return: random generated string
159 """
160
161 return "".join(
162 secrets.choice(string.ascii_letters + string.digits) for i in range(size)
163 )