14ac5d1f63c1f95694d22809fe65c4ad218dcbc6
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.
16 from juju
.machine
import Machine
17 from juju
.application
import Application
18 from juju
.action
import Action
19 from juju
.unit
import Unit
23 class N2VCDeploymentStatus(Enum
):
26 COMPLETED
= "completed"
33 Dict class that allows to access the keys like attributes
36 def __getattribute__(self
, name
):
41 class EntityType(Enum
):
43 APPLICATION
= Application
48 def has_value(cls
, value
):
49 return value
in cls
._value
2member
_map
_ # pylint: disable=E1101
52 def get_entity(cls
, value
):
54 cls
._value
2member
_map
_[value
] # pylint: disable=E1101
55 if value
in cls
._value
2member
_map
_ # pylint: disable=E1101
56 else None # pylint: disable=E1101
60 def get_entity_from_delta(cls
, delta_entity
: str):
62 Get Value from delta entity
64 :param: delta_entity: Possible values are "machine", "application", "unit", "action"
66 for v
in cls
._value
2member
_map
_: # pylint: disable=E1101
67 if v
.__name
__.lower() == delta_entity
:
68 return cls
.get_entity(v
)
73 "pending": N2VCDeploymentStatus
.PENDING
,
74 "started": N2VCDeploymentStatus
.COMPLETED
,
77 "waiting": N2VCDeploymentStatus
.RUNNING
,
78 "maintenance": N2VCDeploymentStatus
.RUNNING
,
79 "blocked": N2VCDeploymentStatus
.RUNNING
,
80 "error": N2VCDeploymentStatus
.FAILED
,
81 "active": N2VCDeploymentStatus
.COMPLETED
,
84 "pending": N2VCDeploymentStatus
.PENDING
,
85 "running": N2VCDeploymentStatus
.RUNNING
,
86 "completed": N2VCDeploymentStatus
.COMPLETED
,
89 "waiting": N2VCDeploymentStatus
.RUNNING
,
90 "maintenance": N2VCDeploymentStatus
.RUNNING
,
91 "blocked": N2VCDeploymentStatus
.RUNNING
,
92 "error": N2VCDeploymentStatus
.FAILED
,
93 "active": N2VCDeploymentStatus
.COMPLETED
,
99 "api_endpoints": Dict(
100 {"table": "admin", "filter": {"_id": "juju"}, "key": "api_endpoints"}
106 def obj_to_yaml(obj
: object) -> str:
108 Converts object to yaml format
112 dump_text
= yaml
.dump(obj
, default_flow_style
=False, indent
=2)
114 lines
= dump_text
.splitlines()
115 # remove !!python/object tags
118 index
= line
.find("!!python/object")
121 yaml_text
+= line
+ "\n"
125 def obj_to_dict(obj
: object) -> dict:
127 Converts object to dictionary format
130 # convert obj to yaml
131 yaml_text
= obj_to_yaml(obj
)
133 return yaml
.load(yaml_text
, Loader
=yaml
.Loader
)