Adding cell client functionality.

Adds CLI and python cell crud functionality as well as testing.

Change-Id: I9d75746ddd9139d7ad43ad249b1ffbbb5c4015a4
Implements: blueprint craton-client-access-inventory (partial)
Closes-Bug: #1613346
Closes-Bug: #1613347
Closes-Bug: #1613349
Closes-Bug: #1613350
This commit is contained in:
Chris Spencer 2016-08-16 11:55:17 -07:00
parent a031680530
commit f3de28a73a
5 changed files with 513 additions and 1 deletions

View File

@ -0,0 +1,175 @@
# -*- 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.
"""Cells resource and resource shell wrapper."""
from cratonclient.common import cliutils
from cratonclient import exceptions as exc
from cratonclient.v1.cells import CELL_FIELDS as c_fields
@cliutils.arg('region',
metavar='<region>',
type=int,
help='ID of the region that the cell belongs to.')
@cliutils.arg('id',
metavar='<cell>',
type=int,
help='ID of the cell.')
def do_cell_show(cc, args):
"""Show detailed information about a cell."""
cell = cc.inventory(args.region).cells.get(args.id)
data = {f: getattr(cell, f, '') for f in c_fields}
cliutils.print_dict(data, wrap=72)
@cliutils.arg('-r', '--region',
metavar='<region>',
type=int,
required=True,
help='ID of the region that the cell belongs to.')
@cliutils.arg('--detail',
action='store_true',
default=False,
help='Show detailed information about the cells.')
@cliutils.arg('--limit',
metavar='<limit>',
type=int,
help='Maximum number of cells to return.')
@cliutils.arg('--sort-key',
metavar='<field>',
help='Cell field that will be used for sorting.')
@cliutils.arg('--sort-dir',
metavar='<direction>',
default='asc',
help='Sort direction: "asc" (default) or "desc".')
@cliutils.arg('--fields',
nargs='+',
metavar='<fields>',
default=[],
help='Comma-separated list of fields to display. '
'Only these fields will be fetched from the server. '
'Can not be used when "--detail" is specified')
def do_cell_list(cc, args):
"""Print list of cells which are registered with the Craton service."""
params = {}
default_fields = ['id', 'name']
if args.limit is not None:
if args.limit < 0:
raise exc.CommandError('Invalid limit specified. Expected '
'non-negative limit, got {0}'
.format(args.limit))
params['limit'] = args.limit
if args.detail:
fields = c_fields
params['detail'] = args.detail
elif args.fields:
fields = {x: c_fields[x] for x in args.fields}
else:
fields = {x: c_fields[x] for x in default_fields}
if args.sort_key is not None:
fields_map = dict(zip(fields.keys(), fields.keys()))
# TODO(cmspence): Do we want to allow sorting by field heading value?
try:
sort_key = fields_map[args.sort_key]
except KeyError:
raise exc.CommandError(
'{0} is an invalid key for sorting, valid values for '
'--sort-key are: {1}'.format(args.sort_key, c_fields.keys())
)
params['sort_key'] = sort_key
if args.sort_dir is not None:
if args.sort_dir not in ('asc', 'desc'):
raise exc.CommandError('Invalid sort direction specified. The '
'expected valid values for --sort-dir '
'are: "asc", "desc".')
params['sort_dir'] = args.sort_dir
cells = cc.inventory(args.region).cells.list(**params)
cliutils.print_list(cells, list(fields))
@cliutils.arg('-n', '--name',
metavar='<name>',
required=True,
help='Name of the cell.')
@cliutils.arg('-p', '--project',
dest='project_id',
metavar='<project>',
type=int,
required=True,
help='ID of the project that the cell belongs to.')
@cliutils.arg('-r', '--region',
dest='region_id',
metavar='<region>',
type=int,
required=True,
help='ID of the region that the cell belongs to.')
@cliutils.arg('--note',
help='Note about the cell.')
def do_cell_create(cc, args):
"""Register a new cell with the Craton service."""
fields = {k: v for (k, v) in vars(args).items()
if k in c_fields and not (v is None)}
cell = cc.inventory(args.region_id).cells.create(**fields)
data = {f: getattr(cell, f, '') for f in c_fields}
cliutils.print_dict(data, wrap=72)
@cliutils.arg('region',
metavar='<region>',
type=int,
help='Current ID of the region that the cell belongs to.')
@cliutils.arg('id',
metavar='<cell>',
type=int,
help='ID of the cell.')
@cliutils.arg('-n', '--name',
metavar='<name>',
required=True,
help='Name of the cell.')
@cliutils.arg('-p', '--project',
dest='project_id',
metavar='<project>',
type=int,
required=True,
help='Desired ID of the project that the cell should change to.')
@cliutils.arg('-r', '--region',
dest='region_id',
metavar='<region>',
type=int,
required=True,
help='Desired ID of the region that the cell should change to.')
@cliutils.arg('--note',
help='Note about the cell.')
def do_cell_update(cc, args):
"""Update a cell that is registered with the Craton service."""
fields = {k: v for (k, v) in vars(args).items()
if k in c_fields and not (v is None)}
cell = cc.inventory(args.region).cells.update(**fields)
data = {f: getattr(cell, f, '') for f in c_fields}
cliutils.print_dict(data, wrap=72)
@cliutils.arg('region',
metavar='<region>',
type=int,
help='ID of the region that the cell belongs to.')
@cliutils.arg('id',
metavar='<cell>',
type=int,
help='ID of the cell.')
def do_cell_delete(cc, args):
"""Delete a cell that is registered with the Craton service."""
response = cc.inventory(args.region).cells.delete(args.id)
print("Cell {0} was {1}successfully deleted.".
format(args.id, '' if response else 'un'))

