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

Waypoint application input vars #833

Merged
merged 33 commits into from
Jun 4, 2024
Merged

Conversation

paladin-devops
Copy link
Contributor

@paladin-devops paladin-devops commented May 6, 2024

🛠️ Description

Add support for input vars to Waypoint applications.

On the waypoint_application resource, there is a distinction for how an input variable was set. app_input_vars are configurable by the waypoint_application resource. template_input_vars are input variables that are set for an application, but were configured by the template, which was used for the creation of the app. The waypoint_application data source does not make this distinction, and all input variables are "filed under" the input_vars attribute of the data source.

Until the HCP Waypoint API is updated to return this information (if it ever does), the waypoint_application resource assumes that the type of an input variable in the TF configuration for the resource is the type, so long as the name of the variable matches. Arguably, this assumption should be made by the API. If/when this changes, the code to work around this should be removed, and the provider should defer to the type that the API says an input variable is.

Acceptance test output:

=== RUN   TestAccWaypoint_Application_DataSource_basic
--- PASS: TestAccWaypoint_Application_DataSource_basic (11.98s)
PASS

Process finished with the exit code 0

=== RUN   TestAccWaypoint_Application_DataSource_WithInputVars
--- PASS: TestAccWaypoint_Application_DataSource_WithInputVars (17.98s)
PASS

Process finished with the exit code 0

=== RUN   TestAccWaypoint_ApplicationInputVariables
--- PASS: TestAccWaypoint_ApplicationInputVariables (7.39s)
PASS

Process finished with the exit code 0

=== RUN   TestAccWaypoint_Application_basic
--- PASS: TestAccWaypoint_Application_basic (5.42s)
PASS

Process finished with the exit code 0

=== RUN   TestAccWaypoint_ApplicationInputVariables_OnTemplate
--- PASS: TestAccWaypoint_ApplicationInputVariables_OnTemplate (7.71s)
PASS

Fixes WAYP-2173.

@paladin-devops paladin-devops self-assigned this May 6, 2024
@paladin-devops paladin-devops marked this pull request as ready for review May 21, 2024 13:35
@paladin-devops paladin-devops requested review from a team as code owners May 21, 2024 13:35
@paladin-devops paladin-devops marked this pull request as draft May 23, 2024 20:17
* Add variable type to application input vars

* Set variable options on a template to be computed

* Enforce uniqueness in a template's variable options, if provided

* Add acceptance test for templates where a template is user editable, instead of using variable options
This commit updates Waypoint application creation to omit the `waypoint_application` input variable from the variables returned to the provider from HCP Waypoint. This variable is implicitly set by the service, and is never expected to be in the config. Since it's not in the config, but is returned by the API, if it is added to the state, Terraform errors since the config and state don't match, even though that is expected.

Another change in this PR is that the type of the input variable for an application is set in the state based on what is in the config, NOT the API response. This is because the API presently does not return the type of the variable. If and when that changes, this can be undone (as noted in the comment with this change).
Basic test case is updated to only test basic aspects of a template, not variables with options. That has been split into a new test case.
* Implement value converter interface for input vars, to use "As" for conversion from a TF value to a struct

* Refactor code to read input vars into the plan/state into readInputs function

* Set list of input vars to a null list if there are no inputs

* Update tests to use corrected variable options

* Add variable type to data source schema, though it won't be set yet
This test simulates a template which sets two variables and an application created using that template.
The list of input vars removed by this commit is only needed during creation of an app, not a read.
This commit splits up the input vars for an application that are set by a template and an app into two separate parts of the schema for the resource. The function readInputs is updated to make the distinction.
Update acc test to test that template vars are set for an application that uses a template which configured a value for an input var.
The variable type for the template-configured input variables of an application is optional. This will remain true unless the API changes to always return a variable's type.
The waypoint_application data source was updated to no longer use the `readInputs` method created to help with reading input vars for the waypoint_application resource. Instead it's handled in a loop in the data source's read function, because it doesn't split vars between app and tpl input vars. Tests were also updated accordingly here.
@paladin-devops paladin-devops marked this pull request as ready for review May 24, 2024 01:01
// One might see an opportunity here to use an embedded struct to avoid code duplication;
// however, this is not currently possible in the framework. See this issue for more details:
// https://github.com/hashicorp/terraform-plugin-framework/issues/242
TemplateInputVars types.Set `tfsdk:"template_input_vars"`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Depending upon how this issue is addressed, we may be able to simplify this struct to embed ApplicationDataSourceModel into this one.

Copy link
Member

@briancain briancain left a comment

Choose a reason for hiding this comment

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

Did a quick read through the code, and overall looks good! Makes sense to defer action stuff for now...

Copy link
Contributor

@manish-hashicorp manish-hashicorp left a comment

Choose a reason for hiding this comment

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

Rubber stamping

Copy link
Contributor

@HenryEstberg HenryEstberg left a comment

Choose a reason for hiding this comment

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

LGTM, with maybe one small nonblocking typo

}

// FromTerraform5Value implements the ValueConverter interface
func (vc *varConverter) FromTerraform5Value(val tftypes.Value) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (vc *varConverter) FromTerraform5Value(val tftypes.Value) error {
func (vc *varConverter) FromTerraformValue(val tftypes.Value) error {

Is this a typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FromTerraform5Value is correct, it implements the ValueConverter interface, allowing us to use As here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI @HenryEstberg I ultimately ended up removing this code after chatting with @catsby and pivoting to using ElementsAs to do the conversion, over implementing the conversion in the loop later in this file via As (which is what used this interface implementation).

Copy link
Contributor

@catsby catsby left a comment

Choose a reason for hiding this comment

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

I have some questions on the data model split and naming of things

@@ -80,6 +80,26 @@ func (d *DataSourceApplication) Schema(ctx context.Context, req datasource.Schem
Computed: true,
Description: "Internal Namespace ID.",
},
"app_input_vars": schema.SetNestedAttribute{
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we name them simply input_variables? The app prefix seems to stutter a bit (application.app_input..) and in the Template resource we use the full word variables and not just vars

return
}

resp.Diagnostics.Append(readInputs(ctx, inputVars, data, varTypes)...)
Copy link
Contributor

Choose a reason for hiding this comment

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

there is indirection here that I think is unnecessary. Is there a reason to embed the readInputs function call inside this Append? What about readInputs makes it particularly suited to return diagnostics as opposed to simply formatting and returning two []*InputVar and we do the SetValueFrom inline with this READ method, and do resp.Diagnostics.AddError there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Returning diagnostics (which is returned by the call to types.SetValueFrom within readInputs) helps reduce code duplication, because if we returned two []*InputVar we'd need to make the SetValueFrom calls wherever readInputs is called. It's not a large amount of duplicated code though, so I'm OK with moving the SetValueFrom to the calling function side to reduce the unnecessary indirection.

paladin-devops and others added 5 commits June 3, 2024 16:03
The set of input vars for the Waypoint application data source should be null if there are not any variables.

Co-authored-by: Clint <[email protected]>
Doing this is necessary for drift detection.

Co-authored-by: Clint <[email protected]>
@paladin-devops paladin-devops requested a review from a team as a code owner June 4, 2024 17:59
This is simpler than implementing the value converter interface.
Typo in new field name "input_variables" was fixed. The # of variables also has changed since `waypoint_application` is counted among the variables.
The # of variables has changed since `waypoint_application` is counted among the variables.
@paladin-devops paladin-devops merged commit 68a655f into main Jun 4, 2024
6 checks passed
@paladin-devops paladin-devops deleted the WAYP-2173-app-input-vars branch June 4, 2024 21:19
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.

6 participants