Files
cloudkitty/cloudkitty/api/app.py
Aaron-DH 2ba69c4a0b Modify policy of get total/summary
Update policy of get_total/summary, admin user can
get rate info of all tenants and non-admin user can
get rate info of itself.

For example, get total api change as follows:
    cloudkitty total-get
        -- get total of current tenant(get tenant from context)
    cloudkitty total-get -t tenant_id
        -- get total of specified tenant if allowed(admin or owner)
    cloudkitty total-get --all-tenants
        -- get total of all tenants if allowed(admin)

Change-Id: I47dd5f310f18b7e5211165069692827047cdccd6
closes-bug:#1644399
2017-01-26 13:52:09 +01:00

140 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2014 Objectif Libre
#
# 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.
#
# @author: Stéphane Albert
#
import logging
import os
from wsgiref import simple_server
from oslo_config import cfg
from oslo_log import log
from paste import deploy
import pecan
from cloudkitty.api import config as api_config
from cloudkitty.api import hooks
from cloudkitty.i18n import _LI
from cloudkitty import storage
LOG = log.getLogger(__name__)
auth_opts = [
cfg.StrOpt('api_paste_config',
default="api_paste.ini",
help="Configuration file for WSGI definition of API."
),
cfg.StrOpt('auth_strategy',
choices=['noauth', 'keystone'],
default='keystone',
help=("The strategy to use for auth. Supports noauth and "
"keystone")),
]
api_opts = [
cfg.IPOpt('host_ip',
default="0.0.0.0",
help='Host serving the API.'),
cfg.PortOpt('port',
default=8889,
help='Host port serving the API.'),
cfg.BoolOpt('pecan_debug',
default=False,
help='Toggle Pecan Debug Middleware.'),
]
CONF = cfg.CONF
CONF.register_opts(auth_opts)
CONF.register_opts(api_opts, group='api')
def get_pecan_config():
# Set up the pecan configuration
filename = api_config.__file__.replace('.pyc', '.py')
return pecan.configuration.conf_from_file(filename)
def setup_app(pecan_config=None, extra_hooks=None):
app_conf = get_pecan_config()
storage_backend = storage.get_storage()
app_hooks = [
hooks.RPCHook(),
hooks.StorageHook(storage_backend),
hooks.ContextHook(),
]
app = pecan.make_app(
app_conf.app.root,
static_root=app_conf.app.static_root,
template_path=app_conf.app.template_path,
debug=CONF.api.pecan_debug,
force_canonical=getattr(app_conf.app, 'force_canonical', True),
hooks=app_hooks,
guess_content_type_from_ext=False
)
return app
def load_app():
cfg_file = None
cfg_path = cfg.CONF.api_paste_config
if not os.path.isabs(cfg_path):
cfg_file = CONF.find_file(cfg_path)
elif os.path.exists(cfg_path):
cfg_file = cfg_path
if not cfg_file:
raise cfg.ConfigFilesNotFoundError([cfg.CONF.api_paste_config])
LOG.info(_LI("Full WSGI config used: %s"), cfg_file)
return deploy.loadapp("config:" + cfg_file)
def build_server():
# Create the WSGI server and start it
host = CONF.api.host_ip
port = CONF.api.port
LOG.info(_LI('Starting server in PID %s'), os.getpid())
LOG.info(_LI("Configuration:"))
cfg.CONF.log_opt_values(LOG, logging.INFO)
if host == '0.0.0.0':
LOG.info(_LI('serving on 0.0.0.0:%(sport)s, view at '
'http://127.0.0.1:%(vport)s'),
{'sport': port, 'vport': port})
else:
LOG.info(_LI("serving on http://%(host)s:%(port)s"),
{'host': host, 'port': port})
server_cls = simple_server.WSGIServer
handler_cls = simple_server.WSGIRequestHandler
app = load_app()
srv = simple_server.make_server(
host,
port,
app,
server_cls,
handler_cls)
return srv
def app_factory(global_config, **local_conf):
return setup_app()