View File

@ -10,11 +10,14 @@
# 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 cells_shell
from cratonclient.shell.v1 import hosts_shell
from cratonclient.shell.v1 import regions_shell
COMMAND_MODULES = [
# TODO(cmspence): project_shell, cell_shell, device_shell, user_shell, etc.
regions_shell,
hosts_shell,
cells_shell,
]

View File

@ -0,0 +1,277 @@
# 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.cells_shell` module."""
import mock
import re
from argparse import Namespace
from testtools import matchers
from cratonclient import exceptions as exc
from cratonclient.shell.v1 import cells_shell
from cratonclient.tests import base
from cratonclient.v1 import cells
class TestCellsShell(base.ShellTestCase):
"""Test our craton cells shell commands."""
re_options = re.DOTALL | re.MULTILINE
cell_valid_fields = None
cell_invalid_field = None
def setUp(self):
"""Setup required test fixtures."""
super(TestCellsShell, self).setUp()
self.cell_valid_fields = Namespace(project_id=1,
region_id=1,
name='mock_cell')
self.cell_invalid_field = Namespace(project_id=1,
region_id=1,
name='mock_cell',
invalid_foo='ignored')
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_success(self, mock_list):
"""Verify that no arguments prints out all project cells."""
self.shell('cell-list -r 1')
self.assertTrue(mock_list.called)
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_parse_param_success(self, mock_list):
"""Verify that success of parsing a subcommand argument."""
self.shell('cell-list -r 1 --limit 0')
self.assertTrue(mock_list.called)
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_limit_0_success(self, mock_list):
"""Verify that --limit 0 prints out all project cells."""
self.shell('cell-list -r 1 --limit 0')
mock_list.assert_called_once_with(limit=0)
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_limit_positive_num_success(self, mock_list):
"""Verify --limit X, where X is a positive integer, succeeds.
The command will print out X number of project cells.
"""
self.shell('cell-list -r 1 --limit 1')
mock_list.assert_called_once_with(limit=1)
def test_cell_list_limit_negative_num_failure(self):
"""Verify --limit X, where X is a negative integer, fails.
The command will cause a Command Error message response.
"""
self.assertRaises(exc.CommandError,
self.shell,
'cell-list -r 1 --limit -1')
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_detail_success(self, mock_list):
"""Verify --detail argument successfully pass detail to Client."""
self.shell('cell-list -r 1 --detail')
mock_list.assert_called_once_with(detail=True)
@mock.patch('cratonclient.v1.cells.CellManager.list')
@mock.patch('cratonclient.common.cliutils.print_list')
def test_cell_list_fields_success(self, mock_printlist, mock_list):
"""Verify --fields argument successfully passed to Client."""
self.shell('cell-list -r 1 --fields id name')
mock_list.assert_called_once_with()
mock_printlist.assert_called_once_with(mock.ANY,
list({'id': 'ID',
'name': 'Name'}))
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_detail_and_fields_specified(self, mock_list):
"""Verify --fields ignored when --detail argument passed in."""
self.shell('cell-list -r 1 --fields id name --detail')
mock_list.assert_called_once_with(detail=True)
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_sort_key_field_key_success(self, mock_list):
"""Verify --sort-key arguments successfully passed to Client."""
self.shell('cell-list -r 1 --sort-key name')
mock_list.assert_called_once_with(sort_key='name',
sort_dir='asc')
def test_cell_list_sort_key_invalid(self):
"""Verify --sort-key with invalid args, fails with Command Error."""
self.assertRaises(exc.CommandError,
self.shell,
'cell-list -r 1 --sort-key invalid')
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_sort_dir_not_passed_without_sort_key(self, mock_list):
"""Verify --sort-dir arg ignored without --sort-key."""
self.shell('cell-list -r 1 --sort-dir desc')
mock_list.assert_called_once_with()
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_sort_dir_asc_success(self, mock_list):
"""Verify --sort-dir asc successfully passed to Client."""
self.shell('cell-list -r 1 --sort-key name --sort-dir asc')
mock_list.assert_called_once_with(sort_key='name',
sort_dir='asc')
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_sort_dir_desc_success(self, mock_list):
"""Verify --sort-dir desc successfully passed to Client."""
self.shell('cell-list -r 1 --sort-key name --sort-dir desc')
mock_list.assert_called_once_with(sort_key='name',
sort_dir='desc')
def test_cell_list_sort_dir_invalid_value(self):
"""Verify --sort-dir with invalid args, fails with Command Error."""
self.assertRaises(exc.CommandError,
self.shell,
'cell-list -r 1 --sort-key name --sort-dir invalid')
def test_cell_create_missing_required_args(self):
"""Verify that missing required args results in error message."""
expected_responses = [
'.*?^usage: craton cell-create',
'.*?^craton cell-create: error:.*$'
]
stdout, stderr = self.shell('cell-create')
actual_output = stdout + stderr
for r in expected_responses:
self.assertThat(actual_output,
matchers.MatchesRegex(r, self.re_options))
@mock.patch('cratonclient.v1.cells.CellManager.create')
def test_do_cell_create_calls_cell_manager_with_fields(self, mock_create):
"""Verify that do cell create calls CellManager create."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
mock.ANY,
'http://127.0.0.1/')
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
cells_shell.do_cell_create(client, self.cell_valid_fields)
mock_create.assert_called_once_with(**vars(self.cell_valid_fields))
@mock.patch('cratonclient.v1.cells.CellManager.create')
def test_do_cell_create_ignores_unknown_fields(self, mock_create):
"""Verify that do cell create ignores unknown field."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
mock.ANY,
'http://127.0.0.1/')
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
cells_shell.do_cell_create(client, self.cell_invalid_field)
mock_create.assert_called_once_with(**vars(self.cell_valid_fields))
def test_cell_update_missing_required_args(self):
"""Verify that missing required args results in error message."""
expected_responses = [
'.*?^usage: craton cell-update',
'.*?^craton cell-update: error:.*$',
]
stdout, stderr = self.shell('cell-update')
actual_output = stdout + stderr
for r in expected_responses:
self.assertThat(actual_output,
matchers.MatchesRegex(r, self.re_options))
@mock.patch('cratonclient.v1.cells.CellManager.update')
def test_do_cell_update_calls_cell_manager_with_fields(self, mock_update):
"""Verify that do cell update calls CellManager create."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
mock.ANY,
'http://127.0.0.1/')
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
valid_input = Namespace(region=1,
id=1,
name='mock_cell')
cells_shell.do_cell_update(client, valid_input)
vars(valid_input).pop('region')
mock_update.assert_called_once_with(**vars(valid_input))
@mock.patch('cratonclient.v1.cells.CellManager.update')
def test_do_cell_update_ignores_unknown_fields(self, mock_update):
"""Verify that do cell create ignores unknown field."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
mock.ANY,
'http://127.0.0.1/')
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
invalid_input = Namespace(region=1,
id=1,
name='mock_cell',
invalid=True)
cells_shell.do_cell_update(client, invalid_input)
vars(invalid_input).pop('region')
vars(invalid_input).pop('invalid')
mock_update.assert_called_once_with(**vars(invalid_input))
def test_cell_show_missing_required_args(self):
"""Verify that missing required args results in error message."""
expected_responses = [
'.*?^usage: craton cell-show',
'.*?^craton cell-show: error:.*$',
]
stdout, stderr = self.shell('cell-show')
actual_output = stdout + stderr
for r in expected_responses:
self.assertThat(actual_output,
matchers.MatchesRegex(r, self.re_options))
@mock.patch('cratonclient.v1.cells.CellManager.get')
def test_do_cell_show_calls_cell_manager_with_fields(self, mock_get):
"""Verify that do cell update calls CellManager create."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
mock.ANY,
'http://127.0.0.1/')
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
test_args = Namespace(id=1, region=1)
cells_shell.do_cell_show(client, test_args)
mock_get.assert_called_once_with(vars(test_args)['id'])
def test_cell_delete_missing_required_args(self):
"""Verify that missing required args results in error message."""
expected_responses = [
'.*?^usage: craton cell-delete',
'.*?^craton cell-delete: error:.*$',
]
stdout, stderr = self.shell('cell-delete')
for r in expected_responses:
self.assertThat((stdout + stderr),
matchers.MatchesRegex(r, self.re_options))
@mock.patch('cratonclient.v1.cells.CellManager.delete')
def test_do_cell_delete_calls_cell_manager_with_fields(self, mock_delete):
"""Verify that do cell update calls CellManager create."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
mock.ANY,
'http://127.0.0.1/')
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
test_args = Namespace(id=1, region=1)
cells_shell.do_cell_delete(client, test_args)
mock_delete.assert_called_once_with(vars(test_args)['id'])

