GeezerVison is showing github.com — refactor(firewall): per-verb file layout (P3.6) (#15) · Episkey-G/ucloud-cli@73ad182
File treeExpand file treeCollapse file tree Open diff view settings products/firewall/internal/firewall
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +91Lines changed: 91 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package firewall | | 2 | + | | 3 | +import ( | | 4 | + "fmt" | | 5 | + "strings" | | 6 | + | | 7 | + "github.com/spf13/cobra" | | 8 | + | | 9 | + "github.com/ucloud/ucloud-sdk-go/services/unet" | | 10 | + | | 11 | + "github.com/ucloud/ucloud-cli/internal/common" | | 12 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 13 | + "github.com/ucloud/ucloud-cli/pkg/command" | | 14 | +) | | 15 | + | | 16 | +// newAddRule ucloud firewall add-rule | | 17 | +func newAddRule(ctx *cli.Context) *cobra.Command { | | 18 | + var rulesFilePath string | | 19 | + var fwIDs []string | | 20 | + client := cli.NewServiceClient(ctx, unet.NewClient) | | 21 | + req := client.NewUpdateFirewallRequest() | | 22 | + cmd := &cobra.Command{ | | 23 | + Use: "add-rule", | | 24 | + Short: "Add rule to firewall instance", | | 25 | + Long: "Add rule to firewall instance", | | 26 | + Example: `ucloud firewall add-rule --fw-id firewall-2xxxxz/test.lxj2 --rules "TCP|24|0.0.0.0/0|ACCEPT|HIGH" --rules-file firewall_rules.txt`, | | 27 | + Run: func(c *cobra.Command, args []string) { | | 28 | + if req.Rule == nil && rulesFilePath == "" { | | 29 | + fmt.Fprintln(ctx.Err(), "Error: flags rules and rules-file can't be both empty") | | 30 | + return | | 31 | + } | | 32 | + results := []cli.OpResultRow{} | | 33 | + for _, fwID := range fwIDs { | | 34 | + id := ctx.PickResourceID(fwID) | | 35 | + req.FWId = &id | | 36 | + firewall, err := getFirewall(ctx, *req.FWId, *req.ProjectId, *req.Region) | | 37 | + if err != nil { | | 38 | + ctx.HandleError(err) | | 39 | + return | | 40 | + } | | 41 | + ruleMap := map[string]bool{} | | 42 | + for _, r := range firewall.Rule { | | 43 | + ruleStr := fmt.Sprintf("%s|%s|%s|%s|%s", r.ProtocolType, r.DstPort, r.SrcIP, r.RuleAction, r.Priority) | | 44 | + ruleMap[ruleStr] = true | | 45 | + } | | 46 | + if rulesFilePath != "" { | | 47 | + rules, err := parseRulesFromFile(rulesFilePath) | | 48 | + if err != nil { | | 49 | + ctx.HandleError(err) | | 50 | + return | | 51 | + } | | 52 | + req.Rule = append(req.Rule, rules...) | | 53 | + } | | 54 | + for _, r := range req.Rule { | | 55 | + ruleMap[r] = true | | 56 | + } | | 57 | + req.Rule = []string{} | | 58 | + for r := range ruleMap { | | 59 | + r = strings.TrimSpace(r) | | 60 | + req.Rule = append(req.Rule, r) | | 61 | + } | | 62 | + _, err = client.UpdateFirewall(req) | | 63 | + if err != nil { | | 64 | + ctx.HandleError(err) | | 65 | + return | | 66 | + } | | 67 | + fmt.Fprintf(ctx.ProgressWriter(), "firewall[%s] updated\n", fwID) | | 68 | + results = append(results, cli.OpResultRow{ResourceID: id, Action: "add-rule", Status: "Updated"}) | | 69 | + } | | 70 | + ctx.EmitResult(results...) | | 71 | + }, | | 72 | + } | | 73 | + flags := cmd.Flags() | | 74 | + flags.SortFlags = false | | 75 | + | | 76 | + flags.StringSliceVar(&fwIDs, "fw-id", nil, "Required. Resource ID of firewalls to update") | | 77 | + flags.StringSliceVar(&req.Rule, "rules", nil, "Required if rules-file is empay. Rules to add to firewall. Schema:'Protocol|Port|IP|Action|Level'. See 'ucloud firewall create --help' for detail.") | | 78 | + flags.StringVar(&rulesFilePath, "rules-file", "", "Required if rules is empty. Path of rules file, in which each rule occupies one line. Schema: Protocol|Port|IP|Action|Level.") | | 79 | + req.Region = flags.String("region", ctx.DefaultRegion(), "Optional. Region, see 'ucloud region'") | | 80 | + req.ProjectId = flags.String("project-id", ctx.DefaultProjectID(), "Optional. Project-id, see 'ucloud project list'") | | 81 | + | | 82 | + command.SetCompletion(cmd, "fw-id", func() []string { | | 83 | + return getFirewallIDNames(ctx, *req.ProjectId, *req.Region) | | 84 | + }) | | 85 | + command.SetCompletion(cmd, "rules-file", func() []string { | | 86 | + return common.GetFileList("") | | 87 | + }) | | 88 | + | | 89 | + cmd.MarkFlagRequired("fw-id") | | 90 | + return cmd | | 91 | +} |
Collapse file Copy file name to clipboard +61Lines changed: 61 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package firewall | | 2 | + | | 3 | +import ( | | 4 | + "fmt" | | 5 | + | | 6 | + "github.com/spf13/cobra" | | 7 | + | | 8 | + "github.com/ucloud/ucloud-sdk-go/services/unet" | | 9 | + sdk "github.com/ucloud/ucloud-sdk-go/ucloud" | | 10 | + | | 11 | + "github.com/ucloud/ucloud-cli/pkg/cli" | | 12 | + "github.com/ucloud/ucloud-cli/pkg/command" | | 13 | +) | | 14 | + | | 15 | +// newApply ucloud firewall apply | | 16 | +func newApply(ctx *cli.Context) *cobra.Command { | | 17 | + client := cli.NewServiceClient(ctx, unet.NewClient) | | 18 | + req := client.NewGrantFirewallRequest() | | 19 | + resourceIDs := []string{} | | 20 | + fwID := "" | | 21 | + cmd := &cobra.Command{ | | 22 | + Use: "apply", | | 23 | + Short: "Applay firewall to ucloud service", | | 24 | + Long: "Applay firewall to ucloud service", | | 25 | + Example: "ucloud firewall apply --fw-id firewall-xxx --resource-id uhost-xxx --resource-type uhost", | | 26 | + Run: func(c *cobra.Command, args []string) { | | 27 | + req.FWId = sdk.String(ctx.PickResourceID(fwID)) | | 28 | + results := []cli.OpResultRow{} | | 29 | + for _, id := range resourceIDs { | | 30 | + req.ResourceId = sdk.String(id) | | 31 | + _, err := client.GrantFirewall(req) | | 32 | + if err != nil { | | 33 | + ctx.HandleError(err) | | 34 | + continue | | 35 | + } | | 36 | + fmt.Fprintf(ctx.ProgressWriter(), "firewall[%s] applied to %s[%s]\n", fwID, *req.ResourceType, id) | | 37 | + results = append(results, cli.OpResultRow{ResourceID: id, Action: "apply", Status: "Applied"}) | | 38 | + } | | 39 | + ctx.EmitResult(results...) | | 40 | + }, | | 41 | + } | | 42 | + flags := cmd.Flags() | | 43 | + flags.SortFlags = false | | 44 | + | | 45 | + flags.StringVar(&fwID, "fw-id", "", "Required. Resource ID of firewall to apply to some ucloud resource") | | 46 | + req.ResourceType = flags.String("resource-type", "", "Required. Resource type of resource to be applied firewall. Range 'uhost','unatgw','upm','hadoophost','fortresshost','udhost','udockhost','dbaudit'.") | | 47 | + flags.StringSliceVar(&resourceIDs, "resource-id", nil, "Resource ID of resources to be applied firewall") | | 48 | + req.Region = flags.String("region", ctx.DefaultRegion(), "Optional. Region, see 'ucloud region'") | | 49 | + req.ProjectId = flags.String("project-id", ctx.DefaultProjectID(), "Optional. Project-id, see 'ucloud project list'") | | 50 | + | | 51 | + command.SetFlagValues(cmd, "resource-type", "uhost", "unatgw", "upm", "hadoophost", "fortresshost", "udhost", "udockhost", "dbaudit") | | 52 | + command.SetCompletion(cmd, "fw-id", func() []string { | | 53 | + return getFirewallIDNames(ctx, *req.ProjectId, *req.Region) | | 54 | + }) | | 55 | + | | 56 | + cmd.MarkFlagRequired("fw-id") | | 57 | + cmd.MarkFlagRequired("resource-id") | | 58 | + cmd.MarkFlagRequired("resource-type") | | 59 | + | | 60 | + return cmd | | 61 | +} |
|