Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/products.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions products/uddos/internal/mainland/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Package mainland ...
//
// @Brief 国内高防命令组聚合
//
// @File cmd.go
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
//
// @CopyRights(C) UCloud All rights reserved.
package mainland

import (
"github.com/spf13/cobra"

"github.com/ucloud/ucloud-cli/pkg/cli"
mainlandip "github.com/ucloud/ucloud-cli/products/uddos/internal/mainland/ip"
mainlandrule "github.com/ucloud/ucloud-cli/products/uddos/internal/mainland/rule"
mainlandsvc "github.com/ucloud/ucloud-cli/products/uddos/internal/mainland/service"
)

// NewCommand 构建 uddos mainland 命令组
//
// @Brief 构建国内高防命令组并挂载 service、ip 子命令
//
// @Param ctx *cli.Context
//
// @Return *cobra.Command
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
func NewCommand(ctx *cli.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "mainland",
Short: "Manage mainland China DDoS high-protection services",
Long: "Manage UCloud mainland China (国内高防) DDoS protection services and IPs",
Args: cobra.NoArgs,
}
cmd.AddCommand(mainlandip.NewCommand(ctx))
cmd.AddCommand(mainlandrule.NewCommand(ctx))
cmd.AddCommand(mainlandsvc.NewCommand(ctx))
return cmd
}
56 changes: 56 additions & 0 deletions products/uddos/internal/mainland/ip/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Package ip ...
//
// @Brief 国内高防IP管理命令聚合
//
// @File cmd.go
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
//
// @CopyRights(C) UCloud All rights reserved.
package ip

import (
"github.com/spf13/cobra"

"github.com/ucloud/ucloud-cli/pkg/cli"
)

// NewCommand 构建 uddos mainland ip 命令组
//
// @Brief 构建国内高防 ip 命令组并挂载子命令
//
// @Param ctx *cli.Context
//
// @Return *cobra.Command
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
func NewCommand(ctx *cli.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "ip",
Short: "Manage mainland DDoS protection IPs",
Long: "List, create and delete mainland BGP DDoS protection IPs",
Args: cobra.NoArgs,
}
cmd.AddCommand(newList(ctx))
cmd.AddCommand(newCreate(ctx))
cmd.AddCommand(newDelete(ctx))
return cmd
}

func strVal(m map[string]interface{}, key string) string {
v, _ := m[key].(string)
return v
}

func intVal(m map[string]interface{}, key string) int {
v, _ := m[key].(float64)
return int(v)
}
83 changes: 83 additions & 0 deletions products/uddos/internal/mainland/ip/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Package ip ...
//
// @Brief 创建国内高防IP命令
//
// @File create.go
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
//
// @CopyRights(C) UCloud All rights reserved.
package ip

import (
"fmt"

"github.com/spf13/cobra"
"github.com/ucloud/ucloud-sdk-go/services/uaccount"

"github.com/ucloud/ucloud-cli/pkg/cli"
)

// newCreate 构建 uddos mainland ip create 命令
//
// @Brief 构建国内高防 ip create 子命令,调用 CreateBGPServiceIP
//
// @Param ctx *cli.Context
//
// @Return *cobra.Command
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
func newCreate(ctx *cli.Context) *cobra.Command {
var resourceID, typeIP, remark, tag string

cmd := &cobra.Command{
Use: "create",
Short: "Create a mainland BGP high-protection IP",
Long: "Create a new BGP DDoS protection IP for the specified mainland service",
Example: " ucloud uddos mainland ip create --resource-id ghp-xxxxx",
Run: func(cmd *cobra.Command, args []string) {
client := cli.NewServiceClient(ctx, uaccount.NewClient)
params := map[string]interface{}{
"Action": "CreateBGPServiceIP",
"ResourceId": resourceID,
"TypeIP": typeIP,
}
if cmd.Flags().Changed("remark") {
params["Remark"] = remark
}
if cmd.Flags().Changed("tag") {
params["Tag"] = tag
}
req := client.NewGenericRequest()
if err := req.SetPayload(params); err != nil {
ctx.HandleError(fmt.Errorf("set payload: %w", err))
return
}
resp, err := client.GenericInvoke(req)
if err != nil {
ctx.HandleError(fmt.Errorf("CreateBGPServiceIP: %w", err))
return
}
payload := resp.GetPayload()
defenceIP, _ := payload["DefenceIP"].(string)
fmt.Fprintf(ctx.ProgressWriter(), "BGP IP created: %s\n", defenceIP)
ctx.EmitResult(cli.OpResultRow{ResourceID: defenceIP, Action: "create", Status: "Created"})
},
}

flags := cmd.Flags()
flags.StringVar(&resourceID, "resource-id", "", "Required. Service resource ID")
flags.StringVar(&typeIP, "type-ip", "TypeFree", "Optional. IP type: TypeFree or TypeCharge, default TypeFree")
flags.StringVar(&remark, "remark", "", "Optional. Remark for this IP")
flags.StringVar(&tag, "tag", "", "Optional. Business group tag")
cmd.MarkFlagRequired("resource-id")
return cmd
}
84 changes: 84 additions & 0 deletions products/uddos/internal/mainland/ip/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Package ip ...
//
// @Brief 删除国内高防IP命令
//
// @File delete.go
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
//
// @CopyRights(C) UCloud All rights reserved.
package ip

import (
"fmt"

"github.com/spf13/cobra"
"github.com/ucloud/ucloud-sdk-go/services/uaccount"

"github.com/ucloud/ucloud-cli/pkg/cli"
)

