Use default Previous Work Dir if there is none
[osm/N2VC.git] / n2vc / utils.py
1 # 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
15 from enum import Enum
16 from juju.machine import Machine
17 from juju.application import Application
18 from juju.action import Action
19 from juju.unit import Unit
20
21
22 class N2VCDeploymentStatus(Enum):
23 PENDING = "pending"
24 RUNNING = "running"
25 COMPLETED = "completed"
26 FAILED = "failed"
27 UNKNOWN = "unknown"
28
29
30 class Dict(dict):
31 """
32 Dict class that allows to access the keys like attributes
33 """
34
35 def __getattribute__(self, name):
36 if name in self:
37 return self[name]
38
39
40 class EntityType(Enum):
41 MACHINE = Machine
42 APPLICATION = Application
43 ACTION = Action
44 UNIT = Unit
45
46 @classmethod
47 def has_value(cls, value):
48 return value in cls._value2member_map_ # pylint: disable=E1101
49
50 @classmethod
51 def get_entity(cls, value):
52 return (
53 cls._value2member_map_[value] # pylint: disable=E1101
54 if value in cls._value2member_map_ # pylint: disable=E1101
55 else None # pylint: disable=E1101
56 )
57
58 @classmethod
59 def get_entity_from_delta(cls, delta_entity: str):
60 """
61 Get Value from delta entity
62
63 :param: delta_entity: Possible values are "machine", "application", "unit", "action"
64 """
65 for v in cls._value2member_map_: # pylint: disable=E1101
66 if v.__name__.lower() == delta_entity:
67 return cls.get_entity(v)
68
69
70 FinalStatus = Dict(
71 {
72 EntityType.MACHINE: Dict({"field": "agent_status", "status": ["started"]}),
73 EntityType.APPLICATION: Dict(
74 {"field": "status", "status": ["active", "blocked"]}
75 ),
76 EntityType.ACTION: Dict(
77 {"field": "status", "status": ["completed", "failed", "cancelled"]}
78 ),
79 }
80 )
81
82 JujuStatusToOSM = {
83 EntityType.MACHINE: {
84 "pending": N2VCDeploymentStatus.PENDING,
85 "started": N2VCDeploymentStatus.COMPLETED,
86 },
87 EntityType.APPLICATION: {
88 "waiting": N2VCDeploymentStatus.RUNNING,
89 "maintenance": N2VCDeploymentStatus.RUNNING,
90 "blocked": N2VCDeploymentStatus.RUNNING,
91 "error": N2VCDeploymentStatus.FAILED,
92 "active": N2VCDeploymentStatus.COMPLETED,
93 },
94 EntityType.ACTION: {
95 "running": N2VCDeploymentStatus.RUNNING,
96 "completed": N2VCDeploymentStatus.COMPLETED,
97 },
98 EntityType.UNIT: {
99 "waiting": N2VCDeploymentStatus.RUNNING,
100 "maintenance": N2VCDeploymentStatus.RUNNING,
101 "blocked": N2VCDeploymentStatus.RUNNING,
102 "error": N2VCDeploymentStatus.FAILED,
103 "active": N2VCDeploymentStatus.COMPLETED,
104 },
105 }