blob: 16a47332cf284cba606dc27b5af3e281d902e15a [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
David Garcia4fee80e2020-05-13 12:18:38 +020018from enum import Enum
19from juju.machine import Machine
20from juju.application import Application
21from juju.action import Action
22from juju.unit import Unit
David Garcia475a7222020-09-21 16:19:15 +020023from n2vc.exceptions import N2VCInvalidCertificate
24
25
26def base64_to_cacert(b64string):
27 """Convert the base64-encoded string containing the VCA CACERT.
28
29 The input string....
30
31 """
32 try:
33 cacert = base64.b64decode(b64string).decode("utf-8")
34
35 cacert = re.sub(r"\\n", r"\n", cacert,)
36 except binascii.Error as e:
37 raise N2VCInvalidCertificate(message="Invalid CA Certificate: {}".format(e))
38
39 return cacert
David Garcia4fee80e2020-05-13 12:18:38 +020040
41
42class N2VCDeploymentStatus(Enum):
43 PENDING = "pending"
44 RUNNING = "running"
45 COMPLETED = "completed"
46 FAILED = "failed"
47 UNKNOWN = "unknown"
48
49
50class Dict(dict):
51 """
52 Dict class that allows to access the keys like attributes
53 """
54
55 def __getattribute__(self, name):
56 if name in self:
57 return self[name]
58
59
60class EntityType(Enum):
61 MACHINE = Machine
62 APPLICATION = Application
63 ACTION = Action
64 UNIT = Unit
65
66 @classmethod
67 def has_value(cls, value):
68 return value in cls._value2member_map_ # pylint: disable=E1101
69
70 @classmethod
71 def get_entity(cls, value):
72 return (
73 cls._value2member_map_[value] # pylint: disable=E1101
74 if value in cls._value2member_map_ # pylint: disable=E1101
75 else None # pylint: disable=E1101
76 )
77
78 @classmethod
79 def get_entity_from_delta(cls, delta_entity: str):
80 """
81 Get Value from delta entity
82
83 :param: delta_entity: Possible values are "machine", "application", "unit", "action"
84 """
85 for v in cls._value2member_map_: # pylint: disable=E1101
86 if v.__name__.lower() == delta_entity:
87 return cls.get_entity(v)
88
89
David Garcia4fee80e2020-05-13 12:18:38 +020090JujuStatusToOSM = {
David Garciac38a6962020-09-16 13:31:33 +020091 "machine": {
David Garcia4fee80e2020-05-13 12:18:38 +020092 "pending": N2VCDeploymentStatus.PENDING,
93 "started": N2VCDeploymentStatus.COMPLETED,
94 },
David Garciac38a6962020-09-16 13:31:33 +020095 "application": {
David Garcia4fee80e2020-05-13 12:18:38 +020096 "waiting": N2VCDeploymentStatus.RUNNING,
97 "maintenance": N2VCDeploymentStatus.RUNNING,
98 "blocked": N2VCDeploymentStatus.RUNNING,
99 "error": N2VCDeploymentStatus.FAILED,
100 "active": N2VCDeploymentStatus.COMPLETED,
101 },
David Garciac38a6962020-09-16 13:31:33 +0200102 "action": {
David Garcia2f66c4d2020-06-19 11:40:18 +0200103 "pending": N2VCDeploymentStatus.PENDING,
David Garcia4fee80e2020-05-13 12:18:38 +0200104 "running": N2VCDeploymentStatus.RUNNING,
105 "completed": N2VCDeploymentStatus.COMPLETED,
106 },
David Garciac38a6962020-09-16 13:31:33 +0200107 "unit": {
David Garcia4fee80e2020-05-13 12:18:38 +0200108 "waiting": N2VCDeploymentStatus.RUNNING,
109 "maintenance": N2VCDeploymentStatus.RUNNING,
110 "blocked": N2VCDeploymentStatus.RUNNING,
111 "error": N2VCDeploymentStatus.FAILED,
112 "active": N2VCDeploymentStatus.COMPLETED,
113 },
114}
David Garcia2f66c4d2020-06-19 11:40:18 +0200115
116DB_DATA = Dict(
117 {
118 "api_endpoints": Dict(
119 {"table": "admin", "filter": {"_id": "juju"}, "key": "api_endpoints"}
120 )
121 }
122)