Skip to content

Commit

Permalink
Make use of _flatten_request_body_recursive(...) wherever possible (#…
Browse files Browse the repository at this point in the history
…484)

* Make use of _flatten_request_body_recursive(...) wherever possible

* Drop database changes
  • Loading branch information
lgarber-akamai authored Dec 13, 2024
1 parent be1017b commit 40b88cb
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 134 deletions.
10 changes: 7 additions & 3 deletions linode_api4/groups/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from linode_api4.errors import UnexpectedResponseError
from linode_api4.groups import Group
from linode_api4.objects import Base, Disk, Image
from linode_api4.objects import Disk, Image
from linode_api4.objects.base import _flatten_request_body_recursive
from linode_api4.util import drop_null_keys


Expand Down Expand Up @@ -58,7 +59,7 @@ def create(
:rtype: Image
"""
params = {
"disk_id": disk.id if issubclass(type(disk), Base) else disk,
"disk_id": disk,
"label": label,
"description": description,
"tags": tags,
Expand All @@ -67,7 +68,10 @@ def create(
if cloud_init:
params["cloud_init"] = cloud_init

result = self.client.post("/images", data=drop_null_keys(params))
result = self.client.post(
"/images",
data=_flatten_request_body_recursive(drop_null_keys(params)),
)

if not "id" in result:
raise UnexpectedResponseError(
Expand Down
119 changes: 52 additions & 67 deletions linode_api4/groups/linode.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import base64
import os
from collections.abc import Iterable
from typing import Optional, Union
from typing import Any, Dict, Optional, Union

from linode_api4 import InstanceDiskEncryptionType
from linode_api4.common import load_and_validate_keys
from linode_api4.errors import UnexpectedResponseError
from linode_api4.groups import Group
from linode_api4.objects import (
Base,
ConfigInterface,
Firewall,
Image,
Instance,
InstanceDiskEncryptionType,
Kernel,
PlacementGroup,
StackScript,
Type,
)
from linode_api4.objects.base import _flatten_request_body_recursive
from linode_api4.objects.filtering import Filter
from linode_api4.objects.linode import _expand_placement_group_assignment
from linode_api4.paginated_list import PaginatedList
from linode_api4.objects.linode import (
Backup,
InstancePlacementGroupAssignment,
_expand_placement_group_assignment,
)
from linode_api4.util import drop_null_keys


class LinodeGroup(Group):
Expand Down Expand Up @@ -135,9 +139,20 @@ def instance_create(
region,
image=None,
authorized_keys=None,
firewall: Optional[Union[Firewall, int]] = None,
backup: Optional[Union[Backup, int]] = None,
stackscript: Optional[Union[StackScript, int]] = None,
disk_encryption: Optional[
Union[InstanceDiskEncryptionType, str]
] = None,
placement_group: Optional[
Union[
InstancePlacementGroupAssignment,
PlacementGroup,
Dict[str, Any],
int,
]
] = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -290,65 +305,45 @@ def instance_create(
This usually indicates that you are using
an outdated library.
"""

ret_pass = None
if image and not "root_pass" in kwargs:
ret_pass = Instance.generate_root_password()
kwargs["root_pass"] = ret_pass

authorized_keys = load_and_validate_keys(authorized_keys)

if "stackscript" in kwargs:
# translate stackscripts
kwargs["stackscript_id"] = (
kwargs["stackscript"].id
if issubclass(type(kwargs["stackscript"]), Base)
else kwargs["stackscript"]
)
del kwargs["stackscript"]

if "backup" in kwargs:
# translate backups
kwargs["backup_id"] = (
kwargs["backup"].id
if issubclass(type(kwargs["backup"]), Base)
else kwargs["backup"]
)
del kwargs["backup"]

if "firewall" in kwargs:
fw = kwargs.pop("firewall")
kwargs["firewall_id"] = fw.id if isinstance(fw, Firewall) else fw

if "interfaces" in kwargs:
interfaces = kwargs.get("interfaces")
if interfaces is not None and isinstance(interfaces, Iterable):
kwargs["interfaces"] = [
i._serialize() if isinstance(i, ConfigInterface) else i
for i in interfaces
]

if "placement_group" in kwargs:
kwargs["placement_group"] = _expand_placement_group_assignment(
kwargs.get("placement_group")
)
interfaces = kwargs.get("interfaces", None)
if interfaces is not None and isinstance(interfaces, Iterable):
kwargs["interfaces"] = [
i._serialize() if isinstance(i, ConfigInterface) else i
for i in interfaces
]

params = {
"type": ltype.id if issubclass(type(ltype), Base) else ltype,
"region": region.id if issubclass(type(region), Base) else region,
"image": (
(image.id if issubclass(type(image), Base) else image)
if image
"type": ltype,
"region": region,
"image": image,
"authorized_keys": load_and_validate_keys(authorized_keys),
# These will automatically be flattened below
"firewall_id": firewall,
"backup_id": backup,
"stackscript_id": stackscript,
# Special cases
"disk_encryption": (
str(disk_encryption) if disk_encryption else None
),
"placement_group": (
_expand_placement_group_assignment(placement_group)
if placement_group
else None
),
"authorized_keys": authorized_keys,
}

if disk_encryption is not None:
params["disk_encryption"] = str(disk_encryption)

params.update(kwargs)

result = self.client.post("/linode/instances", data=params)
result = self.client.post(
"/linode/instances",
data=_flatten_request_body_recursive(drop_null_keys(params)),
)

if not "id" in result:
raise UnexpectedResponseError(
Expand Down Expand Up @@ -421,19 +416,6 @@ def stackscript_create(
:returns: The new StackScript
:rtype: StackScript
"""
image_list = None
if type(images) is list or type(images) is PaginatedList:
image_list = [
d.id if issubclass(type(d), Base) else d for d in images
]
elif type(images) is Image:
image_list = [images.id]
elif type(images) is str:
image_list = [images]
else:
raise ValueError(
"images must be a list of Images or a single Image"
)

script_body = script
if not script.startswith("#!"):
Expand All @@ -448,14 +430,17 @@ def stackscript_create(

params = {
"label": label,
"images": image_list,
"images": images,
"is_public": public,
"script": script_body,
"description": desc if desc else "",
}
params.update(kwargs)

result = self.client.post("/linode/stackscripts", data=params)
result = self.client.post(
"/linode/stackscripts",
data=_flatten_request_body_recursive(params),
)

if not "id" in result:
raise UnexpectedResponseError(
Expand Down
14 changes: 8 additions & 6 deletions linode_api4/groups/volume.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from linode_api4.errors import UnexpectedResponseError
from linode_api4.groups import Group
from linode_api4.objects import Base, Volume, VolumeType
from linode_api4.objects import Volume, VolumeType
from linode_api4.objects.base import _flatten_request_body_recursive


class VolumeGroup(Group):
Expand Down Expand Up @@ -57,14 +58,15 @@ def create(self, label, region=None, linode=None, size=20, **kwargs):
params = {
"label": label,
"size": size,
"region": region.id if issubclass(type(region), Base) else region,
"linode_id": (
linode.id if issubclass(type(linode), Base) else linode
),
"region": region,
"linode_id": linode,
}
params.update(kwargs)

result = self.client.post("/volumes", data=params)
result = self.client.post(
"/volumes",
data=_flatten_request_body_recursive(params),
)

if not "id" in result:
raise UnexpectedResponseError(
Expand Down
Loading

0 comments on commit 40b88cb

Please sign in to comment.