Compare commits

..

1 Commits

Author SHA1 Message Date
Thierry Carrez
bcab489385 Update .gitreview for stable/mitaka
Change-Id: Idb73a4b508786a8843e63e8832c641d779d0613f
2016-03-28 14:40:38 +02:00
13 changed files with 118 additions and 250 deletions

View File

@@ -1,5 +1,5 @@
[gerrit]
host=review.opendev.org
host=review.openstack.org
port=29418
project=openstack/python-cloudkittyclient.git
defaultbranch=stable/newton
defaultbranch=stable/mitaka

6
MANIFEST.in Normal file
View File

@@ -0,0 +1,6 @@
include AUTHORS
include ChangeLog
exclude .gitignore
exclude .gitreview
global-exclude *.pyc

View File

@@ -10,10 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import time
from keystoneclient import adapter
from keystoneclient.auth.identity import v2 as v2_auth
from keystoneclient.auth.identity import v3 as v3_auth
from keystoneclient import discover
@@ -25,7 +21,6 @@ import six.moves.urllib.parse as urlparse
from cloudkittyclient.common import utils
from cloudkittyclient import exc
from cloudkittyclient.openstack.common.apiclient import auth
from cloudkittyclient.openstack.common.apiclient import client
from cloudkittyclient.openstack.common.apiclient import exceptions
@@ -208,13 +203,8 @@ class AuthPlugin(auth.BaseAuthPlugin):
"""
has_token = self.opts.get('token') or self.opts.get('auth_token')
no_auth = has_token and self.opts.get('endpoint')
has_project = (self.opts.get('project_id')
or (self.opts.get('project_name')
and (self.opts.get('user_domain_name')
or self.opts.get('user_domain_id'))))
has_tenant = self.opts.get('tenant_id') or self.opts.get('tenant_name')
has_credential = (self.opts.get('username')
and (has_project or has_tenant)
has_credential = (self.opts.get('username') and has_tenant
and self.opts.get('password')
and self.opts.get('auth_url'))
missing = not (no_auth or has_credential)
@@ -263,28 +253,9 @@ def get_client(version, **kwargs):
:param api_version: the API version to use ('1')
:param kwargs: keyword args containing credentials, either:
* session: a keystoneauth/keystoneclient session object
* service_type: The default service_type for URL discovery
* service_name: The default service_name for URL discovery
* interface: The default interface for URL discovery
(Default: public)
* region_name: The default region_name for URL discovery
* endpoint_override: Always use this endpoint URL for requests
for this cloudkittyclient
* auth: An auth plugin to use instead of the session one
* user_agent: The User-Agent string to set
(Default is python-cloudkittyclient)
* connect_retries: the maximum number of retries that should be
attempted for connection errors
* logger: A logging object
or (DEPRECATED):
* os_token: pre-existing token to re-use
* os_endpoint: Cloudkitty API endpoint
or (DEPRECATED):
or:
* os_username: name of user
* os_password: user's password
* os_user_id: user's id
@@ -346,87 +317,3 @@ def get_auth_plugin(endpoint, **kwargs):
project_domain_id=kwargs.get('project_domain_id')
)
return auth_plugin
LEGACY_OPTS = ('auth_plugin', 'auth_url', 'token', 'insecure', 'cacert',
'tenant_id', 'project_id', 'username', 'password',
'project_name', 'tenant_name',
'user_domain_name', 'user_domain_id',
'project_domain_name', 'project_domain_id',
'key_file', 'cert_file', 'verify', 'timeout', 'cert')
def construct_http_client(**kwargs):
kwargs = kwargs.copy()
if kwargs.get('session') is not None:
# Drop legacy options
for opt in LEGACY_OPTS:
kwargs.pop(opt, None)
return SessionClient(
session=kwargs.pop('session'),
service_type=kwargs.pop('service_type', 'rating') or 'rating',
interface=kwargs.pop('interface', kwargs.pop('endpoint_type',
'publicURL')),
region_name=kwargs.pop('region_name', None),
user_agent=kwargs.pop('user_agent', 'python-cloudkittyclient'),
auth=kwargs.get('auth', None),
timings=kwargs.pop('timings', None),
**kwargs)
else:
return client.BaseClient(client.HTTPClient(
auth_plugin=kwargs.get('auth_plugin'),
region_name=kwargs.get('region_name'),
endpoint_type=kwargs.get('endpoint_type'),
original_ip=kwargs.get('original_ip'),
verify=kwargs.get('verify'),
cert=kwargs.get('cert'),
timeout=kwargs.get('timeout'),
timings=kwargs.get('timings'),
keyring_saver=kwargs.get('keyring_saver'),
debug=kwargs.get('debug'),
user_agent=kwargs.get('user_agent'),
http=kwargs.get('http')
))
@contextlib.contextmanager
def record_time(times, enabled, *args):
"""Record the time of a specific action.
:param times: A list of tuples holds time data.
:type times: list
:param enabled: Whether timing is enabled.
:type enabled: bool
:param args: Other data to be stored besides time data, these args
will be joined to a string.
"""
if not enabled:
yield
else:
start = time.time()
yield
end = time.time()
times.append((' '.join(args), start, end))
class SessionClient(adapter.LegacyJsonAdapter):
def __init__(self, *args, **kwargs):
self.times = []
self.timings = kwargs.pop('timings', False)
super(SessionClient, self).__init__(*args, **kwargs)
def request(self, url, method, **kwargs):
kwargs.setdefault('headers', kwargs.get('headers', {}))
# NOTE(sileht): The standard call raises errors from
# keystoneauth, where we need to raise the cloudkittyclient errors.
raise_exc = kwargs.pop('raise_exc', True)
with record_time(self.times, self.timings, method, url):
resp, body = super(SessionClient, self).request(url,
method,
raise_exc=False,
**kwargs)
if raise_exc and resp.status_code >= 400:
raise exc.from_response(resp, body)
return resp

View File

@@ -198,11 +198,12 @@ class CloudkittyShell(object):
@staticmethod
def no_project_and_domain_set(args):
return not (((args.os_project_id or (args.os_project_name and
(args.os_project_domain_name or
args.os_project_domain_id)))
and (args.os_user_domain_name or args.os_user_domain_id))
or (args.os_tenant_id or args.os_tenant_name))
if not (args.os_project_id or (args.os_project_name and
(args.os_user_domain_name or args.os_user_domain_id)) or
(args.os_tenant_id or args.os_tenant_name)):
return True
else:
return False
def main(self, argv):
parsed = self.parse_args(argv)
@@ -241,24 +242,20 @@ class CloudkittyShell(object):
"env[OS_USER_DOMAIN_NAME] or "
"a domain_id via either "
"--os-user-domain-id or via "
"env[OS_USER_DOMAIN_ID]\n\n"
"As an alternative to project_id, "
"you can provide a project_name via "
"either --os-project-name or via "
"env[OS_PROJECT_NAME] and "
"a project_domain_name via either "
"--os-project-domain-name or via "
"env[OS_PROJECT_DOMAIN_NAME] or "
"a project_domain_id via either "
"--os-project-domain-id or via "
"env[OS_PROJECT_DOMAIN_ID]")
"env[OS_USER_DOMAIN_ID]")
if not (self.auth_plugin.opts['tenant_id']
or self.auth_plugin.opts['tenant_name']):
raise exc.CommandError("You must provide a tenant_id via "
"either --os-tenant-id or via "
"env[OS_TENANT_ID]")
if not self.auth_plugin.opts['auth_url']:
raise exc.CommandError("You must provide an auth url via "
"either --os-auth-url or via "
"env[OS_AUTH_URL]")
client_kwargs = {}
client_kwargs = vars(args)
client_kwargs.update(self.auth_plugin.opts)
client_kwargs['auth_plugin'] = self.auth_plugin
client = ckclient.get_client(api_version, **client_kwargs)

View File

@@ -45,16 +45,6 @@ class ClientTest(utils.BaseTestCase):
def setUp(self):
super(ClientTest, self).setUp()
def test_client_v1_with_session(self):
resp = mock.Mock(status_code=200, text=b'')
resp.json.return_value = {"modules": []}
session = mock.Mock()
session.request.return_value = resp
c = client.get_client(1, session=session)
c.modules.list()
self.assertTrue(session.request.called)
self.assertTrue(resp.json.called)
def test_client_version(self):
c1 = self.create_client(env=FAKE_ENV, api_version=1)
self.assertIsInstance(c1, v1client.Client)
@@ -147,8 +137,7 @@ class ClientTest(utils.BaseTestCase):
env = FAKE_ENV.copy()
env['cacert'] = '/path/to/cacert'
client = self.create_client(env)
self.assertEqual('/path/to/cacert',
client.http_client.http_client.verify)
self.assertEqual('/path/to/cacert', client.client.verify)
def test_v1_client_certfile_and_keyfile(self):
env = FAKE_ENV.copy()
@@ -156,4 +145,4 @@ class ClientTest(utils.BaseTestCase):
env['key_file'] = '/path/to/keycert'
client = self.create_client(env)
self.assertEqual(('/path/to/cert', '/path/to/keycert'),
client.http_client.http_client.cert)
client.client.cert)

View File

@@ -622,21 +622,6 @@ class MappingManagerTest(utils.BaseTestCase):
self.api = client.BaseClient(self.http_client)
self.mgr = hashmap.MappingManager(self.api)
def test_get_mappings_by_group(self):
mappings = self.mgr.findall(group_id=GROUP2['group_id'])
expect = [
'GET', ('/v1/rating/module_config/hashmap/mappings?group_id=' +
GROUP2['group_id'])]
self.http_client.assert_called(*expect)
self.assertEqual(1, len(mappings))
mapping = mappings[0]
self.assertEqual(FIELD1['field_id'], mapping.field_id)
self.assertEqual(FIELD_MAPPING1['group_id'], mapping.group_id)
self.assertEqual(FIELD_MAPPING1['mapping_id'], mapping.mapping_id)
self.assertEqual(FIELD_MAPPING1['value'], mapping.value)
self.assertEqual(FIELD_MAPPING1['cost'], mapping.cost)
self.assertEqual(FIELD_MAPPING1['type'], mapping.type)
def test_get_a_mapping(self):
resource = self.mgr.get(mapping_id=FIELD_MAPPING1['mapping_id'])
expect = [
@@ -715,27 +700,6 @@ class ThresholdManagerTest(utils.BaseTestCase):
self.api = client.BaseClient(self.http_client)
self.mgr = hashmap.ThresholdManager(self.api)
def test_get_thresholds_by_group(self):
mappings = self.mgr.findall(group_id=GROUP3['group_id'])
expect = [
'GET', ('/v1/rating/module_config/hashmap/thresholds?group_id=' +
GROUP3['group_id'])]
self.http_client.assert_called(*expect)
self.assertEqual(1, len(mappings))
mapping = mappings[0]
self.assertEqual(SERVICE_THRESHOLD1['threshold_id'],
mapping.threshold_id)
self.assertEqual(SERVICE_THRESHOLD1['service_id'],
mapping.service_id)
self.assertEqual(SERVICE_THRESHOLD1['group_id'],
mapping.group_id)
self.assertEqual(SERVICE_THRESHOLD1['level'],
mapping.level)
self.assertEqual(SERVICE_THRESHOLD1['cost'],
mapping.cost)
self.assertEqual(SERVICE_THRESHOLD1['map_type'],
mapping.map_type)
def test_get_a_threshold(self):
resource = self.mgr.get(
threshold_id=SERVICE_THRESHOLD1['threshold_id'])

View File

@@ -16,6 +16,7 @@
from stevedore import extension
from cloudkittyclient import client as ckclient
from cloudkittyclient.openstack.common.apiclient import client
from cloudkittyclient.v1 import collector
from cloudkittyclient.v1 import core
from cloudkittyclient.v1 import report
@@ -27,33 +28,33 @@ SUBMODULES_NAMESPACE = 'cloudkitty.client.modules'
class Client(object):
"""Client for the Cloudkitty v1 API.
:param session: a keystoneauth/keystoneclient session object
:type session: keystoneclient.session.Session
:param str service_type: The default service_type for URL discovery
:param str service_name: The default service_name for URL discovery
:param str interface: The default interface for URL discovery
(Default: public)
:param str region_name: The default region_name for URL discovery
:param str endpoint_override: Always use this endpoint URL for requests
for this cloudkittyclient
:param auth: An auth plugin to use instead of the session one
:type auth: keystoneclient.auth.base.BaseAuthPlugin
:param str user_agent: The User-Agent string to set
(Default is python-cloudkittyclient)
:param int connect_retries: the maximum number of retries that should be
attempted for connection errors
:param logger: A logging object
:type logger: logging.Logger
:param string endpoint: A user-supplied endpoint URL for the cloudkitty
service.
:param function token: Provides token for authentication.
:param integer timeout: Allows customization of the timeout for client
http requests. (optional)
"""
def __init__(self, *args, **kwargs):
"""Initialize a new client for the Cloudkitty v1 API."""
self.auth_plugin = (kwargs.get('auth_plugin')
or ckclient.get_auth_plugin(*args, **kwargs))
self.client = client.HTTPClient(
auth_plugin=self.auth_plugin,
region_name=kwargs.get('region_name'),
endpoint_type=kwargs.get('endpoint_type'),
original_ip=kwargs.get('original_ip'),
verify=kwargs.get('verify'),
cert=kwargs.get('cert'),
timeout=kwargs.get('timeout'),
timings=kwargs.get('timings'),
keyring_saver=kwargs.get('keyring_saver'),
debug=kwargs.get('debug'),
user_agent=kwargs.get('user_agent'),
http=kwargs.get('http')
)
if not kwargs.get('auth_plugin'):
kwargs['auth_plugin'] = ckclient.get_auth_plugin(*args, **kwargs)
self.auth_plugin = kwargs.get('auth_plugin')
self.http_client = ckclient.construct_http_client(**kwargs)
self.http_client = client.BaseClient(self.client)
self.modules = core.CloudkittyModuleManager(self.http_client)
self.collector = collector.CollectorManager(self.http_client)
self.reports = report.ReportManager(self.http_client)

View File

@@ -17,7 +17,7 @@
from cloudkittyclient.common import utils
@utils.arg('-c', '--collector',
@utils.arg('--collector',
help='Collector name to filter on.',
required=False,
default=None)
@@ -29,7 +29,7 @@ def do_collector_mapping_list(cc, args):
utils.print_list(data, fields, fields_labels, sortby=0)
@utils.arg('-s', '--service',
@utils.arg('--service',
help='Which service to get the mapping for.',
required=True)
def do_collector_mapping_get(cc, args):
@@ -38,10 +38,10 @@ def do_collector_mapping_get(cc, args):
utils.print_dict(data.to_dict())
@utils.arg('-c', '--collector',
@utils.arg('--collector',
help='Map a service to this collector.',
required=True)
@utils.arg('-s', '--service',
@utils.arg('--service',
help='Map a collector to this service.',
required=True)
def do_collector_mapping_create(cc, args):
@@ -51,7 +51,7 @@ def do_collector_mapping_create(cc, args):
utils.print_dict(out.to_dict())
@utils.arg('-s', '--service',
@utils.arg('--service',
help='Filter on this service.',
required=True)
def do_collector_mapping_delete(cc, args):
@@ -60,7 +60,7 @@ def do_collector_mapping_delete(cc, args):
cc.collector.mappings.delete(mapping_id=args.service)
@utils.arg('-n', '--name',
@utils.arg('--name',
help='Name of the collector.',
required=True)
def do_collector_state_get(cc, args):
@@ -69,7 +69,7 @@ def do_collector_state_get(cc, args):
utils.print_dict(data.to_dict())
@utils.arg('-n', '--name',
@utils.arg('--name',
help='Name of the collector.',
required=True)
def do_collector_state_enable(cc, args):
@@ -78,7 +78,7 @@ def do_collector_state_enable(cc, args):
utils.print_dict(new_state.to_dict())
@utils.arg('-n', '--name',
@utils.arg('--name',
help='Name of the collector.',
required=True)
def do_collector_state_disable(cc, args):

View File

@@ -126,9 +126,6 @@ def common_hashmap_mapping_arguments(create=False):
@utils.arg('-g', '--group-id',
help='Group id',
required=False)
@utils.arg('-p', '--project-id',
help='Project/tenant id',
required=False)
@functools.wraps(func)
def _wrapped(*args, **kwargs):
return func(*args, **kwargs)
@@ -152,7 +149,6 @@ def do_hashmap_mapping_create(cc, args={}):
'service_id': 'service_id',
'field_id': 'field_id',
'group_id': 'group_id',
'project_id': 'tenant_id',
}
fields = {}
for k, v in vars(args).items():
@@ -175,7 +171,6 @@ def do_hashmap_mapping_update(cc, args={}):
'value': 'value',
'type': 'type',
'group_id': 'group_id',
'project_id': 'tenant_id',
}
try:
mapping = cc.hashmap.mappings.get(mapping_id=args.mapping_id)
@@ -197,15 +192,10 @@ def do_hashmap_mapping_update(cc, args={}):
@utils.arg('-g', '--group-id',
help='Group id',
required=False)
@utils.arg('-p', '--project-id',
help='Project/tenant id',
required=False)
def do_hashmap_mapping_list(cc, args={}):
"""List mappings."""
if (args.group_id is None and
args.service_id is None and args.field_id is None):
raise exc.CommandError("Provide either group-id, service-id or "
"field-id")
if args.service_id is None and args.field_id is None:
raise exc.CommandError("Provide either service-id or field-id")
try:
mappings = cc.hashmap.mappings.list(service_id=args.service_id,
field_id=args.field_id,
@@ -215,10 +205,10 @@ def do_hashmap_mapping_list(cc, args={}):
else:
field_labels = ['Mapping id', 'Value', 'Cost',
'Type', 'Field id',
'Service id', 'Group id', 'Tenant id']
'Service id', 'Group id']
fields = ['mapping_id', 'value', 'cost',
'type', 'field_id',
'service_id', 'group_id', 'tenant_id']
'service_id', 'group_id']
utils.print_list(mappings, fields, field_labels,
sortby=0)
@@ -289,15 +279,12 @@ def common_hashmap_threshold_arguments(create=False):
@utils.arg('-c', '--cost',
help='Threshold cost',
required=create)
@utils.arg('-t', '--type',
@utils.arg('-m', '--map-type',
help='Threshold type (flat, rate)',
required=False)
@utils.arg('-g', '--group-id',
help='Group id',
required=False)
@utils.arg('-p', '--project-id',
help='Project/tenant id',
required=False)
@functools.wraps(func)
def _wrapped(*args, **kwargs):
return func(*args, **kwargs)
@@ -317,11 +304,10 @@ def do_hashmap_threshold_create(cc, args={}):
arg_to_field_mapping = {
'level': 'level',
'cost': 'cost',
'type': 'type',
'map_type': 'map_type',
'service_id': 'service_id',
'field_id': 'field_id',
'group_id': 'group_id',
'project_id': 'tenant_id',
}
fields = {}
for k, v in vars(args).items():
@@ -332,7 +318,7 @@ def do_hashmap_threshold_create(cc, args={}):
utils.print_dict(out.to_dict())
@utils.arg('-i', '--threshold-id',
@utils.arg('-t', '--threshold-id',
help='Threshold id',
required=True)
@common_hashmap_threshold_arguments()
@@ -342,9 +328,8 @@ def do_hashmap_threshold_update(cc, args={}):
'threshold_id': 'threshold_id',
'cost': 'cost',
'level': 'level',
'type': 'type',
'map_type': 'map_type',
'group_id': 'group_id',
'project_id': 'tenant_id',
}
try:
threshold = cc.hashmap.thresholds.get(threshold_id=args.threshold_id)
@@ -370,9 +355,6 @@ def do_hashmap_threshold_update(cc, args={}):
type=_bool_strict, metavar='{True,False}',
help='If True, list only orhpaned thresholds',
required=False)
@utils.arg('-p', '--project-id',
help='Project/tenant id',
required=False)
def do_hashmap_threshold_list(cc, args={}):
"""List thresholds."""
if (args.group_id is None and
@@ -389,14 +371,14 @@ def do_hashmap_threshold_list(cc, args={}):
else:
field_labels = ['Threshold id', 'Level', 'Cost',
'Type', 'Field id',
'Service id', 'Group id', 'Tenant id']
'Service id', 'Group id']
fields = ['threshold_id', 'level', 'cost',
'type', 'field_id',
'service_id', 'group_id', 'tenant_id']
'map_type', 'field_id',
'service_id', 'group_id']
utils.print_list(thresholds, fields, field_labels, sortby=0)
@utils.arg('-i', '--threshold-id',
@utils.arg('-t', '--threshold-id',
help='Threshold uuid',
required=True)
def do_hashmap_threshold_delete(cc, args={}):
@@ -407,7 +389,7 @@ def do_hashmap_threshold_delete(cc, args={}):
raise exc.CommandError('Threshold not found: %s' % args.threshold_id)
@utils.arg('-i', '--threshold-id',
@utils.arg('-t', '--threshold-id',
help='Threshold uuid',
required=True)
def do_hashmap_threshold_get(cc, args={}):
@@ -419,7 +401,7 @@ def do_hashmap_threshold_get(cc, args={}):
utils.print_dict(threshold.to_dict())
@utils.arg('-i', '--threshold-id',
@utils.arg('-t', '--threshold-id',
help='Threshold uuid',
required=True)
def do_hashmap_threshold_group(cc, args={}):

View File

@@ -0,0 +1,42 @@
Name: python-cloudkittyclient
Version: @VERSION@
Release: 1%{?dist}
Summary: Client library for CloudKitty
License: ASL 2.0
Source: %{name}-%{version}.tar.gz
BuildArch: noarch
BuildRequires: python-setuptools
BuildRequires: python-pbr
BuildRequires: python-keystoneclient
BuildRequires: python-stevedore
BuildRequires: python-babel
Requires: python-keystoneclient
Requires: python-stevedore
Requires: python-babel
%description
Client library for CloudKitty
%prep
%setup -q
%build
%{__python} setup.py build
%install
%{__python} setup.py install -O1 --skip-build --root %{buildroot}
#export PYTHONPATH="$( pwd ):$PYTHONPATH"
#sphinx-build -b html doc/source html
%files
%{_bindir}/*
%{python_sitelib}/*
#%doc html
%changelog
* Sun Apr 19 2015 Gauvain Pocentek <gauvain.pocentek@objectif-libre.com> @VERSION@-1
- Initial release

0
doc/source/conf.py Normal file → Executable file
View File

View File

@@ -3,10 +3,9 @@
# process, which may cause wedges in the gate later.
pbr>=1.6 # Apache-2.0
Babel>=2.3.4 # BSD
python-keystoneclient!=1.8.0,!=2.1.0,>=1.7.0 # Apache-2.0
stevedore>=1.10.0 # Apache-2.0
Babel>=1.3 # BSD
python-keystoneclient!=1.8.0,!=2.1.0,>=1.6.0 # Apache-2.0
stevedore>=1.5.0 # Apache-2.0
oslo.i18n>=2.1.0 # Apache-2.0
oslo.serialization>=1.10.0 # Apache-2.0
oslo.utils>=3.11.0 # Apache-2.0
PrettyTable<0.8,>=0.7 # BSD
oslo.utils>=3.5.0 # Apache-2.0

View File

@@ -5,6 +5,7 @@
hacking<0.10,>=0.9.2
coverage>=3.6 # Apache-2.0
discover # BSD
python-subunit>=0.0.18 # Apache-2.0/BSD
sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 # BSD
oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0