Skip to content

feat: Expose managed variables via API #6134

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

Merged
merged 44 commits into from
Feb 15, 2023

Conversation

mtojek
Copy link
Member

@mtojek mtojek commented Feb 9, 2023

Related: #5980

This PR introduces the concept of template version variables, that are pulled from an imported template, and stored in the database table. If the imported variable is marked as required, a user is expected to pass the value, otherwise, the import job fails fast.

To use managed template variables, the user is required to define an extra flag in the template:

provider "coder" {
  feature_use_managed_variables = "true"
}

  • Create database model for "template_version_variables" (name, type, description, sensitive, required).
  • Import variables from template. Introduce flag:feature_use_managed_variables
  • Expose variables behind the endpoint /variables
  • Pass required variables to import the template
  • Use variables from db to build a workspace (check workspacebuild, workspace, auto-start)
  • Write unit tests
  • Review FIXMEs

@mtojek mtojek self-assigned this Feb 9, 2023
@mtojek mtojek changed the title 5980 manage temp variables feat: Add support for managed admin variables Feb 9, 2023
@mtojek mtojek changed the title feat: Add support for managed admin variables feat: Store and expose TF variables via API Feb 9, 2023
@mtojek mtojek changed the title feat: Store and expose TF variables via API feat: Expose managed variables via API Feb 9, 2023
@mtojek mtojek marked this pull request as ready for review February 14, 2023 13:29
@mtojek mtojek requested review from kylecarbs and mafredri February 14, 2023 13:29
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Looking great! I didn't look too closely at the actual TF parsing as it's a bit unfamiliar to me.

template_version_id uuid not null references template_versions (id) on delete cascade,
name text not null,
description text not null,
type text not null,
Copy link
Member

Choose a reason for hiding this comment

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

I saw this was listed as an enum in the API, should we turn it into an enum in the DB as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

To be honest, we don't intend to run any conditional logic based on the type, so I'm not convinced. Terraform template ensures the type validation here, so only TF var types are welcome. Also, we don't do this validation on rich parameters, so I would leave it consistent with them.

type TemplateVersionVariable struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type" enums:"string,number,bool"`
Copy link
Member

Choose a reason for hiding this comment

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

I didn't see that these enum values are enforced anywhere, should they be? (I posted a suggestion in the migration to use database enum.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Replied above

if err != nil {
return nil, xerrors.Errorf("parse variable %q default: %w", variable.Name, err)
}
defaultData = string(defaultDataRaw)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure what variable.Default looks like, so wondering if the idea with json marshaling here is to turn a string value like hello into "hello"? Or maybe the purpose is entirely different 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

According to Terraform, the variable can have simple types like strings... but also lists, arrays, etc. hence marshaling. Anyway, the idea is not new but borrowed from convertVariableToParameter.

@@ -175,7 +192,8 @@ message Parse {
string directory = 1;
}
message Complete {
repeated ParameterSchema parameter_schemas = 2;
repeated ParameterSchema parameter_schemas = 1;
Copy link
Member

Choose a reason for hiding this comment

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

Is it safe/backwards compatible to change number here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, let's reorder these just in case. BTW for some reason, parameter_schemas was the 2nd item, but no 1st field?

@mtojek mtojek requested a review from mafredri February 15, 2023 10:31
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -175,6 +192,7 @@ message Parse {
string directory = 1;
}
message Complete {
repeated TemplateVariable template_variables = 1;
repeated ParameterSchema parameter_schemas = 2;
Copy link
Member

Choose a reason for hiding this comment

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

@kylecarbs is this safe or should we leave 1 empty and add template_variables as 3?

Copy link
Member

Choose a reason for hiding this comment

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

I think it should be fine.

Customers aren't running provisioner daemons externally at the moment, so this incompatibility should be alright.

@mtojek mtojek merged commit 3b7b96a into coder:main Feb 15, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants