Created shell support for hosts_list and sub command parsing.
Added hosts_shell, tests_hosts_shell, and common package for cliutils to support handling hosts-list sub command with no arguments. In parallel, the ability to handle subcommand parsing was implemented in a basic form in order to implement and test "craton host-list" with no additional args. Added optional arguments to craton shell and accompanying unit tests for username, password, and url to enable creating a session and instantiate the client properly to enable other unit tests without hardcoding dummy values. Change-Id: I05a42a06e0436e7a0de6b898d4d6e50168e6dd36
This commit is contained in:
1
cratonclient/common/__init__.py
Normal file
1
cratonclient/common/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
"""Common Craton common classes and functions."""
|
||||||
109
cratonclient/common/cliutils.py
Normal file
109
cratonclient/common/cliutils.py
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# 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.
|
||||||
|
"""Craton CLI helper classes and functions."""
|
||||||
|
import os
|
||||||
|
import prettytable
|
||||||
|
import six
|
||||||
|
|
||||||
|
from oslo_utils import encodeutils
|
||||||
|
|
||||||
|
|
||||||
|
def arg(*args, **kwargs):
|
||||||
|
"""Decorator for CLI args.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
>>> @arg("name", help="Name of the new entity.")
|
||||||
|
... def entity_create(args):
|
||||||
|
... pass
|
||||||
|
"""
|
||||||
|
def _decorator(func):
|
||||||
|
"""Decorator definition."""
|
||||||
|
add_arg(func, *args, **kwargs)
|
||||||
|
return func
|
||||||
|
|
||||||
|
return _decorator
|
||||||
|
|
||||||
|
|
||||||
|
def add_arg(func, *args, **kwargs):
|
||||||
|
"""Bind CLI arguments to a shell.py `do_foo` function."""
|
||||||
|
if not hasattr(func, 'arguments'):
|
||||||
|
func.arguments = []
|
||||||
|
|
||||||
|
# NOTE(sirp): avoid dups that can occur when the module is shared across
|
||||||
|
# tests.
|
||||||
|
if (args, kwargs) not in func.arguments:
|
||||||
|
# Because of the semantics of decorator composition if we just append
|
||||||
|
# to the options list positional options will appear to be backwards.
|
||||||
|
func.arguments.insert(0, (args, kwargs))
|
||||||
|
|
||||||
|
|
||||||
|
def print_list(objs, fields, formatters=None, sortby_index=0,
|
||||||
|
mixed_case_fields=None, field_labels=None):
|
||||||
|
"""Print a list or objects as a table, one row per object.
|
||||||
|
|
||||||
|
:param objs: iterable of :class:`Resource`
|
||||||
|
:param fields: attributes that correspond to columns, in order
|
||||||
|
:param formatters: `dict` of callables for field formatting
|
||||||
|
:param sortby_index: index of the field for sorting table rows
|
||||||
|
:param mixed_case_fields: fields corresponding to object attributes that
|
||||||
|
have mixed case names (e.g., 'serverId')
|
||||||
|
:param field_labels: Labels to use in the heading of the table, default to
|
||||||
|
fields.
|
||||||
|
"""
|
||||||
|
formatters = formatters or {}
|
||||||
|
mixed_case_fields = mixed_case_fields or []
|
||||||
|
field_labels = field_labels or fields
|
||||||
|
if len(field_labels) != len(fields):
|
||||||
|
raise ValueError("Field labels list %(labels)s has different number "
|
||||||
|
"of elements than fields list %(fields)s",
|
||||||
|
{'labels': field_labels, 'fields': fields})
|
||||||
|
|
||||||
|
if sortby_index is None:
|
||||||
|
kwargs = {}
|
||||||
|
else:
|
||||||
|
kwargs = {'sortby': field_labels[sortby_index]}
|
||||||
|
pt = prettytable.PrettyTable(field_labels)
|
||||||
|
pt.align = 'l'
|
||||||
|
|
||||||
|
for o in objs:
|
||||||
|
row = []
|
||||||
|
for field in fields:
|
||||||
|
if field in formatters:
|
||||||
|
row.append(formatters[field](o))
|
||||||
|
else:
|
||||||
|
if field in mixed_case_fields:
|
||||||
|
field_name = field.replace(' ', '_')
|
||||||
|
else:
|
||||||
|
field_name = field.lower().replace(' ', '_')
|
||||||
|
data = getattr(o, field_name, '')
|
||||||
|
row.append(data)
|
||||||
|
pt.add_row(row)
|
||||||
|
|
||||||
|
if six.PY3:
|
||||||
|
print(encodeutils.safe_encode(pt.get_string(**kwargs)).decode())
|
||||||
|
else:
|
||||||
|
print(encodeutils.safe_encode(pt.get_string(**kwargs)))
|
||||||
|
|
||||||
|
|
||||||
|
def env(*args, **kwargs):
|
||||||
|
"""Return the first environment variable set.
|
||||||
|
|
||||||
|
If all are empty, defaults to '' or keyword arg `default`.
|
||||||
|
"""
|
||||||
|
for arg in args:
|
||||||
|
value = os.environ.get(arg)
|
||||||
|
if value:
|
||||||
|
return value
|
||||||
|
return kwargs.get('default', '')
|
||||||
@@ -1,12 +1 @@
|
|||||||
# 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.
|
|
||||||
"""Command-line application that interfaces with Craton API."""
|
"""Command-line application that interfaces with Craton API."""
|
||||||
|
|||||||
@@ -19,6 +19,13 @@ import sys
|
|||||||
|
|
||||||
from oslo_utils import encodeutils
|
from oslo_utils import encodeutils
|
||||||
|
|
||||||
|
from cratonclient import __version__
|
||||||
|
from cratonclient import session as craton
|
||||||
|
|
||||||
|
from cratonclient.common import cliutils
|
||||||
|
from cratonclient.shell.v1 import shell
|
||||||
|
from cratonclient.v1 import client
|
||||||
|
|
||||||
|
|
||||||
class CratonShell(object):
|
class CratonShell(object):
|
||||||
"""Class used to handle shell definition and parsing."""
|
"""Class used to handle shell definition and parsing."""
|
||||||
@@ -36,18 +43,95 @@ class CratonShell(object):
|
|||||||
|
|
||||||
parser.add_argument('-h', '--help',
|
parser.add_argument('-h', '--help',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help=argparse.SUPPRESS)
|
help=argparse.SUPPRESS,
|
||||||
|
)
|
||||||
|
parser.add_argument('--version',
|
||||||
|
action='version',
|
||||||
|
version=__version__,
|
||||||
|
)
|
||||||
|
parser.add_argument('--craton-url',
|
||||||
|
default=cliutils.env('CRATON_URL'),
|
||||||
|
help='Defaults to env[CRATON_URL]',
|
||||||
|
)
|
||||||
|
parser.add_argument('--craton-project-id',
|
||||||
|
type=int,
|
||||||
|
default=1,
|
||||||
|
help='Defaults to 1',
|
||||||
|
)
|
||||||
|
parser.add_argument('--os-username',
|
||||||
|
default=cliutils.env('OS_USERNAME'),
|
||||||
|
help='Defaults to env[OS_USERNAME]',
|
||||||
|
)
|
||||||
|
parser.add_argument('--os-password',
|
||||||
|
default=cliutils.env('OS_PASSWORD'),
|
||||||
|
help='Defaults to env[OS_PASSWORD]',
|
||||||
|
)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
# NOTE(cmspence): Credit for this get_subcommand_parser function
|
||||||
|
# goes to the magnumclient developers and contributors.
|
||||||
|
def get_subcommand_parser(self):
|
||||||
|
"""Get subcommands by parsing COMMAND_MODULES."""
|
||||||
|
parser = self.get_base_parser()
|
||||||
|
|
||||||
|
self.subcommands = {}
|
||||||
|
subparsers = parser.add_subparsers(metavar='<subcommand>',
|
||||||
|
dest='subparser_name')
|
||||||
|
command_modules = shell.COMMAND_MODULES
|
||||||
|
for command_module in command_modules:
|
||||||
|
self._find_subparsers(subparsers, command_module)
|
||||||
|
self._find_subparsers(subparsers, self)
|
||||||
|
return parser
|
||||||
|
|
||||||
|
# NOTE(cmspence): Credit for this function goes to the
|
||||||
|
# magnumclient developers and contributors.
|
||||||
|
def _find_subparsers(self, subparsers, actions_module):
|
||||||
|
"""Find subparsers by looking at *_shell files."""
|
||||||
|
help_formatter = argparse.HelpFormatter
|
||||||
|
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
|
||||||
|
command = attr[3:].replace('_', '-')
|
||||||
|
callback = getattr(actions_module, attr)
|
||||||
|
desc = callback.__doc__ or ''
|
||||||
|
action_help = desc.strip()
|
||||||
|
arguments = getattr(callback, 'arguments', [])
|
||||||
|
subparser = (subparsers.add_parser(command,
|
||||||
|
help=action_help,
|
||||||
|
description=desc,
|
||||||
|
add_help=False,
|
||||||
|
formatter_class=help_formatter)
|
||||||
|
)
|
||||||
|
subparser.add_argument('-h', '--help',
|
||||||
|
action='help',
|
||||||
|
help=argparse.SUPPRESS)
|
||||||
|
self.subcommands[command] = subparser
|
||||||
|
for (args, kwargs) in arguments:
|
||||||
|
subparser.add_argument(*args, **kwargs)
|
||||||
|
subparser.set_defaults(func=callback)
|
||||||
|
|
||||||
def main(self, argv):
|
def main(self, argv):
|
||||||
"""Main entry-point for cratonclient shell argument parsing."""
|
"""Main entry-point for cratonclient shell argument parsing."""
|
||||||
parser = self.get_base_parser()
|
parser = self.get_base_parser()
|
||||||
(options, args) = parser.parse_known_args(argv)
|
(options, args) = parser.parse_known_args(argv)
|
||||||
|
subcommand_parser = (
|
||||||
|
self.get_subcommand_parser()
|
||||||
|
)
|
||||||
|
self.parser = subcommand_parser
|
||||||
|
|
||||||
if options.help or not argv:
|
if options.help or not argv:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
args = subcommand_parser.parse_args(argv)
|
||||||
|
|
||||||
|
session = craton.Session(
|
||||||
|
username=args.os_username,
|
||||||
|
token=args.os_password,
|
||||||
|
project_id=args.craton_project_id,
|
||||||
|
)
|
||||||
|
self.cc = client.Client(session, args.craton_url)
|
||||||
|
|
||||||
|
args.func(self.cc, args)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main entry-point for cratonclient's CLI."""
|
"""Main entry-point for cratonclient's CLI."""
|
||||||
|
|||||||
1
cratonclient/shell/v1/__init__.py
Normal file
1
cratonclient/shell/v1/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
"""Shell libraries for version 1 of Craton's API."""
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
# 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
|
# not use this file except in compliance with the License. You may obtain
|
||||||
# a copy of the License at
|
# a copy of the License at
|
||||||
@@ -9,8 +11,13 @@
|
|||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
"""Hosts resource and resource shell wrapper."""
|
||||||
"""Command-line interface to the OpenStack Craton API V1."""
|
from cratonclient.common import cliutils
|
||||||
|
|
||||||
|
|
||||||
# TODO(cmspence): from cratonclient.v1 import client
|
def do_host_list(cc, args):
|
||||||
|
"""Print list of hosts which are registered with the Craton service."""
|
||||||
|
params = {}
|
||||||
|
columns = ['id', 'name']
|
||||||
|
hosts = cc.hosts.list(args.craton_project_id, **params)
|
||||||
|
cliutils.print_list(hosts, columns)
|
||||||
19
cratonclient/shell/v1/shell.py
Normal file
19
cratonclient/shell/v1/shell.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# 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.
|
||||||
|
"""Command-line interface to the OpenStack Craton API V1."""
|
||||||
|
from cratonclient.shell.v1 import hosts_shell
|
||||||
|
|
||||||
|
COMMAND_MODULES = [
|
||||||
|
# TODO(cmspence): project_shell, regions_shell,
|
||||||
|
# cell_shell, device_shell, user_shell, etc.
|
||||||
|
hosts_shell,
|
||||||
|
]
|
||||||
27
cratonclient/tests/unit/test_hosts_shell.py
Normal file
27
cratonclient/tests/unit/test_hosts_shell.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# 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.
|
||||||
|
|
||||||
|
"""Tests for `cratonclient.shell.v1.hosts_shell` module."""
|
||||||
|
|
||||||
|
import mock
|
||||||
|
|
||||||
|
from cratonclient.tests import base
|
||||||
|
|
||||||
|
|
||||||
|
class TestHostsShell(base.ShellTestCase):
|
||||||
|
"""Test our craton hosts shell commands."""
|
||||||
|
|
||||||
|
@mock.patch('cratonclient.v1.hosts.HostManager.list')
|
||||||
|
def test_host_list_success(self, mock_list):
|
||||||
|
"""Verify that no arguments prints out all project hosts."""
|
||||||
|
self.shell('host-list')
|
||||||
|
self.assertTrue(mock_list.called)
|
||||||
@@ -58,6 +58,43 @@ class TestMainShell(base.ShellTestCase):
|
|||||||
self.assertThat((stdout + stderr),
|
self.assertThat((stdout + stderr),
|
||||||
matchers.MatchesRegex(r, self.re_options))
|
matchers.MatchesRegex(r, self.re_options))
|
||||||
|
|
||||||
|
@mock.patch('cratonclient.v1.client.Client')
|
||||||
|
def test_main_craton_url(self, mock_client):
|
||||||
|
"""Verify that craton-url command is used for client connection."""
|
||||||
|
self.shell('--craton-url http://localhost:9999/ host-list')
|
||||||
|
mock_client.assert_called_with(mock.ANY, 'http://localhost:9999/')
|
||||||
|
|
||||||
|
@mock.patch('cratonclient.session.Session')
|
||||||
|
@mock.patch('cratonclient.v1.client.Client')
|
||||||
|
def test_main_craton_project_id(self, mock_client, mock_session):
|
||||||
|
"""Verify --craton-project-id command is used for client connection."""
|
||||||
|
self.shell('--craton-project-id 99 host-list')
|
||||||
|
mock_session.assert_called_with(username=mock.ANY,
|
||||||
|
token=mock.ANY,
|
||||||
|
project_id=99)
|
||||||
|
mock_client.assert_called_with(mock.ANY, mock.ANY)
|
||||||
|
|
||||||
|
@mock.patch('cratonclient.session.Session')
|
||||||
|
@mock.patch('cratonclient.v1.client.Client')
|
||||||
|
def test_main_os_username(self, mock_client, mock_session):
|
||||||
|
"""Verify --os-username command is used for client connection."""
|
||||||
|
self.shell('--os-username test host-list')
|
||||||
|
mock_session.assert_called_with(username='test',
|
||||||
|
token=mock.ANY,
|
||||||
|
project_id=mock.ANY)
|
||||||
|
mock_client.assert_called_with(mock.ANY, mock.ANY)
|
||||||
|
|
||||||
|
@mock.patch('cratonclient.session.Session')
|
||||||
|
@mock.patch('cratonclient.v1.client.Client')
|
||||||
|
def test_main_os_password(self, mock_client, mock_session):
|
||||||
|
"""Verify --os-password command is used for client connection."""
|
||||||
|
self.shell('--os-password test host-list')
|
||||||
|
mock_session.assert_called_with(username=mock.ANY,
|
||||||
|
token='test',
|
||||||
|
project_id=mock.ANY)
|
||||||
|
|
||||||
|
mock_client.assert_called_with(mock.ANY, mock.ANY)
|
||||||
|
|
||||||
@mock.patch('cratonclient.shell.main.CratonShell.main')
|
@mock.patch('cratonclient.shell.main.CratonShell.main')
|
||||||
def test_main_catches_exception(self, cratonShellMainMock):
|
def test_main_catches_exception(self, cratonShellMainMock):
|
||||||
"""Verify exceptions will be caught and shell will exit properly."""
|
"""Verify exceptions will be caught and shell will exit properly."""
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
# of appearance. Changing the order has an impact on the overall integration
|
# of appearance. Changing the order has an impact on the overall integration
|
||||||
# process, which may cause wedges in the gate later.
|
# process, which may cause wedges in the gate later.
|
||||||
|
|
||||||
pbr>=1.6
|
PrettyTable>=0.7,<0.8 # BSD
|
||||||
|
six>=1.9.0 # MIT
|
||||||
|
oslo.utils>=3.16.0 # Apache-2.0
|
||||||
|
pbr>=1.6 # Apache-2.0
|
||||||
requests>=2.10.0 # Apache-2.0
|
requests>=2.10.0 # Apache-2.0
|
||||||
oslo.utils>=3.11.0 # Apache-2.0
|
|
||||||
|
|||||||
@@ -43,4 +43,4 @@ input_file = cratonclient/locale/cratonclient.pot
|
|||||||
[extract_messages]
|
[extract_messages]
|
||||||
keywords = _ gettext ngettext l_ lazy_gettext
|
keywords = _ gettext ngettext l_ lazy_gettext
|
||||||
mapping_file = babel.cfg
|
mapping_file = babel.cfg
|
||||||
output_file = cratonclient/locale/cratonclient.pot
|
output_file = cratonclient/locale/cratonclient.pot
|
||||||
Reference in New Issue
Block a user