Init commit for distil client

Change-Id: I335b53edb9b1d2d9d7615fb8d7cec71b653c6ace
This commit is contained in:
Fei Long Wang 2016-06-09 11:37:27 +12:00
parent d62eee5ce9
commit 4b7f8361a1
19 changed files with 765 additions and 0 deletions

23
.gitignore vendored Normal file
View File

@ -0,0 +1,23 @@
.coverage
subunit.log
.venv
*,cover
cover
*.pyc
.idea
*.sw?
*~
AUTHORS
build
dist
python_zaqarclient.egg-info
ChangeLog
run_tests.err.log
.testrepository
.tox
doc/source/api
doc/build
*.egg
.eggs/*
# Files created by releasenotes build
releasenotes/build

4
.testr.conf Normal file
View File

@ -0,0 +1,4 @@
[DEFAULT]
test_command=${PYTHON:-python} -m subunit.run discover -t ./ ${OS_TEST_PATH:-./distilclient/tests/unit} $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list

8
README.rst Normal file
View File

@ -0,0 +1,8 @@
Client Library for Catalyst Cloud Rating API
How to build package for Pypi
=============================
1. Change the version in setup.cfg
2. export PBR_VERSION=<version>
3. python setup.py sdist
4. python setup.py sdist upload

0
distilclient/__init__.py Normal file
View File

117
distilclient/client.py Normal file
View File

@ -0,0 +1,117 @@
# Copyright (C) 2014 Catalyst IT Ltd
#
# 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 requests
from keystoneclient.v2_0.client import Client as Keystone
from requests.exceptions import ConnectionError
from urlparse import urljoin
class Client(object):
def __init__(self, distil_url=None, os_auth_token=None,
os_username=None, os_password=None,
os_tenant_id=None, os_tenant_name=None,
os_auth_url=None, os_region_name=None,
os_cacert=None, insecure=False,
os_service_type='rating', os_endpoint_type='publicURL'):
self.insecure = insecure
if os_auth_token and distil_url:
self.auth_token = os_auth_token
self.endpoint = distil_url
else:
ks = Keystone(username=os_username,
password=os_password,
tenant_id=os_tenant_id,
tenant_name=os_tenant_name,
auth_url=os_auth_url,
region_name=os_region_name,
cacert=os_cacert,
insecure=insecure)
if os_auth_token:
self.auth_token = os_auth_token
else:
self.auth_token = ks.auth_token
if distil_url:
self.endpoint = distil_url
else:
self.endpoint = ks.service_catalog.url_for(
service_type=os_service_type,
endpoint_type=os_endpoint_type
)
def collect_usage(self):
url = urljoin(self.endpoint, "collect_usage")
headers = {"Content-Type": "application/json",
"X-Auth-Token": self.auth_token}
try:
response = requests.post(url, headers=headers,
verify=not self.insecure)
if response.status_code != 200:
raise AttributeError("Usage cycle failed: %s code: %s" %
(response.text, response.status_code))
else:
return response.json()
except ConnectionError as e:
print e
def last_collected(self):
url = urljoin(self.endpoint, "last_collected")
headers = {"Content-Type": "application/json",
"X-Auth-Token": self.auth_token}
try:
response = requests.get(url, headers=headers,
verify=not self.insecure)
if response.status_code != 200:
raise AttributeError("Get last collected failed: %s code: %s" %
(response.text, response.status_code))
else:
return response.json()
except ConnectionError as e:
print e
def get_usage(self, tenant, start, end):
return self._query_usage(tenant, start, end, "get_usage")
def get_rated(self, tenant, start, end):
return self._query_usage(tenant, start, end, "get_rated")
def _query_usage(self, tenant, start, end, endpoint):
url = urljoin(self.endpoint, endpoint)
headers = {"X-Auth-Token": self.auth_token}
params = {"tenant": tenant,
"start": start,
"end": end
}
try:
response = requests.get(url, headers=headers,
params=params,
verify=not self.insecure)
if response.status_code != 200:
raise AttributeError("Get usage failed: %s code: %s" %
(response.text, response.status_code))
else:
return response.json()
except ConnectionError as e:
print e

17
distilclient/exc.py Normal file
View File

@ -0,0 +1,17 @@
# Copyright (C) 2014 Catalyst IT Ltd
#
# 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.
class CommandError(BaseException):
"""Invalid usage of CLI."""

187
distilclient/shell.py Normal file
View File

@ -0,0 +1,187 @@
# Copyright (C) 2014 Catalyst IT Ltd
#
# 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.
#!/usr/bin/env python
import os
import json
import sys
from client import Client
import exc
if __name__ == '__main__':
main(sys.argv[1:])
def main():
import argparse
parser = argparse.ArgumentParser()
#main args:
parser.add_argument('-k', '--insecure',
default=False,
action='store_true',
help="Explicitly allow distilclient to "
"perform \"insecure\" SSL (https) requests. "
"The server's certificate will "
"not be verified against any certificate "
"authorities. This option should be used with "
"caution.")
parser.add_argument('--os-cacert',
metavar='<ca-certificate-file>',
dest='os_cacert',
help='Path of CA TLS certificate(s) used to verify'
'the remote server\'s certificate. Without this '
'option distil looks for the default system '
'CA certificates.')
parser.add_argument('--os-username',
default=os.environ.get('OS_USERNAME'),
help='Defaults to env[OS_USERNAME]')
parser.add_argument('--os-password',
default=os.environ.get('OS_PASSWORD'),
help='Defaults to env[OS_PASSWORD]')
parser.add_argument('--os-tenant-id',
default=os.environ.get('OS_TENANT_ID'),
help='Defaults to env[OS_TENANT_ID]')
parser.add_argument('--os-tenant-name',
default=os.environ.get('OS_TENANT_NAME'),
help='Defaults to env[OS_TENANT_NAME]')
parser.add_argument('--os-auth-url',
default=os.environ.get('OS_AUTH_URL'),
help='Defaults to env[OS_AUTH_URL]')
parser.add_argument('--os-region-name',
default=os.environ.get('OS_REGION_NAME'),
help='Defaults to env[OS_REGION_NAME]')
parser.add_argument('--os-auth-token',
default=os.environ.get('OS_AUTH_TOKEN'),
help='Defaults to env[OS_AUTH_TOKEN]')
parser.add_argument('--os-service-type',
help='Defaults to env[OS_SERVICE_TYPE].',
default='rating')
parser.add_argument('--os-endpoint-type',
help='Defaults to env[OS_ENDPOINT_TYPE].',
default='publicURL')
parser.add_argument("--distil-url",
help="Distil endpoint, defaults to env[DISTIL_URL]",
default=os.environ.get('DISTIL_URL'))
# commands:
subparsers = parser.add_subparsers(help='commands', dest='command')
usage_parser = subparsers.add_parser(
'collect-usage', help=('process usage for all tenants'))
last_collected_parser = subparsers.add_parser(
'last-collected', help=('get last collected time'))
get_usage_parser = subparsers.add_parser(
'get-usage', help=('get raw aggregated usage'))
get_usage_parser.add_argument(
"-t", "--tenant", dest="tenant",
help='Tenant to get usage for',
required=True)
get_usage_parser.add_argument(
"-s", "--start", dest="start",
help="Start time",
required=True)
get_usage_parser.add_argument(
"-e", "--end", dest="end",
help="End time",
required=True)
get_rated_parser = subparsers.add_parser(
'get-rated', help=('get rated usage'))
get_rated_parser.add_argument(
"-t", "--tenant", dest="tenant",
help='Tenant to get usage for',
required=True)
get_rated_parser.add_argument(
"-s", "--start", dest="start",
help="Start time",
required=True)
get_rated_parser.add_argument(
"-e", "--end", dest="end",
help="End time")
args = parser.parse_args()
if not (args.os_auth_token and args.distil_url):
if not args.os_username:
raise exc.CommandError("You must provide a username via "
"either --os-username or via "
"env[OS_USERNAME]")
if not args.os_password:
raise exc.CommandError("You must provide a password via "
"either --os-password or via "
"env[OS_PASSWORD]")
if not (args.os_tenant_id or args.os_tenant_name):
raise exc.CommandError("You must provide a tenant_id via "
"either --os-tenant-id or via "
"env[OS_TENANT_ID]")
if not args.os_auth_url:
raise exc.CommandError("You must provide an auth url via "
"either --os-auth-url or via "
"env[OS_AUTH_URL]")
kwargs = vars(args)
client = Client(kwargs.get('distil_url', None),
kwargs.get('os_auth_token', None),
kwargs.get('os_username', None),
kwargs.get('os_password', None),
kwargs.get('os_tenant_id', None),
kwargs.get('os_tenant_name', None),
kwargs.get('os_auth_url', None),
kwargs.get('os_region_name', None),
kwargs.get('os_cacert', None),
kwargs.get('insecure', None),
kwargs.get('os_service_type', None),
kwargs.get('os_endpoint_type', None))
if args.command == 'collect-usage':
response = client.collect_usage()
print json.dumps(response, indent=2)
if args.command == 'last-collected':
response = client.last_collected()
print json.dumps(response, indent=2)
if args.command == 'get-usage':
response = client.get_usage(args.tenant, args.start, args.end)
print json.dumps(response, indent=2)
if args.command == 'get-rated':
response = client.get_rated(args.tenant, args.start, args.end)
print json.dumps(response, indent=2)

View File

View File

View File

@ -0,0 +1,21 @@
# Copyright (C) 2016 Catalyst IT Ltd
#
# 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 testtools
class ClientTest(testtools.TestCase):
def test_fake(self):
self.assertTrue(1 == 1)

29
distilclient/version.py Normal file
View File

@ -0,0 +1,29 @@
# Copyright (c) 2013 Red Hat, Inc.
# Copyright 2012 OpenStack LLC
#
# 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 pkg_resources
try:
# First, try to get our version out of PKG-INFO. If we're installed,
# this'll let us find our version without pulling in pbr. After all, if
# we're installed on a system, we're not in a Git-managed source tree, so
# pbr doesn't really buy us anything.
version_string = pkg_resources.get_provider(
pkg_resources.Requirement.parse('python-distilclient')).version
except pkg_resources.DistributionNotFound:
# No PKG-INFO? We're probably running from a checkout, then. Let pbr do
# its thing to figure out a version number.
import pbr.version
version_string = str(pbr.version.VersionInfo('python-distilclient'))

232
doc/conf.py Normal file
View File

@ -0,0 +1,232 @@
# -*- coding: utf-8 -*-
#
# Distil documentation build configuration file, created by
# sphinx-quickstart on Sat May 1 15:17:47 2010.
#
# This file is execfile()d with the current directory set
# to its containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys
import os
import subprocess
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('../../'))
sys.path.insert(0, os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('./'))
# -- General configuration ----------------------------------------------------
# Add any Sphinx extension module names here, as strings.
# They can be extensions coming with Sphinx (named 'sphinx.ext.*')
# or your custom ones.
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.coverage',
'sphinx.ext.ifconfig',
'sphinx.ext.intersphinx',
'sphinx.ext.graphviz',
'oslosphinx',
]
# autodoc generation is a bit aggressive and a nuisance
# when doing heavy text edit cycles. Execute "export SPHINX_DEBUG=1"
# in your terminal to disable
todo_include_todos = True
# Add any paths that contain templates here, relative to this directory.
# Changing the path so that the Hudson build output contains GA code
# and the source docs do not contain the code so local, offline sphinx builds
# are "clean."
templates_path = []
if os.getenv('HUDSON_PUBLISH_DOCS'):
templates_path = ['_ga', '_templates']
else:
templates_path = ['_templates']
# The suffix of source filenames.
source_suffix = '.rst'
# The encoding of source files.
#source_encoding = 'utf-8'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'distilclient'
copyright = u'2010-present, OpenStack Foundation'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
from distilclient.version import version_string
# The full version, including alpha/beta/rc tags.
version = release = version_string
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
# List of documents that shouldn't be included in the build.
unused_docs = [
'api_ext/rst_extension_template',
'installer',
]
# List of directories, relative to source directory, that shouldn't be searched
# for source files.
exclude_trees = []
# The reST default role (used for this markup: `text`) to use
# for all documents.
#default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
add_module_names = False
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting.
modindex_common_prefix = ['distilclient.']
# -- Options for man page output ----------------------------------------------
# Grouping the document tree for man pages.
# List of tuples 'sourcefile', 'target', u'title', u'Authors name', 'manual'
# -- Options for HTML output --------------------------------------------------
# The theme to use for HTML and HTML Help pages. Major themes that come with
# Sphinx are currently 'default' and 'sphinxdoc'.
# html_theme_path = ["."]
# html_theme = '_theme'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
#html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = []
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'
git_cmd = ["git", "log", "--pretty=format:'%ad, commit %h'",
"--date=local", "-n1"]
html_last_updated_fmt = subprocess.Popen(
git_cmd, stdout=subprocess.PIPE).communicate()[0]
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}
# If false, no module index is generated.
#html_use_modindex = True
# If false, no index is generated.
#html_use_index = True
# If true, the index is split into individual pages for each letter.
#html_split_index = False
# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''
# If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = ''
# Output file base name for HTML help builder.
htmlhelp_basename = 'distilclientdoc'
# -- Options for LaTeX output -------------------------------------------------
# The paper size ('letter' or 'a4').
#latex_paper_size = 'letter'
# The font size ('10pt', '11pt' or '12pt').
#latex_font_size = '10pt'
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass
# [howto/manual]).
latex_documents = [
('index', 'Distilclient.tex', u'Distil Library Documentation',
u'Anso Labs, LLC', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False
# Additional stuff for the LaTeX preamble.
#latex_preamble = ''
# Documents to append as an appendix to all manuals.
#latex_appendices = []
# If false, no module index is generated.
#latex_use_modindex = True

0
doc/index.rst Normal file
View File

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
six>=1.9.0 # MIT
pbr>=1.6 # Apache-2.0
python-keystoneclient>=1.7.0,!=1.8.0,!=2.1.0 # Apache-2.0

39
setup.cfg Normal file
View File

@ -0,0 +1,39 @@
[metadata]
name = python-distilclient
summary = Client library for Catalyst Cloud Rating API
description-file =
README.rst
license = Apache License, Version 2.0
author = Catalyst IT Cloud team
author-email = cloud@catalyst.net.nz
home-page = http://www.catalyst.net.nz/
classifier =
Development Status :: 5 - Production/Stable
Environment :: Console
Environment :: OpenStack
Intended Audience :: Information Technology
Intended Audience :: Developers
Intended Audience :: System Administrators
License :: OSI Approved :: Apache Software License
Operating System :: POSIX :: Linux
Programming Language :: Python
Programming Language :: Python :: 2.7
[global]
setup-hooks =
pbr.hooks.setup_hook
[entry_points]
console_scripts =
distil = distilclient.shell:main
[files]
packages =
distilclient
[nosetests]
where=tests
verbosity=2
[wheel]
universal = 1

14
setup.py Normal file
View File

@ -0,0 +1,14 @@
import setuptools
# In python < 2.7.4, a lazy loading of package `pbr` will break
# setuptools if some other modules registered functions in `atexit`.
# solution from: http://bugs.python.org/issue15881#msg170215
try:
import multiprocessing # noqa
except ImportError:
pass
setuptools.setup(
setup_requires=['pbr>=1.8'],
pbr=True)

21
test-requirements.txt Normal file
View File

@ -0,0 +1,21 @@
# The order of packages is significant, because pip processes them in the order
# of appearance. Changing the order has an impact on the overall integration
# process, which may cause wedges in the gate later.
hacking>=0.11.0,<0.12 # Apache-2.0
coverage>=3.6 # Apache-2.0
discover # BSD
fixtures>=3.0.0 # Apache-2.0/BSD
keyring>=5.5.1 # MIT/PSF
mock>=2.0 # BSD
requests-mock>=0.7.0 # Apache-2.0
sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 # BSD
os-client-config>=1.13.1 # Apache-2.0
oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0
testrepository>=0.0.18 # Apache-2.0/BSD
testscenarios>=0.4 # Apache-2.0/BSD
testtools>=1.4.0 # MIT
tempest>=11.0.0 # Apache-2.0
# releasenotes
reno>=1.6.2 # Apache2

50
tox.ini Normal file
View File

@ -0,0 +1,50 @@
[tox]
minversion = 1.6
envlist = py34,py27,pep8
skipsdist = True
[testenv]
usedevelop = True
# Customize pip command, add -U to force updates.
install_command = pip install -U {opts} {packages}
setenv = VIRTUAL_ENV={envdir}
NOSE_WITH_OPENSTACK=1
NOSE_OPENSTACK_COLOR=1
NOSE_OPENSTACK_RED=0.05
NOSE_OPENSTACK_YELLOW=0.025
NOSE_OPENSTACK_SHOW_ELAPSED=1
NOSE_OPENSTACK_STDOUT=1
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands = python setup.py testr --slowest --testr-args='{posargs}'
whitelist_externals = find
[tox:jenkins]
sitepackages = True
[testenv:pep8]
commands = flake8
[testenv:cover]
setenv = NOSE_WITH_COVERAGE=1
[testenv:venv]
commands = {posargs}
[testenv:docs]
commands = python setup.py build_sphinx
[flake8]
# Following checks should be enabled in the future.
#
# H404 multi line docstring should start without a leading new line
# H405 multi line docstring summary not separated with an empty line
#
# Following checks are ignored on purpose.
#
# Additional checks are also ignored on purpose: F811, F821
ignore = F811,F821,H404,H405,H233,H306,E265,W291,W391,F841
show-source = True
exclude=.venv,.git,.tox,dist,*lib/python*,*egg,build,doc/source/conf.py,releasenotes