From 5566adb85e299d424c4b269a0a3a027461f6e62b Mon Sep 17 00:00:00 2001 From: Anish Reddy <2anishreddy@gmail.com> Date: Wed, 10 Apr 2024 08:28:23 -0500 Subject: [PATCH] fetching additional resources reserved in a lease (#31) * 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. --- blazarclient/command.py | 3 ++- blazarclient/v1/leases.py | 30 +++++++++++++++++++++++- blazarclient/v1/shell_commands/leases.py | 15 ++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/blazarclient/command.py b/blazarclient/command.py index b84e9bb..bc2eca4 100644 --- a/blazarclient/command.py +++ b/blazarclient/command.py @@ -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: @@ -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()))) diff --git a/blazarclient/v1/leases.py b/blazarclient/v1/leases.py index 2fb59cf..1db10ad 100644 --- a/blazarclient/v1/leases.py +++ b/blazarclient/v1/leases.py @@ -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 @@ -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, @@ -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, diff --git a/blazarclient/v1/shell_commands/leases.py b/blazarclient/v1/shell_commands/leases.py index 02b13b7..a47e6bc 100644 --- a/blazarclient/v1/shell_commands/leases.py +++ b/blazarclient/v1/shell_commands/leases.py @@ -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."""