Extract the formatter methods as common utils
* Extract the formatter methods of image, flavor, address as common utils * Support formatting address info of "server manage" command Change-Id: I217a8b7e2489994eee4cf77cd373dc41722dbffe
This commit is contained in:
parent
fc7fa8cc98
commit
63f1e18393
@ -12,7 +12,6 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
#
|
#
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
@ -35,3 +34,32 @@ def get_response_body(resp):
|
|||||||
else:
|
else:
|
||||||
body = None
|
body = None
|
||||||
return body
|
return body
|
||||||
|
|
||||||
|
|
||||||
|
def addresses_formatter(network_client, networks):
|
||||||
|
output = []
|
||||||
|
for (network, addresses) in networks.items():
|
||||||
|
if not addresses:
|
||||||
|
continue
|
||||||
|
addrs = [addr['addr'] for addr in addresses]
|
||||||
|
network_data = network_client.find_network(
|
||||||
|
network, ignore_missing=False)
|
||||||
|
net_ident = network_data.name or network_data.id
|
||||||
|
addresses_csv = ', '.join(addrs)
|
||||||
|
group = "%s=%s" % (net_ident, addresses_csv)
|
||||||
|
output.append(group)
|
||||||
|
return '; '.join(output)
|
||||||
|
|
||||||
|
|
||||||
|
def image_formatter(image_client, image_id):
|
||||||
|
if image_id:
|
||||||
|
image = image_client.images.get(image_id)
|
||||||
|
return '%s (%s)' % (image.name, image_id)
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
def flavor_formatter(bc_client, flavor_id):
|
||||||
|
if flavor_id:
|
||||||
|
flavor = bc_client.flavor.get(flavor_id)
|
||||||
|
return '%s (%s)' % (flavor.name, flavor_id)
|
||||||
|
return ''
|
||||||
|
@ -17,15 +17,13 @@
|
|||||||
"""Mogan v1 manageable servers action implementations"""
|
"""Mogan v1 manageable servers action implementations"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import logging
|
|
||||||
|
|
||||||
from osc_lib.cli import parseractions
|
from osc_lib.cli import parseractions
|
||||||
from osc_lib.command import command
|
from osc_lib.command import command
|
||||||
from osc_lib import utils
|
from osc_lib import utils
|
||||||
|
|
||||||
from moganclient.common.i18n import _
|
from moganclient.common.i18n import _
|
||||||
|
from moganclient.common import utils as cli_utils
|
||||||
LOG = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class ListManageableServer(command.Lister):
|
class ListManageableServer(command.Lister):
|
||||||
@ -124,22 +122,10 @@ class ManageServer(command.ShowOne):
|
|||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def _format_image_field(self, data):
|
|
||||||
image_client = self.app.client_manager.image
|
|
||||||
image_uuid = data._info.pop('image_uuid')
|
|
||||||
if image_uuid:
|
|
||||||
image = image_client.images.get(image_uuid)
|
|
||||||
return '%s (%s)' % (image.name, image_uuid)
|
|
||||||
|
|
||||||
def _format_flavor_field(self, data):
|
|
||||||
bc_client = self.app.client_manager.baremetal_compute
|
|
||||||
flavor_uuid = data._info.pop('flavor_uuid')
|
|
||||||
if flavor_uuid:
|
|
||||||
flavor = bc_client.flavor.get(flavor_uuid)
|
|
||||||
return '%s (%s)' % (flavor.name, flavor_uuid)
|
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
bc_client = self.app.client_manager.baremetal_compute
|
bc_client = self.app.client_manager.baremetal_compute
|
||||||
|
image_client = self.app.client_manager.image
|
||||||
|
network_client = self.app.client_manager.network
|
||||||
|
|
||||||
boot_kwargs = dict(
|
boot_kwargs = dict(
|
||||||
name=parsed_args.name,
|
name=parsed_args.name,
|
||||||
@ -152,8 +138,13 @@ class ManageServer(command.ShowOne):
|
|||||||
data._info.update(
|
data._info.update(
|
||||||
{
|
{
|
||||||
'properties': utils.format_dict(data._info.pop('metadata')),
|
'properties': utils.format_dict(data._info.pop('metadata')),
|
||||||
'flavor': self._format_flavor_field(data),
|
'flavor': cli_utils.flavor_formatter(
|
||||||
'image': self._format_image_field(data)
|
bc_client, data._info.pop('flavor_uuid')),
|
||||||
|
'image': cli_utils.image_formatter(
|
||||||
|
image_client, data._info.pop('image_uuid')),
|
||||||
|
'addresses': cli_utils.addresses_formatter(
|
||||||
|
network_client,
|
||||||
|
data._info.pop('addresses'))
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
|
|
||||||
"""Mogan v1 Baremetal server action implementations"""
|
"""Mogan v1 Baremetal server action implementations"""
|
||||||
|
import functools
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
@ -27,25 +27,11 @@ from osc_lib import exceptions
|
|||||||
from osc_lib import utils
|
from osc_lib import utils
|
||||||
|
|
||||||
from moganclient.common.i18n import _
|
from moganclient.common.i18n import _
|
||||||
|
from moganclient.common import utils as cli_utils
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def _addresses_formatter(network_client, networks):
|
|
||||||
output = []
|
|
||||||
for (network, addresses) in networks.items():
|
|
||||||
if not addresses:
|
|
||||||
continue
|
|
||||||
addrs = [addr['addr'] for addr in addresses]
|
|
||||||
network_data = network_client.find_network(
|
|
||||||
network, ignore_missing=False)
|
|
||||||
net_ident = network_data.name or network_data.id
|
|
||||||
addresses_csv = ', '.join(addrs)
|
|
||||||
group = "%s=%s" % (net_ident, addresses_csv)
|
|
||||||
output.append(group)
|
|
||||||
return '; '.join(output)
|
|
||||||
|
|
||||||
|
|
||||||
class ServersActionBase(command.Command):
|
class ServersActionBase(command.Command):
|
||||||
def _get_parser_with_action(self, prog_name, action):
|
def _get_parser_with_action(self, prog_name, action):
|
||||||
parser = super(ServersActionBase, self).get_parser(prog_name)
|
parser = super(ServersActionBase, self).get_parser(prog_name)
|
||||||
@ -394,8 +380,8 @@ class ListServer(command.Lister):
|
|||||||
data = bc_client.server.list(detailed=True,
|
data = bc_client.server.list(detailed=True,
|
||||||
all_projects=parsed_args.all_projects)
|
all_projects=parsed_args.all_projects)
|
||||||
net_client = self.app.client_manager.network
|
net_client = self.app.client_manager.network
|
||||||
addr_formatter = lambda addr: _addresses_formatter(net_client, addr)
|
addr_fmt = functools.partial(cli_utils.addresses_formatter, net_client)
|
||||||
formatters = {'addresses': addr_formatter,
|
formatters = {'addresses': addr_fmt,
|
||||||
'metadata': utils.format_dict
|
'metadata': utils.format_dict
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -455,20 +441,6 @@ class ShowServer(command.ShowOne):
|
|||||||
)
|
)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def _format_image_field(self, data):
|
|
||||||
image_client = self.app.client_manager.image
|
|
||||||
image_uuid = data._info.pop('image_uuid')
|
|
||||||
if image_uuid:
|
|
||||||
image = image_client.images.get(image_uuid)
|
|
||||||
return '%s (%s)' % (image.name, image_uuid)
|
|
||||||
|
|
||||||
def _format_flavor_field(self, data):
|
|
||||||
bc_client = self.app.client_manager.baremetal_compute
|
|
||||||
flavor_uuid = data._info.pop('flavor_uuid')
|
|
||||||
if flavor_uuid:
|
|
||||||
flavor = bc_client.flavor.get(flavor_uuid)
|
|
||||||
return '%s (%s)' % (flavor.name, flavor_uuid)
|
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
bc_client = self.app.client_manager.baremetal_compute
|
bc_client = self.app.client_manager.baremetal_compute
|
||||||
data = utils.find_resource(
|
data = utils.find_resource(
|
||||||
@ -478,14 +450,17 @@ class ShowServer(command.ShowOne):
|
|||||||
# Special mapping for columns to make the output easier to read:
|
# Special mapping for columns to make the output easier to read:
|
||||||
# 'metadata' --> 'properties'
|
# 'metadata' --> 'properties'
|
||||||
network_client = self.app.client_manager.network
|
network_client = self.app.client_manager.network
|
||||||
|
image_client = self.app.client_manager.image
|
||||||
data._info.update(
|
data._info.update(
|
||||||
{
|
{
|
||||||
'properties': utils.format_dict(data._info.pop('metadata')),
|
'properties': utils.format_dict(data._info.pop('metadata')),
|
||||||
'addresses': _addresses_formatter(
|
'addresses': cli_utils.addresses_formatter(
|
||||||
network_client,
|
network_client,
|
||||||
data._info.pop('addresses')),
|
data._info.pop('addresses')),
|
||||||
'image': self._format_image_field(data),
|
'image': cli_utils.image_formatter(
|
||||||
'flavor': self._format_flavor_field(data)
|
image_client, data._info.pop('image_uuid')),
|
||||||
|
'flavor': cli_utils.flavor_formatter(
|
||||||
|
bc_client, data._info.pop('flavor_uuid'))
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -161,11 +161,11 @@ class TestServerManage(TestManageableServer):
|
|||||||
data=called_data)
|
data=called_data)
|
||||||
self.assertEqual(self.columns, columns)
|
self.assertEqual(self.columns, columns)
|
||||||
expected_data = (
|
expected_data = (
|
||||||
fk_server.addresses,
|
'',
|
||||||
fk_server.availability_zone,
|
fk_server.availability_zone,
|
||||||
fk_server.created_at,
|
fk_server.created_at,
|
||||||
fk_server.description,
|
fk_server.description,
|
||||||
None,
|
'',
|
||||||
'test-image (%s)' % fk_server.image_uuid,
|
'test-image (%s)' % fk_server.image_uuid,
|
||||||
fk_server.links,
|
fk_server.links,
|
||||||
fk_server.name,
|
fk_server.name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user