-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Labels support for servers & storages, server group support
- Loading branch information
Showing
6 changed files
with
154 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from upcloud_api.upcloud_resource import UpCloudResource | ||
|
||
|
||
class Label(UpCloudResource): | ||
""" | ||
Class representation of UpCloud resource label | ||
""" | ||
|
||
ATTRIBUTES = { | ||
'key': "", | ||
'value': "", | ||
} | ||
|
||
def __init__(self, key="", value="") -> None: | ||
""" | ||
Initialize label. | ||
Set both values for label if given | ||
""" | ||
self.key = key | ||
self.value = value | ||
|
||
def __str__(self) -> str: | ||
return f"{self.key}={self.value}" | ||
|
||
def to_dict(self): | ||
""" | ||
Return a dict that can be serialised to JSON and sent to UpCloud's API. | ||
""" | ||
body = { | ||
'key': self.key, | ||
'value': self.value, | ||
} | ||
|
||
return body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from enum import Enum | ||
|
||
from upcloud_api.upcloud_resource import UpCloudResource | ||
|
||
|
||
class ServerGroupAffinityPolicy(str, Enum): | ||
""" | ||
Enum representation of affinity policy for a server group | ||
""" | ||
|
||
STRICT_ANTI_AFFINITY = 'strict' | ||
ANTI_AFFINITY_PREFERRED = 'yes' | ||
NO_ANTI_AFFINITY = 'no' | ||
|
||
|
||
class ServerGroup(UpCloudResource): | ||
""" | ||
Class representation of UpCloud server group resource | ||
""" | ||
|
||
ATTRIBUTES = { | ||
'anti_affinity': ServerGroupAffinityPolicy.NO_ANTI_AFFINITY, | ||
'labels': None, | ||
'servers': None, | ||
'title': None, | ||
'uuid': None, | ||
} | ||
|
||
def __str__(self) -> str: | ||
return self.title | ||
|
||
def to_dict(self): | ||
""" | ||
Return a dict that can be serialised to JSON and sent to UpCloud's API. | ||
""" | ||
body = { | ||
'title': self.title, | ||
} | ||
|
||
if hasattr(self, 'anti_affinity'): | ||
body['anti_affinity'] = f"{self.anti_affinity}" | ||
|
||
if hasattr(self, 'servers'): | ||
servers = [] | ||
for server in self.servers: | ||
if isinstance(server, server.Server) and hasattr(server, 'uuid'): | ||
servers.append(server.uuid) | ||
else: | ||
servers.append(server) | ||
|
||
if hasattr(self, 'labels'): | ||
dict_labels = {'label': []} | ||
for label in self.labels: | ||
dict_labels['label'].append(label.to_dict()) | ||
body['labels'] = dict_labels | ||
|
||
return {'server_group': body} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters