Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added minimal cluster support #331

Merged
merged 15 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ These are the contributors to pylxd according to the Github repository.
chrismacnaughton Chris MacNaughton
ppkt Karol Werner
mrtc0 Kohei Morita
felix-engelmann Felix Engelmann
=============== ==================================

41 changes: 41 additions & 0 deletions integration/test_cluster_members.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2016 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from integration.testing import IntegrationTestCase


class ClusterMemberTestCase(IntegrationTestCase):

def setUp(self):
super(ClusterMemberTestCase, self).setUp()

if not self.client.has_api_extension('clustering'):
self.skipTest('Required LXD API extension not available!')


class TestClusterMembers(ClusterMemberTestCase):
"""Tests for `Client.cluster_members.`"""

def test_get(self):
"""A cluster member is fetched by its name."""

members = self.client.cluster.members.all()

random_member_name = "%s" % members[0].name
random_member_url = "%s" % members[0].url

member = self.client.cluster.members.get(random_member_name)

new_url = "%s" % member.url
self.assertEqual(random_member_url, new_url)
8 changes: 8 additions & 0 deletions pylxd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ def get(self, *args, **kwargs):
def post(self, *args, **kwargs):
"""Perform an HTTP POST."""
kwargs['timeout'] = kwargs.get('timeout', self._timeout)
target = kwargs.pop("target", None)

if target is not None:
params = kwargs.get("params", {})
params["target"] = target
kwargs["params"] = params

response = self.session.post(self._api_endpoint, *args, **kwargs)
# Prior to LXD 2.0.3, successful synchronous requests returned 200,
# rather than 201.
Expand Down Expand Up @@ -296,6 +303,7 @@ def __init__(
requests.exceptions.InvalidURL):
raise exceptions.ClientConnectionFailed()

self.cluster = managers.ClusterManager(self)
self.certificates = managers.CertificateManager(self)
self.containers = managers.ContainerManager(self)
self.images = managers.ImageManager(self)
Expand Down
14 changes: 14 additions & 0 deletions pylxd/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ class StoragePoolManager(BaseManager):
manager_for = 'pylxd.models.StoragePool'


class ClusterMemberManager(BaseManager):
manager_for = 'pylxd.models.ClusterMember'


class ClusterManager(BaseManager):

manager_for = 'pylxd.models.Cluster'

def __init__(self, client, *args, **kwargs):
super(ClusterManager, self).__init__(client, *args, **kwargs)
self._client = client
self.members = ClusterMemberManager(client)


@contextmanager
def web_socket_manager(manager):
try:
Expand Down
1 change: 1 addition & 0 deletions pylxd/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pylxd.models.cluster import (Cluster, ClusterMember) # NOQA
from pylxd.models.certificate import Certificate # NOQA
from pylxd.models.container import Container, Snapshot # NOQA
from pylxd.models.image import Image # NOQA
Expand Down
3 changes: 3 additions & 0 deletions pylxd/models/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def __getattribute__(self, name):
try:
return super(Model, self).__getattribute__(name)
except AttributeError:
print(self.__attributes__)
print(self.__slots__)
if name in self.__attributes__:
self.sync()
return super(Model, self).__getattribute__(name)
Expand Down Expand Up @@ -153,6 +155,7 @@ def sync(self, rollback=False):
# on existing attributes.
response = self.api.get()
payload = response.json()['metadata']
print(payload)
for key, val in payload.items():
if key not in self.__dirty__ or rollback:
try:
Expand Down
79 changes: 79 additions & 0 deletions pylxd/models/cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright (c) 2016 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from pylxd.models import _model as model
from pylxd import managers


class Cluster(model.Model):
"""An LXD Cluster.
"""

server_name = model.Attribute()
enabled = model.Attribute()
member_config = model.Attribute()

members = model.Manager()

def __init__(self, *args, **kwargs):
super(Cluster, self).__init__(*args, **kwargs)
self.members = managers.ClusterMemberManager(self.client, self)

@property
def api(self):
return self.client.api.cluster

@classmethod
def get(cls, client, *args):
"""Get cluster details"""
print(args)
response = client.api.cluster.get()
print(response.json())
container = cls(client, **response.json()['metadata'])
return container


