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

Allow using default params #1646

Closed

Conversation

divbhasin
Copy link
Contributor

@divbhasin divbhasin commented Jun 19, 2021

What does this change

Adds a new default value option to the porter parameters generate ... command survey if there is a default value defined for the chosen parameter in the bundle.

Screen Shot 2021-06-19 at 4 52 58 PM

To check, we can run porter parameters show ... and see that the default value has been set:

Screen Shot 2021-06-19 at 4 53 57 PM

What issue does it fix

Closes #1604

If there is not an existing issue, please make sure we have context on why this change is needed. See our Contributing Guide for examples of when an existing issue isn't necessary.

Notes for the reviewer

Environment variable is still the default option. Not sure if we should the "default value" option the new default for when the survey opens. Also, would appreciate tips on if there are better approaches to organize the code.

Checklist

  • Unit Tests
  • Documentation
  • Schema (porter.yaml)

@divbhasin divbhasin force-pushed the allow-using-default-params branch from 170abf4 to 1b93de1 Compare June 19, 2021 21:01
@divbhasin divbhasin marked this pull request as ready for review June 19, 2021 21:01
Copy link
Member

@carolynvs carolynvs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is going in the right direction! 👍

I want to call out that this is a slightly different set of functionality than what was outlined in #1604. The issue originally asked that when the user selects "use default value" that the parameter would not be included in the parameter set. By not specifying the value, when the parameter set is used, the default value of the bundle is used instead.

This will instead lock in the parameter set to the default value, and if the bundle changes the default value, continue to use the value set.

So you will need to change the implementation to not persist a parameter value when the user selects "use default" instead.

@@ -52,7 +52,9 @@ func (opts *GenerateParametersOptions) genParameterSet(fn generator) (parameters
if parameters.IsInternal(name, opts.Bundle) {
continue
}
c, err := fn(name, surveyParameters)
defaultVal, _ := getDefaultParamValue(opts.Bundle, name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this function returns an error, we should handle it here instead of silently ignoring it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carolynvs I don't know what would be the best way to handle this error. In most cases, it means there is no default value found, so in my opinion, we should ignore and move on so the user can select from any of the other options. We can perhaps a log a message saying this param does not have a default value according to the bundle. Returning the error here is currently also causing a test to fail.

options := []string{questionSecret, questionValue, questionEnvVar, questionPath, questionCommand}
questionDefault := fmt.Sprintf("use default value (%s)", defaultVal)

if defaultVal != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameters can default to an empty string, and can also be other types than string. One way to deal with that is to make defaultVal an interface{}, and check for null.

When we need to print the value, this is the logic:

var printedValue string
switch val := v.Value.(type) {
case string:
printedValue = val
default:
b, err := json.Marshal(v.Value)
if err != nil {
return "error rendering value"
}
printedValue = string(b)
}

Which handles when the parameter is a complex type such as an array or object.

return "", fmt.Errorf("parameter definition for %s is empty", name)
}

if def.Default != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can return the default value directly without any comparison or formatting, because we will render the value for display in the genSurvey function.

Signed-off-by: div_bhasin <[email protected]>
@divbhasin divbhasin force-pushed the allow-using-default-params branch from 32a8cef to cb97d13 Compare July 1, 2021 20:02
@divbhasin
Copy link
Contributor Author

@carolynvs Made the changes that you requested. The first screenshot remains but the second screenshot now changes to:

Screen Shot 2021-07-01 at 4 04 49 PM

If I am understanding correctly, we should leave the param value empty like above and it will just get filled in with the default at run time.

Signed-off-by: div_bhasin <[email protected]>
@divbhasin divbhasin requested a review from carolynvs July 1, 2021 20:13
@carolynvs
Copy link
Member

@divbhasin The expectation is that when I generate parameters and select a default, that parameter is not included in the parameter set at all. So in the second screenshot, you would not need the name parameter listed at all.

For example, this is what it generates now with your PR:

$ ./bin/porter parameters show pr1646 -o json
{
  "schemaVersion": "1.0.0-DRAFT+TODO",
  "name": "pr1646",
  "created": "2021-07-02T10:28:31.385435-05:00",
  "modified": "2021-07-02T10:28:31.385435-05:00",
  "parameters": [
    {
      "name": "name",
      "source": null
    }
  ]
}

and this is what we need:

$ ./bin/porter parameters show pr1646 -o json
{
  "schemaVersion": "1.0.0-DRAFT+TODO",
  "name": "pr1646",
  "created": "2021-07-02T10:28:31.385435-05:00",
  "modified": "2021-07-02T10:28:31.385435-05:00",
  "parameters": []
}

@carolynvs
Copy link
Member

@divbhasin Looks like there is a failing unit test related to your change TestGoodParametersName. Let me know if you need help fixing it!

@carolynvs carolynvs self-assigned this Feb 20, 2022
@kichristensen
Copy link
Contributor

A related PR have been merged, #3100, allowing skipping a value instead of showing the default value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants