1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
# Copyright (c) 2019 University of Chicago.
#
# 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.
import logging
from blazarclient import command
from blazarclient import utils
RESOURCE_ID_PATTERN = '^[0-9]+$'
class ShowAllocations(command.ShowCommand):
"""Show allocations for resource identified by type and ID."""
resource = 'allocation'
json_indent = 4
id_pattern = RESOURCE_ID_PATTERN
name_key = 'hypervisor_hostname'
log = logging.getLogger(__name__ + '.ShowHostAllocation')
def get_parser(self, prog_name):
parser = super(ShowAllocations, self).get_parser(prog_name)
parser.add_argument(
'resource_type',
choices=['host'],
help='Show allocations for a resource type'
)
if self.allow_names:
help_str = 'ID or name of %s to look up'
else:
help_str = 'ID of %s to look up'
parser.add_argument('id', metavar="RESOURCE",
help=help_str % "resource")
parser.add_argument(
'--reservation-id',
dest='reservation_id',
default=None,
help='Show only allocations with specific reservation_id'
)
parser.add_argument(
'--lease-id',
dest='lease_id',
default=None,
help='Show only allocations with specific lease_id'
)
return parser
def get_data(self, parsed_args):
self.log.debug('get_data(%s)' % parsed_args)
blazar_client = self.get_client()
resource_manager = getattr(blazar_client, self.resource)
if self.allow_names:
res_id = utils.find_resource_id_by_name_or_id(
blazar_client,
parsed_args.resource_type,
parsed_args.id,
self.name_key,
self.id_pattern)
else:
res_id = parsed_args.id
data = resource_manager.get(
self.args2body(parsed_args)['resource'], res_id)
if parsed_args.lease_id is not None:
data['reservations'] = list(
filter(lambda d: d['lease_id'] == parsed_args.lease_id,
data['reservations']))
if parsed_args.reservation_id is not None:
data['reservations'] = list(
filter(lambda d: d['id'] == parsed_args.reservation_id,
data['reservations']))
self.format_output_data(data)
return list(zip(*sorted(data.items())))
def args2body(self, parsed_args):
params = {}
if parsed_args.resource_type == 'host':
params.update(dict(resource='os-hosts'))
return params
class ListAllocations(command.ListCommand):
"""List allocations for all resources of a type."""
resource = 'allocation'
log = logging.getLogger(__name__ + '.ListHostAllocations')
list_columns = ['resource_id', 'reservations']
def get_parser(self, prog_name):
parser = super(ListAllocations, self).get_parser(prog_name)
parser.add_argument(
'resource_type',
choices=['host'],
help='Show allocations for a resource type'
)
parser.add_argument(
'--reservation-id',
dest='reservation_id',
default=None,
help='Show only allocations with specific reservation_id'
)
parser.add_argument(
'--lease-id',
dest='lease_id',
default=None,
help='Show only allocations with specific lease_id'
)
parser.add_argument(
'--sort-by', metavar="<allocation_column>",
help='column name used to sort result',
default='resource_id'
)
return parser
def get_data(self, parsed_args):
self.log.debug('get_data(%s)' % parsed_args)
data = self.retrieve_list(parsed_args)
for resource in data:
if parsed_args.lease_id is not None:
resource['reservations'] = list(
filter(lambda d: d['lease_id'] == parsed_args.lease_id,
resource['reservations']))
if parsed_args.reservation_id is not None:
resource['reservations'] = list(
filter(lambda d: d['id'] == parsed_args.reservation_id,
resource['reservations']))
return self.setup_columns(data, parsed_args)
def args2body(self, parsed_args):
params = super(ListAllocations, self).args2body(parsed_args)
if parsed_args.resource_type == 'host':
params.update(dict(resource='os-hosts'))
return params
|