Skip to content

Commit

Permalink
Merge pull request #489 from albertodonato/machine-location-none-if-n…
Browse files Browse the repository at this point in the history
…o-cluster

set Instance.location to None for non-clustred servers
  • Loading branch information
albertodonato authored Nov 2, 2021
2 parents c5db7ad + ac0119c commit a321990
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
9 changes: 6 additions & 3 deletions pylxd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,8 @@ def __init__(
) as e:
raise exceptions.ClientConnectionFailed(str(e))

if (
self.project not in (None, "default")
and "projects" not in self.host_info["api_extensions"]
if self.project not in (None, "default") and not self.has_api_extension(
"projects"
):
raise exceptions.ClientConnectionFailed(
"Remote server doesn't handle projects"
Expand All @@ -416,6 +415,10 @@ def __init__(
def trusted(self):
return self.host_info["auth"] == "trusted"

@property
def server_clustered(self):
return self.host_info["environment"].get("server_clustered", False)

@property
def resources(self):
if self._resource_cache is None:
Expand Down
6 changes: 6 additions & 0 deletions pylxd/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class Instance(model.Model):

_endpoint = "instances"

def __setattr__(self, name, value):
if name == "location" and not self.client.server_clustered:
# LXD reports "none" as location when not in a cluster
value = None
super().__setattr__(name, value)

@property
def api(self):
return self.client.api[self._endpoint][self.name]
Expand Down
12 changes: 11 additions & 1 deletion pylxd/models/tests/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,27 @@ def test_create(self):

self.assertEqual(config["name"], an_new_instance.name)

def test_create_remote(self):
def test_create_remote_location(self):
"""A new instance is created at target."""
config = {"name": "an-new-remote-instance"}

# the server must be in a cluster for the location to be set
self.client.host_info["environment"]["server_clustered"] = True

an_new_remote_instance = models.Instance.create(
self.client, config, wait=True, target="an-remote"
)

self.assertEqual(config["name"], an_new_remote_instance.name)
self.assertEqual("an-remote", an_new_remote_instance.location)

def test_create_location_none(self):
config = {"name": "an-new-remote-instance"}

instance = models.Instance.create(self.client, config, wait=True)

self.assertIsNone(instance.location)

def test_exists(self):
"""A instance exists."""
name = "an-instance"
Expand Down
28 changes: 28 additions & 0 deletions pylxd/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ def test_connection_trusted(self):

self.assertTrue(an_client.trusted)

def test_server_clustered_false_no_info(self):
"""Client.server_clustered is False if the info is not available in metadata."""
response = mock.MagicMock(status_code=200)
response.json.return_value = {"metadata": {"environment": {}}}
self.get.return_value = response
a_client = client.Client()
self.assertFalse(a_client.server_clustered)

def test_server_clustered_false(self):
"""Client.server_clustered is False if not clustered."""
response = mock.MagicMock(status_code=200)
response.json.return_value = {
"metadata": {"environment": {"server_clustered": False}}
}
self.get.return_value = response
a_client = client.Client()
self.assertFalse(a_client.server_clustered)

def test_server_clustered_true(self):
"""Client.server_clustered is True if clustered."""
response = mock.MagicMock(status_code=200)
response.json.return_value = {
"metadata": {"environment": {"server_clustered": True}}
}
self.get.return_value = response
a_client = client.Client()
self.assertTrue(a_client.server_clustered)

def test_authenticate(self):
"""A client is authenticated."""
response = mock.MagicMock(status_code=200)
Expand Down

0 comments on commit a321990

Please sign in to comment.