From 5fb5e8378778dc7bee6e6e761c1b0a3af2e235ab Mon Sep 17 00:00:00 2001 From: Komal Thareja Date: Wed, 12 Oct 2022 11:07:32 -0400 Subject: [PATCH 1/3] add support for close/remove delegation --- fabric_mgmt_cli/__init__.py | 2 +- fabric_mgmt_cli/managecli/manage_command.py | 81 +++++++++++++++++++++ fabric_mgmt_cli/managecli/managecli.py | 50 ++++++++++++- fabric_mgmt_cli/managecli/show_command.py | 23 +++--- requirements.txt | 5 +- 5 files changed, 146 insertions(+), 15 deletions(-) diff --git a/fabric_mgmt_cli/__init__.py b/fabric_mgmt_cli/__init__.py index 62b04c5..caa0f14 100644 --- a/fabric_mgmt_cli/__init__.py +++ b/fabric_mgmt_cli/__init__.py @@ -1 +1 @@ -__VERSION__ = "1.2.1" +__VERSION__ = "1.3.0rc1" diff --git a/fabric_mgmt_cli/managecli/manage_command.py b/fabric_mgmt_cli/managecli/manage_command.py index f5652b0..3643148 100644 --- a/fabric_mgmt_cli/managecli/manage_command.py +++ b/fabric_mgmt_cli/managecli/manage_command.py @@ -426,6 +426,87 @@ def delete_dead_slices(self, *, actor_name: str, callback_topic: str, id_token: callback_topic=callback_topic, id_token=id_token) else: print("No Dead/closing slices to remove") + except Exception as e: + self.logger.error(f"Exception occurred e: {e}") + self.logger.error(traceback.format_exc()) + + def do_close_delegation(self, *, did: str, actor_name: str, callback_topic: str, + id_token: str) -> Tuple[bool, Error]: + """ + Close delegation by invoking Management Actor Close delegation API + @param did delegation id + @param actor_name actor name + @param callback_topic callback topic + @param id_token identity token + @return Tuple[bool, Error] indicating success or failure status and error containing failure details + """ + actor = self.get_actor(actor_name=actor_name) + if actor is None: + raise Exception(f"Invalid arguments actor_name {actor_name} not found") + + try: + actor.prepare(callback_topic=callback_topic) + return actor.close_delegation(did=did), actor.get_last_error() + except Exception as e: + self.logger.error(f"Exception occurred e: {e}") + self.logger.error(traceback.format_exc()) + + return False, actor.get_last_error() + + def close_delegation(self, *, did: str, actor_name: str, callback_topic: str, id_token: str): + """ + Close delegation + @param did delegation id + @param actor_name actor name + @param callback_topic callback topic + @param id_token identity token + """ + try: + result, error = self.do_close_delegation(did=did, actor_name=actor_name, + callback_topic=callback_topic, id_token=id_token) + print(result) + if result is False: + self.print_result(status=error.get_status()) + except Exception as e: + self.logger.error(f"Exception occurred e: {e}") + self.logger.error(traceback.format_exc()) + + def do_remove_delegation(self, *, did: str, actor_name: str, callback_topic: str, + id_token: str) -> Tuple[bool, Error]: + """ + Remove delegation by invoking Management Actor Remove delegation API + @param did delegation id + @param actor_name actor name + @param callback_topic callback topic + @param id_token identity token + @return Tuple[bool, Error] indicating success or failure status and error containing failure details + """ + actor = self.get_actor(actor_name=actor_name) + if actor is None: + raise Exception("Invalid arguments actor_name {} not found".format(actor_name)) + + try: + actor.prepare(callback_topic=callback_topic) + return actor.remove_delegation(did=did), actor.get_last_error() + except Exception as e: + self.logger.error(f"Exception occurred e: {e}") + self.logger.error(traceback.format_exc()) + return False, actor.get_last_error() + + def remove_delegation(self, *, did: str, actor_name: str, callback_topic: str, id_token: str): + """ + Remove delegation + @param did delegation id + @param actor_name actor name + @param callback_topic callback topic + @param id_token identity token + """ + try: + result, error = self.do_remove_delegation(did=did, actor_name=actor_name, callback_topic=callback_topic, + id_token=id_token) + print(result) + if result is False: + self.print_result(status=error.get_status()) except Exception as e: self.logger.error(f"Exception occurred e: {e}") self.logger.error(traceback.format_exc()) \ No newline at end of file diff --git a/fabric_mgmt_cli/managecli/managecli.py b/fabric_mgmt_cli/managecli/managecli.py index bffef3d..b22dc8f 100644 --- a/fabric_mgmt_cli/managecli/managecli.py +++ b/fabric_mgmt_cli/managecli/managecli.py @@ -120,15 +120,19 @@ def removealldead(ctx, email, actor, idtoken, refreshtoken): @click.option('--idtoken', default=None, help='Fabric Identity Token', required=False) @click.option('--refreshtoken', default=None, help='Fabric Refresh Token', required=False) @click.option('--email', default=None, help='User email', required=False) +@click.option('--state', + type=click.Choice(['nascent', 'configuring', 'stableok', 'stableerror', 'modifyok', 'modifyerror', + 'closing', 'dead'], + case_sensitive=False), required=False) @click.pass_context -def query(ctx, actor, sliceid, slicename, idtoken, refreshtoken, email): +def query(ctx, actor, sliceid, slicename, idtoken, refreshtoken, email, state): """ Get slice(s) from an actor """ try: idtoken = KafkaProcessorSingleton.get().start(id_token=idtoken, refresh_token=refreshtoken, ignore_tokens=True) mgmt_command = ShowCommand(logger=KafkaProcessorSingleton.get().logger) mgmt_command.get_slices(actor_name=actor, callback_topic=KafkaProcessorSingleton.get().get_callback_topic(), - slice_id=sliceid, slice_name=slicename, id_token=idtoken, email=email) + slice_id=sliceid, slice_name=slicename, id_token=idtoken, email=email, state=state) KafkaProcessorSingleton.get().stop() except Exception as e: # traceback.print_exc() @@ -315,6 +319,48 @@ def query(ctx, actor, sliceid, did, state, idtoken, refreshtoken): click.echo('Error occurred: {}'.format(e)) +@delegations.command() +@click.option('--did', help='Delegation Id', required=True) +@click.option('--actor', help='Actor Name', required=True) +@click.option('--idtoken', default=None, help='Fabric Identity Token', required=False) +@click.option('--refreshtoken', default=None, help='Fabric Refresh Token', required=False) +@click.pass_context +def close(ctx, did, actor, idtoken, refreshtoken): + """ Closes delegation for an actor + """ + try: + idtoken = KafkaProcessorSingleton.get().start(id_token=idtoken, refresh_token=refreshtoken, ignore_tokens=True) + mgmt_command = ManageCommand(logger=KafkaProcessorSingleton.get().logger) + mgmt_command.close_delegation(did=did, actor_name=actor, + callback_topic=KafkaProcessorSingleton.get().get_callback_topic(), + id_token=idtoken) + KafkaProcessorSingleton.get().stop() + except Exception as e: + # traceback.print_exc() + click.echo('Error occurred: {}'.format(e)) + + +@delegations.command() +@click.option('--did', help='Delegation Id', required=True) +@click.option('--actor', help='Actor Name', required=True) +@click.option('--idtoken', default=None, help='Fabric Identity Token', required=False) +@click.option('--refreshtoken', default=None, help='Fabric Refresh Token', required=False) +@click.pass_context +def remove(ctx, did, actor, idtoken, refreshtoken): + """ Removes sliver for an actor + """ + try: + idtoken = KafkaProcessorSingleton.get().start(id_token=idtoken, refresh_token=refreshtoken, ignore_tokens=True) + mgmt_command = ManageCommand(logger=KafkaProcessorSingleton.get().logger) + mgmt_command.remove_delegation(did=did, actor_name=actor, + callback_topic=KafkaProcessorSingleton.get().get_callback_topic(), + id_token=idtoken) + KafkaProcessorSingleton.get().stop() + except Exception as e: + # traceback.print_exc() + click.echo('Error occurred: {}'.format(e)) + + @click.group() @click.pass_context def maintenance(ctx): diff --git a/fabric_mgmt_cli/managecli/show_command.py b/fabric_mgmt_cli/managecli/show_command.py index 3dea5b4..56072c8 100644 --- a/fabric_mgmt_cli/managecli/show_command.py +++ b/fabric_mgmt_cli/managecli/show_command.py @@ -44,10 +44,10 @@ class ShowCommand(Command): def get_slices(self, *, actor_name: str, callback_topic: str, slice_id: str, slice_name: str, id_token: str, - email: str): + email: str, state: str): try: slices, error = self.do_get_slices(actor_name=actor_name, callback_topic=callback_topic, slice_id=slice_id, - slice_name=slice_name, id_token=id_token, email=email) + slice_name=slice_name, id_token=id_token, email=email, state=state) if slices is not None and len(slices) > 0: for s in slices: self.__print_slice(slice_object=s) @@ -90,7 +90,7 @@ def get_delegations(self, *, actor_name: str, callback_topic: str, slice_id: str print("Exception occurred while processing get_delegations {}".format(e)) def do_get_slices(self, *, actor_name: str, callback_topic: str, slice_id: str = None, slice_name: str = None, - id_token: str = None, email: str = None) -> Tuple[List[SliceAvro], Error]: + id_token: str = None, email: str = None, state: str = None) -> Tuple[List[SliceAvro] or None, Error]: actor = self.get_actor(actor_name=actor_name) if actor is None: @@ -98,7 +98,12 @@ def do_get_slices(self, *, actor_name: str, callback_topic: str, slice_id: str = try: actor.prepare(callback_topic=callback_topic) sid = ID(uid=slice_id) if slice_id is not None else None - return actor.get_slices(slice_id=sid, slice_name=slice_name, email=email), actor.get_last_error() + slice_state = None + if state is not None: + slice_state = [SliceState.translate(state_name=state).value] + + result = actor.get_slices(slice_id=sid, slice_name=slice_name, email=email, state=slice_state) + return result, actor.get_last_error() except Exception: ex_str = traceback.format_exc() self.logger.error(ex_str) @@ -106,7 +111,7 @@ def do_get_slices(self, *, actor_name: str, callback_topic: str, slice_id: str = def do_get_reservations(self, *, actor_name: str, callback_topic: str, slice_id: str = None, rid: str = None, state: str = None, id_token: str = None, - email: str = None) -> Tuple[List[ReservationMng], Error]: + email: str = None) -> Tuple[List[ReservationMng] or None, Error]: actor = self.get_actor(actor_name=actor_name) if actor is None: @@ -126,7 +131,7 @@ def do_get_reservations(self, *, actor_name: str, callback_topic: str, slice_id: return None, actor.get_last_error() def do_get_delegations(self, *, actor_name: str, callback_topic: str, slice_id: str = None, did: str = None, - state: str = None, id_token: str = None) -> Tuple[List[DelegationAvro], Error]: + state: str = None, id_token: str = None) -> Tuple[List[DelegationAvro] or None, Error]: actor = self.get_actor(actor_name=actor_name) if actor is None: @@ -142,9 +147,9 @@ def do_get_delegations(self, *, actor_name: str, callback_topic: str, slice_id: return actor.get_delegations(delegation_id=did, slice_id=sid, state=delegation_state), actor.get_last_error() except Exception as e: + self.logger.error(f"Exception occurred while fetching delegations: e {e}") + self.logger.error(traceback.format_exc()) traceback.print_exc() - ex_str = traceback.format_exc() - self.logger.error(ex_str) return None, actor.get_last_error() @staticmethod @@ -208,7 +213,7 @@ def __print_slice(*, slice_object: SliceAvro): print(f"Slice owner: {slice_object.get_owner()}") if slice_object.get_state() is not None: - print(f"Slice state: {SliceState(slice_object.get_state())}") + print(f"Slice state: {str(SliceState(slice_object.get_state()))}") if slice_object.get_lease_end() is not None: print(f"Lease time: {slice_object.get_lease_end()}") diff --git a/requirements.txt b/requirements.txt index 78a5d72..334d1c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,5 @@ certifi==2020.12.5 attrs==20.3.0 Jinja2==2.11.3 MarkupSafe==1.1.1 -python-dateutil==2.8.1 -fabric-cf==1.2.2 -fabric-credmgr-client==1.2 +fabric-cf==1.3.1rc2 +fabric-credmgr-client==1.3.2 From d8b28b5320372291fa18111cd4720ab906f5464d Mon Sep 17 00:00:00 2001 From: Komal Thareja Date: Wed, 12 Oct 2022 22:39:56 -0400 Subject: [PATCH 2/3] fix versions --- fabric_mgmt_cli/__init__.py | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fabric_mgmt_cli/__init__.py b/fabric_mgmt_cli/__init__.py index caa0f14..0b0ad2a 100644 --- a/fabric_mgmt_cli/__init__.py +++ b/fabric_mgmt_cli/__init__.py @@ -1 +1 @@ -__VERSION__ = "1.3.0rc1" +__VERSION__ = "1.3.0" diff --git a/requirements.txt b/requirements.txt index 334d1c6..71c4a95 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,5 +8,5 @@ certifi==2020.12.5 attrs==20.3.0 Jinja2==2.11.3 MarkupSafe==1.1.1 -fabric-cf==1.3.1rc2 +fabric-cf==1.3.1 fabric-credmgr-client==1.3.2 From d308e22ba345968d58d15d8033a0cf979ff7a24b Mon Sep 17 00:00:00 2001 From: Komal Thareja Date: Thu, 13 Oct 2022 09:02:08 -0400 Subject: [PATCH 3/3] display state name for delegation --- fabric_mgmt_cli/managecli/show_command.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/fabric_mgmt_cli/managecli/show_command.py b/fabric_mgmt_cli/managecli/show_command.py index 56072c8..1602ec4 100644 --- a/fabric_mgmt_cli/managecli/show_command.py +++ b/fabric_mgmt_cli/managecli/show_command.py @@ -81,7 +81,7 @@ def get_delegations(self, *, actor_name: str, callback_topic: str, slice_id: str slice_id=slice_id, did=did, state=state, id_token=id_token) if delegations is not None and len(delegations) > 0: for d in delegations: - d.print() + ShowCommand.__print_delegation(dlg_object=d) else: print("Status: {}".format(error.get_status())) except Exception as e: @@ -218,3 +218,21 @@ def __print_slice(*, slice_object: SliceAvro): if slice_object.get_lease_end() is not None: print(f"Lease time: {slice_object.get_lease_end()}") print("") + + @staticmethod + def __print_delegation(*, dlg_object: DelegationAvro): + """ + Prints the Delegation Object + """ + print("") + print("Delegation ID: {} Slice ID: {}".format(dlg_object.delegation_id, dlg_object.slice.get_slice_id())) + if dlg_object.delegation_name is not None: + print("Delegation Name: {}".format(dlg_object.delegation_name)) + if dlg_object.sequence is not None: + print("Sequence: {}".format(dlg_object.sequence)) + if dlg_object.state is not None: + print(f"State: {DelegationState(dlg_object.state)}") + if dlg_object.graph is not None: + print("Graph: {}".format(dlg_object.graph)) + print("") +