Merge "Replace keystoneclient by keystoneauth1"

This commit is contained in:
Jenkins 2015-09-21 13:56:51 +00:00 committed by Gerrit Code Review
commit b0601db91a
5 changed files with 58 additions and 126 deletions

View File

@ -13,19 +13,14 @@
import os
from keystoneclient.auth import base
from oslo_config import cfg
import six
from keystoneauth1 import loading
from keystoneauth1 import plugin
class GnocchiNoAuthException(Exception):
pass
class GnocchiNoAuthPlugin(base.BaseAuthPlugin):
class GnocchiNoAuthPlugin(plugin.BaseAuthPlugin):
"""No authentication plugin for Gnocchi
This is a keystoneclient plugin that instead of
This is a keystoneauth plugin that instead of
doing authentication, it just fill the 'x-user-id'
and 'x-project-id' headers with the user provided one.
"""
@ -50,51 +45,33 @@ class GnocchiNoAuthPlugin(base.BaseAuthPlugin):
def get_endpoint(self, session, **kwargs):
return self._endpoint
@classmethod
def get_options(cls):
options = super(GnocchiNoAuthPlugin, cls).get_options()
class GnocchiOpt(loading.Opt):
@property
def argparse_args(self):
return ['--%s' % o.name for o in self._all_opts]
@property
def argparse_default(self):
# select the first ENV that is not false-y or return None
for o in self._all_opts:
v = os.environ.get('GNOCCHI_%s' % o.name.replace('-', '_').upper())
if v:
return v
return self.default
class GnocchiNoAuthLoader(loading.BaseLoader):
@property
def plugin_class(self):
return GnocchiNoAuthPlugin
def get_options(self):
options = super(GnocchiNoAuthLoader, self).get_options()
options.extend([
cfg.StrOpt('user-id', help='User ID', required=True),
cfg.StrOpt('project-id', help='Project ID', required=True),
cfg.StrOpt('gnocchi-endpoint', help='Gnocchi endpoint',
GnocchiOpt('user-id', help='User ID', required=True),
GnocchiOpt('project-id', help='Project ID', required=True),
GnocchiOpt('gnocchi-endpoint', help='Gnocchi endpoint',
dest="endpoint", required=True),
])
return options
@classmethod
def register_argparse_arguments(cls, parser):
"""Register the CLI options provided by a specific plugin.
Given a plugin class convert it's options into argparse arguments and
add them to a parser.
:param parser: the parser to attach argparse options.
:type parser: argparse.ArgumentParser
"""
# NOTE(jamielennox): ideally oslo_config would be smart enough to
# handle all the Opt manipulation that goes on in this file. However it
# is currently not. Options are handled in as similar a way as
# possible to oslo_config such that when available we should be able to
# transition.
# NOTE(sileht): We override the keystoneclient one to remove OS prefix
# and allow to use required parameters
for opt in cls.get_options():
args = []
envs = []
for o in [opt] + opt.deprecated_opts:
args.append('--%s' % o.name)
envs.append('GNOCCHI_%s' % o.name.replace('-', '_').upper())
# select the first ENV that is not false-y or return None
env_vars = (os.environ.get(e) for e in envs)
default = six.next(six.moves.filter(None, env_vars), None)
parser.add_argument(*args,
default=default or opt.default,
metavar=opt.metavar,
help=opt.help,
dest='os_%s' % opt.dest,
required=opt.required)

View File

@ -21,7 +21,8 @@ import warnings
from cliff import app
from cliff import commandmanager
from keystoneclient.auth import cli as keystoneclient_cli
from keystoneauth1 import adapter
from keystoneauth1 import loading
from keystoneclient import exceptions
from gnocchiclient import client
@ -79,25 +80,6 @@ class GnocchiShell(app.App):
dest='region_name',
default=os.environ.get('OS_REGION_NAME'),
help='Authentication region name (Env: OS_REGION_NAME)')
parser.add_argument(
'--os-cacert',
metavar='<ca-bundle-file>',
dest='cacert',
default=os.environ.get('OS_CACERT'),
help='CA certificate bundle file (Env: OS_CACERT)')
verify_group = parser.add_mutually_exclusive_group()
verify_group.add_argument(
'--verify',
action='store_true',
default=None,
help='Verify server certificate (default)',
)
verify_group.add_argument(
'--insecure',
action='store_true',
default=None,
help='Disable server certificate verification',
)
parser.add_argument(
'--os-interface',
metavar='<interface>',
@ -108,15 +90,11 @@ class GnocchiShell(app.App):
' Valid interface types: [admin, public, internal].'
' (Env: OS_INTERFACE)')
parser.add_argument('--timeout',
default=600,
type=_positive_non_zero_int,
help='Number of seconds to wait for a response.')
plugin = keystoneclient_cli.register_argparse_arguments(
loading.register_session_argparse_arguments(parser=parser)
plugin = loading.register_auth_argparse_arguments(
parser=parser, argv=sys.argv, default="password")
if plugin != noauth.GnocchiNoAuthPlugin:
if not isinstance(plugin, noauth.GnocchiNoAuthLoader):
parser.add_argument(
'--gnocchi-endpoint',
metavar='<endpoint>',
@ -129,19 +107,20 @@ class GnocchiShell(app.App):
def initialize_app(self, argv):
super(GnocchiShell, self).initialize_app(argv)
if hasattr(self.options, "endpoint"):
endpoint = self.options.endpoint
endpoint_override = self.options.endpoint
else:
endpoint = None
auth_plugin = keystoneclient_cli.load_from_argparse_arguments(
endpoint_override = None
auth_plugin = loading.load_auth_from_argparse_arguments(
self.options)
self.client = client.Client(self.api_version,
auth=auth_plugin,
endpoint=endpoint,
region_name=self.options.region_name,
interface=self.options.interface,
verify=self.options.verify,
cert=self.options.cacert,
timeout=self.options.timeout)
session = loading.load_session_from_argparse_arguments(
self.options, auth=auth_plugin)
session = adapter.Adapter(session, service_type='metric',
interface=self.options.interface,
region_name=self.options.region_name,
endpoint_override=endpoint_override)
self.client = client.Client(self.api_version, session=session)
def clean_up(self, cmd, result, err):
if err and isinstance(err, exceptions.HttpError):

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from keystoneclient import session as keystoneclient_session
from gnocchiclient.v1 import archive_policy
from gnocchiclient.v1 import archive_policy_rule
from gnocchiclient.v1 import metric
@ -24,44 +22,22 @@ from gnocchiclient.v1 import resource
class Client(object):
"""Client for the Gnocchi v1 API.
:param string auth: An optional keystoneclient authentication plugin
to authenticate the session with
:type auth: :py:class:`keystoneclient.auth.base.BaseAuthPlugin`
:param endpoint: The optional Gnocchi API endpoint
:type endpoint: str
:param interface: The endpoint interface ('public', 'internal', 'admin')
:type interface: str
:param region_name: The keystone region name
:type region_name: str
:param \*\*kwargs: Any option supported by
:py:class:`keystoneclient.session.Session`
:param string session: session
:type session: :py:class:`keystoneauth.adapter.Adapter`
"""
_VERSION = "v1"
def __init__(self, auth=None, endpoint=None, interface=None,
region_name=None, **kwargs):
"""Initialize a new client for the Gnocchi v1 API."""
self.api = keystoneclient_session.Session(auth, **kwargs)
def __init__(self, session=None):
"""Initialize a new client for the Gnocchi v1 API.
"""
self.api = session
self.resource = resource.ResourceManager(self)
self.archive_policy = archive_policy.ArchivePolicyManager(self)
self.archive_policy_rule = (
archive_policy_rule.ArchivePolicyRuleManager(self))
self.metric = metric.MetricManager(self)
self.interface = interface
self.region_name = region_name
self._endpoint = endpoint
@property
def endpoint(self):
if self._endpoint is None:
self._endpoint = self.api.get_endpoint(
service_type='metric', interface=self.interface,
region_name=self.region_name)
return self._endpoint
def _build_url(self, url_suffix):
return "%s/%s/%s" % (self.endpoint.rstrip("/"),
self._VERSION,
url_suffix)
return "%s/%s" % (self._VERSION, url_suffix)

View File

@ -8,4 +8,4 @@ cliff>=1.14.0 # Apache-2.0
oslo.i18n>=1.5.0 # Apache-2.0
oslo.serialization>=1.4.0 # Apache-2.0
oslo.utils>=2.0.0 # Apache-2.0
python-keystoneclient>=1.6.0
keystoneauth1>=1.0.0

View File

@ -51,8 +51,8 @@ gnocchi.cli.v1 =
measures_add = gnocchiclient.v1.metric_cli:CliMeasuresAdd
measures_aggregation = gnocchiclient.v1.metric_cli:CliMeasuresAggregation
keystoneclient.auth.plugin =
gnocchi-noauth = gnocchiclient.noauth:GnocchiNoAuthPlugin
keystoneauth1.plugin =
gnocchi-noauth = gnocchiclient.noauth:GnocchiNoAuthLoader
[build_sphinx]