Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit709db5b

Browse files
committed
fixup! cli: templatecreate: add CLI flags --max-ttl --min-autostart-interval
1 parent245795e commit709db5b

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

‎cli/create.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/coder/coder/cli/cliflag"
1212
"github.com/coder/coder/cli/cliui"
13+
"github.com/coder/coder/coderd/autobuild/schedule"
1314
"github.com/coder/coder/coderd/util/ptr"
1415
"github.com/coder/coder/codersdk"
1516
)
@@ -114,10 +115,22 @@ func create() *cobra.Command {
114115
}
115116
}
116117

117-
schedSpec:=buildSchedule(autostartMinute,autostartHour,autostartDow,tzName)
118+
schedSpec,err:=validSchedule(
119+
autostartMinute,
120+
autostartHour,
121+
autostartDow,
122+
tzName,
123+
time.Duration(template.MinAutostartIntervalMillis)*time.Millisecond,
124+
)
125+
iferr!=nil {
126+
returnxerrors.Errorf("Invalid autostart schedule: %w",err)
127+
}
118128
ifttl<time.Minute {
119129
returnxerrors.Errorf("TTL must be at least 1 minute")
120130
}
131+
ifttlMax:=time.Duration(template.MaxTTLMillis)*time.Millisecond;ttl>ttlMax {
132+
returnxerrors.Errorf("TTL must be below template maximum %s",ttlMax)
133+
}
121134

122135
templateVersion,err:=client.TemplateVersion(cmd.Context(),template.ActiveVersionID)
123136
iferr!=nil {
@@ -257,11 +270,17 @@ func create() *cobra.Command {
257270
returncmd
258271
}
259272

260-
funcbuildSchedule(minute,hour,dow,tzNamestring)*string {
261-
ifminute==""||hour==""||dow=="" {
262-
returnnil
273+
funcvalidSchedule(minute,hour,dow,tzNamestring,min time.Duration) (*string,error) {
274+
schedSpec:=fmt.Sprintf("CRON_TZ=%s %s %s * * %s",tzName,minute,hour,dow)
275+
276+
sched,err:=schedule.Weekly(schedSpec)
277+
iferr!=nil {
278+
returnnil,err
263279
}
264280

265-
schedSpec:=fmt.Sprintf("CRON_TZ=%s %s %s * * %s",tzName,minute,hour,dow)
266-
return&schedSpec
281+
ifschedMin:=sched.Min();schedMin<min {
282+
returnnil,xerrors.Errorf("minimum autostart interval %s is above template constraint %s",schedMin,min)
283+
}
284+
285+
return&schedSpec,nil
267286
}

‎cli/create_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestCreate(t *testing.T) {
8585
cmd.SetIn(pty.Input())
8686
cmd.SetOut(pty.Output())
8787
err:=cmd.Execute()
88-
assert.ErrorContains(t,err,"ttl_ms: ttl must be below template maximum 12h0m0s")
88+
assert.ErrorContains(t,err,"TTL must be below template maximum 12h0m0s")
8989
})
9090

9191
t.Run("BelowTemplateMinAutostartInterval",func(t*testing.T) {
@@ -111,7 +111,7 @@ func TestCreate(t *testing.T) {
111111
cmd.SetIn(pty.Input())
112112
cmd.SetOut(pty.Output())
113113
err:=cmd.Execute()
114-
assert.ErrorContains(t,err,"Minimum autostart interval 1m0sbelowtemplateminimum 1h0m0s")
114+
assert.ErrorContains(t,err,"minimum autostart interval 1m0sis abovetemplateconstraint 1h0m0s")
115115
})
116116

117117
t.Run("CreateErrInvalidTz",func(t*testing.T) {

‎coderd/templates.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,16 @@ func convertTemplates(templates []database.Template, workspaceCounts []database.
314314

315315
funcconvertTemplate(template database.Template,workspaceOwnerCountuint32) codersdk.Template {
316316
return codersdk.Template{
317-
ID:template.ID,
318-
CreatedAt:template.CreatedAt,
319-
UpdatedAt:template.UpdatedAt,
320-
OrganizationID:template.OrganizationID,
321-
Name:template.Name,
322-
Provisioner:codersdk.ProvisionerType(template.Provisioner),
323-
ActiveVersionID:template.ActiveVersionID,
324-
WorkspaceOwnerCount:workspaceOwnerCount,
325-
Description:template.Description,
317+
ID:template.ID,
318+
CreatedAt:template.CreatedAt,
319+
UpdatedAt:template.UpdatedAt,
320+
OrganizationID:template.OrganizationID,
321+
Name:template.Name,
322+
Provisioner:codersdk.ProvisionerType(template.Provisioner),
323+
ActiveVersionID:template.ActiveVersionID,
324+
WorkspaceOwnerCount:workspaceOwnerCount,
325+
Description:template.Description,
326+
MaxTTLMillis:time.Duration(template.MaxTtl).Milliseconds(),
327+
MinAutostartIntervalMillis:time.Duration(template.MinAutostartInterval).Milliseconds(),
326328
}
327329
}

‎codersdk/templates.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ import (
1313
// Template is the JSON representation of a Coder template. This type matches the
1414
// database object for now, but is abstracted for ease of change later on.
1515
typeTemplatestruct {
16-
ID uuid.UUID`json:"id"`
17-
CreatedAt time.Time`json:"created_at"`
18-
UpdatedAt time.Time`json:"updated_at"`
19-
OrganizationID uuid.UUID`json:"organization_id"`
20-
Namestring`json:"name"`
21-
ProvisionerProvisionerType`json:"provisioner"`
22-
ActiveVersionID uuid.UUID`json:"active_version_id"`
23-
WorkspaceOwnerCountuint32`json:"workspace_owner_count"`
24-
Descriptionstring`json:"description"`
16+
ID uuid.UUID`json:"id"`
17+
CreatedAt time.Time`json:"created_at"`
18+
UpdatedAt time.Time`json:"updated_at"`
19+
OrganizationID uuid.UUID`json:"organization_id"`
20+
Namestring`json:"name"`
21+
ProvisionerProvisionerType`json:"provisioner"`
22+
ActiveVersionID uuid.UUID`json:"active_version_id"`
23+
WorkspaceOwnerCountuint32`json:"workspace_owner_count"`
24+
Descriptionstring`json:"description"`
25+
MaxTTLMillisint64`json:"max_ttl_ms"`
26+
MinAutostartIntervalMillisint64`json:"min_autostart_interval_ms"`
2527
}
2628

2729
typeUpdateActiveTemplateVersionstruct {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp