aws / aws/aws-cli

Supported nested conditionals in wizards definition

Open
#7,389 1 comment 0 reactions 1 assignee Claimed by @RyanFitzSimmonsAK View on GitHub
feature-request p2 wizard
Dominant language
Python
Stars
17.3k
Forks
4.6k
Avg merge
1d 2h
Merged PRs (30d)
13

Description

### Describe the feature

We added support for if statements in wizards several years ago (https://github.com/aws/aws-cli/blob/v2/awscli/customizations/wizard/core.py#L204-L208), but given the regex based nature of the implementation it cannot support nested conditionals in an if statement.

### Use Case

My specific use case is that I want to create an `aws iam wizard new-role` wizard that supports not only optionally adding a CLI profile that supports `source_profile` but supports all the credential sources you can configure in addition to just the source profile (see docs [here](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-role.html)).

Specifically I want to do something like this:

```yaml
ask_profile_config:
shortname: CLI config
description: Determine CLI configuration
values:
wants_config_profile:
type: prompt
description: Do you want to create a new CLI profile with this role?
choices:
- display: Yes
actual_value: yes
- display: No
actual_value: no
new_profile_name:
type: prompt
description: Enter the name of the new profile
condition:
variable: wants_config_profile
equals: yes
existing_profiles:
type: sharedconfig
operation: ListProfiles
source_type:
type: prompt
description: Select the credential source
condition:
variable: wants_config_profile
equals: yes
choices:
- display: Source Profile
actual_value: wants_source_profile
- display: Environment Variables
actual_value: wants_env_source
- display: EC2 Instance Metadata
actual_value: wants_imds_source
- display: ECS Container Task Role
actual_value: wants_ecs_source
source_profile:
type: prompt
description: Name of the source profile
choices: existing_profiles
condition:
variable: source_type
equals: wants_source_profile
```

And then:

```yaml
preview:
shortname: Preview
description: Preview results
values:
preview_cli_command_value:
type: template
value: |

... omitted for breveity...

... note the nested if statements below ...

{% if {wants_config_profile} == yes %}
{% if {source_type} == wants_source_profile %}
aws configure set source_profile {source_profile} --profile {new_profile_name}
{% endif %}
{% if {source_type} == wants_env_source %}
aws configure set credential_source Environment --profile {new_profile_name}
{% endif %}
{% if {source_type} == wants_imds_source %}
aws configure set credential_source Ec2InstanceMetadata --profile {new_profile_name}
{% endif %}
{% if {source_type} == wants_ecs_source %}
aws configure set credential_source EcsContainer --profile {new_profile_name}
{% endif %}

role_arn=$(aws iam get-role --role-name "{role_name}" \
--query Role.Arn --output text)

aws configure set role_arn "$role_arn" --profile {new_profile_name}
{% endif %}

```

### Proposed Solution

While ideally I'd love if we just pulled in jinja2 and called it good, in practice I think this will mean we'll actually need to implement a basic lexer/parser/tree walker (interpreter) to handle this case which I don't mind implementing if necessary.

### Other Information

_No response_

### Acknowledgements

- [ ] This feature might incur a breaking change
- [X] I may be able to implement this feature request

### CLI version used

v2

### Environment details (OS name and version, etc.)

All of them

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.