Initial client code

This commit is contained in:
Mehdi Abaakouk
2015-08-28 11:57:52 +02:00
parent d88c413b77
commit a131229f01
10 changed files with 271 additions and 6 deletions

20
gnocchiclient/client.py Normal file
View File

@@ -0,0 +1,20 @@
# 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.
from oslo_utils import importutils
def Client(version, *args, **kwargs):
module = 'gnocchiclient.v%s.client' % version
module = importutils.import_module(module)
client_class = getattr(module, 'Client')
return client_class(*args, **kwargs)

28
gnocchiclient/i18n.py Normal file
View File

@@ -0,0 +1,28 @@
# 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.
import oslo_i18n as i18n
_translators = i18n.TranslatorFactory(domain='gnocchiclient')
# The primary translation function using the well-known name "_"
_ = _translators.primary
# Translators for log levels.
#
# The abbreviated names are meant to reflect the usual use of a short
# name like '_'. The "L" is for "log" and the other letter comes from
# the level.
_LI = _translators.log_info
_LW = _translators.log_warning
_LE = _translators.log_error
_LC = _translators.log_critical

71
gnocchiclient/shell.py Normal file
View File

@@ -0,0 +1,71 @@
# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
# 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.
import sys
from cliff import app
from cliff import commandmanager
from keystoneclient.auth import cli as keystoneclient_cli
from gnocchiclient import client
from gnocchiclient.version import __version__
class GnocchiShell(app.App):
def __init__(self, api_version):
super(GnocchiShell, self).__init__(
description='Gnocchi command line client',
# FIXME(sileht): get version from pbr
version=__version__,
command_manager=commandmanager.CommandManager(
'gnocchi.cli.v%s' % api_version),
deferred_help=True,
)
self.api_version = api_version
self.auth_plugin = None
self.client = None
def build_option_parser(self, description, version):
"""Return an argparse option parser for this application.
Subclasses may override this method to extend
the parser with more global options.
:param description: full description of the application
:paramtype description: str
:param version: version number for the application
:paramtype version: str
"""
parser = super(GnocchiShell, self).build_option_parser(description,
version)
keystoneclient_cli.register_argparse_arguments(parser=parser,
argv=sys.argv,
default="password")
return parser
def initialize_app(self, argv):
self.auth_plugin = keystoneclient_cli.load_from_argparse_arguments(
self.options)
self.client = client.Client(self.api_version, self.auth_plugin)
def main(args=None):
if args is None:
args = sys.argv[1:]
# FIXME(sileht): read this from argv and env
api_version = "1"
return GnocchiShell(api_version).run(args)

View File

View File

@@ -0,0 +1,58 @@
# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
# 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.
from keystoneclient import session as keystoneclient_session
class Manager(object):
def __init__(self, client):
self.client = client
class ResourceManager(Manager):
def list(self, resource_type="generic", details=False, history=False):
details = "true" if details else "false"
history = "true" if history else "false"
url = self.client.url("resource/%s?details=%s&history=%s" % (
resource_type, details, history))
return self.client.api.get(url).json()
class Client(object):
"""Client for the Gnocchi v1 API.
:param string auth: An keystoneclient authentication plugin to
authenticate the session with
:type auth: :py:class:`keystoneclient.auth.base.BaseAuthPlugin`
"""
VERSION = "v1"
def __init__(self, auth, *args, **kwargs):
"""Initialize a new client for the Gnocchi v1 API."""
self.api = keystoneclient_session.Session(auth, *args, **kwargs)
self.resource = ResourceManager(self)
self._endpoint = None
@property
def endpoint(self):
if self._endpoint is None:
self._endpoint = self.api.get_endpoint(service_type='metric')
return self._endpoint
def url(self, url_suffix):
return "%s/%s/%s" % (self.endpoint.rstrip("/"),
self.VERSION,
url_suffix)

64
gnocchiclient/v1/shell.py Normal file
View File

@@ -0,0 +1,64 @@
# Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
# 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.
from cliff import lister
from cliff import show
class ResourceList(lister.Lister):
COLS = ('id', 'type',
'project_id', 'user_id',
'started_at', 'ended_at',
'revision_start', 'revision_end')
def get_parser(self, prog_name):
parser = super(ResourceList, self).get_parser(prog_name)
parser.add_argument("--details", action='store_true',
help="Show all attributes of generic resources"),
parser.add_argument("--history", action='store_true',
help="Show history of the resources"),
parser.add_argument("resource_type",
default="generic",
nargs='?',
help="Type of resource")
return parser
def take_action(self, parsed_args):
resources = self.app.client.resource.list(
resource_type=parsed_args.resource_type,
details=parsed_args.details,
history=parsed_args.history)
return self.COLS, [self._resource2tuple(r) for r in resources]
@classmethod
def _resource2tuple(cls, resource):
return tuple([resource[k] for k in cls.COLS])
class ResourceShow(show.ShowOne):
def get_parser(self, prog_name):
parser = super(ResourceShow, self).get_parser(prog_name)
parser.add_argument("resource_type",
default="generic",
nargs='?',
help="Type of resource")
parser.add_argument("resource_id",
help="ID of a resource")
return parser
def take_action(self, parsed_args):
return self.app.client.resource.get(
resource_type=parsed_args.resource_type,
resource_id=parsed_args.resource_id).items()

18
gnocchiclient/version.py Normal file
View File

@@ -0,0 +1,18 @@
#
# 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.
#
import pbr.version
__version__ = pbr.version.VersionInfo('python-gnocchiclient').version_string()

View File

@@ -1,6 +0,0 @@
[DEFAULT]
# The list of modules to copy from oslo-incubator.git
# The base module to hold the copy of openstack.common
base=gnocchiclient

View File

@@ -4,3 +4,7 @@
pbr<2.0,>=1.4
Babel>=1.3
cliff>=1.14.0 # Apache-2.0
oslo.i18n>=1.5.0 # Apache-2.0
oslo.utils>=2.0.0 # Apache-2.0
python-keystoneclient>=1.6.0

View File

@@ -23,6 +23,14 @@ classifier =
packages =
gnocchiclient
[entry_points]
console_scripts =
gnocchi = gnocchiclient.shell:main
gnocchi.cli.v1 =
resource_list = gnocchiclient.v1.shell:ResourceList
resource_show = gnocchiclient.v1.shell:ResourceShow
[build_sphinx]
source-dir = doc/source
build-dir = doc/build