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

Commit eccaa79

Browse files
authored
Add coder providers rename command (#320)
* Add coder providers rename command * Update interface.go
1 parent 6a76b7d commit eccaa79

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

coder-sdk/interface.go

+3
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,7 @@ type Client interface {
232232
// UnCordonWorkspaceProvider changes an existing cordoned providers status to 'Ready';
233233
// allowing it to continue creating new workspaces and provisioning resources for them.
234234
UnCordonWorkspaceProvider(ctx context.Context, id string) error
235+
236+
// RenameWorkspaceProvider changes an existing providers name field.
237+
RenameWorkspaceProvider(ctx context.Context, id string, name string) error
235238
}

coder-sdk/workspace_providers.go

+15
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,18 @@ func (c *DefaultClient) UnCordonWorkspaceProvider(ctx context.Context, id string
123123
}
124124
return nil
125125
}
126+
127+
// RenameWorkspaceProviderReq defines the request parameters for changing a workspace provider name.
128+
type RenameWorkspaceProviderReq struct {
129+
Name string `json:"name"`
130+
}
131+
132+
// RenameWorkspaceProvider changes an existing cordoned providers name field.
133+
func (c *DefaultClient) RenameWorkspaceProvider(ctx context.Context, id string, name string) error {
134+
req := RenameWorkspaceProviderReq{Name: name}
135+
err := c.requestBody(ctx, http.MethodPatch, "/api/private/resource-pools/"+id, req, nil)
136+
if err != nil {
137+
return err
138+
}
139+
return nil
140+
}

internal/cmd/providers.go

+33
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func providersCmd() *cobra.Command {
2929
deleteProviderCmd(),
3030
cordonProviderCmd(),
3131
unCordonProviderCmd(),
32+
renameProviderCmd(),
3233
)
3334
return cmd
3435
}
@@ -284,3 +285,35 @@ coder providers uncordon my-workspace-provider`,
284285
}
285286
return cmd
286287
}
288+
289+
func renameProviderCmd() *cobra.Command {
290+
cmd := &cobra.Command{
291+
Use: "rename [old_name] [new_name]",
292+
Args: xcobra.ExactArgs(2),
293+
Short: "rename a workspace provider.",
294+
Long: "Changes the name field of an existing workspace provider.",
295+
Example: `# rename a workspace provider from 'built-in' to 'us-east-1'
296+
coder providers rename build-in us-east-1`,
297+
RunE: func(cmd *cobra.Command, args []string) error {
298+
ctx := cmd.Context()
299+
client, err := newClient(ctx)
300+
if err != nil {
301+
return err
302+
}
303+
304+
oldName := args[0]
305+
newName := args[1]
306+
provider, err := coderutil.ProviderByName(ctx, client, oldName)
307+
if err != nil {
308+
return err
309+
}
310+
311+
if err := client.RenameWorkspaceProvider(ctx, provider.ID, newName); err != nil {
312+
return err
313+
}
314+
clog.LogSuccess(fmt.Sprintf("provider %s successfully renamed to %s", oldName, newName))
315+
return nil
316+
},
317+
}
318+
return cmd
319+
}

0 commit comments

Comments
 (0)