Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
Merged
Prev Previous commit
Next Next commit
Support restoring policy template to default
  • Loading branch information
lilshoff committed Jun 18, 2021
commit 56a3c7558508e50eecf77dce20b8cdb2fabf9a2e
1 change: 1 addition & 0 deletions docs/coder_workspaces_policy-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ coder workspaces policy-template [flags]
### Options

```
--default Restore policy template to default value
--dry-run skip setting policy template, but view errors/warnings about how this policy template would impact existing workspaces
-f, --filepath string full path to local policy template file.
-h, --help help for policy-template
Expand Down
56 changes: 31 additions & 25 deletions internal/cmd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,11 +756,12 @@ func buildUpdateReq(ctx context.Context, client coder.Client, conf updateConf) (

func setPolicyTemplate() *cobra.Command {
var (
ref string
repo string
filepath string
dryRun bool
scope string
ref string
repo string
filepath string
dryRun bool
defaultTemplate bool
scope string
)

cmd := &cobra.Command{
Expand All @@ -778,31 +779,35 @@ func setPolicyTemplate() *cobra.Command {
return clog.Error("Invalid 'scope' value", "Valid scope values: site")
}

if filepath == "" {
return clog.Error("Missing required parameter --filepath or -f", "")
if filepath == "" && !defaultTemplate {
return clog.Error("Missing required parameter --filepath or --default", "Must specify a template to set")
}

var rd io.Reader
b, err := ioutil.ReadFile(filepath)
if err != nil {
return xerrors.Errorf("read local file: %w", err)
}
rd = bytes.NewReader(b)

req := coder.ParseTemplateRequest{
RepoURL: repo,
Ref: ref,
Local: rd,
OrgID: coder.SkipTemplateOrg,
Filepath: ".coder/coder.yaml",
}
templateID := ""
if filepath != "" {
var rd io.Reader
b, err := ioutil.ReadFile(filepath)
if err != nil {
return xerrors.Errorf("read local file: %w", err)
}
rd = bytes.NewReader(b)

req := coder.ParseTemplateRequest{
RepoURL: repo,
Ref: ref,
Local: rd,
OrgID: coder.SkipTemplateOrg,
Filepath: ".coder/coder.yaml",
}

version, err := client.ParseTemplate(ctx, req)
if err != nil {
return handleAPIError(err)
version, err := client.ParseTemplate(ctx, req)
if err != nil {
return handleAPIError(err)
}
templateID = version.TemplateID
}

resp, err := client.SetPolicyTemplate(ctx, version.TemplateID, coder.TemplateScope(scope), dryRun)
resp, err := client.SetPolicyTemplate(ctx, templateID, coder.TemplateScope(scope), dryRun)
if err != nil {
return handleAPIError(err)
}
Expand All @@ -823,5 +828,6 @@ func setPolicyTemplate() *cobra.Command {
cmd.Flags().BoolVarP(&dryRun, "dry-run", "", false, "skip setting policy template, but view errors/warnings about how this policy template would impact existing workspaces")
cmd.Flags().StringVarP(&filepath, "filepath", "f", "", "full path to local policy template file.")
cmd.Flags().StringVar(&scope, "scope", "site", "scope of impact for the policy template. Supported values: site")
cmd.Flags().BoolVar(&defaultTemplate, "default", false, "Restore policy template to default configuration")
return cmd
}