a661e050289721aa352b85311474f91e15e508aa
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
25 from typing
import Tuple
28 def base64_to_cacert(b64string
):
29 """Convert the base64-encoded string containing the VCA CACERT.
35 cacert
= base64
.b64decode(b64string
).decode("utf-8")
42 except binascii
.Error
as e
:
43 raise N2VCInvalidCertificate(message
="Invalid CA Certificate: {}".format(e
))
48 class N2VCDeploymentStatus(Enum
):
51 COMPLETED
= "completed"
58 Dict class that allows to access the keys like attributes
61 def __getattribute__(self
, name
):
66 class EntityType(Enum
):
68 APPLICATION
= Application
73 def has_value(cls
, value
):
74 return value
in cls
._value
2member
_map
_ # pylint: disable=E1101
77 def get_entity(cls
, value
):
79 cls
._value
2member
_map
_[value
] # pylint: disable=E1101
80 if value
in cls
._value
2member
_map
_ # pylint: disable=E1101
81 else None # pylint: disable=E1101
85 def get_entity_from_delta(cls
, delta_entity
: str):
87 Get Value from delta entity
89 :param: delta_entity: Possible values are "machine", "application", "unit", "action"
91 for v
in cls
._value
2member
_map
_: # pylint: disable=E1101
92 if v
.__name
__.lower() == delta_entity
:
93 return cls
.get_entity(v
)
98 "pending": N2VCDeploymentStatus
.PENDING
,
99 "started": N2VCDeploymentStatus
.COMPLETED
,
102 "waiting": N2VCDeploymentStatus
.RUNNING
,
103 "maintenance": N2VCDeploymentStatus
.RUNNING
,
104 "blocked": N2VCDeploymentStatus
.RUNNING
,
105 "error": N2VCDeploymentStatus
.FAILED
,
106 "active": N2VCDeploymentStatus
.COMPLETED
,
109 "pending": N2VCDeploymentStatus
.PENDING
,
110 "running": N2VCDeploymentStatus
.RUNNING
,
111 "completed": N2VCDeploymentStatus
.COMPLETED
,
114 "waiting": N2VCDeploymentStatus
.RUNNING
,
115 "maintenance": N2VCDeploymentStatus
.RUNNING
,
116 "blocked": N2VCDeploymentStatus
.RUNNING
,
117 "error": N2VCDeploymentStatus
.FAILED
,
118 "active": N2VCDeploymentStatus
.COMPLETED
,
123 def obj_to_yaml(obj
: object) -> str:
125 Converts object to yaml format
129 dump_text
= yaml
.dump(obj
, default_flow_style
=False, indent
=2)
131 lines
= dump_text
.splitlines()
132 # remove !!python/object tags
135 index
= line
.find("!!python/object")
138 yaml_text
+= line
+ "\n"
142 def obj_to_dict(obj
: object) -> dict:
144 Converts object to dictionary format
147 # convert obj to yaml
148 yaml_text
= obj_to_yaml(obj
)
150 return yaml
.load(yaml_text
, Loader
=yaml
.Loader
)
153 def get_ee_id_components(ee_id
: str) -> Tuple
[str, str, str]:
155 Get model, application and machine components from an execution environment id
157 :return: model_name, application_name, machine_id
159 parts
= ee_id
.split(".")
161 raise Exception("invalid ee id.")
162 model_name
= parts
[0]
163 application_name
= parts
[1]
164 machine_id
= parts
[2]
165 return model_name
, application_name
, machine_id