Skip to content

Commit

Permalink
Quick/dirty cluster.members implementation
Browse files Browse the repository at this point in the history
This is a quick and dirty implementation of cluster and
cluster.members endpoints.  There would still be work to do:

 * tests
 * enables 'members' from the cluster object.

It's a bit of a hack, and could probably be improved!
  • Loading branch information
ajkavanagh committed Dec 11, 2018
1 parent e6304b8 commit 755185a
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 57 deletions.
4 changes: 1 addition & 3 deletions pylxd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ def __getattr__(self, name):
# Special case for storage_pools which needs to become 'storage-pools'
if name == 'storage_pools':
name = 'storage-pools'
if name == 'cluster_members':
name = 'cluster/members'
return self.__class__('{}/{}'.format(self._api_endpoint, name),
cert=self.session.cert,
verify=self.session.verify)
Expand Down Expand Up @@ -305,7 +303,7 @@ def __init__(
requests.exceptions.InvalidURL):
raise exceptions.ClientConnectionFailed()

self.cluster_members = managers.ClusterMemberManager(self)
self.cluster = managers.ClusterManager(self)
self.certificates = managers.CertificateManager(self)
self.containers = managers.ContainerManager(self)
self.images = managers.ImageManager(self)
Expand Down
22 changes: 18 additions & 4 deletions pylxd/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import importlib
import inspect

from pylxd import managers


class BaseManager(object):
"""A BaseManager class for handling collection operations."""
Expand All @@ -27,10 +29,6 @@ def __init__(self, *args, **kwargs):
return super(BaseManager, self).__init__()


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


class CertificateManager(BaseManager):
manager_for = 'pylxd.models.Certificate'

Expand All @@ -39,6 +37,22 @@ class ContainerManager(BaseManager):
manager_for = 'pylxd.models.Container'


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 = managers.ClusterMemberManager(client)

@property
def members(self):
return self._members

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


class ImageManager(BaseManager):
manager_for = 'pylxd.models.Image'

Expand Down
2 changes: 1 addition & 1 deletion pylxd/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pylxd.models.cluster_member import ClusterMember # NOQA
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
1 change: 1 addition & 0 deletions pylxd/models/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,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
74 changes: 74 additions & 0 deletions pylxd/models/cluster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from pylxd import managers
from pylxd.exceptions import LXDAPIException
from pylxd.models import _model as model

class Cluster(model.Model):
"""An LXD Container.
This class is not intended to be used directly, but rather to be used
via `Client.containers.create`.
"""

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 certificate."""

server_name = model.Attribute(readonly=True)
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()

@property
def api(self):
return self.client.api.cluster.members[self.server_name]

@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'])

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

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

48 changes: 0 additions & 48 deletions pylxd/models/cluster_member.py

This file was deleted.

2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[metadata]
name = pylxd
summary = python library for lxd
version = 2.2.8
version = 2.2.9
description-file =
README.rst
author = Paul Hummer and others (see CONTRIBUTORS.rst)
Expand Down

0 comments on commit 755185a

Please sign in to comment.