2022-06-08 15:14:57 +01:00
package cli
import (
"fmt"
2023-03-08 01:14:58 +11:00
"net/http"
2023-07-20 06:35:41 -07:00
"strings"
2022-06-08 15:14:57 +01:00
"time"
"golang.org/x/xerrors"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
2022-06-08 15:14:57 +01:00
)
2023-03-23 17:42:20 -05:00
func ( r * RootCmd ) templateEdit () * clibase . Cmd {
2022-06-08 15:14:57 +01:00
var (
2023-08-29 11:35:05 -07:00
name string
displayName string
description string
icon string
defaultTTL time . Duration
maxTTL time . Duration
autostopRequirementDaysOfWeek [] string
autostopRequirementWeeks int64
failureTTL time . Duration
inactivityTTL time . Duration
allowUserCancelWorkspaceJobs bool
allowUserAutostart bool
allowUserAutostop bool
2022-06-08 15:14:57 +01:00
)
2023-03-23 17:42:20 -05:00
client := new ( codersdk . Client )
2022-06-08 15:14:57 +01:00
2023-03-23 17:42:20 -05:00
cmd := & clibase . Cmd {
Use : "edit <template>" ,
Middleware : clibase . Chain (
clibase . RequireNArgs ( 1 ),
r . InitClient ( client ),
),
2022-06-08 15:14:57 +01:00
Short : "Edit the metadata of a template by name." ,
2023-03-23 17:42:20 -05:00
Handler : func ( inv * clibase . Invocation ) error {
2023-05-10 12:57:11 -07:00
// This clause can be removed when workspace_actions is no longer experimental
if failureTTL != 0 || inactivityTTL != 0 {
experiments , exErr := client . Experiments ( inv . Context ())
if exErr != nil {
return xerrors . Errorf ( "get experiments: %w" , exErr )
}
if ! experiments . Enabled ( codersdk . ExperimentWorkspaceActions ) {
return xerrors . Errorf ( "--failure-ttl and --inactivityTTL are experimental features. Use the workspace_actions CODER_EXPERIMENTS flag to set these configuration values." )
}
}
2023-08-29 11:35:05 -07:00
unsetAutostopRequirementDaysOfWeek := len ( autostopRequirementDaysOfWeek ) == 1 && autostopRequirementDaysOfWeek [ 0 ] == "none"
requiresEntitlement := ( len ( autostopRequirementDaysOfWeek ) > 0 && ! unsetAutostopRequirementDaysOfWeek ) ||
autostopRequirementWeeks > 0 ||
2023-07-20 06:35:41 -07:00
! allowUserAutostart ||
! allowUserAutostop ||
maxTTL != 0 ||
failureTTL != 0 ||
inactivityTTL != 0
if requiresEntitlement {
2023-03-23 17:42:20 -05:00
entitlements , err := client . Entitlements ( inv . Context ())
2023-03-08 01:14:58 +11:00
var sdkErr * codersdk . Error
if xerrors . As ( err , & sdkErr ) && sdkErr . StatusCode () == http . StatusNotFound {
2023-05-10 12:57:11 -07:00
return xerrors . Errorf ( "your deployment appears to be an AGPL deployment, so you cannot set --max-ttl, --failure-ttl, --inactivityTTL, --allow-user-autostart=false or --allow-user-autostop=false" )
2023-03-08 01:14:58 +11:00
} else if err != nil {
return xerrors . Errorf ( "get entitlements: %w" , err )
}
if ! entitlements . Features [ codersdk . FeatureAdvancedTemplateScheduling ]. Enabled {
2023-05-10 12:57:11 -07:00
return xerrors . Errorf ( "your license is not entitled to use advanced template scheduling, so you cannot set --max-ttl, --failure-ttl, --inactivityTTL, --allow-user-autostart=false or --allow-user-autostop=false" )
2023-03-08 01:14:58 +11:00
}
}
2023-03-23 17:42:20 -05:00
organization , err := CurrentOrganization ( inv , client )
2022-06-08 15:14:57 +01:00
if err != nil {
return xerrors . Errorf ( "get current organization: %w" , err )
}
2023-03-23 17:42:20 -05:00
template , err := client . TemplateByName ( inv . Context (), organization . ID , inv . Args [ 0 ])
2022-06-08 15:14:57 +01:00
if err != nil {
return xerrors . Errorf ( "get workspace template: %w" , err )
}
2023-07-20 06:35:41 -07:00
// Copy the default value if the list is empty, or if the user
// specified the "none" value clear the list.
2023-08-29 11:35:05 -07:00
if len ( autostopRequirementDaysOfWeek ) == 0 {
autostopRequirementDaysOfWeek = template . AutostopRequirement . DaysOfWeek
2023-07-20 06:35:41 -07:00
}
2023-08-29 11:35:05 -07:00
if unsetAutostopRequirementDaysOfWeek {
autostopRequirementDaysOfWeek = [] string {}
2023-07-20 06:35:41 -07:00
}
2022-06-08 15:14:57 +01:00
// NOTE: coderd will ignore empty fields.
req := codersdk . UpdateTemplateMeta {
2023-07-20 06:35:41 -07:00
Name : name ,
DisplayName : displayName ,
Description : description ,
Icon : icon ,
DefaultTTLMillis : defaultTTL . Milliseconds (),
MaxTTLMillis : maxTTL . Milliseconds (),
2023-08-29 11:35:05 -07:00
AutostopRequirement : & codersdk . TemplateAutostopRequirement {
DaysOfWeek : autostopRequirementDaysOfWeek ,
Weeks : autostopRequirementWeeks ,
2023-07-20 06:35:41 -07:00
},
2023-05-10 12:57:11 -07:00
FailureTTLMillis : failureTTL . Milliseconds (),
2023-08-24 13:25:54 -05:00
TimeTilDormantMillis : inactivityTTL . Milliseconds (),
2022-11-21 11:43:53 +01:00
AllowUserCancelWorkspaceJobs : allowUserCancelWorkspaceJobs ,
2023-04-04 22:48:35 +10:00
AllowUserAutostart : allowUserAutostart ,
AllowUserAutostop : allowUserAutostop ,
2022-06-08 15:14:57 +01:00
}
2023-03-23 17:42:20 -05:00
_ , err = client . UpdateTemplateMeta ( inv . Context (), template . ID , req )
2022-06-08 15:14:57 +01:00
if err != nil {
return xerrors . Errorf ( "update template metadata: %w" , err )
}
2023-06-07 00:22:58 -05:00
_ , _ = fmt . Fprintf ( inv . Stdout , "Updated template metadata at %s!\n" , cliui . DefaultStyles . DateTimeStamp . Render ( time . Now (). Format ( time . Stamp )))
2022-06-08 15:14:57 +01:00
return nil
},
}
2023-03-23 17:42:20 -05:00
cmd . Options = clibase . OptionSet {
{
Flag : "name" ,
Description : "Edit the template name." ,
Value : clibase . StringOf ( & name ),
},
{
Flag : "display-name" ,
Description : "Edit the template display name." ,
Value : clibase . StringOf ( & displayName ),
},
{
Flag : "description" ,
Description : "Edit the template description." ,
Value : clibase . StringOf ( & description ),
},
{
Flag : "icon" ,
Description : "Edit the template icon path." ,
Value : clibase . StringOf ( & icon ),
},
{
Flag : "default-ttl" ,
2023-08-25 13:39:12 -04:00
Description : "Edit the template default time before shutdown - workspaces created from this template default to this value. Maps to \"Default autostop\" in the UI." ,
2023-03-23 17:42:20 -05:00
Value : clibase . DurationOf ( & defaultTTL ),
},
{
Flag : "max-ttl" ,
2023-08-25 13:39:12 -04:00
Description : "Edit the template maximum time before shutdown - workspaces created from this template must shutdown within the given duration after starting, regardless of user activity. This is an enterprise-only feature. Maps to \"Max lifetime\" in the UI." ,
2023-03-23 17:42:20 -05:00
Value : clibase . DurationOf ( & maxTTL ),
},
2023-07-20 06:35:41 -07:00
{
2023-08-29 11:35:05 -07:00
Flag : "autostop-requirement-weekdays" ,
Description : "Edit the template autostop requirement weekdays - workspaces created from this template must be restarted on the given weekdays. To unset this value for the template (and disable the autostop requirement for the template), pass 'none'." ,
2023-07-20 06:35:41 -07:00
// TODO(@dean): unhide when we delete max_ttl
Hidden : true ,
2023-08-29 11:35:05 -07:00
Value : clibase . Validate ( clibase . StringArrayOf ( & autostopRequirementDaysOfWeek ), func ( value * clibase . StringArray ) error {
2023-07-20 06:35:41 -07:00
v := value . GetSlice ()
if len ( v ) == 1 && v [ 0 ] == "none" {
return nil
}
_ , err := codersdk . WeekdaysToBitmap ( v )
if err != nil {
2023-08-29 11:35:05 -07:00
return xerrors . Errorf ( "invalid autostop requirement days of week %q: %w" , strings . Join ( v , "," ), err )
2023-07-20 06:35:41 -07:00
}
return nil
}),
},
{
2023-08-29 11:35:05 -07:00
Flag : "autostop-requirement-weeks" ,
Description : "Edit the template autostop requirement weeks - workspaces created from this template must be restarted on an n-weekly basis." ,
2023-07-20 06:35:41 -07:00
// TODO(@dean): unhide when we delete max_ttl
Hidden : true ,
2023-08-29 11:35:05 -07:00
Value : clibase . Int64Of ( & autostopRequirementWeeks ),
2023-07-20 06:35:41 -07:00
},
2023-05-10 12:57:11 -07:00
{
Flag : "failure-ttl" ,
2023-08-25 13:39:12 -04:00
Description : "Specify a failure TTL for workspaces created from this template. It is the amount of time after a failed \"start\" build before coder automatically schedules a \"stop\" build to cleanup.This licensed feature's default is 0h (off). Maps to \"Failure cleanup\" in the UI." ,
2023-05-10 12:57:11 -07:00
Default : "0h" ,
Value : clibase . DurationOf ( & failureTTL ),
},
{
Flag : "inactivity-ttl" ,
2023-08-25 13:39:12 -04:00
Description : "Specify an inactivity TTL for workspaces created from this template. It is the amount of time the workspace is not used before it is be stopped and auto-locked. This includes across multiple builds (e.g. auto-starts and stops). This licensed feature's default is 0h (off). Maps to \"Dormancy threshold\" in the UI." ,
2023-05-10 12:57:11 -07:00
Default : "0h" ,
Value : clibase . DurationOf ( & inactivityTTL ),
},
2023-03-23 17:42:20 -05:00
{
Flag : "allow-user-cancel-workspace-jobs" ,
Description : "Allow users to cancel in-progress workspace jobs." ,
Default : "true" ,
Value : clibase . BoolOf ( & allowUserCancelWorkspaceJobs ),
},
2023-04-04 22:48:35 +10:00
{
Flag : "allow-user-autostart" ,
Description : "Allow users to configure autostart for workspaces on this template. This can only be disabled in enterprise." ,
Default : "true" ,
Value : clibase . BoolOf ( & allowUserAutostart ),
},
{
Flag : "allow-user-autostop" ,
Description : "Allow users to customize the autostop TTL for workspaces on this template. This can only be disabled in enterprise." ,
Default : "true" ,
Value : clibase . BoolOf ( & allowUserAutostop ),
},
2023-03-23 17:42:20 -05:00
cliui . SkipPromptOption (),
}
2022-06-08 15:14:57 +01:00
return cmd
}