Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions maas/client/bones/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def fromURL(cls, url, *, credentials=None, insecure=False):
# For now just re-raise as SessionError.
raise SessionError(str(error))
else:
session = cls(description, credentials)
session = cls(url, description, credentials)
session.scheme = urlparse(url).scheme
session.insecure = insecure
return session
Expand All @@ -49,7 +49,7 @@ def fromProfile(cls, profile):

:see: `ProfileStore`.
"""
session = cls(profile.description, profile.credentials)
session = cls(profile.url, profile.description, profile.credentials)
session.scheme = urlparse(profile.url).scheme
session.insecure = profile.other.get("insecure", False)
return session
Expand All @@ -75,7 +75,7 @@ async def login(cls, url, *, username=None, password=None, insecure=False):
profile = await helpers.login(
url=url, username=username, password=password, insecure=insecure
)
session = cls(profile.description, profile.credentials)
session = cls(url, profile.description, profile.credentials)
session.scheme = urlparse(url).scheme
session.insecure = insecure
return profile, session
Expand All @@ -90,7 +90,7 @@ async def connect(cls, url, *, apikey=None, insecure=False):
instance made using the profile.
"""
profile = await helpers.connect(url=url, apikey=apikey, insecure=insecure)
session = cls(profile.description, profile.credentials)
session = cls(url, profile.description, profile.credentials)
session.scheme = urlparse(url).scheme
session.insecure = insecure
return profile, session
Expand All @@ -100,13 +100,15 @@ async def connect(cls, url, *, apikey=None, insecure=False):
insecure = False
debug = False

def __init__(self, description, credentials=None):
def __init__(self, url, description, credentials=None):
"""Construct a `SessionAPI`.

:param url: MAAS URL
:param description: The description of the remote API. See `fromURL`.
:param credentials: Credentials for the remote system. Optional.
"""
super(SessionAPI, self).__init__()
self.__url = url
self.__description = description
self.__credentials = credentials
self.__populate()
Expand All @@ -116,15 +118,15 @@ def __populate(self):
if self.__credentials is None:
for resource in resources:
if resource["anon"] is not None:
handler = HandlerAPI(resource["anon"], resource, self)
handler = HandlerAPI(self.__url, resource["anon"], resource, self)
setattr(self, handler.name, handler)
else:
for resource in resources:
if resource["auth"] is not None:
handler = HandlerAPI(resource["auth"], resource, self)
handler = HandlerAPI(self.__url, resource["auth"], resource, self)
setattr(self, handler.name, handler)
elif resource["anon"] is not None:
handler = HandlerAPI(resource["anon"], resource, self)
handler = HandlerAPI(self.__url, resource["anon"], resource, self)
setattr(self, handler.name, handler)

@property
Expand Down Expand Up @@ -154,16 +156,18 @@ class HandlerAPI:
operations.
"""

def __init__(self, handler, resource, session):
def __init__(self, url, handler, resource, session):
"""Construct a `HandlerAPI`.

:param url: MAAS URL
:param handler: The handler description from the overall API
description document. See `SessionAPI`.
:param resource: The parent of `handler` in the API description
document. XXX: This does not appear to be needed.
:param session: The `SessionAPI`.
"""
super(HandlerAPI, self).__init__()
self.__url = url
self.__handler = handler
self.__resource = resource
self.__session = session
Expand All @@ -187,7 +191,8 @@ def uri(self):
This will typically contain replacement patterns; these are
interpolated in `CallAPI`.
"""
return self.__handler["uri"]
url = urlparse(self.__url)
return f"{url.scheme}://{url.netloc}{self.__handler['path']}"

@property
def params(self):
Expand Down
12 changes: 8 additions & 4 deletions maas/client/bones/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ class TestActionAPI_APIVersions(TestCase):
for name, version, description in testing.api_descriptions
)

url = "http://127.0.0.1:8080/MAAS/api/2.0/"

def test__Version_read(self):
session = bones.SessionAPI(self.description)
session = bones.SessionAPI(self.url, self.description)
action = session.Version.read
self.assertThat(
action,
Expand All @@ -91,7 +93,7 @@ def test__Machines_deployment_status(self):
if self.version > (2, 0):
self.skipTest("Machines.deployment_status only in <= 2.0")

session = bones.SessionAPI(self.description, ("a", "b", "c"))
session = bones.SessionAPI(self.url, self.description, ("a", "b", "c"))
action = session.Machines.deployment_status
self.assertThat(
action,
Expand All @@ -106,7 +108,7 @@ def test__Machines_deployment_status(self):
)

def test__Machines_power_parameters(self):
session = bones.SessionAPI(self.description, ("a", "b", "c"))
session = bones.SessionAPI(self.url, self.description, ("a", "b", "c"))
action = session.Machines.power_parameters
self.assertThat(
action,
Expand All @@ -129,9 +131,11 @@ class TestCallAPI_APIVersions(TestCase):
for name, version, description in testing.api_descriptions
)

url = "http://127.0.0.1:8080/MAAS/api/2.0/"

def test__marshals_lists_into_query_as_repeat_parameters(self):
system_ids = list(str(uuid1()) for _ in range(3))
session = bones.SessionAPI(self.description, ("a", "b", "c"))
session = bones.SessionAPI(self.url, self.description, ("a", "b", "c"))
call = session.Machines.power_parameters.bind()
call.dispatch = Mock()

Expand Down