This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfilter_vm_action.py
55 lines (43 loc) · 1.71 KB
/
filter_vm_action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
FilterVmAction - custom action.
Simple action for filtering VM on the presence of metadata/extra spec
"evacuate" flag
"""
from mistral.actions.openstack.actions import NovaAction
from mistral.workflow.utils import Result
class FilterVmException(Exception):
pass
class FilterVmAction(NovaAction):
"""
Filter and return VMs whith the flag 'evacuate' either on vm metadtata
or flavor extra spec.
"""
def __init__(self, metadata, flavor, uuid):
"""init."""
self._metadata = metadata
self._flavor = flavor
self._uuid = uuid
def run(self):
"""Entry point for the action execution."""
client = self._get_client()
metadata = self._metadata
if str(metadata.get('evacuate')).upper() == 'TRUE':
return Result(data={'evacuate': True, 'uuid': self._uuid})
elif str(metadata.get('evacuate')).upper() == 'FALSE':
return Result(data={'evacuate': False, 'uuid': self._uuid})
# Ether is no metadata for vm - check flavor.
try:
# Maybe this should be done in different action
# only once per whole workflow.
# In case there is ~100 VMs to evacuate, there will be
# the same amount of calls to nova API.
flavor = filter(
lambda f: f.id == self._flavor,
client.flavors.list()
)[0]
except IndexError:
raise FilterVmException('Flavor not found')
evacuate = flavor.get_keys().get('evacuation:evacuate')
if str(evacuate).upper() == 'TRUE':
return Result(data={'evacuate': True, 'uuid': self._uuid})
return Result(data={'evacuate': False, 'uuid': self._uuid})