This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
feat: support setting policy template via local file or restoring to default #366
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
84cbadd
Support setting policy template via local file or remote repo
lilshoff 8bf005c
Print workspace id on failure to fetch name/org
lilshoff b0b8e17
Lint
lilshoff 72637a5
Gendocs
lilshoff 6379202
Remove support for remote tracked policy templates
lilshoff 84ed3bb
Gendocs
lilshoff d831363
Add flag for 'scope' with default value 'site'
lilshoff 56a3c75
Support restoring policy template to default
lilshoff abb7b7a
Gendocs
lilshoff 6d3d061
Update command description
lilshoff 7e08daa
Add summary of broken/impacted workspaces when setting policy template
lilshoff fcfcc4e
Refine string building
lilshoff cd8242f
Refine string building pt2
lilshoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## coder workspaces policy-template | ||
|
||
Set workspace policy template | ||
|
||
### Synopsis | ||
|
||
Set workspace policy template or restore to default configuration. This feature is for site admins only. | ||
|
||
``` | ||
coder workspaces policy-template [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
--default Restore policy template to default configuration | ||
--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 | ||
--scope string scope of impact for the policy template. Supported values: site (default "site") | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-v, --verbose show verbose output | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [coder workspaces](coder_workspaces.md) - Interact with Coder workspaces | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ func workspacesCmd() *cobra.Command { | |
workspaceFromConfigCmd(true), | ||
workspaceFromConfigCmd(false), | ||
editWorkspaceCmd(), | ||
setPolicyTemplate(), | ||
) | ||
return cmd | ||
} | ||
|
@@ -752,3 +753,85 @@ func buildUpdateReq(ctx context.Context, client coder.Client, conf updateConf) ( | |
} | ||
return &updateReq, nil | ||
} | ||
|
||
func setPolicyTemplate() *cobra.Command { | ||
var ( | ||
ref string | ||
repo string | ||
filepath string | ||
dryRun bool | ||
defaultTemplate bool | ||
scope string | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "policy-template", | ||
Short: "Set workspace policy template", | ||
Long: "Set workspace policy template or restore to default configuration. This feature is for site admins only.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
client, err := newClient(ctx, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if scope != coder.TemplateScopeSite { | ||
return clog.Error("Invalid 'scope' value", "Valid scope values: site") | ||
} | ||
|
||
if filepath == "" && !defaultTemplate { | ||
return clog.Error("Missing required parameter --filepath or --default", "Must specify a template to set") | ||
} | ||
|
||
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) | ||
} | ||
templateID = version.TemplateID | ||
} | ||
|
||
resp, err := client.SetPolicyTemplate(ctx, templateID, coder.TemplateScope(scope), dryRun) | ||
if err != nil { | ||
return handleAPIError(err) | ||
} | ||
|
||
for _, mc := range resp.MergeConflicts { | ||
workspace, err := client.WorkspaceByID(ctx, mc.WorkspaceID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the extra details. In my dogfood of 200 workspaces this was still pretty quick 👍 |
||
if err != nil { | ||
fmt.Printf("Workspace %q:\n", mc.WorkspaceID) | ||
} else { | ||
fmt.Printf("Workspace %q in organization %q:\n", workspace.Name, workspace.OrganizationID) | ||
} | ||
|
||
fmt.Println(mc.String()) | ||
} | ||
|
||
fmt.Println("Summary:") | ||
fmt.Println(coder.WorkspaceTemplateMergeConflicts(resp.MergeConflicts).Summary()) | ||
|
||
return nil | ||
}, | ||
} | ||
cmd.Flags().BoolVarP(&dryRun, "dry-run", "", false, "skip setting policy template, but view errors/warnings about how this policy template would impact existing workspaces") | ||
lilshoff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something that might be helpful is some sort of summary at the end. I have 200 workspaces in my dogfood, and it would be nice to see something like:
In the future, it would be great to merge-common errors and report which users are affected maybe? But for now I think a simple summary like that would be great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a summary at the end.
Mine isn't too exciting bc I regularly delete my workspaces in my dev deployment, so I only have like 2.
I'm curious to see what you think of the summary when there are 200 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very curious about why @Emyrk 200 workspaces 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, my deployment has 200 workspaces because I was testing the response times of our apis at those numbers. I made a little deployment branch to make them fast, but I never made anything to delete them in bulk haha.
I figure I'll just keep em as a bit of a "stress test" for the UI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The summary at 200 is not the most helpful thing, but I'm not worried about optimizing for 200 workspaces right now. Just thought a summary would be nice even for like ~30 workspaces which is reasonable at a company