From 5c641643d4b889cbac0175fbd3b40da5b713df13 Mon Sep 17 00:00:00 2001 From: mbasnight Date: Wed, 22 Feb 2012 11:59:59 -0600 Subject: [PATCH] Making a data/view model based on remote objects * removing the novaclient from the actual service --- reddwarf/database/models.py | 92 ++++++++++++++++++++++++++++++++++++ reddwarf/database/service.py | 18 +++++-- reddwarf/database/views.py | 43 +++++++++++++++++ 3 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 reddwarf/database/models.py create mode 100644 reddwarf/database/views.py diff --git a/reddwarf/database/models.py b/reddwarf/database/models.py new file mode 100644 index 0000000000..a352d9dfc8 --- /dev/null +++ b/reddwarf/database/models.py @@ -0,0 +1,92 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010-2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +"""Model classes that form the core of instances functionality.""" + +import logging +import netaddr +from reddwarf.common import config +from novaclient.v1_1.client import Client + +LOG = logging.getLogger('reddwarf.database.models') + +PROXY_ADMIN_USER = config.Config.get('reddwarf_proxy_admin_user', 'admin') +PROXY_ADMIN_PASS = config.Config.get('reddwarf_proxy_admin_pass', '3de4922d8b6ac5a1aad9') +PROXY_ADMIN_TENANT_NAME = config.Config.get('reddwarf_proxy_admin_tenant_name', 'admin') +PROXY_AUTH_URL = config.Config.get('reddwarf_auth_url', 'http://0.0.0.0:5000/v2.0') + +class ModelBase(object): + + _data_fields = [] + _auto_generated_attrs = [] + + def _validate(self): + pass + + def data(self, **options): + data_fields = self._data_fields + self._auto_generated_attrs + return dict([(field, self[field]) for field in data_fields]) + + +class RemoteModelBase(ModelBase): + + # This should be set by the remote model during init time + # The data() method will be using this + _data_object = None + + @classmethod + def get_client(cls, proxy_token): + client = Client(PROXY_ADMIN_USER, PROXY_ADMIN_PASS, + PROXY_ADMIN_TENANT_NAME, PROXY_AUTH_URL, token=proxy_token) + client.authenticate() + return client + + def data_item(self, data_object): + data_fields = self._data_fields + self._auto_generated_attrs + return dict([(field, getattr(data_object,field)) for field in data_fields]) + + # data magic that will allow for a list of _data_object or a single item + # if the object is a list, it will turn it into a list of hash's again + def data(self, **options): + if self._data_object is None: + raise LookupError("data object is None") + if isinstance(self._data_object, list): + return [self.data_item(item) for item in self._data_object] + else: + return self.data_item(self._data_object) + + +class Instance(RemoteModelBase): + + _data_fields = ['name', 'status', 'updated', 'id', 'flavor'] + + def __init__(self, proxy_token, uuid): + self._data_object = self.get_client(proxy_token).servers.get(uuid) + + +class Instances(Instance): + + def __init__(self, proxy_token): + self._data_object = self.get_client(proxy_token).servers.list() + + def __iter__(self): + for item in self._data_object: + yield item + + +class DatabaseModelBase(ModelBase): + _auto_generated_attrs = ["id", "created_at", "updated_at"] \ No newline at end of file diff --git a/reddwarf/database/service.py b/reddwarf/database/service.py index 8e0aedabe5..22c5fd5f96 100644 --- a/reddwarf/database/service.py +++ b/reddwarf/database/service.py @@ -22,6 +22,8 @@ import webob.exc from novaclient.v1_1.client import Client from reddwarf.common import config from reddwarf.common import wsgi +from reddwarf.database import models +from reddwarf.database import views LOG = logging.getLogger('reddwarf.database.service') @@ -35,7 +37,6 @@ class BaseController(wsgi.Controller): self.proxy_admin_tenant_name = config.Config.get('reddwarf_proxy_admin_tenant_name', 'admin') self.auth_url = config.Config.get('reddwarf_auth_url', 'http://0.0.0.0:5000/v2.0') - def get_client(self, req): proxy_token = req.headers["X-Auth-Token"] client = Client(self.proxy_admin_user, self.proxy_admin_pass, @@ -48,9 +49,18 @@ class InstanceController(BaseController): def index(self, req, tenant_id): """Return all instances.""" - LOG.info("in index!") - LOG.info(self.get_client(req).servers.list()) - return "Im in index!" + servers = models.Instances(req.headers["X-Auth-Token"]).data() + return wsgi.Result(views.InstancesView(servers).data(), 201) + + def show(self, req, tenant_id, id): + """Return a single instance.""" + server = models.Instance(req.headers["X-Auth-Token"], id).data() + return wsgi.Result(views.InstanceView(server).data(), 201) + + def create(self, req, body, tenant_id): + server = self.get_client(req).servers.create(body['name'], body['image'], body['flavor']) + LOG.info(server) + return "server created %s" % server.__dict__ class API(wsgi.Router): diff --git a/reddwarf/database/views.py b/reddwarf/database/views.py new file mode 100644 index 0000000000..ca653643a8 --- /dev/null +++ b/reddwarf/database/views.py @@ -0,0 +1,43 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +class InstanceView(object): + + def __init__(self, instance): + self.instance = instance + + #TODO(hub-cap): fix the link generation + def data(self): + return {"instance": { + "id": self.instance['id'], + "name": self.instance['name'], + "links": "Links will be coming in the future" + }, + } + +class InstancesView(object): + + def __init__(self, instances): + self.instances = instances + + def data(self): + data = [] + # These are model instances + for instance in self.instances: + data.append(InstanceView(instance).data()) + + return data \ No newline at end of file