trivial: Normalize some client usage
Ahead of rework in this area. Change-Id: I1b1c2370967381903970870da8cbe0868b1e23e1 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
@@ -90,17 +90,19 @@ class ProjectCleanup(command.Command):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
sdk = self.app.client_manager.sdk_connection
|
connection = self.app.client_manager.sdk_connection
|
||||||
|
|
||||||
if parsed_args.auth_project:
|
if parsed_args.auth_project:
|
||||||
project_connect = sdk
|
# is we've got a project already configured, use the connection
|
||||||
|
# as-is
|
||||||
|
pass
|
||||||
elif parsed_args.project:
|
elif parsed_args.project:
|
||||||
project = sdk.identity.find_project(
|
project = connection.identity.find_project(
|
||||||
name_or_id=parsed_args.project, ignore_missing=False
|
name_or_id=parsed_args.project, ignore_missing=False
|
||||||
)
|
)
|
||||||
project_connect = sdk.connect_as_project(project)
|
connection = connection.connect_as_project(project)
|
||||||
|
|
||||||
if project_connect:
|
if connection:
|
||||||
status_queue: queue.Queue[ty.Any] = queue.Queue()
|
status_queue: queue.Queue[ty.Any] = queue.Queue()
|
||||||
parsed_args.max_width = int(
|
parsed_args.max_width = int(
|
||||||
os.environ.get('CLIFF_MAX_TERM_WIDTH', 0)
|
os.environ.get('CLIFF_MAX_TERM_WIDTH', 0)
|
||||||
@@ -120,7 +122,7 @@ class ProjectCleanup(command.Command):
|
|||||||
if parsed_args.updated_before:
|
if parsed_args.updated_before:
|
||||||
filters['updated_at'] = parsed_args.updated_before
|
filters['updated_at'] = parsed_args.updated_before
|
||||||
|
|
||||||
project_connect.project_cleanup(
|
connection.project_cleanup(
|
||||||
dry_run=True,
|
dry_run=True,
|
||||||
status_queue=status_queue,
|
status_queue=status_queue,
|
||||||
filters=filters,
|
filters=filters,
|
||||||
@@ -150,7 +152,7 @@ class ProjectCleanup(command.Command):
|
|||||||
|
|
||||||
self.log.warning(_('Deleting resources'))
|
self.log.warning(_('Deleting resources'))
|
||||||
|
|
||||||
project_connect.project_cleanup(
|
connection.project_cleanup(
|
||||||
dry_run=False,
|
dry_run=False,
|
||||||
status_queue=status_queue,
|
status_queue=status_queue,
|
||||||
filters=filters,
|
filters=filters,
|
||||||
|
|||||||
@@ -438,15 +438,15 @@ class CacheImageForAggregate(command.Command):
|
|||||||
)
|
)
|
||||||
raise exceptions.CommandError(msg)
|
raise exceptions.CommandError(msg)
|
||||||
|
|
||||||
|
image_client = self.app.client_manager.sdk_connection.image
|
||||||
|
|
||||||
aggregate = compute_client.find_aggregate(
|
aggregate = compute_client.find_aggregate(
|
||||||
parsed_args.aggregate, ignore_missing=False
|
parsed_args.aggregate, ignore_missing=False
|
||||||
)
|
)
|
||||||
|
|
||||||
images = []
|
images = []
|
||||||
for img in parsed_args.image:
|
for img in parsed_args.image:
|
||||||
image = self.app.client_manager.sdk_connection.image.find_image(
|
image = image_client.find_image(img, ignore_missing=False)
|
||||||
img, ignore_missing=False
|
|
||||||
)
|
|
||||||
images.append(image.id)
|
images.append(image.id)
|
||||||
|
|
||||||
compute_client.aggregate_precache_images(aggregate.id, images)
|
compute_client.aggregate_precache_images(aggregate.id, images)
|
||||||
|
|||||||
@@ -88,9 +88,12 @@ class CreateNetworkTrunk(command.ShowOne):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
network_client = self.app.client_manager.network
|
||||||
attrs = _get_attrs_for_trunk(self.app.client_manager, parsed_args)
|
identity_client = self.app.client_manager.identity
|
||||||
obj = client.create_trunk(**attrs)
|
attrs = _get_attrs_for_trunk(
|
||||||
|
network_client, identity_client, parsed_args
|
||||||
|
)
|
||||||
|
obj = network_client.create_trunk(**attrs)
|
||||||
display_columns, columns = _get_columns(obj)
|
display_columns, columns = _get_columns(obj)
|
||||||
data = osc_utils.get_dict_properties(
|
data = osc_utils.get_dict_properties(
|
||||||
obj, columns, formatters=_formatters
|
obj, columns, formatters=_formatters
|
||||||
@@ -112,12 +115,12 @@ class DeleteNetworkTrunk(command.Command):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
network_client = self.app.client_manager.network
|
||||||
result = 0
|
result = 0
|
||||||
for trunk in parsed_args.trunk:
|
for trunk in parsed_args.trunk:
|
||||||
try:
|
try:
|
||||||
trunk_id = client.find_trunk(trunk).id
|
trunk_id = network_client.find_trunk(trunk).id
|
||||||
client.delete_trunk(trunk_id)
|
network_client.delete_trunk(trunk_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
result += 1
|
result += 1
|
||||||
LOG.error(
|
LOG.error(
|
||||||
@@ -150,8 +153,8 @@ class ListNetworkTrunk(command.Lister):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
network_client = self.app.client_manager.network
|
||||||
data = client.trunks()
|
data = network_client.trunks()
|
||||||
headers: tuple[str, ...] = ('ID', 'Name', 'Parent Port', 'Description')
|
headers: tuple[str, ...] = ('ID', 'Name', 'Parent Port', 'Description')
|
||||||
columns: tuple[str, ...] = ('id', 'name', 'port_id', 'description')
|
columns: tuple[str, ...] = ('id', 'name', 'port_id', 'description')
|
||||||
if parsed_args.long:
|
if parsed_args.long:
|
||||||
@@ -215,11 +218,14 @@ class SetNetworkTrunk(command.Command):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
network_client = self.app.client_manager.network
|
||||||
trunk_id = client.find_trunk(parsed_args.trunk)
|
identity_client = self.app.client_manager.identity
|
||||||
attrs = _get_attrs_for_trunk(self.app.client_manager, parsed_args)
|
trunk_id = network_client.find_trunk(parsed_args.trunk)
|
||||||
|
attrs = _get_attrs_for_trunk(
|
||||||
|
network_client, identity_client, parsed_args
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
client.update_trunk(trunk_id, **attrs)
|
network_client.update_trunk(trunk_id, **attrs)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
msg = _("Failed to set trunk '%(t)s': %(e)s") % {
|
msg = _("Failed to set trunk '%(t)s': %(e)s") % {
|
||||||
't': parsed_args.trunk,
|
't': parsed_args.trunk,
|
||||||
@@ -228,10 +234,10 @@ class SetNetworkTrunk(command.Command):
|
|||||||
raise exceptions.CommandError(msg)
|
raise exceptions.CommandError(msg)
|
||||||
if parsed_args.set_subports:
|
if parsed_args.set_subports:
|
||||||
subport_attrs = _get_attrs_for_subports(
|
subport_attrs = _get_attrs_for_subports(
|
||||||
self.app.client_manager, parsed_args
|
network_client, parsed_args
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
client.add_trunk_subports(trunk_id, subport_attrs)
|
network_client.add_trunk_subports(trunk_id, subport_attrs)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
msg = _("Failed to add subports to trunk '%(t)s': %(e)s") % {
|
msg = _("Failed to add subports to trunk '%(t)s': %(e)s") % {
|
||||||
't': parsed_args.trunk,
|
't': parsed_args.trunk,
|
||||||
@@ -251,9 +257,9 @@ class ShowNetworkTrunk(command.ShowOne):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
network_client = self.app.client_manager.network
|
||||||
trunk_id = client.find_trunk(parsed_args.trunk).id
|
trunk_id = network_client.find_trunk(parsed_args.trunk).id
|
||||||
obj = client.get_trunk(trunk_id)
|
obj = network_client.get_trunk(trunk_id)
|
||||||
display_columns, columns = _get_columns(obj)
|
display_columns, columns = _get_columns(obj)
|
||||||
data = osc_utils.get_dict_properties(
|
data = osc_utils.get_dict_properties(
|
||||||
obj, columns, formatters=_formatters
|
obj, columns, formatters=_formatters
|
||||||
@@ -275,9 +281,9 @@ class ListNetworkSubport(command.Lister):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
network_client = self.app.client_manager.network
|
||||||
trunk_id = client.find_trunk(parsed_args.trunk)
|
trunk_id = network_client.find_trunk(parsed_args.trunk)
|
||||||
data = client.get_trunk_subports(trunk_id)
|
data = network_client.get_trunk_subports(trunk_id)
|
||||||
headers: tuple[str, ...] = (
|
headers: tuple[str, ...] = (
|
||||||
'Port',
|
'Port',
|
||||||
'Segmentation Type',
|
'Segmentation Type',
|
||||||
@@ -324,10 +330,10 @@ class UnsetNetworkTrunk(command.Command):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
network_client = self.app.client_manager.network
|
||||||
attrs = _get_attrs_for_subports(self.app.client_manager, parsed_args)
|
attrs = _get_attrs_for_subports(network_client, parsed_args)
|
||||||
trunk_id = client.find_trunk(parsed_args.trunk)
|
trunk_id = network_client.find_trunk(parsed_args.trunk)
|
||||||
client.delete_trunk_subports(trunk_id, attrs)
|
network_client.delete_trunk_subports(trunk_id, attrs)
|
||||||
|
|
||||||
|
|
||||||
_formatters = {
|
_formatters = {
|
||||||
@@ -343,7 +349,7 @@ def _get_columns(item):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _get_attrs_for_trunk(client_manager, parsed_args):
|
def _get_attrs_for_trunk(network_client, identity_client, parsed_args):
|
||||||
attrs: dict[str, ty.Any] = {}
|
attrs: dict[str, ty.Any] = {}
|
||||||
if parsed_args.name is not None:
|
if parsed_args.name is not None:
|
||||||
attrs['name'] = str(parsed_args.name)
|
attrs['name'] = str(parsed_args.name)
|
||||||
@@ -354,18 +360,15 @@ def _get_attrs_for_trunk(client_manager, parsed_args):
|
|||||||
if parsed_args.disable:
|
if parsed_args.disable:
|
||||||
attrs['admin_state_up'] = False
|
attrs['admin_state_up'] = False
|
||||||
if 'parent_port' in parsed_args and parsed_args.parent_port is not None:
|
if 'parent_port' in parsed_args and parsed_args.parent_port is not None:
|
||||||
port_id = client_manager.network.find_port(parsed_args.parent_port)[
|
port_id = network_client.find_port(parsed_args.parent_port)['id']
|
||||||
'id'
|
|
||||||
]
|
|
||||||
attrs['port_id'] = port_id
|
attrs['port_id'] = port_id
|
||||||
if 'add_subports' in parsed_args and parsed_args.add_subports is not None:
|
if 'add_subports' in parsed_args and parsed_args.add_subports is not None:
|
||||||
attrs[SUB_PORTS] = _format_subports(
|
attrs[SUB_PORTS] = _format_subports(
|
||||||
client_manager, parsed_args.add_subports
|
network_client, parsed_args.add_subports
|
||||||
)
|
)
|
||||||
|
|
||||||
# "trunk set" command doesn't support setting project.
|
# "trunk set" command doesn't support setting project.
|
||||||
if 'project' in parsed_args and parsed_args.project is not None:
|
if 'project' in parsed_args and parsed_args.project is not None:
|
||||||
identity_client = client_manager.identity
|
|
||||||
project_id = identity_utils.find_project(
|
project_id = identity_utils.find_project(
|
||||||
identity_client,
|
identity_client,
|
||||||
parsed_args.project,
|
parsed_args.project,
|
||||||
@@ -376,12 +379,12 @@ def _get_attrs_for_trunk(client_manager, parsed_args):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
def _format_subports(client_manager, subports):
|
def _format_subports(network_client, subports):
|
||||||
attrs = []
|
attrs = []
|
||||||
for subport in subports:
|
for subport in subports:
|
||||||
subport_attrs = {}
|
subport_attrs = {}
|
||||||
if subport.get('port'):
|
if subport.get('port'):
|
||||||
port_id = client_manager.network.find_port(subport['port'])['id']
|
port_id = network_client.find_port(subport['port'])['id']
|
||||||
subport_attrs['port_id'] = port_id
|
subport_attrs['port_id'] = port_id
|
||||||
if subport.get('segmentation-id'):
|
if subport.get('segmentation-id'):
|
||||||
try:
|
try:
|
||||||
@@ -400,17 +403,17 @@ def _format_subports(client_manager, subports):
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
def _get_attrs_for_subports(client_manager, parsed_args):
|
def _get_attrs_for_subports(network_client, parsed_args):
|
||||||
attrs = []
|
attrs = []
|
||||||
if 'set_subports' in parsed_args and parsed_args.set_subports is not None:
|
if 'set_subports' in parsed_args and parsed_args.set_subports is not None:
|
||||||
attrs = _format_subports(client_manager, parsed_args.set_subports)
|
attrs = _format_subports(network_client, parsed_args.set_subports)
|
||||||
if (
|
if (
|
||||||
'unset_subports' in parsed_args
|
'unset_subports' in parsed_args
|
||||||
and parsed_args.unset_subports is not None
|
and parsed_args.unset_subports is not None
|
||||||
):
|
):
|
||||||
subports_list = []
|
subports_list = []
|
||||||
for subport in parsed_args.unset_subports:
|
for subport in parsed_args.unset_subports:
|
||||||
port_id = client_manager.network.find_port(subport)['id']
|
port_id = network_client.find_port(subport)['id']
|
||||||
subports_list.append({'port_id': port_id})
|
subports_list.append({'port_id': port_id})
|
||||||
attrs = subports_list
|
attrs = subports_list
|
||||||
return attrs
|
return attrs
|
||||||
|
|||||||
Reference in New Issue
Block a user