~ubuntu-branches/ubuntu/raring/cinder/raring-updates

« back to all changes in this revision

Viewing changes to cinder/api/contrib/services.py

Tags: upstream-2013.1~g2
ImportĀ upstreamĀ versionĀ 2013.1~g2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2012 IBM
 
4
# All Rights Reserved.
 
5
#
 
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
7
#    not use this file except in compliance with the License. You may obtain
 
8
#    a copy of the License at
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
#    Unless required by applicable law or agreed to in writing, software
 
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
15
#    License for the specific language governing permissions and limitations
 
16
#    under the License.
 
17
 
 
18
 
 
19
import webob.exc
 
20
 
 
21
from cinder.api import extensions
 
22
from cinder.api.openstack import wsgi
 
23
from cinder.api import xmlutil
 
24
from cinder import db
 
25
from cinder import exception
 
26
from cinder.openstack.common import log as logging
 
27
from cinder.openstack.common import timeutils
 
28
from cinder import utils
 
29
 
 
30
 
 
31
LOG = logging.getLogger(__name__)
 
32
authorize = extensions.extension_authorizer('volume', 'services')
 
33
 
 
34
 
 
35
class ServicesIndexTemplate(xmlutil.TemplateBuilder):
 
36
    def construct(self):
 
37
        root = xmlutil.TemplateElement('services')
 
38
        elem = xmlutil.SubTemplateElement(root, 'service', selector='services')
 
39
        elem.set('binary')
 
40
        elem.set('host')
 
41
        elem.set('zone')
 
42
        elem.set('status')
 
43
        elem.set('state')
 
44
        elem.set('update_at')
 
45
 
 
46
        return xmlutil.MasterTemplate(root, 1)
 
47
 
 
48
 
 
49
class ServicesUpdateTemplate(xmlutil.TemplateBuilder):
 
50
    def construct(self):
 
51
        root = xmlutil.TemplateElement('host')
 
52
        root.set('host')
 
53
        root.set('service')
 
54
        root.set('disabled')
 
55
 
 
56
        return xmlutil.MasterTemplate(root, 1)
 
57
 
 
58
 
 
59
class ServiceController(object):
 
60
    @wsgi.serializers(xml=ServicesIndexTemplate)
 
61
    def index(self, req):
 
62
        """
 
63
        Return a list of all running services. Filter by host & service name.
 
64
        """
 
65
        context = req.environ['cinder.context']
 
66
        authorize(context)
 
67
        now = timeutils.utcnow()
 
68
        services = db.service_get_all(context)
 
69
 
 
70
        host = ''
 
71
        if 'host' in req.GET:
 
72
            host = req.GET['host']
 
73
        service = ''
 
74
        if 'service' in req.GET:
 
75
            service = req.GET['service']
 
76
        if host:
 
77
            services = [s for s in services if s['host'] == host]
 
78
        if service:
 
79
            services = [s for s in services if s['binary'] == service]
 
80
 
 
81
        svcs = []
 
82
        for svc in services:
 
83
            delta = now - (svc['updated_at'] or svc['created_at'])
 
84
            alive = abs(utils.total_seconds(delta))
 
85
            art = (alive and "up") or "down"
 
86
            active = 'enabled'
 
87
            if svc['disabled']:
 
88
                active = 'disabled'
 
89
            svcs.append({"binary": svc['binary'], 'host': svc['host'],
 
90
                         'zone': svc['availability_zone'],
 
91
                         'status': active, 'state': art,
 
92
                         'updated_at': svc['updated_at']})
 
93
        return {'services': svcs}
 
94
 
 
95
    @wsgi.serializers(xml=ServicesUpdateTemplate)
 
96
    def update(self, req, id, body):
 
97
        """Enable/Disable scheduling for a service"""
 
98
        context = req.environ['cinder.context']
 
99
        authorize(context)
 
100
 
 
101
        if id == "enable":
 
102
            disabled = False
 
103
        elif id == "disable":
 
104
            disabled = True
 
105
        else:
 
106
            raise webob.exc.HTTPNotFound("Unknown action")
 
107
 
 
108
        try:
 
109
            host = body['host']
 
110
            service = body['service']
 
111
        except (TypeError, KeyError):
 
112
            raise webob.exc.HTTPUnprocessableEntity()
 
113
 
 
114
        try:
 
115
            svc = db.service_get_by_args(context, host, service)
 
116
            if not svc:
 
117
                raise webob.exc.HTTPNotFound('Unknown service')
 
118
 
 
119
            db.service_update(context, svc['id'], {'disabled': disabled})
 
120
        except exception.ServiceNotFound:
 
121
            raise webob.exc.HTTPNotFound("service not found")
 
122
 
 
123
        return {'host': host, 'service': service, 'disabled': disabled}
 
124
 
 
125
 
 
126
class Services(extensions.ExtensionDescriptor):
 
127
    """Services support"""
 
128
 
 
129
    name = "Services"
 
130
    alias = "os-services"
 
131
    namespace = "http://docs.openstack.org/volume/ext/services/api/v2"
 
132
    updated = "2012-10-28T00:00:00-00:00"
 
133
 
 
134
    def get_resources(self):
 
135
        resources = []
 
136
        resource = extensions.ResourceExtension('os-services',
 
137
                                                ServiceController())
 
138
        resources.append(resource)
 
139
        return resources