class ClusterMember(model.Model):
"""A LXD cluster member."""

name = model.Attribute(readonly=True)
felix-engelmann marked this conversation as resolved.
Show resolved Hide resolved
url = model.Attribute(readonly=True)
database = model.Attribute(readonly=True)
state = model.Attribute(readonly=True)
server_name = model.Attribute(readonly=True)
status = model.Attribute(readonly=True)
message = model.Attribute(readonly=True)

cluster = model.Parent()

@classmethod
def get(cls, client, name):
"""Get a cluster member by name."""
response = client.api.cluster.members[name].get()

return cls(client, **response.json()['metadata'])
felix-engelmann marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def all(cls, client, *args):
"""Get all certificates."""
response = client.api.cluster.members.get()

nodes = []
for node in response.json()['metadata']:
name = node.split('/')[-1]
nodes.append(cls(client, server_name=name))
return nodes

@property
def api(self):
return self.client.api.cluster.members[self.server_name]
4 changes: 2 additions & 2 deletions pylxd/models/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ def all(cls, client):
return containers

@classmethod
def create(cls, client, config, wait=False):
def create(cls, client, config, wait=False, target=None):
"""Create a new container config."""
response = client.api.containers.post(json=config)
response = client.api.containers.post(json=config, target=target)

if wait:
client.operations.wait_for_operation(response.json()['operation'])
Expand Down
53 changes: 53 additions & 0 deletions pylxd/tests/mock_lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,31 @@ def snapshot_DELETE(request, context):
},


# Cluster Members
{
'text': json.dumps({
'type': 'sync',
'metadata': [
'http://pylxd.test/1.0/certificates/an-member',
'http://pylxd.test/1.0/certificates/nd-member',
]}),
'method': 'GET',
'url': r'^http://pylxd.test/1.0/cluster/members$',
},
{
'text': json.dumps({
'type': 'sync',
'metadata': {
"name": "an-member",
"url": "https://10.1.1.101:8443",
"database": "true",
"state": "Online",
}}),
'method': 'GET',
'url': r'^http://pylxd.test/1.0/cluster/members/an-member$', # NOQA
},


# Containers
{
'text': json.dumps({
Expand All @@ -212,6 +237,11 @@ def snapshot_DELETE(request, context):
'method': 'POST',
'url': r'^http://pylxd.test/1.0/containers$',
},
{
'text': containers_POST,
'method': 'POST',
'url': r'^http://pylxd.test/1.0/containers\?target=an-remote',
},
{
'json': {
'type': 'sync',
Expand Down Expand Up @@ -293,6 +323,29 @@ def snapshot_DELETE(request, context):
'method': 'GET',
'url': r'^http://pylxd.test/1.0/containers/an-container/state$', # NOQA
},
{
'json': {
'type': 'sync',
'metadata': {
'name': 'an-new-remote-container',

'architecture': "x86_64",
'config': {
'security.privileged': "true",
},
'created_at': "1983-06-16T00:00:00-00:00",
'last_used_at': "1983-06-16T00:00:00-00:00",
'description': "Some description",
'location': "an-remote",
'status': "Running",
'status_code': 103,
'unsupportedbypylxd': "This attribute is not supported by "\
"pylxd. We want to test whether the mere presence of it "\
"makes it crash."
}},
'method': 'GET',
'url': r'^http://pylxd.test/1.0/containers/an-new-remote-container$',
},
{
'status_code': 202,
'json': {
Expand Down
31 changes: 31 additions & 0 deletions pylxd/tests/models/test_cluster_member.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2016 Canonical Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from pylxd.tests import testing


class TestClusterMember(testing.PyLXDTestCase):
"""Tests for pylxd.models.ClusterMember."""

def test_get(self):
"""A cluster member is retrieved."""
member = self.client.cluster.members.get('an-member')

self.assertEqual('https://10.1.1.101:8443', member.url)

def test_all(self):
"""All cluster members are returned."""
members = self.client.cluster.members.all()

self.assertIn('an-member', [m.server_name for m in members])
10 changes: 10 additions & 0 deletions pylxd/tests/models/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ def test_create(self):

self.assertEqual(config['name'], an_new_container.name)

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

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

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

def test_exists(self):
"""A container exists."""
name = 'an-container'
Expand Down