Skip to content

Commit 5d962df

Browse files
Add size param (#441)
GitOrigin-RevId: d2daf376f87886e30e416acdd6da8e579e64c743
1 parent 4f786b7 commit 5d962df

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

cmd/ssh.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ You can specify the service ID, service name, or specific instance ID as an argu
3232
# Connect to an ephemeral instance
3333
render ssh srv-abc123 --ephemeral
3434
35+
# Connect to an ephemeral instance with a specific plan size
36+
render ssh srv-abc123 --ephemeral --size standard
37+
3538
# Pass through ssh arguments
3639
render ssh srv-abc123 -- -L 5432:localhost:5432`,
3740
}
@@ -128,6 +131,7 @@ func init() {
128131
rootCmd.AddCommand(sshCmd)
129132

130133
sshCmd.Flags().BoolP("ephemeral", "e", false, "Connect to an ephemeral instance")
134+
sshCmd.Flags().String("size", "", "Plan name to use for the ephemeral instance (e.g. starter, standard, pro). Only valid with --ephemeral.")
131135

132136
sshCmd.RunE = func(cmd *cobra.Command, args []string) error {
133137
ctx := cmd.Context()

pkg/tui/views/ssh.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ type SSHInput struct {
2323
InstanceID string // Set when a specific instance is selected or provided
2424
Project *client.Project
2525
EnvironmentIDs []string
26-
Ephemeral bool `cli:"ephemeral"`
26+
Ephemeral bool `cli:"ephemeral"`
27+
Size string `cli:"size"`
2728

2829
Args []string
2930
}
@@ -47,22 +48,35 @@ func handleSSHError(err error) error {
4748
// createEphemeralShell creates an ephemeral shell pod for the given service
4849
// and returns the ephemeral shell id from the API response (empty string if
4950
// the response did not include one). This must be called before SSH'ing into
50-
// an ephemeral shell.
51-
func createEphemeralShell(ctx context.Context, serviceID string) (string, error) {
51+
// an ephemeral shell. If size is non-empty, it is sent as the requested
52+
// instance plan name.
53+
func createEphemeralShell(ctx context.Context, serviceID, size string) (string, error) {
5254
apiCfg, err := config.DefaultAPIConfig()
5355
if err != nil {
5456
return "", fmt.Errorf("failed to get API config: %w", err)
5557
}
5658

5759
url := fmt.Sprintf("%sservices/%s/ephemeral-shell", apiCfg.Host, serviceID)
5860

59-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(nil))
61+
var reqBody io.Reader = bytes.NewReader(nil)
62+
if size != "" {
63+
payload, err := json.Marshal(client.CreateEphemeralShellJSONRequestBody{Size: &size})
64+
if err != nil {
65+
return "", fmt.Errorf("failed to encode request body: %w", err)
66+
}
67+
reqBody = bytes.NewReader(payload)
68+
}
69+
70+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, reqBody)
6071
if err != nil {
6172
return "", fmt.Errorf("failed to create request: %w", err)
6273
}
6374

6475
req.Header = client.AddHeaders(req.Header, apiCfg.Key)
6576
req.Header.Set("Accept", "application/json")
77+
if size != "" {
78+
req.Header.Set("Content-Type", "application/json")
79+
}
6680

6781
resp, err := http.DefaultClient.Do(req)
6882
if err != nil {
@@ -185,9 +199,16 @@ func loadDataSSH(ctx context.Context, in *SSHInput) (*exec.Cmd, error) {
185199
}
186200
}
187201

202+
if in.Size != "" && !in.Ephemeral {
203+
return nil, tui.UserFacingError{
204+
Title: "Invalid flags",
205+
Message: "--size is only supported with --ephemeral.",
206+
}
207+
}
208+
188209
if in.Ephemeral {
189210
// Create the ephemeral shell pod first
190-
shellID, err := createEphemeralShell(ctx, serviceInfo.Id)
211+
shellID, err := createEphemeralShell(ctx, serviceInfo.Id, in.Size)
191212
if err != nil {
192213
return nil, tui.UserFacingError{
193214
Title: "Failed to create ephemeral shell",

0 commit comments

Comments
 (0)