56
cratonclient/v1/cells.py Normal file
View File

@ -0,0 +1,56 @@
# -*- 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.
"""Regions manager code."""
from cratonclient import crud
class Cell(crud.Resource):
"""Representation of a Region."""
pass
class CellManager(crud.CRUDClient):
"""A manager for cells."""
key = 'cell'
base_path = '/cells'
resource_class = Cell
region_id = 0
def __init__(self, region_id, session, url):
"""Initialize our CellManager object with region, session, and url."""
super(CellManager, self).__init__(session, url)
self.region_id = region_id
def list(self, **kwargs):
"""Retrieve the cells in a specific region."""
kwargs['region_id'] = self.region_id
return super(CellManager, self).list(**kwargs)
def create(self, **kwargs):
"""Create a cell in a specific region."""
kwargs['region_id'] = self.region_id
return super(CellManager, self).create(**kwargs)
CELL_FIELDS = {
'id': 'ID',
'region_id': 'Region ID',
'project_id': 'Project ID',
'name': 'Name',
'note': 'Note',
'created_at': 'Created At',
'update_at': 'Updated At'
}

View File

@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
"""Top-level client for version 1 of Craton's API."""
from cratonclient.v1 import cells
from cratonclient.v1 import hosts
@ -32,4 +32,5 @@ class Inventory(object):
"""
# TODO(cmspence): self.region = self.regions.get(region=region_id)
self.hosts = hosts.HostManager(region_id, session, url)
self.cells = cells.CellManager(region_id, session, url)
# TODO(cmspence): self.users, self.projects, self.workflows