-
Notifications
You must be signed in to change notification settings - Fork 20
/
privnet_update.go
95 lines (84 loc) · 2.44 KB
/
privnet_update.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
package cmd
import (
"fmt"
"net"
"os"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// updateCmd represents the update command
var privnetUpdateCmd = &cobra.Command{
Use: "update <name | id>",
Short: "Update a private network",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}
network, err := getNetworkByName(args[0])
if err != nil {
return err
}
id := network.ID.String()
name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
desc, err := cmd.Flags().GetString("description")
if err != nil {
return err
}
startip, err := cmd.Flags().GetString("startip")
if err != nil {
return err
}
endip, err := cmd.Flags().GetString("endip")
if err != nil {
return err
}
netmask, err := cmd.Flags().GetString("netmask")
if err != nil {
return err
}
return privnetUpdate(id, name, desc, startip, endip, netmask)
},
}
func privnetUpdate(id, name, desc, startIPAddr, endIPAddr, netmask string) error {
uuid, err := egoscale.ParseUUID(id)
if err != nil {
return fmt.Errorf("update the network by ID, got %q", id)
}
startIP := net.ParseIP(startIPAddr)
endIP := net.ParseIP(endIPAddr)
netmaskIP := net.ParseIP(netmask)
req := &egoscale.UpdateNetwork{
ID: uuid,
DisplayText: desc,
Name: name,
StartIP: startIP,
EndIP: endIP,
Netmask: netmaskIP,
}
resp, err := asyncRequest(req, fmt.Sprintf("Updating the network %q ", id))
if err != nil {
return err
}
newNet := resp.(*egoscale.Network)
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"Name", "Description", "ID", "DHCP"})
table.Append([]string{
newNet.Name,
newNet.DisplayText,
newNet.ID.String(),
dhcpRange(newNet.StartIP, newNet.EndIP, newNet.Netmask)})
table.Render()
return nil
}
func init() {
privnetUpdateCmd.Flags().StringP("name", "n", "", "Private network name")
privnetUpdateCmd.Flags().StringP("description", "d", "", "Private network description")
privnetUpdateCmd.Flags().StringP("startip", "s", "", "the beginning IP address in the network IP range. Required for managed networks.")
privnetUpdateCmd.Flags().StringP("endip", "e", "", "the ending IP address in the network IP range. Required for managed networks.")
privnetUpdateCmd.Flags().StringP("netmask", "m", "", "the netmask of the network. Required for managed networks")
privnetCmd.AddCommand(privnetUpdateCmd)
}