-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpns_cmd.go
104 lines (85 loc) · 2.6 KB
/
vpns_cmd.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2017-2021 Ingram Micro Inc.
package cmd
import (
"strings"
"github.com/ingrammicro/cio/api/network"
"github.com/ingrammicro/cio/utils"
"github.com/ingrammicro/cio/utils/format"
"github.com/urfave/cli"
)
// WireUpVPN prepares common resources to send request to Concerto API
func WireUpVPN(c *cli.Context) (ds *network.VPNService, f format.Formatter) {
f = format.GetFormatter()
config, err := utils.GetConcertoConfig()
if err != nil {
f.PrintFatal("Couldn't wire up config", err)
}
hcs, err := utils.NewHTTPConcertoService(config)
if err != nil {
f.PrintFatal("Couldn't wire up concerto service", err)
}
ds, err = network.NewVPNService(hcs)
if err != nil {
f.PrintFatal("Couldn't wire up VPN service", err)
}
return ds, f
}
// VPNShow subcommand function
func VPNShow(c *cli.Context) error {
debugCmdFuncInfo(c)
vpnSvc, formatter := WireUpVPN(c)
checkRequiredFlags(c, []string{"vpc-id"}, formatter)
vpn, err := vpnSvc.GetVPN(c.String("vpc-id"))
if err != nil {
formatter.PrintFatal("Couldn't receive VPN data", err)
}
if err = formatter.PrintItem(*vpn); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// VPNCreate subcommand function
func VPNCreate(c *cli.Context) error {
debugCmdFuncInfo(c)
vpnSvc, formatter := WireUpVPN(c)
checkRequiredFlags(c, []string{"vpc-id", "public-ip", "psk", "exposed-cidrs", "vpn-plan-id"}, formatter)
vpnIn := map[string]interface{}{
"public_ip": c.String("public-ip"),
"psk": c.String("psk"),
"exposed_cidrs": utils.RemoveDuplicates(strings.Split(c.String("exposed-cidrs"), ",")),
"vpn_plan_id": c.String("vpn-plan-id"),
}
vpn, err := vpnSvc.CreateVPN(c.String("vpc-id"), &vpnIn)
if err != nil {
formatter.PrintFatal("Couldn't create VPN", err)
}
if err = formatter.PrintItem(*vpn); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// VPNDelete subcommand function
func VPNDelete(c *cli.Context) error {
debugCmdFuncInfo(c)
vpnSvc, formatter := WireUpVPN(c)
checkRequiredFlags(c, []string{"vpc-id"}, formatter)
err := vpnSvc.DeleteVPN(c.String("vpc-id"))
if err != nil {
formatter.PrintFatal("Couldn't delete VPN", err)
}
return nil
}
// VPNPlanList subcommand function
func VPNPlanList(c *cli.Context) error {
debugCmdFuncInfo(c)
vpcSvc, formatter := WireUpVPN(c)
checkRequiredFlags(c, []string{"vpc-id"}, formatter)
vpns, err := vpcSvc.ListVPNPlans(c.String("vpc-id"))
if err != nil {
formatter.PrintFatal("Couldn't receive VPN data", err)
}
if err = formatter.PrintList(vpns); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}