get_all resourcetype query filter across drivers

Change-Id: I31bafcf41fd27c20a092bdc531b94db26aae9c90
This commit is contained in:
Lakshmi N Sampath 2014-05-07 14:34:02 -07:00
parent f57bf579fc
commit e6ab5303b2
7 changed files with 140 additions and 25 deletions

View File

@ -13,15 +13,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import pecan
from pecan.rest import RestController
from wsme.api import Response
from wsme.rest.json import fromjson
from wsmeext.pecan import wsexpose
from graffiti.api.model.v1.resource import Resource
from graffiti.api.model.v1.resource_query import ResourceQuery
from graffiti.common import driver_factory
from graffiti.common.exception import DriverNotFound
from graffiti.common.exception import DriverNotFoundForResourceType
import six
@ -94,25 +101,50 @@ class ResourceController(RestController):
@wsexpose([Resource], six.text_type, six.text_type)
def get_all(self, resource_type=None, query_string=None):
print "args: resource_type=%s, query_string=%s" % \
(resource_type, query_string)
auth_token = pecan.request.headers.get('X-Auth-Token')
if not resource_type:
resource_type = self.default_resource_type
driver = driver_factory.get_driver(resource_type)
if driver.resource:
res_list = driver.resource.find_resources(
query_string,
auth_token
)
if res_list:
return res_list.itervalues()
resource_types = []
if query_string:
doc = json.loads(query_string)
resource_query = fromjson(ResourceQuery, doc)
resource_types = resource_query.resource_types
else:
resource = Response(
Resource(),
status_code=404,
error="Driver not found for the resource type")
return resource
if not resource_type:
resource_types.append(self.default_resource_type)
driver_resources = ResourceController.__group_resource_types_by_driver(
driver_factory.get_resource_types(),
resource_types
)
all_resource_list = []
for driver_name in driver_resources.keys():
req_resource_types = driver_resources[driver_name]
resource_query = ResourceQuery()
resource_query.resource_types = req_resource_types
try:
print "Invoking driver(%s) for resource types(%s):" % \
(driver_name, req_resource_types)
driver = driver_factory.get_driver_by_name(driver_name)
except DriverNotFound:
resource = Response(
Resource(),
status_code=404,
error="Driver not found for the resource type")
return resource
if driver.resource:
res_list = driver.resource.find_resources(
resource_query,
auth_token
)
if res_list:
all_resource_list += res_list.values()
if all_resource_list:
return all_resource_list
return []
@ -166,3 +198,30 @@ class ResourceController(RestController):
)
return resource
@staticmethod
def __group_resource_types_by_driver(
all_resource_types,
request_resource_types):
driver_resource_types = dict()
for resource_type in request_resource_types:
if resource_type in all_resource_types.keys():
driver_name = all_resource_types[resource_type]
else:
raise DriverNotFoundForResourceType(
resource_type=resource_type
)
resource_list = []
if driver_name in driver_resource_types.keys():
resource_list = driver_resource_types[driver_name]
if not resource_list:
resource_list = []
resource_list.append(resource_type)
driver_resource_types[driver_name] = resource_list
else:
resource_list.append(resource_type)
driver_resource_types[driver_name] = resource_list
return driver_resource_types

View File

@ -30,7 +30,7 @@ class DBResourceDAO(ResourceDAOBase):
def get_resource(self, id):
pass
def find_resources(self, query_string):
def find_resources(self, resource_query):
pass
def set_resource(self, id=None, resource_definition=None):

View File

@ -0,0 +1,26 @@
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# 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.
import wsme
from wsme import types
class ResourceQuery(types.Base):
resource_types = wsme.wsattr([types.text], mandatory=False)
#_wsme_attr_order = ('resource_types')
def __init__(self, **kwargs):
super(ResourceQuery, self).__init__(**kwargs)

View File

@ -63,6 +63,36 @@ def get_driver(resource_type):
raise exception.DriverNotFound(driver_name=driver_name)
def get_driver_by_name(driver_name):
"""Simple method to get a ref to an instance of a driver by the
name.
Driver loading is handled by the DriverFactory class. This method
conveniently wraps that class and returns the actual driver object.
:param driver_name: name of the registered driver
:returns: An instance of a class which implements
graffiti.drivers.base.BaseResourceDriver
:raises: DriverNotFound if the requested driver_name could not be
found in the "graffiti.drivers" namespace.
"""
try:
factory = DriverFactory()
return factory[driver_name].obj
except KeyError:
raise exception.DriverNotFound(driver_name=driver_name)
def get_resource_types():
"""Returns a dictionary of resource type and driver name
:returns:dictionary with resource type as key and driver name
as its value
"""
return DriverFactory()._resource_types
class DriverFactory(object):
"""Discover, load and manage the drivers available."""

View File

@ -62,10 +62,10 @@ class ResourceInterface(object):
"""
@abc.abstractmethod
def find_resources(self, query_string, auth_token,
def find_resources(self, resource_query, auth_token,
endpoint_id=None, **kwargs):
"""Find resources matching the query
:param query_string: query expression
:param resource_query: query object
:param auth_token: keystone auth_token of request user
:param endpoint_id: id for locating the cloud resource provider
:param **kwargs: Include additional info required by the driver,

View File

@ -103,17 +103,17 @@ class GlanceResourceDriver(base.ResourceInterface):
image = glance_client.images.get(resource.id)
image.update(properties=image_properties, purge_props=False)
def find_resources(self, query_string, auth_token,
def find_resources(self, resource_query, auth_token,
endpoint_id=None, **kwargs):
"""Find resources matching the query
:param query_string: query expression. Include resource type(s)
:param resource_query: query object. Includes resource type(s)
:param auth_token: keystone auth_token of request user
:param endpoint_id: id for locating the cloud resource provider
:param **kwargs: Include additional info required by the driver,
:returns list of resources
"""
resource_list = dict()
if not query_string:
if resource_query:
glance_client = self.__get_glance_client(endpoint_id, auth_token)
images = glance_client.images.list()
for image in list(images):

View File

@ -43,16 +43,16 @@ class LocalResourceDriver(base.ResourceInterface):
res = self._resource_dao.get_resource(resource_id)
return res
def find_resources(self, query_string, auth_token,
def find_resources(self, resource_query, auth_token,
endpoint_id=None, **kwargs):
"""Find resources matching the query
:param query_string: query expression. Include resource type(s)
:param resource_query: query object. Includes resource type(s)
:param auth_token: keystone auth_token of request user
:param endpoint_id: id for locating the cloud resource provider
:param **kwargs: Include additional info required by the driver,
:returns list of resources
"""
res_list = self._resource_dao.find_resources(query_string)
res_list = self._resource_dao.find_resources(resource_query)
if res_list:
return res_list