1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
# Copyright 2013 OpenStack LLC.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
# @author: Salvatore Orlando, VMware
20
from quantum.api import extensions
21
from quantum.api.v2 import attributes
22
from quantum.api.v2 import base
23
from quantum import context
24
from quantum.db import servicetype_db
25
from quantum import manager
26
from quantum.openstack.common import log as logging
27
from quantum.plugins.common import constants
30
LOG = logging.getLogger(__name__)
32
RESOURCE_NAME = "service-type"
33
COLLECTION_NAME = "%ss" % RESOURCE_NAME
34
SERVICE_ATTR = 'service_class'
35
PLUGIN_ATTR = 'plugin'
36
DRIVER_ATTR = 'driver'
37
EXT_ALIAS = RESOURCE_NAME
39
# Attribute Map for Service Type Resource
40
RESOURCE_ATTRIBUTE_MAP = {
42
'id': {'allow_post': False, 'allow_put': False,
44
'name': {'allow_post': True, 'allow_put': True,
45
'validate': {'type:string': None},
46
'is_visible': True, 'default': ''},
47
'default': {'allow_post': False, 'allow_put': False,
49
#TODO(salvatore-orlando): Service types should not have ownership
50
'tenant_id': {'allow_post': True, 'allow_put': False,
51
'required_by_policy': True,
53
'num_instances': {'allow_post': False, 'allow_put': False,
55
'service_definitions': {'allow_post': True, 'allow_put': True,
56
'is_visible': True, 'default': None,
57
'validate': {'type:service_definitions':
63
def set_default_svctype_id(original_id):
65
svctype_mgr = servicetype_db.ServiceTypeManager.get_instance()
66
# Fetch default service type - it must exist
67
res = svctype_mgr.get_service_types(context.get_admin_context(),
68
filters={'default': [True]})
73
def _validate_servicetype_ref(data, valid_values=None):
74
""" Verify the service type id exists """
76
svctype_mgr = servicetype_db.ServiceTypeManager.get_instance()
78
svctype_mgr.get_service_type(context.get_admin_context(),
80
except servicetype_db.ServiceTypeNotFound:
81
return _("The service type '%s' does not exist") % svc_type_id
84
def _validate_service_defs(data, valid_values=None):
85
""" Validate the list of service definitions. """
88
return _("No service type definition was provided. At least a "
89
"service type definition must be provided")
90
f_name = _validate_service_defs.__name__
93
# Do a copy of the original object so we can easily
94
# pop out stuff from it
95
svc_def_copy = svc_def.copy()
97
svc_name = svc_def_copy.pop(SERVICE_ATTR)
98
plugin_name = svc_def_copy.pop(PLUGIN_ATTR)
100
msg = (_("Required attributes missing in service "
101
"definition: %s") % svc_def)
102
LOG.error("%(f_name)s: %(msg)s", locals())
104
# Validate 'service' attribute
105
if not svc_name in constants.ALLOWED_SERVICES:
106
msg = (_("Service name '%s' unspecified "
107
"or invalid") % svc_name)
108
LOG.error("%(f_name)s: %(msg)s", locals())
110
# Validate 'plugin' attribute
112
msg = (_("Plugin name not specified in "
113
"service definition %s") % svc_def)
114
LOG.error("%(f_name)s: %(msg)s", locals())
116
# TODO(salvatore-orlando): This code will need to change when
117
# multiple plugins for each adv service will be supported
118
svc_plugin = manager.QuantumManager.get_service_plugins().get(
121
msg = _("No plugin for service '%s'") % svc_name
122
LOG.error("%(f_name)s: %(msg)s", locals())
124
if svc_plugin.get_plugin_name() != plugin_name:
125
msg = _("Plugin name '%s' is not correct ") % plugin_name
126
LOG.error("%(f_name)s: %(msg)s", locals())
128
# Validate 'driver' attribute (just check it's a string)
129
# FIXME(salvatore-orlando): This should be a list
130
# Note: using get() instead of pop() as pop raises if the
131
# key is not found, which might happen for the driver
132
driver = svc_def_copy.get(DRIVER_ATTR)
134
msg = attributes._validate_string(driver,)
137
del svc_def_copy[DRIVER_ATTR]
138
# Anything left - it should be an error
139
if len(svc_def_copy):
140
msg = (_("Unparseable attributes found in "
141
"service definition %s") % svc_def)
142
LOG.error("%(f_name)s: %(msg)s", locals())
145
LOG.exception(_("Exception while parsing service "
146
"definition:%s"), svc_def)
147
msg = (_("Was expecting a dict for service definition, found "
148
"the following: %s") % svc_def)
149
LOG.error("%(f_name)s: %(msg)s", locals())
152
return (_("%s: provided data are not iterable") %
153
_validate_service_defs.__name__)
155
attributes.validators['type:service_definitions'] = _validate_service_defs
156
attributes.validators['type:servicetype_ref'] = _validate_servicetype_ref
159
class Servicetype(object):
163
return _("Quantum Service Type Management")
170
def get_description(cls):
171
return _("API for retrieving and managing service types for "
172
"Quantum advanced services")
175
def get_namespace(cls):
176
return "http://docs.openstack.org/ext/quantum/service-type/api/v1.0"
179
def get_updated(cls):
180
return "2013-01-20T00:00:00-00:00"
183
def get_resources(cls):
184
""" Returns Extended Resource for service type management """
185
controller = base.create_resource(
186
COLLECTION_NAME, RESOURCE_NAME,
187
servicetype_db.ServiceTypeManager.get_instance(),
188
RESOURCE_ATTRIBUTE_MAP[COLLECTION_NAME])
189
return [extensions.ResourceExtension(COLLECTION_NAME,