-
Notifications
You must be signed in to change notification settings - Fork 18
/
firewall_create.go
56 lines (47 loc) · 1.22 KB
/
firewall_create.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
package cmd
import (
"os"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// createCmd represents the create command
var firewallCreateCmd = &cobra.Command{
Use: "create <name>",
Short: "Create security group",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
desc, err := cmd.Flags().GetString("description")
if err != nil {
return err
}
return firewallCreate(args[0], desc)
},
}
func firewallCreate(name, desc string) error {
req := &egoscale.CreateSecurityGroup{Name: name}
if desc != "" {
req.Description = desc
}
resp, err := cs.RequestWithContext(gContext, req)
if err != nil {
return err
}
sgResp := resp.(*egoscale.SecurityGroup)
table := table.NewTable(os.Stdout)
if desc == "" {
table.SetHeader([]string{"Name", "ID"})
table.Append([]string{sgResp.Name, sgResp.ID.String()})
} else {
table.SetHeader([]string{"Name", "Description", "ID"})
table.Append([]string{sgResp.Name, sgResp.Description, sgResp.ID.String()})
}
table.Render()
return nil
}
func init() {
firewallCreateCmd.Flags().StringP("description", "d", "", "Security group description")
firewallCmd.AddCommand(firewallCreateCmd)
}