/
vpn.go
53 lines (46 loc) · 1.38 KB
/
vpn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cmd
import (
"github.com/metal-stack/metal-go/api/client/vpn"
"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newVPNCmd(c *config) *cobra.Command {
vpnCmd := &cobra.Command{
Use: "vpn",
Short: "access VPN",
Long: "access VPN",
}
vpnKeyCmd := &cobra.Command{
Use: "key",
Short: "create an auth key",
Long: "create an auth key to connect to VPN",
Example: `auth key for tailscale can be created by this command:
metalctl vpn key \
-- project cluster01
`,
RunE: func(cmd *cobra.Command, args []string) error {
return c.vpnAuthKeyCreate()
},
}
vpnKeyCmd.Flags().String("project", "", "project ID for which auth key should be created")
vpnKeyCmd.Flags().Bool("ephemeral", true, "create an ephemeral key")
must(vpnKeyCmd.MarkFlagRequired("project"))
must(vpnKeyCmd.RegisterFlagCompletionFunc("project", c.comp.ProjectListCompletion))
vpnCmd.AddCommand(vpnKeyCmd)
return vpnCmd
}
func (c *config) vpnAuthKeyCreate() error {
resp, err := c.client.VPN().GetVPNAuthKey(
vpn.NewGetVPNAuthKeyParams().WithBody(
&models.V1VPNRequest{
Pid: pointer.Pointer(viper.GetString("project")),
Ephemeral: pointer.PointerOrNil(viper.GetBool("ephemeral")),
}), nil,
)
if err != nil {
return err
}
return c.describePrinter.Print(resp.Payload.AuthKey)
}