Skip to content

Commit

Permalink
Merge branch 'dev' into test/retry_on_flaky_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ykim-akamai authored Sep 23, 2024
2 parents ae171e0 + ef89bac commit 74ca2fe
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ docs/_build/*
venv
baked_version
.vscode
.DS_Store
85 changes: 53 additions & 32 deletions linode_api4/objects/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
import requests

from linode_api4.errors import ApiError, UnexpectedResponseError
from linode_api4.objects import (
DATE_FORMAT,
Base,
DerivedBase,
Domain,
Image,
Instance,
Property,
StackScript,
Volume,
)
from linode_api4.objects import DATE_FORMAT, Volume
from linode_api4.objects.base import Base, Property
from linode_api4.objects.database import Database
from linode_api4.objects.dbase import DerivedBase
from linode_api4.objects.domain import Domain
from linode_api4.objects.image import Image
from linode_api4.objects.linode import Instance, StackScript
from linode_api4.objects.longview import LongviewClient, LongviewSubscription
from linode_api4.objects.networking import Firewall
from linode_api4.objects.nodebalancer import NodeBalancer
from linode_api4.objects.profile import PersonalAccessToken
from linode_api4.objects.support import SupportTicket
from linode_api4.objects.volume import Volume
from linode_api4.objects.vpc import VPC


class Account(Base):
Expand Down Expand Up @@ -554,10 +553,6 @@ def get_obj_grants():
"""
Returns Grant keys mapped to Object types.
"""
from linode_api4.objects import ( # pylint: disable=import-outside-toplevel
Database,
Firewall,
)

return (
("linode", Instance),
Expand All @@ -569,6 +564,7 @@ def get_obj_grants():
("longview", LongviewClient),
("database", Database),
("firewall", Firewall),
("vpc", VPC),
)


Expand Down Expand Up @@ -641,10 +637,47 @@ def _populate(self, json):
self.global_grants = type("global_grants", (object,), json["global"])

for key, cls in get_obj_grants():
lst = []
for gdct in json[key]:
lst.append(Grant(self._client, cls, gdct))
setattr(self, key, lst)
if key in json:
lst = []
for gdct in json[key]:
lst.append(Grant(self._client, cls, gdct))
setattr(self, key, lst)

@property
def _global_grants_dict(self):
"""
The global grants stored in this object.
"""
return {
k: v
for k, v in vars(self.global_grants).items()
if not k.startswith("_")
}

@property
def _grants_dict(self):
"""
The grants stored in this object.
"""
grants = {}
for key, _ in get_obj_grants():
if hasattr(self, key):
lst = []
for cg in getattr(self, key):
lst.append(cg._serialize())
grants[key] = lst

return grants

def _serialize(self):
"""
Returns the user grants in as JSON the api will accept.
This is only relevant in the context of UserGrants.save
"""
return {
"global": self._global_grants_dict,
**self._grants_dict,
}

def save(self):
"""
Expand All @@ -653,19 +686,7 @@ def save(self):
API Documentation: https://techdocs.akamai.com/linode-api/reference/put-user-grants
"""

req = {
"global": {
k: v
for k, v in vars(self.global_grants).items()
if not k.startswith("_")
},
}

for key, _ in get_obj_grants():
lst = []
for cg in getattr(self, key):
lst.append(cg._serialize())
req[key] = lst
req = self._serialize()

result = self._client.put(
UserGrants.api_endpoint.format(username=self.username), data=req
Expand Down
2 changes: 1 addition & 1 deletion test/integration/linode_client/test_linode_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def test_get_account_settings(test_linode_client):

assert account_settings._populated == True
assert re.search(
"'network_helper':\s*(True|False)", str(account_settings._raw_json)
r"'network_helper':\s*(True|False)", str(account_settings._raw_json)
)


Expand Down
106 changes: 90 additions & 16 deletions test/unit/objects/account_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections.abc import Iterable
from copy import deepcopy
from datetime import datetime
from test.unit.base import ClientBaseCase

Expand All @@ -21,10 +23,12 @@
ServiceTransfer,
StackScript,
User,
UserGrants,
Volume,
get_obj_grants,
)
from linode_api4.objects.account import ChildAccount
from linode_api4.objects.vpc import VPC


class InvoiceTest(ClientBaseCase):
Expand Down Expand Up @@ -204,22 +208,6 @@ def test_get_payment_method(self):
self.assertTrue(paymentMethod.is_default)
self.assertEqual(paymentMethod.type, "credit_card")

def test_get_user_grant(self):
"""
Tests that a user grant is loaded correctly
"""
grants = get_obj_grants()

self.assertTrue(grants.count(("linode", Instance)) > 0)
self.assertTrue(grants.count(("domain", Domain)) > 0)
self.assertTrue(grants.count(("stackscript", StackScript)) > 0)
self.assertTrue(grants.count(("nodebalancer", NodeBalancer)) > 0)
self.assertTrue(grants.count(("volume", Volume)) > 0)
self.assertTrue(grants.count(("image", Image)) > 0)
self.assertTrue(grants.count(("longview", LongviewClient)) > 0)
self.assertTrue(grants.count(("database", Database)) > 0)
self.assertTrue(grants.count(("firewall", Firewall)) > 0)

def test_payment_method_make_default(self):
"""
Tests that making a payment method default creates the correct api request.
Expand Down Expand Up @@ -309,3 +297,89 @@ def test_child_account_create_token(self):
token = child_account.create_token()
self.assertEqual(token.token, "abcdefghijklmnop")
self.assertEqual(m.call_data, {})


def test_get_user_grant():
"""
Tests that a user grant is loaded correctly
"""
grants = get_obj_grants()

assert grants.count(("linode", Instance)) > 0
assert grants.count(("domain", Domain)) > 0
assert grants.count(("stackscript", StackScript)) > 0
assert grants.count(("nodebalancer", NodeBalancer)) > 0
assert grants.count(("volume", Volume)) > 0
assert grants.count(("image", Image)) > 0
assert grants.count(("longview", LongviewClient)) > 0
assert grants.count(("database", Database)) > 0
assert grants.count(("firewall", Firewall)) > 0
assert grants.count(("vpc", VPC)) > 0


def test_user_grants_serialization():
"""
Tests that user grants from JSON is serialized correctly
"""
user_grants_json = {
"database": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"domain": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"firewall": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"global": {
"account_access": "read_only",
"add_databases": True,
"add_domains": True,
"add_firewalls": True,
"add_images": True,
"add_linodes": True,
"add_longview": True,
"add_nodebalancers": True,
"add_placement_groups": True,
"add_stackscripts": True,
"add_volumes": True,
"add_vpcs": True,
"cancel_account": False,
"child_account_access": True,
"longview_subscription": True,
},
"image": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"linode": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"longview": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"nodebalancer": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"stackscript": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"volume": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
"vpc": [
{"id": 123, "label": "example-entity", "permissions": "read_only"}
],
}

expected_serialized_grants = deepcopy(user_grants_json)

for grants in expected_serialized_grants.values():
if isinstance(grants, Iterable):
for grant in grants:
if isinstance(grant, dict) and "label" in grant:
del grant["label"]

assert (
UserGrants(None, None, user_grants_json)._serialize()
== expected_serialized_grants
)

0 comments on commit 74ca2fe

Please sign in to comment.