blob: eaf8bd0bed93c77a5b856c7b8f9d931f318747fe (
plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# This file is part of cloud-init. See LICENSE file for license information.
"""Classes and functions related to event handling."""
from enum import Enum
from typing import Dict, Set
from cloudinit import log as logging
LOG = logging.getLogger(__name__)
class EventScope(Enum):
# NETWORK is currently the only scope, but we want to leave room to
# grow other scopes (e.g., STORAGE) without having to make breaking
# changes to the user config
NETWORK = "network"
def __str__(self): # pylint: disable=invalid-str-returned
return self.value
class EventType(Enum):
"""Event types which can generate maintenance requests for cloud-init."""
# Cloud-init should grow support for the follow event types:
# HOTPLUG
# METADATA_CHANGE
# USER_REQUEST
BOOT = "boot"
BOOT_NEW_INSTANCE = "boot-new-instance"
BOOT_LEGACY = "boot-legacy"
HOTPLUG = "hotplug"
def __str__(self): # pylint: disable=invalid-str-returned
return self.value
def userdata_to_events(user_config: dict) -> Dict[EventScope, Set[EventType]]:
"""Convert userdata into update config format defined on datasource.
Userdata is in the form of (e.g):
{'network': {'when': ['boot']}}
DataSource config is in the form of:
{EventScope.Network: {EventType.BOOT}}
Take the first and return the second
"""
update_config = {}
for scope, scope_list in user_config.items():
try:
new_scope = EventScope(scope)
except ValueError as e:
LOG.warning(
"%s! Update data will be ignored for '%s' scope",
str(e),
scope,
)
continue
try:
new_values = [EventType(x) for x in scope_list["when"]]
except ValueError as e:
LOG.warning(
"%s! Update data will be ignored for '%s' scope",
str(e),
scope,
)
new_values = []
update_config[new_scope] = set(new_values)
return update_config
# vi: ts=4 expandtab
|