@@ -3,6 +3,7 @@ package cli
3
3
import (
4
4
"fmt"
5
5
"net/http"
6
+ "strings"
6
7
"time"
7
8
8
9
"golang.org/x/xerrors"
@@ -20,6 +21,8 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
20
21
icon string
21
22
defaultTTL time.Duration
22
23
maxTTL time.Duration
24
+ restartRequirementDaysOfWeek []string
25
+ restartRequirementWeeks int64
23
26
failureTTL time.Duration
24
27
inactivityTTL time.Duration
25
28
allowUserCancelWorkspaceJobs bool
@@ -48,7 +51,15 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
48
51
}
49
52
}
50
53
51
- if maxTTL != 0 || ! allowUserAutostart || ! allowUserAutostop || failureTTL != 0 || inactivityTTL != 0 {
54
+ unsetRestartRequirementDaysOfWeek := len (restartRequirementDaysOfWeek ) == 1 && restartRequirementDaysOfWeek [0 ] == "none"
55
+ requiresEntitlement := (len (restartRequirementDaysOfWeek ) > 0 && ! unsetRestartRequirementDaysOfWeek ) ||
56
+ restartRequirementWeeks > 0 ||
57
+ ! allowUserAutostart ||
58
+ ! allowUserAutostop ||
59
+ maxTTL != 0 ||
60
+ failureTTL != 0 ||
61
+ inactivityTTL != 0
62
+ if requiresEntitlement {
52
63
entitlements , err := client .Entitlements (inv .Context ())
53
64
var sdkErr * codersdk.Error
54
65
if xerrors .As (err , & sdkErr ) && sdkErr .StatusCode () == http .StatusNotFound {
@@ -71,14 +82,27 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
71
82
return xerrors .Errorf ("get workspace template: %w" , err )
72
83
}
73
84
85
+ // Copy the default value if the list is empty, or if the user
86
+ // specified the "none" value clear the list.
87
+ if len (restartRequirementDaysOfWeek ) == 0 {
88
+ restartRequirementDaysOfWeek = template .RestartRequirement .DaysOfWeek
89
+ }
90
+ if unsetRestartRequirementDaysOfWeek {
91
+ restartRequirementDaysOfWeek = []string {}
92
+ }
93
+
74
94
// NOTE: coderd will ignore empty fields.
75
95
req := codersdk.UpdateTemplateMeta {
76
- Name : name ,
77
- DisplayName : displayName ,
78
- Description : description ,
79
- Icon : icon ,
80
- DefaultTTLMillis : defaultTTL .Milliseconds (),
81
- MaxTTLMillis : maxTTL .Milliseconds (),
96
+ Name : name ,
97
+ DisplayName : displayName ,
98
+ Description : description ,
99
+ Icon : icon ,
100
+ DefaultTTLMillis : defaultTTL .Milliseconds (),
101
+ MaxTTLMillis : maxTTL .Milliseconds (),
102
+ RestartRequirement : & codersdk.TemplateRestartRequirement {
103
+ DaysOfWeek : restartRequirementDaysOfWeek ,
104
+ Weeks : restartRequirementWeeks ,
105
+ },
82
106
FailureTTLMillis : failureTTL .Milliseconds (),
83
107
InactivityTTLMillis : inactivityTTL .Milliseconds (),
84
108
AllowUserCancelWorkspaceJobs : allowUserCancelWorkspaceJobs ,
@@ -126,6 +150,30 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
126
150
Description : "Edit the template maximum time before shutdown - workspaces created from this template must shutdown within the given duration after starting. This is an enterprise-only feature." ,
127
151
Value : clibase .DurationOf (& maxTTL ),
128
152
},
153
+ {
154
+ Flag : "restart-requirement-weekdays" ,
155
+ Description : "Edit the template restart requirement weekdays - workspaces created from this template must be restarted on the given weekdays. To unset this value for the template (and disable the restart requirement for the template), pass 'none'." ,
156
+ // TODO(@dean): unhide when we delete max_ttl
157
+ Hidden : true ,
158
+ Value : clibase .Validate (clibase .StringArrayOf (& restartRequirementDaysOfWeek ), func (value * clibase.StringArray ) error {
159
+ v := value .GetSlice ()
160
+ if len (v ) == 1 && v [0 ] == "none" {
161
+ return nil
162
+ }
163
+ _ , err := codersdk .WeekdaysToBitmap (v )
164
+ if err != nil {
165
+ return xerrors .Errorf ("invalid restart requirement days of week %q: %w" , strings .Join (v , "," ), err )
166
+ }
167
+ return nil
168
+ }),
169
+ },
170
+ {
171
+ Flag : "restart-requirement-weeks" ,
172
+ Description : "Edit the template restart requirement weeks - workspaces created from this template must be restarted on an n-weekly basis." ,
173
+ // TODO(@dean): unhide when we delete max_ttl
174
+ Hidden : true ,
175
+ Value : clibase .Int64Of (& restartRequirementWeeks ),
176
+ },
129
177
{
130
178
Flag : "failure-ttl" ,
131
179
Description : "Specify a failure TTL for workspaces created from this template. This licensed feature's default is 0h (off)." ,
0 commit comments