Skip to content

Commit

Permalink
Merge "API Validation for Trove API"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Jul 3, 2013
2 parents 24a4259 + ccdf59f commit 4419cb1
Show file tree
Hide file tree
Showing 22 changed files with 1,170 additions and 266 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -18,3 +18,4 @@ python-keystoneclient
python-swiftclient
iso8601
oslo.config>=1.1.0
jsonschema>=1.0.0,!=1.4.0,<2
14 changes: 2 additions & 12 deletions trove/backup/service.py
Expand Up @@ -22,6 +22,7 @@
from trove.common import cfg
from trove.openstack.common import log as logging
from trove.openstack.common.gettextutils import _
import trove.common.apischema as apischema

CONF = cfg.CONF
LOG = logging.getLogger(__name__)
Expand All @@ -31,6 +32,7 @@ class BackupController(wsgi.Controller):
"""
Controller for accessing backups in the OpenStack API.
"""
schemas = apischema.backup

def index(self, req, tenant_id):
"""
Expand All @@ -51,7 +53,6 @@ def show(self, req, tenant_id, id):

def create(self, req, body, tenant_id):
LOG.debug("Creating a Backup for tenant '%s'" % tenant_id)
self._validate_create_body(body)
context = req.environ[wsgi.CONTEXT_KEY]
data = body['backup']
instance = data['instance']
Expand All @@ -65,14 +66,3 @@ def delete(self, req, tenant_id, id):
context = req.environ[wsgi.CONTEXT_KEY]
Backup.delete(context, id)
return wsgi.Result(None, 202)

def _validate_create_body(self, body):
try:
body['backup']
body['backup']['name']
body['backup']['instance']
except KeyError as e:
LOG.error(_("Create Backup Required field(s) "
"- %s") % e)
raise exception.TroveError(
"Required element/key - %s was not specified" % e)
312 changes: 312 additions & 0 deletions trove/common/apischema.py
@@ -0,0 +1,312 @@
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# 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.
#

flavorref = {
'oneOf': [
{
"type": "string",
"minLength": 8,
"pattern": 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]'
'|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
},
{
"type": "string",
"maxLength": 5,
"pattern": "[0-9]+"
},
{
"type": "integer"
}]
}

volume_size = {
"oneOf": [
{
"type": "integer",
"minimum": 0
},
{
"type": "string",
"minLength": 1,
"pattern": "[0-9]+"
}]
}

non_empty_string = {
"type": "string",
"minLength": 1,
"maxLength": 255,
"pattern": "^.*[0-9a-zA-Z]+.*$"
}

uuid = {
"type": "string",
"minLength": 1,
"maxLength": 64,
"pattern": "^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}"
"-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$"
}

volume = {
"type": "object",
"required": ["size"],
"properties": {
"size": volume_size,
"required": True
}
}


databases_ref_list = {
"type": "array",
"minItems": 0,
"uniqueItems": True,
"items": {
"type": "object",
"required": ["name"],
"additionalProperties": False,
"properties": {
"name": non_empty_string
}
}
}

databases_ref_list_required = {
"type": "array",
"minItems": 1,
"uniqueItems": True,
"items": {
"type": "object",
"required": ["name"],
"additionalProperties": False,
"properties": {
"name": non_empty_string
}
}
}

databases_ref = {
"type": "object",
"required": ["databases"],
"additionalProperties": False,
"properties": {
"databases": databases_ref_list_required
}
}

databases_def = {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["name"],
"additionalProperties": False,
"properties": {
"name": non_empty_string,
"character_set": non_empty_string,
"collate": non_empty_string
}
}
}

users_list = {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["name", "password"],
"additionalProperties": False,
"properties": {
"name": non_empty_string,
"password": non_empty_string,
"host": non_empty_string,
"databases": databases_ref_list
}
}
}

instance = {
"create": {
"type": "object",
"required": ["instance"],
"additionalProperties": False,
"properties": {
"instance": {
"type": "object",
"required": ["name", "flavorRef", "volume"],
"additionalProperties": False,
"properties": {
"name": non_empty_string,
"flavorRef": flavorref,
"volume": volume,
"databases": databases_def,
"users": users_list,
"restorePoint": {
"type": "object",
"required": ["backupRef"],
"additionalProperties": False,
"properties": {
"backupRef": uuid
}
}
}
}
}
},
"action": {
"resize": {
"volume": {
"type": "object",
"required": ["resize"],
"additionalProperties": False,
"properties": {
"resize": {
"type": "object",
"required": ["volume"],
"additionalProperties": False,
"properties": {
"volume": volume
}
}
}
},
'flavorRef': {
"type": "object",
"required": ["resize"],
"additionalProperties": False,
"properties": {
"resize": {
"type": "object",
"required": ["flavorRef"],
"additionalProperties": False,
"properties": {
"flavorRef": flavorref
}
}
}
}
},
"restart": {
"type": "object",
"required": ["restart"],
"additionalProperties": False,
"properties": {
"restart": {
"type": "object"
}
}
}
}
}

mgmt_instance = {
"action": {
'migrate': {
"type": "object",
"required": ["migrate"],
"additionalProperties": False,
"properties": {
"migrate": {
"type": "object"
}
}
},
"reboot": {
"type": "object",
"required": ["reboot"],
"additionalProperties": False,
"properties": {
"reboot": {
"type": "object"
}
}
},
"stop": {
"type": "object",
"required": ["stop"],
"additionalProperties": False,
"properties": {
"stop": {
"type": "object"
}
}
}
}
}

user = {
"create": {
"name": "users:create",
"type": "object",
"required": ["users"],
"properties": {
"users": users_list
}
},
"update": {
"users": {
"type": "object",
"required": ["users"],
"additionalProperties": False,
"properties": {
"users": users_list
}
},
"databases": databases_ref
}
}

dbschema = {
"create": {
"type": "object",
"required": ["databases"],
"additionalProperties": False,
"properties": {
"databases": databases_def
}
}
}

backup = {
"create": {
"name": "backup:create",
"type": "object",
"required": ["backup"],
"properties": {
"backup": {
"type": "object",
"required": ["instance", "name"],
"properties": {
"description": non_empty_string,
"instance": uuid,
"name": non_empty_string
}
}
}
}
}

account = {
'create': {
"type": "object",
"name": "users",
"required": ["users"],
"additionalProperties": False,
"properties": {
"users": users_list
}
}
}

0 comments on commit 4419cb1

Please sign in to comment.