Remove the Inventory object from the Client

As a matter of design and practicality, we have decided to remove the
Inventory object which was awkward, underspecified, and frankly the
wrong layer of abstraction from the Client. Hosts, Cells, etc. will now
be accessible directly from the client itself.

Change-Id: I4a65761ad39d0b7cb881a97c5f3367cafa9db557
This commit is contained in:
Ian Cordasco 2016-11-21 15:33:16 -06:00
parent 9260ffdefa
commit 5e53de8ac2
8 changed files with 203 additions and 252 deletions

View File

@ -19,17 +19,13 @@ from cratonclient import exceptions as exc
from cratonclient.v1 import cells
@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)
cell = cc.cells.get(args.id)
data = {f: getattr(cell, f, '') for f in cells.CELL_FIELDS}
cliutils.print_dict(data, wrap=72)
@ -98,8 +94,9 @@ def do_cell_list(cc, args):
)
params['sort_key'] = sort_key
params['sort_dir'] = args.sort_dir
params['region_id'] = args.region
listed_cells = cc.inventory(args.region).cells.list(**params)
listed_cells = cc.cells.list(**params)
cliutils.print_list(listed_cells, list(fields))
@ -119,15 +116,11 @@ 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 cells.CELL_FIELDS and not (v is None)}
cell = cc.inventory(args.region_id).cells.create(**fields)
cell = cc.cells.create(**fields)
data = {f: getattr(cell, f, '') for f in cells.CELL_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,
@ -152,15 +145,11 @@ def do_cell_update(cc, args):
'Nothing to update... Please specify one of --name, --region, '
'or --note'
)
cell = cc.inventory(args.region).cells.update(cell_id, **fields)
cell = cc.cells.update(cell_id, **fields)
data = {f: getattr(cell, f, '') for f in cells.CELL_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,
@ -168,7 +157,7 @@ def do_cell_update(cc, args):
def do_cell_delete(cc, args):
"""Delete a cell that is registered with the Craton service."""
try:
response = cc.inventory(args.region).cells.delete(args.id)
response = cc.cells.delete(args.id)
except exc.ClientException as client_exc:
raise exc.CommandError(
'Failed to delete cell {} due to "{}:{}"'.format(

View File

@ -19,17 +19,13 @@ from cratonclient import exceptions as exc
from cratonclient.v1 import hosts
@cliutils.arg('region',
metavar='<region>',
type=int,
help='ID of the region that the host belongs to.')
@cliutils.arg('id',
metavar='<host>',
type=int,
help='ID of the host.')
def do_host_show(cc, args):
"""Show detailed information about a host."""
host = cc.inventory(args.region).hosts.get(args.id)
host = cc.hosts.get(args.id)
data = {f: getattr(host, f, '') for f in hosts.HOST_FIELDS}
cliutils.print_dict(data, wrap=72)
@ -104,8 +100,9 @@ def do_host_list(cc, args):
)
params['sort_key'] = sort_key
params['sort_dir'] = args.sort_dir
params['region_id'] = args.region
host_list = cc.inventory(args.region).hosts.list(**params)
host_list = cc.hosts.list(**params)
cliutils.print_list(host_list, list(fields))
@ -150,15 +147,11 @@ def do_host_create(cc, args):
"""Register a new host with the Craton service."""
fields = {k: v for (k, v) in vars(args).items()
if k in hosts.HOST_FIELDS and (v or v is False)}
host = cc.inventory(args.region_id).hosts.create(**fields)
host = cc.hosts.create(**fields)
data = {f: getattr(host, f, '') for f in hosts.HOST_FIELDS}
cliutils.print_dict(data, wrap=72)
@cliutils.arg('region',
metavar='<region>',
type=int,
help='Current ID of the region that the host belongs to.')
@cliutils.arg('id',
metavar='<host>',
type=int,
@ -197,16 +190,12 @@ def do_host_update(cc, args):
fields = {k: v for (k, v) in vars(args).items()
if k in hosts.HOST_FIELDS and (v or v is False)}
item_id = fields.pop('id')
host = cc.inventory(args.region).hosts.update(item_id, **fields)
host = cc.hosts.update(item_id, **fields)
print("Host {0} has been successfully updated.".format(host.id))
data = {f: getattr(host, f, '') for f in hosts.HOST_FIELDS}
cliutils.print_dict(data, wrap=72)
@cliutils.arg('region',
metavar='<region>',
type=int,
help='ID of the region that the host belongs to.')
@cliutils.arg('id',
metavar='<host>',
type=int,
@ -214,7 +203,7 @@ def do_host_update(cc, args):
def do_host_delete(cc, args):
"""Delete a host that is registered with the Craton service."""
try:
response = cc.inventory(args.region).hosts.delete(args.id)
response = cc.hosts.delete(args.id)
except exc.ClientException as client_exc:
raise exc.CommandError(
'Failed to delete cell {} due to "{}:{}"'.format(

View File

@ -58,7 +58,11 @@ class TestCellsShell(base.ShellTestCase):
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, sort_dir='asc')
mock_list.assert_called_once_with(
limit=0,
sort_dir='asc',
region_id=1,
)
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_limit_positive_num_success(self, mock_list):
@ -67,7 +71,11 @@ class TestCellsShell(base.ShellTestCase):
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, sort_dir='asc')
mock_list.assert_called_once_with(
limit=1,
sort_dir='asc',
region_id=1,
)
def test_cell_list_limit_negative_num_failure(self):
"""Verify --limit X, where X is a negative integer, fails.
@ -82,14 +90,21 @@ class TestCellsShell(base.ShellTestCase):
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, sort_dir='asc')
mock_list.assert_called_once_with(
detail=True,
region_id=1,
sort_dir='asc',
)
@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(sort_dir='asc')
mock_list.assert_called_once_with(
sort_dir='asc',
region_id=1,
)
mock_printlist.assert_called_once_with(mock.ANY,
list({'id': 'ID',
'name': 'Name'}))
@ -98,8 +113,11 @@ class TestCellsShell(base.ShellTestCase):
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')
mock_list.assert_called_once_with(
sort_key='name',
sort_dir='asc',
region_id=1,
)
def test_cell_list_sort_key_invalid(self):
"""Verify --sort-key with invalid args, fails with Command Error."""
@ -111,15 +129,21 @@ class TestCellsShell(base.ShellTestCase):
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_list.assert_called_once_with(
sort_key='name',
sort_dir='asc',
region_id=1,
)
@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')
mock_list.assert_called_once_with(
sort_key='name',
sort_dir='desc',
region_id=1,
)
def test_cell_list_sort_dir_invalid_value(self):
"""Verify --sort-dir with invalid args, fails with Command Error."""
@ -144,12 +168,10 @@ class TestCellsShell(base.ShellTestCase):
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,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.cells = cells.CellManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
cells_shell.do_cell_create(client, self.cell_valid_fields)
mock_create.assert_called_once_with(**vars(self.cell_valid_fields))
@ -157,12 +179,10 @@ class TestCellsShell(base.ShellTestCase):
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,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.cells = cells.CellManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
cells_shell.do_cell_create(client, self.cell_invalid_field)
mock_create.assert_called_once_with(**vars(self.cell_valid_fields))
@ -182,12 +202,10 @@ class TestCellsShell(base.ShellTestCase):
def test_do_cell_update_calls_cell_manager_with_fields(self, mock_update):
"""Verify that do cell update calls CellManager update."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.cells = cells.CellManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
valid_input = Namespace(region=1,
id=1,
name='mock_cell')
@ -198,12 +216,10 @@ class TestCellsShell(base.ShellTestCase):
def test_do_cell_update_ignores_unknown_fields(self, mock_update):
"""Verify that do cell update ignores unknown field."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.cells = cells.CellManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
invalid_input = Namespace(region=1,
id=1,
name='mock_cell',
@ -227,13 +243,11 @@ class TestCellsShell(base.ShellTestCase):
def test_do_cell_show_calls_cell_manager_with_fields(self, mock_get):
"""Verify that do cell show calls CellManager get."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
test_args = Namespace(id=1, region=1)
client.cells = cells.CellManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
test_args = Namespace(id=1)
cells_shell.do_cell_show(client, test_args)
mock_get.assert_called_once_with(vars(test_args)['id'])
@ -252,12 +266,10 @@ class TestCellsShell(base.ShellTestCase):
def test_do_cell_delete_calls_cell_manager_with_fields(self, mock_delete):
"""Verify that do cell delete calls CellManager delete."""
client = mock.Mock()
inventory = mock.Mock()
inventory.cells = cells.CellManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.cells = cells.CellManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
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'])

View File

@ -62,7 +62,11 @@ class TestHostsShell(base.ShellTestCase):
def test_host_list_limit_0_success(self, mock_list):
"""Verify that --limit 0 prints out all project hosts."""
self.shell('host-list -r 1 --limit 0')
mock_list.assert_called_once_with(limit=0, sort_dir='asc')
mock_list.assert_called_once_with(
limit=0,
sort_dir='asc',
region_id=1,
)
@mock.patch('cratonclient.v1.hosts.HostManager.list')
def test_host_list_limit_positive_num_success(self, mock_list):
@ -71,7 +75,11 @@ class TestHostsShell(base.ShellTestCase):
The command will print out X number of project hosts.
"""
self.shell('host-list -r 1 --limit 1')
mock_list.assert_called_once_with(limit=1, sort_dir='asc')
mock_list.assert_called_once_with(
limit=1,
sort_dir='asc',
region_id=1,
)
def test_host_list_limit_negative_num_failure(self):
"""Verify --limit X, where X is a negative integer, fails.
@ -87,21 +95,32 @@ class TestHostsShell(base.ShellTestCase):
"""Verify --cell arguments successfully pass cell to Client."""
for cell_arg in ['-c', '--cell']:
self.shell('host-list -r 1 {0} 1'.format(cell_arg))
mock_list.assert_called_once_with(cell_id=1, sort_dir='asc')
mock_list.assert_called_once_with(
cell_id=1,
sort_dir='asc',
region_id=1,
)
mock_list.reset_mock()
@mock.patch('cratonclient.v1.hosts.HostManager.list')
def test_host_list_detail_success(self, mock_list):
"""Verify --detail argument successfully pass detail to Client."""
self.shell('host-list -r 1 --detail')
mock_list.assert_called_once_with(detail=True, sort_dir='asc')
mock_list.assert_called_once_with(
detail=True,
sort_dir='asc',
region_id=1,
)
@mock.patch('cratonclient.v1.hosts.HostManager.list')
@mock.patch('cratonclient.common.cliutils.print_list')
def test_host_list_fields_success(self, mock_printlist, mock_list):
"""Verify --fields argument successfully passed to Client."""
self.shell('host-list -r 1 --fields id name')
mock_list.assert_called_once_with(sort_dir='asc')
mock_list.assert_called_once_with(
sort_dir='asc',
region_id=1,
)
mock_printlist.assert_called_once_with(mock.ANY,
list({'id': 'ID',
'name': 'Name'}))
@ -110,8 +129,11 @@ class TestHostsShell(base.ShellTestCase):
def test_host_list_sort_key_field_key_success(self, mock_list):
"""Verify --sort-key arguments successfully passed to Client."""
self.shell('host-list -r 1 --sort-key cell_id')
mock_list.assert_called_once_with(sort_key='cell_id',
sort_dir='asc')
mock_list.assert_called_once_with(
sort_key='cell_id',
sort_dir='asc',
region_id=1,
)
def test_host_list_sort_key_invalid(self):
"""Verify --sort-key with invalid args, fails with Command Error."""
@ -123,21 +145,30 @@ class TestHostsShell(base.ShellTestCase):
def test_host_list_sort_dir_not_passed_without_sort_key(self, mock_list):
"""Verify --sort-dir arg ignored without --sort-key."""
self.shell('host-list -r 1 --sort-dir desc')
mock_list.assert_called_once_with(sort_dir='desc')
mock_list.assert_called_once_with(
sort_dir='desc',
region_id=1,
)
@mock.patch('cratonclient.v1.hosts.HostManager.list')
def test_host_list_sort_dir_asc_success(self, mock_list):
"""Verify --sort-dir asc successfully passed to Client."""
self.shell('host-list -r 1 --sort-key name --sort-dir asc')
mock_list.assert_called_once_with(sort_key='name',
sort_dir='asc')
mock_list.assert_called_once_with(
sort_key='name',
sort_dir='asc',
region_id=1,
)
@mock.patch('cratonclient.v1.hosts.HostManager.list')
def test_host_list_sort_dir_desc_success(self, mock_list):
"""Verify --sort-dir desc successfully passed to Client."""
self.shell('host-list -r 1 --sort-key name --sort-dir desc')
mock_list.assert_called_once_with(sort_key='name',
sort_dir='desc')
mock_list.assert_called_once_with(
sort_key='name',
sort_dir='desc',
region_id=1,
)
def test_host_list_sort_dir_invalid_value(self):
"""Verify --sort-dir with invalid args, fails with Command Error."""
@ -162,12 +193,10 @@ class TestHostsShell(base.ShellTestCase):
def test_do_host_create_calls_host_manager_with_fields(self, mock_create):
"""Verify that do host create calls HostManager create."""
client = mock.Mock()
inventory = mock.Mock()
inventory.hosts = hosts.HostManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.hosts = hosts.HostManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
hosts_shell.do_host_create(client, self.host_valid_fields)
mock_create.assert_called_once_with(**vars(self.host_valid_fields))
@ -175,12 +204,10 @@ class TestHostsShell(base.ShellTestCase):
def test_do_host_create_ignores_unknown_fields(self, mock_create):
"""Verify that do host create ignores unknown field."""
client = mock.Mock()
inventory = mock.Mock()
inventory.hosts = hosts.HostManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.hosts = hosts.HostManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
hosts_shell.do_host_create(client, self.host_invalid_field)
mock_create.assert_called_once_with(**vars(self.host_valid_fields))
@ -200,12 +227,10 @@ class TestHostsShell(base.ShellTestCase):
def test_do_host_update_calls_host_manager_with_fields(self, mock_update):
"""Verify that do host update calls HostManager update."""
client = mock.Mock()
inventory = mock.Mock()
inventory.hosts = hosts.HostManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.hosts = hosts.HostManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
valid_input = Namespace(region=1,
id=1,
name='mock_host')
@ -216,12 +241,10 @@ class TestHostsShell(base.ShellTestCase):
def test_do_host_update_ignores_unknown_fields(self, mock_update):
"""Verify that do host update ignores unknown field."""
client = mock.Mock()
inventory = mock.Mock()
inventory.hosts = hosts.HostManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.hosts = hosts.HostManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
invalid_input = Namespace(region=1,
id=1,
name='mock_host',
@ -245,12 +268,10 @@ class TestHostsShell(base.ShellTestCase):
def test_do_host_show_calls_host_manager_with_fields(self, mock_get):
"""Verify that do host show calls HostManager get."""
client = mock.Mock()
inventory = mock.Mock()
inventory.hosts = hosts.HostManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.hosts = hosts.HostManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
test_args = Namespace(id=1, region=1)
hosts_shell.do_host_show(client, test_args)
mock_get.assert_called_once_with(vars(test_args)['id'])
@ -270,12 +291,10 @@ class TestHostsShell(base.ShellTestCase):
def test_do_host_delete_calls_host_manager_with_fields(self, mock_delete):
"""Verify that do host delete calls HostManager delete."""
client = mock.Mock()
inventory = mock.Mock()
inventory.hosts = hosts.HostManager(mock.ANY,
'http://127.0.0.1/',
region_id=mock.ANY)
client.inventory = mock.Mock(name='inventory')
client.inventory.return_value = inventory
client.hosts = hosts.HostManager(
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
test_args = Namespace(id=1, region=1)
hosts_shell.do_host_delete(client, test_args)
mock_delete.assert_called_once_with(vars(test_args)['id'])

View File

@ -1,38 +0,0 @@
# -*- 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.
"""Tests for `cratonclient` module."""
import mock
from cratonclient.tests import base
from cratonclient.v1 import client
class TestCratonclient(base.TestCase):
"""Tests for the top-level module."""
def test_something(self):
"""Do nothing."""
pass
@mock.patch('cratonclient.v1.inventory.Inventory')
def test_client_creates_inventory(self, mock_inventory):
"""Verify that Craton client creates Inventory."""
session = mock.Mock()
url = 'https://10.1.1.8080'
region_id = 1
craton = client.Client(session, url)
craton.inventory(region_id)
mock_inventory.assert_called_once_with(region_id=region_id,
session=session,
url=url + '/v1')

View File

@ -32,8 +32,7 @@ class TestDoShellShow(base.TestShellCommandUsingPrintDict):
cells_shell.do_cell_show(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.get.assert_called_once_with(456)
self.craton_client.cells.get.assert_called_once_with(456)
self.print_dict.assert_called_once_with(
{f: mock.ANY for f in cells.CELL_FIELDS},
wrap=72,
@ -64,8 +63,10 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(sort_dir='asc')
self.craton_client.cells.list.assert_called_once_with(
sort_dir='asc',
region_id=123,
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
@ -83,10 +84,10 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(
self.craton_client.cells.list.assert_called_once_with(
limit=5,
sort_dir='asc',
region_id=123,
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
@ -98,10 +99,10 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(
self.craton_client.cells.list.assert_called_once_with(
sort_dir='asc',
sort_key='name',
region_id=123,
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
@ -120,10 +121,10 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(
self.craton_client.cells.list.assert_called_once_with(
sort_dir='asc',
detail=True,
region_id=123,
)
self.assertEqual(sorted(list(cells.CELL_FIELDS)),
sorted(self.print_list.call_args[0][-1]))
@ -144,9 +145,9 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
cells_shell.do_cell_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.list.assert_called_once_with(
self.craton_client.cells.list.assert_called_once_with(
sort_dir='asc',
region_id=123,
)
self.assertEqual(['id', 'name', 'note'],
sorted(self.print_list.call_args[0][-1]))
@ -175,8 +176,7 @@ class TestDoCellCreate(base.TestShellCommandUsingPrintDict):
cells_shell.do_cell_create(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.create.assert_called_once_with(
self.craton_client.cells.create.assert_called_once_with(
name='New Cell',
region_id=123,
)
@ -188,8 +188,7 @@ class TestDoCellCreate(base.TestShellCommandUsingPrintDict):
cells_shell.do_cell_create(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.create.assert_called_once_with(
self.craton_client.cells.create.assert_called_once_with(
name='New Cell',
region_id=123,
note='This is a note',
@ -203,7 +202,6 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
def args_for(self, **kwargs):
"""Generate arguments for cell-update command."""
kwargs.setdefault('id', 123)
kwargs.setdefault('region', 345)
kwargs.setdefault('name', None)
kwargs.setdefault('region_id', None)
kwargs.setdefault('note', None)
@ -222,8 +220,7 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
cells_shell.do_cell_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(345)
self.inventory.cells.update.assert_called_once_with(
self.craton_client.cells.update.assert_called_once_with(
123,
name='New name',
)
@ -235,8 +232,7 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
cells_shell.do_cell_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(345)
self.inventory.cells.update.assert_called_once_with(
self.craton_client.cells.update.assert_called_once_with(
123,
region_id=678,
)
@ -248,8 +244,7 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
cells_shell.do_cell_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(345)
self.inventory.cells.update.assert_called_once_with(
self.craton_client.cells.update.assert_called_once_with(
123,
note='A new note',
)
@ -265,8 +260,7 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
cells_shell.do_cell_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(345)
self.inventory.cells.update.assert_called_once_with(
self.craton_client.cells.update.assert_called_once_with(
123,
name='A new name for a new region',
region_id=678,
@ -293,39 +287,35 @@ class TestDoCellDelete(base.TestShellCommand):
def test_successful(self):
"""Verify the message we print when successful."""
self.inventory.cells.delete.return_value = True
self.craton_client.cells.delete.return_value = True
args = self.args_for(
region=123,
id=456,
)
cells_shell.do_cell_delete(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.delete.assert_called_once_with(456)
self.craton_client.cells.delete.assert_called_once_with(456)
self.print_func.assert_called_once_with(
'Cell 456 was successfully deleted.'
)
def test_failed(self):
"""Verify the message we print when deletion fails."""
self.inventory.cells.delete.return_value = False
self.craton_client.cells.delete.return_value = False
args = self.args_for(
region=123,
id=456,
)
cells_shell.do_cell_delete(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.delete.assert_called_once_with(456)
self.craton_client.cells.delete.assert_called_once_with(456)
self.print_func.assert_called_once_with(
'Cell 456 was not deleted.'
)
def test_failed_with_exception(self):
"""Verify the message we print when deletion fails."""
self.inventory.cells.delete.side_effect = exceptions.NotFound
self.craton_client.cells.delete.side_effect = exceptions.NotFound
args = self.args_for(
region=123,
id=456,
@ -333,6 +323,5 @@ class TestDoCellDelete(base.TestShellCommand):
self.assertRaisesCommandErrorWith(cells_shell.do_cell_delete, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.cells.delete.assert_called_once_with(456)
self.craton_client.cells.delete.assert_called_once_with(456)
self.assertFalse(self.print_func.called)

View File

@ -32,8 +32,7 @@ class TestDoHostShow(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_show(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(135)
self.inventory.hosts.get.assert_called_once_with(246)
self.craton_client.hosts.get.assert_called_once_with(246)
self.print_dict.assert_called_once_with(
{f: mock.ANY for f in hosts.HOST_FIELDS},
wrap=72,
@ -60,8 +59,10 @@ class TestDoHostList(base.TestShellCommandUsingPrintList):
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(246)
self.inventory.hosts.list.assert_called_once_with(sort_dir='asc')
self.craton_client.hosts.list.assert_called_once_with(
sort_dir='asc',
region_id=246,
)
self.assertSortedPrintListFieldsEqualTo([
'active', 'cell_id', 'device_type', 'id', 'name'
])
@ -72,10 +73,10 @@ class TestDoHostList(base.TestShellCommandUsingPrintList):
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(246)
self.inventory.hosts.list.assert_called_once_with(
self.craton_client.hosts.list.assert_called_once_with(
cell_id=789,
sort_dir='asc',
region_id=246,
)
self.assertSortedPrintListFieldsEqualTo([
'active', 'cell_id', 'device_type', 'id', 'name',
@ -87,10 +88,10 @@ class TestDoHostList(base.TestShellCommandUsingPrintList):
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(246)
self.inventory.hosts.list.assert_called_once_with(
self.craton_client.hosts.list.assert_called_once_with(
detail=True,
sort_dir='asc',
region_id=246,
)
self.assertSortedPrintListFieldsEqualTo([
'access_secret_id',
@ -114,10 +115,10 @@ class TestDoHostList(base.TestShellCommandUsingPrintList):
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(246)
self.inventory.hosts.list.assert_called_once_with(
self.craton_client.hosts.list.assert_called_once_with(
limit=20,
sort_dir='asc',
region_id=246,
)
self.assertSortedPrintListFieldsEqualTo([
'active', 'cell_id', 'device_type', 'id', 'name'
@ -136,8 +137,10 @@ class TestDoHostList(base.TestShellCommandUsingPrintList):
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(246)
self.inventory.hosts.list.assert_called_once_with(sort_dir='asc')
self.craton_client.hosts.list.assert_called_once_with(
sort_dir='asc',
region_id=246,
)
self.assertSortedPrintListFieldsEqualTo([
'cell_id', 'id', 'name',
])
@ -157,10 +160,10 @@ class TestDoHostList(base.TestShellCommandUsingPrintList):
hosts_shell.do_host_list(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(246)
self.inventory.hosts.list.assert_called_once_with(
self.craton_client.hosts.list.assert_called_once_with(
sort_key='ip_address',
sort_dir='asc',
region_id=246,
)
def test_fields_and_detail_raise_command_error(self):
@ -205,8 +208,7 @@ class TestDoHostCreate(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_create(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.create.assert_called_once_with(
self.craton_client.hosts.create.assert_called_once_with(
name='test-hostname',
ip_address='10.0.1.10',
cell_id=246,
@ -225,8 +227,7 @@ class TestDoHostCreate(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_create(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.create.assert_called_once_with(
self.craton_client.hosts.create.assert_called_once_with(
name='test-hostname',
ip_address='10.0.1.10',
cell_id=246,
@ -246,8 +247,7 @@ class TestDoHostCreate(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_create(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.create.assert_called_once_with(
self.craton_client.hosts.create.assert_called_once_with(
name='test-hostname',
ip_address='10.0.1.10',
cell_id=246,
@ -267,8 +267,7 @@ class TestDoHostCreate(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_create(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.create.assert_called_once_with(
self.craton_client.hosts.create.assert_called_once_with(
name='test-hostname',
ip_address='10.0.1.10',
cell_id=246,
@ -293,7 +292,7 @@ class TestDoHostUpdate(base.TestShellCommandUsingPrintDict):
'cratonclient.shell.v1.hosts_shell.print'
)
self.print_mock = self.print_mocker.start()
self.inventory.hosts.update.return_value = mock.Mock(id=246)
self.craton_client.hosts.update.return_value = mock.Mock(id=246)
def tearDown(self):
"""Stop mocking print."""
@ -320,8 +319,7 @@ class TestDoHostUpdate(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.update.assert_called_once_with(
self.craton_client.hosts.update.assert_called_once_with(
246,
active=True,
)
@ -339,8 +337,7 @@ class TestDoHostUpdate(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.update.assert_called_once_with(
self.craton_client.hosts.update.assert_called_once_with(
246,
name='New name',
active=True,
@ -359,8 +356,7 @@ class TestDoHostUpdate(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.update.assert_called_once_with(
self.craton_client.hosts.update.assert_called_once_with(
246,
ip_address='10.1.0.10',
active=True,
@ -379,8 +375,7 @@ class TestDoHostUpdate(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.update.assert_called_once_with(
self.craton_client.hosts.update.assert_called_once_with(
246,
active=False,
)
@ -406,8 +401,7 @@ class TestDoHostUpdate(base.TestShellCommandUsingPrintDict):
hosts_shell.do_host_update(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.update.assert_called_once_with(
self.craton_client.hosts.update.assert_called_once_with(
246,
active=True,
name='New name',
@ -445,7 +439,7 @@ class TestDoHostDelete(base.TestShellCommand):
def test_successful(self):
"""Verify we print our successful message."""
self.inventory.hosts.delete.return_value = True
self.craton_client.hosts.delete.return_value = True
args = self.args_for(
region=123,
id=246,
@ -453,15 +447,14 @@ class TestDoHostDelete(base.TestShellCommand):
hosts_shell.do_host_delete(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.delete.assert_called_once_with(246)
self.craton_client.hosts.delete.assert_called_once_with(246)
self.print_mock.assert_called_once_with(
'Host 246 was successfully deleted.'
)
def test_failed(self):
"""Verify the message we print when deletion fails."""
self.inventory.hosts.delete.return_value = False
self.craton_client.hosts.delete.return_value = False
args = self.args_for(
region=123,
id=246,
@ -469,15 +462,14 @@ class TestDoHostDelete(base.TestShellCommand):
hosts_shell.do_host_delete(self.craton_client, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.delete.assert_called_once_with(246)
self.craton_client.hosts.delete.assert_called_once_with(246)
self.print_mock.assert_called_once_with(
'Host 246 was not deleted.'
)
def test_failed_with_exception(self):
"""Verify we raise a CommandError on client exceptions."""
self.inventory.hosts.delete.side_effect = exceptions.NotFound
self.craton_client.hosts.delete.side_effect = exceptions.NotFound
args = self.args_for(
region=123,
id=246,
@ -485,6 +477,5 @@ class TestDoHostDelete(base.TestShellCommand):
self.assertRaisesCommandErrorWith(hosts_shell.do_host_delete, args)
self.craton_client.inventory.assert_called_once_with(123)
self.inventory.hosts.delete.assert_called_once_with(246)
self.craton_client.hosts.delete.assert_called_once_with(246)
self.assertFalse(self.print_mock.called)

View File

@ -12,7 +12,9 @@
# 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 inventory
from cratonclient.v1 import cells
from cratonclient.v1 import hosts
from cratonclient.v1 import regions
class Client(object):
@ -35,9 +37,7 @@ class Client(object):
if not self._url.endswith('/v1'):
self._url += '/v1'
self._manager_kwargs = {'session': self._session, 'url': self._url}
def inventory(self, region_id):
"""Retrieve inventory for a given region."""
self._manager_kwargs['region_id'] = region_id
return inventory.Inventory(**self._manager_kwargs)
manager_kwargs = {'session': self._session, 'url': self._url}
self.hosts = hosts.HostManager(**manager_kwargs)
self.cells = cells.CellManager(**manager_kwargs)
self.regions = regions.RegionManager(**manager_kwargs)