Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

Commit

Permalink
Split model classes into separate submodule.
Browse files Browse the repository at this point in the history
- To avoid circular dependencies in the resource modules.
- Just import the models needed.
  • Loading branch information
mbarnes authored and ashcrow committed Feb 4, 2016
1 parent 4863ba5 commit fe5a46c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 85 deletions.
57 changes: 2 additions & 55 deletions src/commissaire/handlers/clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,10 @@
import etcd
import json

from commissaire.model import Model
from commissaire.resource import Resource
from commissaire.handlers.hosts import Host
from commissaire.jobs import POOLS, clusterexec


class Cluster(Model):
"""
Representation of a Cluster.
"""
_json_type = dict
_attributes = ('status',)

def __init__(self, **kwargs):
Model.__init__(self, **kwargs)
# Hosts is always calculated, not stored in etcd.
self.hosts = {'total': 0,
'available': 0,
'unavailable': 0}

# FIXME Generalize and move to Model?
def to_json_with_hosts(self):
data = {}
for key in self._attributes:
data[key] = getattr(self, key)
data['hosts'] = self.hosts
return json.dumps(data)


class ClusterRestart(Model):
"""
Representation of a Cluster restart operation.
"""

_json_type = dict
_attributes = (
'status', 'restarted', 'in_process',
'started_at', 'finished_at')


class ClusterUpgrade(Model):
"""
Representation of a Cluster upgrade operation.
"""

_json_type = dict
_attributes = (
'status', 'upgrade_to', 'upgraded', 'in_process',
'started_at', 'finished_at')


class Clusters(Model):
"""
Representation of a group of one or more Clusters.
"""
_json_type = list
_attributes = ('clusters',)
from commissaire.handlers.models import (
Cluster, Clusters, ClusterRestart, ClusterUpgrade, Host)


class ClustersResource(Resource):
Expand Down
21 changes: 1 addition & 20 deletions src/commissaire/handlers/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,9 @@
import etcd
import json

from commissaire.model import Model
from commissaire.queues import INVESTIGATE_QUEUE
from commissaire.resource import Resource


class Host(Model):
"""
Representation of a Host.
"""
_json_type = dict
_attributes = (
'address', 'status', 'os', 'cpus', 'memory',
'space', 'last_check', 'ssh_priv_key')
_hidden_attributes = ('ssh_priv_key', )


class Hosts(Model):
"""
Representation of a group of one or more Hosts.
"""
_json_type = list
_attributes = ('hosts', )
from commissaire.handlers.models import Cluster, Host, Hosts


class HostsResource(Resource):
Expand Down
102 changes: 102 additions & 0 deletions src/commissaire/handlers/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Models for handlers.
"""

import json

from commissaire.model import Model


class Cluster(Model):
"""
Representation of a Cluster.
"""
_json_type = dict
_attributes = ('status',)

def __init__(self, **kwargs):
Model.__init__(self, **kwargs)
# Hosts is always calculated, not stored in etcd.
self.hosts = {'total': 0,
'available': 0,
'unavailable': 0}

# FIXME Generalize and move to Model?
def to_json_with_hosts(self):
data = {}
for key in self._attributes:
data[key] = getattr(self, key)
data['hosts'] = self.hosts
return json.dumps(data)


class ClusterRestart(Model):
"""
Representation of a Cluster restart operation.
"""

_json_type = dict
_attributes = (
'status', 'restarted', 'in_process',
'started_at', 'finished_at')


class ClusterUpgrade(Model):
"""
Representation of a Cluster upgrade operation.
"""

_json_type = dict
_attributes = (
'status', 'upgrade_to', 'upgraded', 'in_process',
'started_at', 'finished_at')


class Clusters(Model):
"""
Representation of a group of one or more Clusters.
"""
_json_type = list
_attributes = ('clusters',)


class Host(Model):
"""
Representation of a Host.
"""
_json_type = dict
_attributes = (
'address', 'status', 'os', 'cpus', 'memory',
'space', 'last_check', 'ssh_priv_key')
_hidden_attributes = ('ssh_priv_key', )


class Hosts(Model):
"""
Representation of a group of one or more Hosts.
"""
_json_type = list
_attributes = ('hosts', )


class Status(Model):
"""
Representation of a Host.
"""
_json_type = dict
_attributes = (
'etcd', 'investigator', 'clusterexecpool')
11 changes: 1 addition & 10 deletions src/commissaire/handlers/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,9 @@
import falcon
import etcd

from commissaire.model import Model
from commissaire.jobs import POOLS
from commissaire.resource import Resource


class Status(Model):
"""
Representation of a Host.
"""
_json_type = dict
_attributes = (
'etcd', 'investigator', 'clusterexecpool')
from commissaire.handlers.models import Status


class StatusResource(Resource):
Expand Down

0 comments on commit fe5a46c

Please sign in to comment.