Graffiti API

Change-Id: I23dd8bd550e5e6b9be0b6356549e576c473250b7
This commit is contained in:
Lakshmi N Sampath 2014-03-11 15:25:24 -07:00
parent d00ab732ae
commit 85a25f5481
20 changed files with 521 additions and 0 deletions

0
graffiti/api/__init__.py Normal file
View File

41
graffiti/api/app.py Normal file
View File

@ -0,0 +1,41 @@
# 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.
from pecan import make_app
from graffiti import model
from graffiti.service import prepare_service
from graffiti.hooks import CorsHook
from oslo.config import cfg
CONF = cfg.CONF
def setup_app(config):
model.init_model()
app_conf = dict(config.app)
prepare_service()
app_hooks = [CorsHook()]
return make_app(
app_conf.pop('root'),
logging=getattr(config, 'logging', {}),
hooks=app_hooks,
**app_conf
)

View File

View File

@ -0,0 +1,33 @@
# 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.
from pecan import expose
from webob.exc import status_map
from graffiti.controllers.versions import V1Controller
class RootController(object):
v1 = V1Controller()
@expose('error.html')
def error(self, status):
try:
status = int(status)
except ValueError: # pragma: no cover
status = 500
message = getattr(status_map.get(status), 'explanation', '')
return dict(status=status, message=message)

View File

View File

@ -0,0 +1,59 @@
# 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.
from pecan.rest import RestController
from wsme.api import Response
from wsmeext.pecan import wsexpose
from graffiti.model.v1.resource import Resource
import six
resources = []
class ResourceController(RestController):
def __init__(self):
super(ResourceController, self).__init__()
self.status = 200
@wsexpose()
def options():
pass
@wsexpose(Resource, six.text_type)
def get_one(self, id):
global resources
for res in resources:
if res.id.lower() == id.lower():
return res
res = Response(Resource(), status_code=404, error="Resource Not Found")
return res
@wsexpose([Resource])
def get_all(self):
global resources
return resources
@wsexpose(Resource, Resource)
def post(self, resource):
global resources
resources.append(resource)

View File

@ -0,0 +1,21 @@
# 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.
from graffiti.controllers.v1.resource import ResourceController
class V1Controller(object):
resource = ResourceController()

36
graffiti/api/hooks.py Normal file
View File

@ -0,0 +1,36 @@
# 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 json
from pecan.hooks import PecanHook
class CorsHook(PecanHook):
def after(self, state):
state.response.headers['Access-Control-Allow-Origin'] = '*'
state.response.headers['Access-Control-Allow-Methods'] = \
'GET, PUT, POST, DELETE, OPTIONS'
state.response.headers['Access-Control-Allow-Headers'] = \
'origin, authorization, accept, content-type'
if not state.response.headers['Content-Length']:
state.response.headers['Content-Length'] = \
str(len(state.response.body))
if state.response.headers['Content-Type'].find('json') != -1:
# Sort the Response Body's JSON
json_str = json.loads(state.response.body)
state.response.body = json.dumps(json_str, sort_keys=True)

View File

@ -0,0 +1,18 @@
# 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.
def init_model():
pass

View File

View File

@ -0,0 +1,31 @@
# 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
from graffiti.model.v1.property import Property
class Capability(types.Base):
properties = wsme.wsattr([Property], mandatory=True)
capability_type = wsme.wsattr(types.text, mandatory=True)
capability_type_namespace = wsme.wsattr(types.text, mandatory=True)
_wsme_attr_order = ('properties', 'capability_type',
'capability_type_namespace')
def __init__(self, **kwargs):
super(Capability, self).__init__(**kwargs)

View File

@ -0,0 +1,27 @@
# 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 Property(types.Base):
name = wsme.wsattr(types.text, mandatory=True)
value = wsme.wsattr(types.text, mandatory=True)
_wsme_attr_order = ('name', 'value')
def __init__(self, **kwargs):
super(Property, self).__init__(**kwargs)

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 Provider(types.Base):
id = wsme.wsattr(types.text, mandatory=True)
_wsme_attr_order = ('id',)
def __init__(self, **kwargs):
super(Provider, self).__init__(**kwargs)

View File

@ -0,0 +1,29 @@
# 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 Requirement(types.Base):
criterion = wsme.wsattr(types.text, mandatory=True)
capability_type = wsme.wsattr(types.text, mandatory=True)
capability_type_namespace = wsme.wsattr(types.text, mandatory=True)
_wsme_attr_order = ('criterion', 'capability_type',
'capability_type_namespace')
def __init__(self, **kwargs):
super(Requirement, self).__init__(**kwargs)

View File

@ -0,0 +1,40 @@
# 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
from graffiti.model.v1.capability import Capability
from graffiti.model.v1.property import Property
from graffiti.model.v1.provider import Provider
from graffiti.model.v1.requirement import Requirement
class Resource(types.Base):
id = wsme.wsattr(types.text, mandatory=True)
type = wsme.wsattr(types.text, mandatory=True)
name = wsme.wsattr(types.text, mandatory=True)
description = wsme.wsattr(types.text, mandatory=False)
provider = wsme.wsattr(Provider, mandatory=True)
properties = wsme.wsattr([Property], mandatory=False)
capabilities = wsme.wsattr([Capability], mandatory=True)
requirements = wsme.wsattr([Requirement], mandatory=True)
_wsme_attr_order = ('id', 'name', 'description', 'type',
'provider', 'properties', 'capabilities',
'requirements')
def __init__(self, **kwargs):
super(Resource, self).__init__(**kwargs)

25
graffiti/api/service.py Normal file
View File

@ -0,0 +1,25 @@
# 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 sys
from oslo.config import cfg
def prepare_service(argv=None):
if argv is None:
argv = sys.argv
cfg.CONF(argv[3:], project='graffiti')

View File

@ -0,0 +1,14 @@
# 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.

View File

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
# Copyright 2010-2011 OpenStack Foundation
# Copyright (c) 2013 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 os
import fixtures
import testtools
_TRUE_VALUES = ('True', 'true', '1', 'yes')
class TestCase(testtools.TestCase):
"""Test case base class for all unit tests."""
def setUp(self):
"""Run before each test method to initialize test environment."""
super(TestCase, self).setUp()
test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
try:
test_timeout = int(test_timeout)
except ValueError:
# If timeout value is invalid do not set a timeout.
test_timeout = 0
if test_timeout > 0:
self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
self.useFixture(fixtures.NestedTempfile())
self.useFixture(fixtures.TempHomeDir())
if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES:
stdout = self.useFixture(fixtures.StringStream('stdout')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES:
stderr = self.useFixture(fixtures.StringStream('stderr')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
self.log_fixture = self.useFixture(fixtures.FakeLogger())

View File

@ -0,0 +1,39 @@
# 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.
"""
test_controller_v1
----------------------------------
Tests for `graffiti` module.
"""
from graffiti.tests import base
from graffiti.controllers.root import RootController
from graffiti.controllers.versions import V1Controller
class TestControllerV1(base.TestCase):
def test_v1_exists(self):
root = RootController()
self.assertIn(hasattr(root, 'v1'), [True])
pass
def test_v1_resource_exists(self):
v1 = V1Controller()
self.assertIn(hasattr(v1, 'resource'), [True])
pass

View File

@ -0,0 +1,29 @@
# 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.
"""
test_graffiti
----------------------------------
Tests for `graffiti` module.
"""
from graffiti.tests import base
class TestGraffiti(base.TestCase):
def test_something(self):
pass