- Notifications
You must be signed in to change notification settings - Fork22
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Changes fromall commits
5418ed7
af25037
56d1ab7
c8c5101
4e37a00
06cf760
9f26791
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
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.
| ||
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 forcoderworkspaces. 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 | ||
| ||
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) | ||
} | ||
rd.SetId(preset.Name) | ||
return nil | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "The presetIDis automatically generated and may change between runs. It is recommended to usethe`name` attribute to identify the preset.", | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "The name of the workspace preset.", | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
"parameters": { | ||
Type: schema.TypeMap, | ||
Description: "Workspace parameters that will be set bythe 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), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.