// newDelete 构建 uddos mainland ip delete 命令
//
// @Brief 构建国内高防 ip delete 子命令,调用 DeleteBGPServiceIP
//
// @Param ctx *cli.Context
//
// @Return *cobra.Command
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
func newDelete(ctx *cli.Context) *cobra.Command {
var resourceID, defenceIP string
var yes bool

cmd := &cobra.Command{
Use: "delete",
Short: "Delete a mainland BGP high-protection IP",
Long: "Delete a BGP DDoS protection IP from the specified mainland service",
Example: " ucloud uddos mainland ip delete --resource-id ghp-xxxxx --defence-ip 1.2.3.4",
Run: func(cmd *cobra.Command, args []string) {
confirmed, err := ctx.Confirm(yes, fmt.Sprintf("Are you sure to delete defence IP %s from service %s?", defenceIP, resourceID))
if err != nil {
ctx.HandleError(fmt.Errorf("confirm: %w", err))
return
}
if !confirmed {
return
}
client := cli.NewServiceClient(ctx, uaccount.NewClient)
params := map[string]interface{}{
"Action": "DeleteBGPServiceIP",
"ResourceId": resourceID,
"DefenceIp": defenceIP,
}
req := client.NewGenericRequest()
if err := req.SetPayload(params); err != nil {
ctx.HandleError(fmt.Errorf("set payload: %w", err))
return
}
_, invokeErr := client.GenericInvoke(req)
if invokeErr != nil {
ctx.HandleError(fmt.Errorf("DeleteBGPServiceIP: %w", invokeErr))
return
}
fmt.Fprintf(ctx.ProgressWriter(), "BGP IP deleted: %s\n", defenceIP)
ctx.EmitResult(cli.OpResultRow{ResourceID: defenceIP, Action: "delete", Status: "Deleted"})
},
}

flags := cmd.Flags()
flags.StringVar(&resourceID, "resource-id", "", "Required. Service resource ID")
flags.StringVar(&defenceIP, "defence-ip", "", "Required. BGP defence IP to delete")
flags.BoolVarP(&yes, "yes", "y", false, "Optional. Skip confirmation prompt")
cmd.MarkFlagRequired("resource-id")
cmd.MarkFlagRequired("defence-ip")
return cmd
}
99 changes: 99 additions & 0 deletions products/uddos/internal/mainland/ip/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Package ip ...
//
// @Brief 查询国内高防IP列表命令
//
// @File list.go
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
//
// @CopyRights(C) UCloud All rights reserved.
package ip

import (
"fmt"

"github.com/spf13/cobra"
"github.com/ucloud/ucloud-sdk-go/services/uaccount"

"github.com/ucloud/ucloud-cli/pkg/cli"
)

// newList 构建 uddos mainland ip list 命令
//
// @Brief 构建国内高防 ip list 子命令,调用 GetBGPServiceIP
//
// @Param ctx *cli.Context
//
// @Return *cobra.Command
//
// @Author leas.li(cc)
//
// @Email leas.li@ucloud.cn
//
// @Date 2026/07/11
func newList(ctx *cli.Context) *cobra.Command {
var resourceID, bgpIP string
var offset, limit int

cmd := &cobra.Command{
Use: "list",
Short: "List mainland BGP high-protection IPs",
Long: "List BGP DDoS protection IP addresses for a mainland service instance",
Example: " ucloud uddos mainland ip list --resource-id ghp-xxxxx",
Run: func(cmd *cobra.Command, args []string) {
client := cli.NewServiceClient(ctx, uaccount.NewClient)
params := map[string]interface{}{
"Action": "GetBGPServiceIP",
"ResourceId": resourceID,
"Offset": offset,
"Limit": limit,
}
if bgpIP != "" {
params["BgpIP"] = bgpIP
}
req := client.NewGenericRequest()
if err := req.SetPayload(params); err != nil {
ctx.HandleError(fmt.Errorf("set payload: %w", err))
return
}
resp, err := client.GenericInvoke(req)
if err != nil {
ctx.HandleError(fmt.Errorf("GetBGPServiceIP: %w", err))
return
}
payload := resp.GetPayload()
gameIPInfo, _ := payload["GameIPInfo"].([]interface{})
rows := make([]IPRow, 0, len(gameIPInfo))
for _, item := range gameIPInfo {
m, ok := item.(map[string]interface{})
if !ok {
continue
}
rows = append(rows, IPRow{
DefenceIP: strVal(m, "DefenceIP"),
UserIP: strVal(m, "UserIP"),
LineType: strVal(m, "LineType"),
Status: strVal(m, "Status"),
Cname: strVal(m, "Cname"),
RuleCnt: intVal(m, "RuleCnt"),
DefenceDDosBaseFlow: intVal(m, "DefenceDDosBaseFlow"),
DefenceDDosMaxFlow: intVal(m, "DefenceDDosMaxFlow"),
Remark: strVal(m, "Remark"),
})
}
ctx.PrintList(rows)
},
}

flags := cmd.Flags()
flags.StringVar(&resourceID, "resource-id", "", "Required. Service resource ID")
flags.StringVar(&bgpIP, "bgp-ip", "", "Optional. Filter by BGP IP address")
flags.IntVar(&offset, "offset", 0, "Optional. Page offset, default 0")
flags.IntVar(&limit, "limit", 20, "Optional. Page size, default 20")
cmd.MarkFlagRequired("resource-id")
return cmd
}
Loading
Loading