Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Add coder providers rename command #320

Merged
merged 2 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions coder-sdk/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,7 @@ type Client interface {
// UnCordonWorkspaceProvider changes an existing cordoned providers status to 'Ready';
// allowing it to continue creating new workspaces and provisioning resources for them.
UnCordonWorkspaceProvider(ctx context.Context, id string) error

// RenameWorkspaceProvider changes an existing providers name field.
RenameWorkspaceProvider(ctx context.Context, id string, name string) error
}
15 changes: 15 additions & 0 deletions coder-sdk/workspace_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,18 @@ func (c *DefaultClient) UnCordonWorkspaceProvider(ctx context.Context, id string
}
return nil
}

// RenameWorkspaceProviderReq defines the request parameters for changing a workspace provider name.
type RenameWorkspaceProviderReq struct {
Name string `json:"name"`
}

// RenameWorkspaceProvider changes an existing cordoned providers name field.
func (c *DefaultClient) RenameWorkspaceProvider(ctx context.Context, id string, name string) error {
req := RenameWorkspaceProviderReq{Name: name}
err := c.requestBody(ctx, http.MethodPatch, "/api/private/resource-pools/"+id, req, nil)
if err != nil {
return err
}
return nil
}
33 changes: 33 additions & 0 deletions internal/cmd/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func providersCmd() *cobra.Command {
deleteProviderCmd(),
cordonProviderCmd(),
unCordonProviderCmd(),
renameProviderCmd(),
)
return cmd
}
Expand Down Expand Up @@ -284,3 +285,35 @@ coder providers uncordon my-workspace-provider`,
}
return cmd
}

func renameProviderCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "rename [old_name] [new_name]",
Args: xcobra.ExactArgs(2),
Short: "rename a workspace provider.",
Long: "Changes the name field of an existing workspace provider.",
Example: `# rename a workspace provider from 'built-in' to 'us-east-1'
coder providers rename build-in us-east-1`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
client, err := newClient(ctx)
if err != nil {
return err
}

oldName := args[0]
newName := args[1]
provider, err := coderutil.ProviderByName(ctx, client, oldName)
if err != nil {
return err
}

if err := client.RenameWorkspaceProvider(ctx, provider.ID, newName); err != nil {
return err
}
clog.LogSuccess(fmt.Sprintf("provider %s successfully renamed to %s", oldName, newName))
return nil
},
}
return cmd
}