Skip to content

Commit 6ee577b

Browse files
add replicated vm ssh-endpoint command to print the ssh endpoint of a vm (#531)
* add subcommand for get verb and ssh-endpoint * no interactive * remove get verb * simplify vm checking * fetch github user from vendor api * error if no github username * Remove unused get
1 parent 82bdf60 commit 6ee577b

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

cli/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
271271
runCmds.InitVMRemove(vmCmd)
272272
runCmds.InitVMSSH(vmCmd)
273273
runCmds.InitVMSCP(vmCmd)
274+
runCmds.InitVMSSHEndpoint(vmCmd)
274275
vmUpdateCmd := runCmds.InitVMUpdateCommand(vmCmd)
275276
runCmds.InitVMUpdateTTL(vmUpdateCmd)
276277

cli/cmd/vm_ssh_endpoint.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/pkg/errors"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func (r *runners) InitVMSSHEndpoint(parent *cobra.Command) *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "ssh-endpoint VM_ID",
13+
Short: "Get the SSH endpoint of a VM",
14+
Long: `Get the SSH endpoint and port of a VM.
15+
16+
The output will be in the format: hostname:port`,
17+
Example: `# Get SSH endpoint for a specific VM by ID
18+
replicated vm ssh-endpoint <id>`,
19+
RunE: r.VMSSHEndpoint,
20+
Args: cobra.ExactArgs(1),
21+
ValidArgsFunction: r.completeVMIDs,
22+
}
23+
parent.AddCommand(cmd)
24+
25+
return cmd
26+
}
27+
28+
func (r *runners) VMSSHEndpoint(cmd *cobra.Command, args []string) error {
29+
vmID := args[0]
30+
31+
vm, err := r.kotsAPI.GetVM(vmID)
32+
if err != nil {
33+
return errors.Wrap(err, "get vm")
34+
}
35+
36+
if vm.DirectSSHEndpoint == "" || vm.DirectSSHPort == 0 {
37+
return errors.Errorf("VM %s does not have SSH endpoint configured", vm.ID)
38+
}
39+
40+
// Get GitHub username from API
41+
githubUsername, err := r.kotsAPI.GetGitHubUsername()
42+
if err != nil {
43+
return errors.Wrap(err, "get github username")
44+
}
45+
46+
// Format the SSH endpoint with username if available
47+
if githubUsername == "" {
48+
return errors.New(`no github account associated with vendor portal user
49+
Visit https://vendor.replicated.com/account-settings to link your account`)
50+
}
51+
52+
fmt.Printf("ssh://%s@%s:%d\n", githubUsername, vm.DirectSSHEndpoint, vm.DirectSSHPort)
53+
54+
return nil
55+
}

pkg/kotsclient/user.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package kotsclient
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/pkg/errors"
8+
)
9+
10+
type GetUserResponse struct {
11+
GitHubUsername string `json:"gitHubUsername"`
12+
}
13+
14+
func (c *VendorV3Client) GetGitHubUsername() (string, error) {
15+
var response GetUserResponse
16+
17+
err := c.DoJSON(context.TODO(), "GET", "/v1/user", http.StatusOK, nil, &response)
18+
if err != nil {
19+
return "", errors.Wrap(err, "failed to fetch GitHub username from vendor API")
20+
}
21+
22+
return response.GitHubUsername, nil
23+
}

0 commit comments

Comments
 (0)