Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random_shuffle need to add position 0 of an array to return 1 single result string #508

Closed
1 task done
mrk-qa opened this issue Jan 9, 2024 · 2 comments
Closed
1 task done

Comments

@mrk-qa
Copy link

mrk-qa commented Jan 9, 2024

Terraform CLI and Provider Versions

Terraform v1.6.6
hashicorp/random v3.6.0

Terraform Configuration

resource "random_shuffle" "types_instance_aws" {
  input        = ["t2.nano", "t2.micro", "t2.small", "t2.medium"]
  result_count = 1
}

output "instance_types_aws" {
  value = random_shuffle.types_instance_aws.result
}

// example: ["t2.nano"]
// need to add position 0 of an array, to return 1 single result from the string

Expected Behavior

Expected that if result_count = 1, return a string

Actual Behavior

Returned an array containing string

Steps to Reproduce

  1. terraform init
  2. terraform apply

How much impact is this issue causing?

Low

Logs

No response

Additional Information

The alternative solution was to add an array at position 0 to return a string only.

Example:

resource "random_shuffle" "types_instance_aws" {
input = ["t2.nano", "t2.micro", "t2.small", "t2.medium"]
result_count = 1
}

output "instance_types_aws" {
value = random_shuffle.types_instance_aws.result[0]
}

Code of Conduct

  • I agree to follow this project's Code of Conduct
@mrk-qa mrk-qa added the bug label Jan 9, 2024
@bflad
Copy link
Member

bflad commented Feb 26, 2024

Hi @mrk-qa 👋 Thank you for raising this issue.

Since the random_shuffle managed resource result attribute is already documented as a list of strings and the provider implementation is following the documented behavior, I'm going to relabel this bug report as a feature request.

Terraform generally uses a static type system throughout to enable static validation of configuration and other practitioner-related enhancements, such as producing higher fidelity error messaging about data handling. This model extends to providers, where provider authors generally should choose a single static type to represent attributes (whether computed or not). While it would be technically possible to implement a dynamic type constraint for the result attribute, enabling it to return either a list of strings if result_count is greater than 1 or a string if result_count is equal to 1, would need to be introduced as a breaking change in a major version and would require that value consuming configurations need to introduce more complexity if the result_count is conditional.

Rather than changing the existing random_shuffle managed resource implementation that is designed for lists, would your use case be suited by the introduction of a separate managed resource that was tailored for "selecting" a single value out of a list?

For example:

# This is a design sketch of what a proposed managed resource
# for selecting a single value out of a list/set might look like. It
# is not available in the hashicorp/random provider today.
resource "random_selection" "example" {
  input = ["one", "two", "three"]
}

output "example" {
  # one of: "one", "two", or "three"
  value = random_selection.example.result
}

This sort of feature request would be less contentious for consideration since the implementation can be done without potentially impacting existing random_shuffle managed resource usage in the ecosystem. Would something like this satisfy your use case?

Please let us know. Thanks!

@bflad bflad added enhancement waiting-response Issues or pull requests waiting for an external response and removed bug labels Feb 26, 2024
@bflad
Copy link
Member

bflad commented May 21, 2024

Closing due to lack of response. If someone would like the solution outlined above or wants to suggest another change without configuration validation considerations for this use case, please comment before this issue is automatically locked or create a new issue. Thanks.

@bflad bflad closed this as not planned Won't fix, can't repro, duplicate, stale May 21, 2024
@github-actions github-actions bot removed the waiting-response Issues or pull requests waiting for an external response label May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants