Skip to content

Commit

Permalink
fetching additional resources reserved in a lease (#31)
Browse files Browse the repository at this point in the history
* fetching additional resources reserved in a lease, such as hosts, networks, and devices

command to accept a new --detail flag, enabling users to request all resources reserved in a lease

* Parallelize calls to get allocations in lease detail get

Parallelize the calls to hosts_in_lease, networks_in_lease, and devices_in_lease using Python's concurrent.futures module. By submitting these calls to a thread pool for concurrent execution and retrieving the results once the tasks are completed, the performance of the get method is potentially improved.
  • Loading branch information
AnishReddyRavula authored Apr 10, 2024
1 parent 1a4e448 commit 5566adb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion blazarclient/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ def get_parser(self, prog_name):

def get_data(self, parsed_args):
self.log.debug('get_data(%s)' % parsed_args)
body = self.args2body(parsed_args)
blazar_client = self.get_client()

if self.allow_names:
Expand All @@ -328,7 +329,7 @@ def get_data(self, parsed_args):
res_id = parsed_args.id

resource_manager = getattr(blazar_client, self.resource)
data = resource_manager.get(res_id)
data = resource_manager.get(res_id, detail=body['detail'])
self.format_output_data(data)
return list(zip(*sorted(data.items())))

Expand Down
30 changes: 29 additions & 1 deletion blazarclient/v1/leases.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from concurrent.futures import ThreadPoolExecutor

from oslo_utils import timeutils

from blazarclient import base
Expand All @@ -32,11 +34,22 @@ def create(self, name, start, end, reservations, events, before_end=None):
resp, body = self.request_manager.post('/leases', body=values)
return body['lease']

def get(self, lease_id):
def get(self, lease_id, detail=False):
"""Describes lease specifications such as name, status and locked
condition.
"""
resp, body = self.request_manager.get('/leases/%s' % lease_id)
if detail and body['lease']:
with ThreadPoolExecutor() as executor:
# Submit the calls
h_future = executor.submit(self.hosts_in_lease, lease_id)
n_future = executor.submit(self.networks_in_lease, lease_id)
d_future = executor.submit(self.devices_in_lease, lease_id)

# Retrieve the results
body['lease']['hosts'] = h_future.result()
body['lease']['networks'] = n_future.result()
body['lease']['devices'] = d_future.result()
return body['lease']

def update(self, lease_id, name=None, prolong_for=None, reduce_by=None,
Expand Down Expand Up @@ -94,6 +107,21 @@ def list(self, sort_by=None):
leases = sorted(leases, key=lambda l: l[sort_by])
return leases

def hosts_in_lease(self, lease_id):
"""List all hosts in lease"""
resp, body = self.request_manager.get(f'/leases/{lease_id}/hosts')
return body['hosts']

def networks_in_lease(self, lease_id):
"""List all networks in lease"""
resp, body = self.request_manager.get(f'/leases/{lease_id}/networks')
return body['networks']

def devices_in_lease(self, lease_id):
"""List all devices in lease"""
resp, body = self.request_manager.get(f'/leases/{lease_id}/devices')
return body['devices']

def _add_lease_date(self, values, lease, key, delta_date, positive_delta):
delta_sec = utils.from_elapsed_time_to_delta(
delta_date,
Expand Down
15 changes: 15 additions & 0 deletions blazarclient/v1/shell_commands/leases.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ class ShowLease(command.ShowCommand):
name_key = 'name'
log = logging.getLogger(__name__ + '.ShowLease')

def get_parser(self, prog_name):
parser = super(ShowLease, self).get_parser(prog_name)
parser.add_argument(
'--detail',
action='store_true',
help='Return all resources reserved in lease.',
default=False
)
return parser

def args2body(self, parsed_args):
params = {}
params['detail'] = parsed_args.detail
return params


class CreateLeaseBase(command.CreateCommand):
"""Create a lease."""
Expand Down

0 comments on commit 5566adb

Please sign in to comment.