1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
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
10
# http://www.apache.org/licenses/LICENSE-2.0
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
21
from cinder.api import extensions
22
from cinder.api.openstack import wsgi
23
from cinder.api import xmlutil
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
31
LOG = logging.getLogger(__name__)
32
authorize = extensions.extension_authorizer('volume', 'services')
35
class ServicesIndexTemplate(xmlutil.TemplateBuilder):
37
root = xmlutil.TemplateElement('services')
38
elem = xmlutil.SubTemplateElement(root, 'service', selector='services')
46
return xmlutil.MasterTemplate(root, 1)
49
class ServicesUpdateTemplate(xmlutil.TemplateBuilder):
51
root = xmlutil.TemplateElement('host')
56
return xmlutil.MasterTemplate(root, 1)
59
class ServiceController(object):
60
@wsgi.serializers(xml=ServicesIndexTemplate)
63
Return a list of all running services. Filter by host & service name.
65
context = req.environ['cinder.context']
67
now = timeutils.utcnow()
68
services = db.service_get_all(context)
72
host = req.GET['host']
74
if 'service' in req.GET:
75
service = req.GET['service']
77
services = [s for s in services if s['host'] == host]
79
services = [s for s in services if s['binary'] == service]
83
delta = now - (svc['updated_at'] or svc['created_at'])
84
alive = abs(utils.total_seconds(delta))
85
art = (alive and "up") or "down"
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}
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']
103
elif id == "disable":
106
raise webob.exc.HTTPNotFound("Unknown action")
110
service = body['service']
111
except (TypeError, KeyError):
112
raise webob.exc.HTTPUnprocessableEntity()
115
svc = db.service_get_by_args(context, host, service)
117
raise webob.exc.HTTPNotFound('Unknown service')
119
db.service_update(context, svc['id'], {'disabled': disabled})
120
except exception.ServiceNotFound:
121
raise webob.exc.HTTPNotFound("service not found")
123
return {'host': host, 'service': service, 'disabled': disabled}
126
class Services(extensions.ExtensionDescriptor):
127
"""Services support"""
130
alias = "os-services"
131
namespace = "http://docs.openstack.org/volume/ext/services/api/v2"
132
updated = "2012-10-28T00:00:00-00:00"
134
def get_resources(self):
136
resource = extensions.ResourceExtension('os-services',
138
resources.append(resource)