Newer
Older
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
from __future__ import annotations
from typing import TYPE_CHECKING, Dict
from common.database.api.context.Keys import KEY_SERVICE_CONSTRAINT
from common.database.api.entity._Entity import _Entity
if TYPE_CHECKING:
from common.database.api.context.Context import Context
from common.database.api.context.service.Service import Service
VALIDATORS = {
'constraint_value': lambda v: v is not None and isinstance(v, str) and (len(v) > 0),
}
TRANSCODERS = {} # no transcoding applied to attributes
class Constraint(_Entity):
def __init__(self, constraint_type : str, parent : 'Service'):
super().__init__(parent, constraint_type, KEY_SERVICE_CONSTRAINT, VALIDATORS, TRANSCODERS)
@property
def parent(self) -> 'Service': return self._parent
@property
def context(self) -> 'Context': return self.parent.context
@property
def context_uuid(self) -> str: return self.parent.context_uuid
@property
def service(self) -> 'Service': return self.parent
@property
def service_uuid(self) -> str: return self.parent.service_uuid
@property
def constraint_type(self) -> str: return self._entity_uuid
def create(self, constraint_value : str) -> 'Constraint':
self.update(update_attributes={
'constraint_value': constraint_value,
})
self.parent.endpoints.add(self.constraint_type)
return self
def update(self, update_attributes={}, remove_attributes=[]) -> 'Constraint':
self.attributes.update(update_attributes=update_attributes, remove_attributes=remove_attributes)
return self
def delete(self) -> None:
self.attributes.delete()
self.parent.endpoints.delete(self.constraint_type)
def dump_id(self) -> Dict:
return {
'constraint_type': self.constraint_type,
}
def dump(self) -> Dict:
attributes = self.attributes.get()
return {
'constraint_type': self.constraint_type,
'constraint_value': attributes.get('constraint_value', None),
}