feat!: drop support for legacy parameters (#7663)

This commit is contained in:
Marcin Tojek
2023-06-02 11:16:46 +02:00
committed by GitHub
parent 2b63492649
commit a7366a8b76
106 changed files with 1153 additions and 8553 deletions

View File

@ -51,10 +51,6 @@ type CreateTemplateVersionRequest struct {
Provisioner ProvisionerType `json:"provisioner" validate:"oneof=terraform echo,required"`
ProvisionerTags map[string]string `json:"tags"`
// ParameterValues allows for additional parameters to be provided
// during the dry-run provision stage.
ParameterValues []CreateParameterRequest `json:"parameter_values,omitempty"`
UserVariableValues []VariableValue `json:"user_variable_values,omitempty"`
}
@ -82,8 +78,7 @@ type CreateTemplateRequest struct {
// This is required on creation to enable a user-flow of validating a
// template works. There is no reason the data-model cannot support empty
// templates, but it doesn't make sense for users.
VersionID uuid.UUID `json:"template_version_id" validate:"required" format:"uuid"`
ParameterValues []CreateParameterRequest `json:"parameter_values,omitempty"`
VersionID uuid.UUID `json:"template_version_id" validate:"required" format:"uuid"`
// DefaultTTLMillis allows optionally specifying the default TTL
// for all workspaces created from this template.
@ -123,7 +118,6 @@ type CreateWorkspaceRequest struct {
TTLMillis *int64 `json:"ttl_ms,omitempty"`
// ParameterValues allows for additional parameters to be provided
// during the initial provision.
ParameterValues []CreateParameterRequest `json:"parameter_values,omitempty"`
RichParameterValues []WorkspaceBuildParameter `json:"rich_parameter_values,omitempty"`
}

View File

@ -1,148 +0,0 @@
package codersdk
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/google/uuid"
)
type ParameterScope string
const (
ParameterTemplate ParameterScope = "template"
ParameterWorkspace ParameterScope = "workspace"
ParameterImportJob ParameterScope = "import_job"
)
type ParameterSourceScheme string
const (
ParameterSourceSchemeNone ParameterSourceScheme = "none"
ParameterSourceSchemeData ParameterSourceScheme = "data"
)
type ParameterDestinationScheme string
const (
ParameterDestinationSchemeNone ParameterDestinationScheme = "none"
ParameterDestinationSchemeEnvironmentVariable ParameterDestinationScheme = "environment_variable"
ParameterDestinationSchemeProvisionerVariable ParameterDestinationScheme = "provisioner_variable"
)
type ParameterTypeSystem string
const (
ParameterTypeSystemNone ParameterTypeSystem = "none"
ParameterTypeSystemHCL ParameterTypeSystem = "hcl"
)
type ComputedParameter struct {
Parameter
SchemaID uuid.UUID `json:"schema_id" format:"uuid"`
DefaultSourceValue bool `json:"default_source_value"`
}
// Parameter represents a set value for the scope.
//
// @Description Parameter represents a set value for the scope.
type Parameter struct {
ID uuid.UUID `json:"id" table:"id" format:"uuid"`
Scope ParameterScope `json:"scope" table:"scope" enums:"template,workspace,import_job"`
ScopeID uuid.UUID `json:"scope_id" table:"scope id" format:"uuid"`
Name string `json:"name" table:"name,default_sort"`
SourceScheme ParameterSourceScheme `json:"source_scheme" table:"source scheme" validate:"ne=none" enums:"none,data"`
DestinationScheme ParameterDestinationScheme `json:"destination_scheme" table:"destination scheme" validate:"ne=none" enums:"none,environment_variable,provisioner_variable"`
CreatedAt time.Time `json:"created_at" table:"created at" format:"date-time"`
UpdatedAt time.Time `json:"updated_at" table:"updated at" format:"date-time"`
SourceValue string `json:"source_value"`
}
type ParameterSchema struct {
ID uuid.UUID `json:"id" format:"uuid"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
JobID uuid.UUID `json:"job_id" format:"uuid"`
Name string `json:"name"`
Description string `json:"description"`
DefaultSourceScheme ParameterSourceScheme `json:"default_source_scheme" enums:"none,data"`
DefaultSourceValue string `json:"default_source_value"`
AllowOverrideSource bool `json:"allow_override_source"`
DefaultDestinationScheme ParameterDestinationScheme `json:"default_destination_scheme" enums:"none,environment_variable,provisioner_variable"`
AllowOverrideDestination bool `json:"allow_override_destination"`
DefaultRefresh string `json:"default_refresh"`
RedisplayValue bool `json:"redisplay_value"`
ValidationError string `json:"validation_error"`
ValidationCondition string `json:"validation_condition"`
ValidationTypeSystem string `json:"validation_type_system"`
ValidationValueType string `json:"validation_value_type"`
// This is a special array of items provided if the validation condition
// explicitly states the value must be one of a set.
ValidationContains []string `json:"validation_contains,omitempty"`
}
// CreateParameterRequest is a structure used to create a new parameter value for a scope.
//
// @Description CreateParameterRequest is a structure used to create a new parameter value for a scope.
type CreateParameterRequest struct {
// CloneID allows copying the value of another parameter.
// The other param must be related to the same template_id for this to
// succeed.
// No other fields are required if using this, as all fields will be copied
// from the other parameter.
CloneID uuid.UUID `json:"copy_from_parameter,omitempty" validate:"" format:"uuid"`
Name string `json:"name" validate:"required"`
SourceValue string `json:"source_value" validate:"required"`
SourceScheme ParameterSourceScheme `json:"source_scheme" validate:"oneof=data,required" enums:"none,data"`
DestinationScheme ParameterDestinationScheme `json:"destination_scheme" validate:"oneof=environment_variable provisioner_variable,required" enums:"none,environment_variable,provisioner_variable"`
}
func (c *Client) CreateParameter(ctx context.Context, scope ParameterScope, id uuid.UUID, req CreateParameterRequest) (Parameter, error) {
res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/parameters/%s/%s", scope, id.String()), req)
if err != nil {
return Parameter{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
return Parameter{}, ReadBodyAsError(res)
}
var param Parameter
return param, json.NewDecoder(res.Body).Decode(&param)
}
func (c *Client) DeleteParameter(ctx context.Context, scope ParameterScope, id uuid.UUID, name string) error {
res, err := c.Request(ctx, http.MethodDelete, fmt.Sprintf("/api/v2/parameters/%s/%s/%s", scope, id.String(), name), nil)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return ReadBodyAsError(res)
}
_, _ = io.Copy(io.Discard, res.Body)
return nil
}
func (c *Client) Parameters(ctx context.Context, scope ParameterScope, id uuid.UUID) ([]Parameter, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/parameters/%s/%s", scope, id.String()), nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, ReadBodyAsError(res)
}
var parameters []Parameter
return parameters, json.NewDecoder(res.Body).Decode(&parameters)
}

View File

@ -131,8 +131,7 @@ func validationEnabled(param TemplateVersionParameter) bool {
// correctly validates.
// @typescript-ignore ParameterResolver
type ParameterResolver struct {
Legacy []Parameter
Rich []WorkspaceBuildParameter
Rich []WorkspaceBuildParameter
}
// ValidateResolve checks the provided value, v, against the parameter, p, and the previous build. If v is nil, it also
@ -173,16 +172,5 @@ func (r *ParameterResolver) findLastValue(p TemplateVersionParameter) *Workspace
return &rp
}
}
// For migration purposes, we also support using a legacy variable
if p.LegacyVariableName != "" {
for _, lp := range r.Legacy {
if lp.Name == p.LegacyVariableName {
return &WorkspaceBuildParameter{
Name: p.Name,
Value: lp.SourceValue,
}
}
}
}
return nil
}

View File

@ -137,45 +137,6 @@ func TestParameterResolver_ValidateResolve_Immutable(t *testing.T) {
require.Equal(t, "", v)
}
func TestParameterResolver_ValidateResolve_Legacy(t *testing.T) {
t.Parallel()
uut := codersdk.ParameterResolver{
Legacy: []codersdk.Parameter{
{Name: "l", SourceValue: "5"},
{Name: "n", SourceValue: "6"},
},
}
p := codersdk.TemplateVersionParameter{
Name: "n",
Type: "number",
Required: true,
LegacyVariableName: "l",
}
v, err := uut.ValidateResolve(p, nil)
require.NoError(t, err)
require.Equal(t, "5", v)
}
func TestParameterResolver_ValidateResolve_PreferRichOverLegacy(t *testing.T) {
t.Parallel()
uut := codersdk.ParameterResolver{
Rich: []codersdk.WorkspaceBuildParameter{{Name: "n", Value: "7"}},
Legacy: []codersdk.Parameter{
{Name: "l", SourceValue: "5"},
{Name: "n", SourceValue: "6"},
},
}
p := codersdk.TemplateVersionParameter{
Name: "n",
Type: "number",
Required: true,
LegacyVariableName: "l",
}
v, err := uut.ValidateResolve(p, nil)
require.NoError(t, err)
require.Equal(t, "7", v)
}
func TestRichParameterValidation(t *testing.T) {
t.Parallel()

View File

@ -14,7 +14,7 @@ import (
type TemplateVersionWarning string
const (
TemplateVersionWarningDeprecatedParameters TemplateVersionWarning = "DEPRECATED_PARAMETERS"
TemplateVersionWarningUnsupportedWorkspaces TemplateVersionWarning = "UNSUPPORTED_WORKSPACES"
)
// TemplateVersion represents a single version of a template.
@ -144,34 +144,6 @@ func (c *Client) TemplateVersionGitAuth(ctx context.Context, version uuid.UUID)
return gitAuth, json.NewDecoder(res.Body).Decode(&gitAuth)
}
// TemplateVersionSchema returns schemas for a template version by ID.
func (c *Client) TemplateVersionSchema(ctx context.Context, version uuid.UUID) ([]ParameterSchema, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/templateversions/%s/schema", version), nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, ReadBodyAsError(res)
}
var params []ParameterSchema
return params, json.NewDecoder(res.Body).Decode(&params)
}
// TemplateVersionParameters returns computed parameters for a template version.
func (c *Client) TemplateVersionParameters(ctx context.Context, version uuid.UUID) ([]ComputedParameter, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/templateversions/%s/parameters", version), nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, ReadBodyAsError(res)
}
var params []ComputedParameter
return params, json.NewDecoder(res.Body).Decode(&params)
}
// TemplateVersionResources returns resources a template version declares.
func (c *Client) TemplateVersionResources(ctx context.Context, version uuid.UUID) ([]WorkspaceResource, error) {
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/templateversions/%s/resources", version), nil)
@ -209,7 +181,6 @@ func (c *Client) TemplateVersionLogsAfter(ctx context.Context, version uuid.UUID
// CreateTemplateVersionDryRun.
type CreateTemplateVersionDryRunRequest struct {
WorkspaceName string `json:"workspace_name"`
ParameterValues []CreateParameterRequest `json:"parameter_values"`
RichParameterValues []WorkspaceBuildParameter `json:"rich_parameter_values"`
UserVariableValues []VariableValue `json:"user_variable_values,omitempty"`
}

View File

@ -67,7 +67,6 @@ type CreateWorkspaceBuildRequest struct {
// ParameterValues are optional. It will write params to the 'workspace' scope.
// This will overwrite any existing parameters with the same name.
// This will not delete old params not included in this list.
ParameterValues []CreateParameterRequest `json:"parameter_values,omitempty"`
RichParameterValues []WorkspaceBuildParameter `json:"rich_parameter_values,omitempty"`
// Log level changes the default logging verbosity of a provider ("info" if empty).