1 # Copyright 2020 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 from juju
.machine
import Machine
21 from juju
.application
import Application
22 from juju
.action
import Action
23 from juju
.unit
import Unit
24 from n2vc
.exceptions
import N2VCInvalidCertificate
27 def base64_to_cacert(b64string
):
28 """Convert the base64-encoded string containing the VCA CACERT.
34 cacert
= base64
.b64decode(b64string
).decode("utf-8")
36 cacert
= re
.sub(r
"\\n", r
"\n", cacert
,)
37 except binascii
.Error
as e
:
38 raise N2VCInvalidCertificate(message
="Invalid CA Certificate: {}".format(e
))
43 class N2VCDeploymentStatus(Enum
):
46 COMPLETED
= "completed"
53 Dict class that allows to access the keys like attributes
56 def __getattribute__(self
, name
):
61 class EntityType(Enum
):
63 APPLICATION
= Application
68 def has_value(cls
, value
):
69 return value
in cls
._value
2member
_map
_ # pylint: disable=E1101
72 def get_entity(cls
, value
):
74 cls
._value
2member
_map
_[value
] # pylint: disable=E1101
75 if value
in cls
._value
2member
_map
_ # pylint: disable=E1101
76 else None # pylint: disable=E1101
80 def get_entity_from_delta(cls
, delta_entity
: str):
82 Get Value from delta entity
84 :param: delta_entity: Possible values are "machine", "application", "unit", "action"
86 for v
in cls
._value
2member
_map
_: # pylint: disable=E1101
87 if v
.__name
__.lower() == delta_entity
:
88 return cls
.get_entity(v
)
93 "pending": N2VCDeploymentStatus
.PENDING
,
94 "started": N2VCDeploymentStatus
.COMPLETED
,
97 "waiting": N2VCDeploymentStatus
.RUNNING
,
98 "maintenance": N2VCDeploymentStatus
.RUNNING
,
99 "blocked": N2VCDeploymentStatus
.RUNNING
,
100 "error": N2VCDeploymentStatus
.FAILED
,
101 "active": N2VCDeploymentStatus
.COMPLETED
,
104 "pending": N2VCDeploymentStatus
.PENDING
,
105 "running": N2VCDeploymentStatus
.RUNNING
,
106 "completed": N2VCDeploymentStatus
.COMPLETED
,
109 "waiting": N2VCDeploymentStatus
.RUNNING
,
110 "maintenance": N2VCDeploymentStatus
.RUNNING
,
111 "blocked": N2VCDeploymentStatus
.RUNNING
,
112 "error": N2VCDeploymentStatus
.FAILED
,
113 "active": N2VCDeploymentStatus
.COMPLETED
,
119 "api_endpoints": Dict(
120 {"table": "admin", "filter": {"_id": "juju"}, "key": "api_endpoints"}
126 def obj_to_yaml(obj
: object) -> str:
128 Converts object to yaml format
132 dump_text
= yaml
.dump(obj
, default_flow_style
=False, indent
=2)
134 lines
= dump_text
.splitlines()
135 # remove !!python/object tags
138 index
= line
.find("!!python/object")
141 yaml_text
+= line
+ "\n"
145 def obj_to_dict(obj
: object) -> dict:
147 Converts object to dictionary format
150 # convert obj to yaml
151 yaml_text
= obj_to_yaml(obj
)
153 return yaml
.load(yaml_text
, Loader
=yaml
.Loader
)