-
Notifications
You must be signed in to change notification settings - Fork 22
feat: allow presets to define prebuilds #373
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
Changes from all commits
5418ed7
af25037
56d1ab7
c8c5101
4e37a00
06cf760
9f26791
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,14 @@ func workspaceDataSource() *schema.Resource { | |
} | ||
_ = rd.Set("start_count", count) | ||
|
||
prebuild := helpers.OptionalEnv(IsPrebuildEnvironmentVariable()) | ||
prebuildCount := 0 | ||
if prebuild == "true" { | ||
prebuildCount = 1 | ||
_ = rd.Set("is_prebuild", true) | ||
} | ||
_ = rd.Set("prebuild_count", prebuildCount) | ||
|
||
name := helpers.OptionalEnvOrDefault("CODER_WORKSPACE_NAME", "default") | ||
rd.Set("name", name) | ||
|
||
|
@@ -83,6 +91,11 @@ func workspaceDataSource() *schema.Resource { | |
Computed: true, | ||
Description: "The access port of the Coder deployment provisioning this workspace.", | ||
}, | ||
"prebuild_count": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be duplication, yes. We could probably get rid of "is_prebuild" and just check the count. Looking at the rest of the code, we are following the pattern that was set by the "transition" and "start_count" parameters. They have the same relationship. I'm not sure whether to remove "is_prebuild" or keep it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like neither
Also looks like there is some duplication here as well:
But I don't know why it's needed. We can ask original author of this approach with |
||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "A computed count, equal to 1 if the workspace is a currently unassigned prebuild. Use this to conditionally act on the status of a prebuild. Actions that do not require user identity can be taken when this value is set to 1. Actions that should only be taken once the workspace has been assigned to a user may be taken when this value is set to 0.", | ||
}, | ||
"start_count": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
|
@@ -98,6 +111,11 @@ func workspaceDataSource() *schema.Resource { | |
Computed: true, | ||
Description: "UUID of the workspace.", | ||
}, | ||
"is_prebuild": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "Similar to `prebuild_count`, but a boolean value instead of a count. This is set to true if the workspace is a currently unassigned prebuild. Once the workspace is assigned, this value will be false.", | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
|
@@ -121,3 +139,7 @@ func workspaceDataSource() *schema.Resource { | |
}, | ||
} | ||
} | ||
|
||
func IsPrebuildEnvironmentVariable() string { | ||
return "CODER_WORKSPACE_IS_PREBUILD" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it can be const instead of func, but up to you |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,59 +12,82 @@ import ( | |
type WorkspacePreset struct { | ||
Name string `mapstructure:"name"` | ||
Parameters map[string]string `mapstructure:"parameters"` | ||
Prebuilds WorkspacePrebuild `mapstructure:"prebuilds"` | ||
} | ||
|
||
type WorkspacePrebuild struct { | ||
Instances int `mapstructure:"instances"` | ||
} | ||
|
||
func workspacePresetDataSource() *schema.Resource { | ||
return &schema.Resource{ | ||
SchemaVersion: 1, | ||
|
||
Description: "Use this data source to predefine common configurations for workspaces.", | ||
Description: "Use this data source to predefine common configurations for coder workspaces. Users will have the option to select a defined preset, which will automatically apply the selected configuration. Any parameters defined in the preset will be applied to the workspace. Parameters that are not defined by the preset will still be configurable when creating a workspace.", | ||
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
var preset WorkspacePreset | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SasSwart btw: I just noticed code in the But still what is the purpose of this code?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the decoding is defunct, yes. I've removed it. |
||
err := mapstructure.Decode(struct { | ||
Name interface{} | ||
Parameters interface{} | ||
Prebuilds struct { | ||
Instances interface{} | ||
} | ||
}{ | ||
Name: rd.Get("name"), | ||
Parameters: rd.Get("parameters"), | ||
Prebuilds: struct { | ||
Instances interface{} | ||
}{ | ||
Instances: rd.Get("prebuilds.0.instances"), | ||
}, | ||
}, &preset) | ||
if err != nil { | ||
return diag.Errorf("decode workspace preset: %s", err) | ||
} | ||
|
||
// MinItems doesn't work with maps, so we need to check the length | ||
// of the map manually. All other validation is handled by the | ||
// schema. | ||
if len(preset.Parameters) == 0 { | ||
return diag.Errorf("expected \"parameters\" to not be an empty map") | ||
} | ||
|
||
rd.SetId(preset.Name) | ||
|
||
return nil | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "ID of the workspace preset.", | ||
Description: "The preset ID is automatically generated and may change between runs. It is recommended to use the `name` attribute to identify the preset.", | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "Name of the workspace preset.", | ||
Description: "The name of the workspace preset.", | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
"parameters": { | ||
Type: schema.TypeMap, | ||
Description: "Parameters of the workspace preset.", | ||
Required: true, | ||
Description: "Workspace parameters that will be set by the workspace preset. For simple templates that only need prebuilds, you may define a preset with zero parameters. Because workspace parameters may change between Coder template versions, preset parameters are allowed to define values for parameters that do not exist in the current template version.", | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
}, | ||
"prebuilds": { | ||
Type: schema.TypeSet, | ||
Description: "Prebuilt workspace configuration related to this workspace preset. Coder will build and maintain workspaces in reserve based on this configuration. When a user creates a new workspace using a preset, they will be assigned a prebuilt workspace, instead of waiting for a new workspace to build.", | ||
Optional: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"instances": { | ||
Type: schema.TypeInt, | ||
Description: "The number of workspaces to keep in reserve for this preset.", | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.IntAtLeast(0), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd rather write it like this:
I think it's more logical, but it's not a big deal