mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: enable setting max port share level during template creation (#14366)
This commit is contained in:
Generated
+8
@@ -9220,6 +9220,14 @@ const docTemplate = `{
|
||||
"description": "Icon is a relative path or external URL that specifies\nan icon to be displayed in the dashboard.",
|
||||
"type": "string"
|
||||
},
|
||||
"max_port_share_level": {
|
||||
"description": "MaxPortShareLevel allows optionally specifying the maximum port share level\nfor workspaces created from the template.",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/codersdk.WorkspaceAgentPortShareLevel"
|
||||
}
|
||||
]
|
||||
},
|
||||
"name": {
|
||||
"description": "Name is the name of the template.",
|
||||
"type": "string"
|
||||
|
||||
Generated
+8
@@ -8209,6 +8209,14 @@
|
||||
"description": "Icon is a relative path or external URL that specifies\nan icon to be displayed in the dashboard.",
|
||||
"type": "string"
|
||||
},
|
||||
"max_port_share_level": {
|
||||
"description": "MaxPortShareLevel allows optionally specifying the maximum port share level\nfor workspaces created from the template.",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/codersdk.WorkspaceAgentPortShareLevel"
|
||||
}
|
||||
]
|
||||
},
|
||||
"name": {
|
||||
"description": "Name is the name of the template.",
|
||||
"type": "string"
|
||||
|
||||
+11
-1
@@ -163,6 +163,7 @@ func (api *API) notifyTemplateDeleted(ctx context.Context, template database.Tem
|
||||
func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
portSharer = *api.PortSharer.Load()
|
||||
createTemplate codersdk.CreateTemplateRequest
|
||||
organization = httpmw.OrganizationParam(r)
|
||||
apiKey = httpmw.APIKey(r)
|
||||
@@ -309,6 +310,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
|
||||
validErrs []codersdk.ValidationError
|
||||
autostopRequirementDaysOfWeekParsed uint8
|
||||
autostartRequirementDaysOfWeekParsed uint8
|
||||
maxPortShareLevel = database.AppSharingLevelOwner // default
|
||||
)
|
||||
if defaultTTL < 0 {
|
||||
validErrs = append(validErrs, codersdk.ValidationError{Field: "default_ttl_ms", Detail: "Must be a positive integer."})
|
||||
@@ -329,6 +331,14 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
|
||||
validErrs = append(validErrs, codersdk.ValidationError{Field: "autostart_requirement.days_of_week", Detail: err.Error()})
|
||||
}
|
||||
}
|
||||
if createTemplate.MaxPortShareLevel != nil {
|
||||
err = portSharer.ValidateTemplateMaxLevel(*createTemplate.MaxPortShareLevel)
|
||||
if err != nil {
|
||||
validErrs = append(validErrs, codersdk.ValidationError{Field: "max_port_share_level", Detail: err.Error()})
|
||||
} else {
|
||||
maxPortShareLevel = database.AppSharingLevel(*createTemplate.MaxPortShareLevel)
|
||||
}
|
||||
}
|
||||
|
||||
if autostopRequirementWeeks < 0 {
|
||||
validErrs = append(validErrs, codersdk.ValidationError{Field: "autostop_requirement.weeks", Detail: "Must be a positive integer."})
|
||||
@@ -386,7 +396,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
|
||||
DisplayName: createTemplate.DisplayName,
|
||||
Icon: createTemplate.Icon,
|
||||
AllowUserCancelWorkspaceJobs: allowUserCancelWorkspaceJobs,
|
||||
MaxPortSharingLevel: database.AppSharingLevelOwner,
|
||||
MaxPortSharingLevel: maxPortShareLevel,
|
||||
})
|
||||
if err != nil {
|
||||
return xerrors.Errorf("insert template: %s", err)
|
||||
|
||||
@@ -401,6 +401,44 @@ func TestPostTemplateByOrganization(t *testing.T) {
|
||||
require.EqualValues(t, 1, got.AutostopRequirement.Weeks)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("MaxPortShareLevel", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
client := coderdtest.New(t, nil)
|
||||
user := coderdtest.CreateFirstUser(t, client)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
|
||||
got, err := client.CreateTemplate(ctx, user.OrganizationID, codersdk.CreateTemplateRequest{
|
||||
Name: "testing",
|
||||
VersionID: version.ID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, codersdk.WorkspaceAgentPortShareLevelPublic, got.MaxPortShareLevel)
|
||||
})
|
||||
|
||||
t.Run("EnterpriseLevelError", func(t *testing.T) {
|
||||
client := coderdtest.New(t, nil)
|
||||
user := coderdtest.CreateFirstUser(t, client)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
|
||||
_, err := client.CreateTemplate(ctx, user.OrganizationID, codersdk.CreateTemplateRequest{
|
||||
Name: "testing",
|
||||
VersionID: version.ID,
|
||||
MaxPortShareLevel: ptr.Ref(codersdk.WorkspaceAgentPortShareLevelPublic),
|
||||
})
|
||||
var apiErr *codersdk.Error
|
||||
require.ErrorAs(t, err, &apiErr)
|
||||
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestTemplatesByOrganization(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user