forked from juju-solutions/interface-kube-control
-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
82 lines (62 loc) · 2.46 KB
/
models.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from typing import Union, Optional
from enum import Enum, auto
class DecodeError(Exception):
pass
class Effect(Enum):
"""Enumerates possible taint effects."""
NoSchedule = auto()
PreferNoSchedule = auto()
NoExecute = auto()
class _ModelObject:
@classmethod
def valid(cls, source: Union[str, "_ModelObject"]) -> "_ModelObject":
if isinstance(source, str):
source = cls.decode(source)
return isinstance(source, cls)
class Taint(_ModelObject):
"""Definition of a Node Taint."""
def __init__(self, key: str, value: Optional[str], effect: Effect) -> None:
self.key = key
self.value = value
self.effect = effect
def __str__(self):
"""Encode a taint object to a string."""
key_value = "=".join(filter(None, (self.key, self.value)))
return f"{key_value}:{self.effect.name}"
def __eq__(self, __o: object) -> bool:
return (self.key, self.value, self.effect) == (__o.key, __o.value, __o.effect)
@classmethod
def decode(cls, source: str):
"""Decode a taint object from a string."""
try:
key_value, effect = source.split(":")
effect_value = Effect[effect]
except ValueError as ex:
raise DecodeError("Taint must contain a single ':'") from ex
except KeyError as ex:
options = ",".join([_.name for _ in Effect])
raise DecodeError(f"Taint effect must be {options}") from ex
key, *value = key_value.split("=")
if len(value) > 1:
# Taints aren't required to have a value
# therefore value can have 0 or 1 elements, but not more than 1
raise DecodeError("Taint cannot contain more than one '='")
return cls(key, next(iter(value), None), effect_value)
class Label(_ModelObject):
"""Definition of a Label."""
def __init__(self, key: str, value: str) -> None:
self.key = key
self.value = value
def __eq__(self, __o: object) -> bool:
return (self.key, self.value) == (__o.key, __o.value)
def __str__(self):
"""Encode a label object to a string."""
return f"{self.key}={self.value}"
@classmethod
def decode(cls, source: str):
"""Decode a label object from a string."""
try:
key, value = source.split("=")
except ValueError as ex:
raise DecodeError("Label must contain a single '='") from ex
return cls(key, value)