~ubuntu-branches/ubuntu/raring/quantum/raring-proposed

« back to all changes in this revision

Viewing changes to quantum/extensions/servicetype.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Yolanda Robla, James Page, Maru Newby
  • Date: 2013-01-11 09:14:35 UTC
  • mfrom: (2.1.17)
  • Revision ID: package-import@ubuntu.com-20130111091435-vaup7dwmtmajy5oe
Tags: 2013.1~g2-0ubuntu1
[ Chuck Short ]
* New upstream version. 
* debian/patches/fix-quantum-configuration.patch: Refreshed.

[ Yolanda Robla ]
* debian/quantum-l3-agent.quantum-metadata-agent.upstart: Add
  upstart configuration for Metadata Agent.
* debian/quantum-l3-agent.install: Added quantum-ns-metadata-proxy,
  quantum-metadata-agent and metadata_agent.ini.
* debian/patches/fix-quantum-configuration.patch: Update rootwrap
  configuration in metadata_agent.ini file.
* debian/changelog: Updated package version
* d/p/fix-quantum-configuration.patch: refresh patches

[ James Page ]
* d/*.install: Install entry points from bin directory instead
  of easy-install ones generated during the package build process
  (LP: #1085038).
* d/control: Drop BD on python-dev-all; its not required.
* d/rules: Install multiple upstart configurations for quantum-l3-agent.
* d/control: Tidy package descriptions.
* d/*.postrm: Drop as debhelper will generate update-rc.d calls in
  maintainer scripts if required.
* d/quantum-common.postinst: Tweak permissions setting so that /etc/quantum
  is not owned/writable by the quantum user, ensure that /etc/quantum/rootwrap*
  is owned by root:root.
* d/*agent*.postinst: Dropped as permissions now correctly set in
  quantum-common.
* d/patches/fix-quantum-configuration.patch: Re-add dropped fixes rootwrap and
  sqlite defaults for all plugins.
* d/control: Added new BD on alembic (>= 0.4.1~), version python-mock >= 1.0b1.

[ Maru Newby ]
* debian/control: Remove unnecessary openvswitch-vswitch dependency
  from quantum-plugin-openvswitch (LP: #1076747).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
# Copyright 2013 OpenStack LLC.
 
3
# All Rights Reserved.
 
4
#
 
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
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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
 
15
#    under the License.
 
16
#
 
17
#    @author: Salvatore Orlando, VMware
 
18
#
 
19
 
 
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
 
28
 
 
29
 
 
30
LOG = logging.getLogger(__name__)
 
31
 
 
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
 
38
 
 
39
# Attribute Map for Service Type Resource
 
40
RESOURCE_ATTRIBUTE_MAP = {
 
41
    COLLECTION_NAME: {
 
42
        'id': {'allow_post': False, 'allow_put': False,
 
43
               'is_visible': True},
 
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,
 
48
                    'is_visible': True},
 
49
        #TODO(salvatore-orlando): Service types should not have ownership
 
50
        'tenant_id': {'allow_post': True, 'allow_put': False,
 
51
                      'required_by_policy': True,
 
52
                      'is_visible': True},
 
53
        'num_instances': {'allow_post': False, 'allow_put': False,
 
54
                          'is_visible': True},
 
55
        'service_definitions': {'allow_post': True, 'allow_put': True,
 
56
                                'is_visible': True, 'default': None,
 
57
                                'validate': {'type:service_definitions':
 
58
                                             None}}
 
59
    }
 
60
}
 
61
 
 
62
 
 
63
def set_default_svctype_id(original_id):
 
64
    if not 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]})
 
69
        return res[0]['id']
 
70
    return original_id
 
71
 
 
72
 
 
73
def _validate_servicetype_ref(data, valid_values=None):
 
74
    """ Verify the service type id exists """
 
75
    svc_type_id = data
 
76
    svctype_mgr = servicetype_db.ServiceTypeManager.get_instance()
 
77
    try:
 
78
        svctype_mgr.get_service_type(context.get_admin_context(),
 
79
                                     svc_type_id)
 
80
    except servicetype_db.ServiceTypeNotFound:
 
81
        return _("The service type '%s' does not exist") % svc_type_id
 
82
 
 
83
 
 
84
def _validate_service_defs(data, valid_values=None):
 
85
    """ Validate the list of service definitions. """
 
86
    try:
 
87
        if len(data) == 0:
 
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__
 
91
        for svc_def in data:
 
92
            try:
 
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()
 
96
                try:
 
97
                    svc_name = svc_def_copy.pop(SERVICE_ATTR)
 
98
                    plugin_name = svc_def_copy.pop(PLUGIN_ATTR)
 
99
                except KeyError:
 
100
                    msg = (_("Required attributes missing in service "
 
101
                             "definition: %s") % svc_def)
 
102
                    LOG.error("%(f_name)s: %(msg)s", locals())
 
103
                    return msg
 
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())
 
109
                    return msg
 
110
                # Validate 'plugin' attribute
 
111
                if not plugin_name:
 
112
                    msg = (_("Plugin name not specified in "
 
113
                             "service definition %s") % svc_def)
 
114
                    LOG.error("%(f_name)s: %(msg)s", locals())
 
115
                    return msg
 
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(
 
119
                    svc_name)
 
120
                if not svc_plugin:
 
121
                    msg = _("No plugin for service '%s'") % svc_name
 
122
                    LOG.error("%(f_name)s: %(msg)s", locals())
 
123
                    return msg
 
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())
 
127
                    return msg
 
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)
 
133
                if driver:
 
134
                    msg = attributes._validate_string(driver,)
 
135
                    if msg:
 
136
                        return msg
 
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())
 
143
                    return msg
 
144
            except TypeError:
 
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())
 
150
                return msg
 
151
    except TypeError:
 
152
        return (_("%s: provided data are not iterable") %
 
153
                _validate_service_defs.__name__)
 
154
 
 
155
attributes.validators['type:service_definitions'] = _validate_service_defs
 
156
attributes.validators['type:servicetype_ref'] = _validate_servicetype_ref
 
157
 
 
158
 
 
159
class Servicetype(object):
 
160
 
 
161
    @classmethod
 
162
    def get_name(cls):
 
163
        return _("Quantum Service Type Management")
 
164
 
 
165
    @classmethod
 
166
    def get_alias(cls):
 
167
        return EXT_ALIAS
 
168
 
 
169
    @classmethod
 
170
    def get_description(cls):
 
171
        return _("API for retrieving and managing service types for "
 
172
                 "Quantum advanced services")
 
173
 
 
174
    @classmethod
 
175
    def get_namespace(cls):
 
176
        return "http://docs.openstack.org/ext/quantum/service-type/api/v1.0"
 
177
 
 
178
    @classmethod
 
179
    def get_updated(cls):
 
180
        return "2013-01-20T00:00:00-00:00"
 
181
 
 
182
    @classmethod
 
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,
 
190
                                             controller)]