From ca4120cddde7ba6b6598faa6fda8ffd9f6cc6e44 Mon Sep 17 00:00:00 2001 From: Artem Goncharov <artem.goncharov@gmail.com> Date: Tue, 17 Sep 2024 20:09:40 +0200 Subject: [PATCH] Extend user path param with `--current-user` Recently support of `--user-id|--user-name` has been added when user_id is a path parameter of the resource. To make it even more smoother extend it with `--user-id <ID>|--user-name <NAME>|--current-user` to allow using current authenticated user while being very explicit (ensure user precisely decides what is desired. Change-Id: I0261140572e76799ea54dc0c9544cf5d094c1120 --- codegenerator/rust_cli.py | 1 + codegenerator/templates/rust_cli/path_parameters.j2 | 5 +++++ codegenerator/templates/rust_macros.j2 | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/codegenerator/rust_cli.py b/codegenerator/rust_cli.py index 13de330..24c714a 100644 --- a/codegenerator/rust_cli.py +++ b/codegenerator/rust_cli.py @@ -1037,6 +1037,7 @@ class RustCliGenerator(BaseGenerator): global_additional_imports.add( f"openstack_sdk::api::{'::'.join(link_res_name.split('/'))}::find as find_{link_res_name.split('/')[-1]}" ) + global_additional_imports.add("eyre::OptionExt") # List of operation variants (based on the body) operation_variants = common.get_operation_variants(spec, args.operation_name) diff --git a/codegenerator/templates/rust_cli/path_parameters.j2 b/codegenerator/templates/rust_cli/path_parameters.j2 index 4e798a5..b8b97bb 100644 --- a/codegenerator/templates/rust_cli/path_parameters.j2 +++ b/codegenerator/templates/rust_cli/path_parameters.j2 @@ -31,6 +31,11 @@ struct {{ res_name | title }}Input { /// {{ res_name | title }} ID. #[arg(long, help_heading = "Path parameters", value_name = "{{ res_name | upper }}_ID")] {{ res_name }}_id: Option<String>, + {%- if res_name == "user" %} + /// Current authenticated user. + #[arg(long, help_heading = "Path parameters", action = clap::ArgAction::SetTrue)] + current_user: bool, + {%- endif %} } {%- endif %} {%- endfor %} diff --git a/codegenerator/templates/rust_macros.j2 b/codegenerator/templates/rust_macros.j2 index 4a439de..4eb86fb 100644 --- a/codegenerator/templates/rust_macros.j2 +++ b/codegenerator/templates/rust_macros.j2 @@ -418,6 +418,18 @@ Some({{ val }}) } }; } + {%- if res_name == "user" and v.remote_name == "user_id" %} + else if self.path.{{ res_name }}.current_{{ res_name }} { + {{ builder }}.{{ v.remote_name }}( + client + .get_auth_info() + .ok_or_eyre("Cannot determine current authentication information")? + .token + .user + .id, + ); + } + {%- endif %} {%- else %} {%- if not v.is_required %} {%- if k != "project_id" %}