-
Notifications
You must be signed in to change notification settings - Fork 20
/
security_group_create.go
75 lines (57 loc) · 1.94 KB
/
security_group_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/utils"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type securityGroupCreateCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"create"`
Name string `cli-arg:"#"`
Description string `cli-usage:"Security Group description"`
}
func (c *securityGroupCreateCmd) cmdAliases() []string { return gCreateAlias }
func (c *securityGroupCreateCmd) cmdShort() string {
return "Create a Security Group"
}
func (c *securityGroupCreateCmd) cmdLong() string {
return fmt.Sprintf(`This command creates a Compute instance Security Group.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&securityGroupShowOutput{}), ", "))
}
func (c *securityGroupCreateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *securityGroupCreateCmd) cmdRun(_ *cobra.Command, _ []string) error {
zone := account.CurrentAccount.DefaultZone
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))
securityGroup := &egoscale.SecurityGroup{
Description: utils.NonEmptyStringPtr(c.Description),
Name: &c.Name,
}
var err error
decorateAsyncOperation(fmt.Sprintf("Creating Security Group %q...", c.Name), func() {
securityGroup, err = globalstate.EgoscaleClient.CreateSecurityGroup(ctx, zone, securityGroup)
})
if err != nil {
return err
}
if !globalstate.Quiet {
return (&securityGroupShowCmd{
cliCommandSettings: c.cliCommandSettings,
SecurityGroup: *securityGroup.ID,
}).cmdRun(nil, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(securityGroupCmd, &securityGroupCreateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}