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
6 changes: 6 additions & 0 deletions cmd/products.gen.go

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/sirupsen/logrus v1.3.0
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/ucloud/ucloud-sdk-go v0.22.86
github.com/ucloud/ucloud-sdk-go v0.22.87
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150
gopkg.in/yaml.v2 v2.2.2
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3A
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ucloud/ucloud-sdk-go v0.22.86 h1:V8lCn0qhKyGgZYSobmKX5+kUJX5QFqh1SWw1yngqXC8=
github.com/ucloud/ucloud-sdk-go v0.22.86/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
github.com/ucloud/ucloud-sdk-go v0.22.87 h1:IAQBH1bl27+nBlBFo/hx5mMuDOMQHFks7cpOTfgxK3U=
github.com/ucloud/ucloud-sdk-go v0.22.87/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
2 changes: 1 addition & 1 deletion products/udisk/product.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# products/udisk/product.yaml — udisk 产品元数据(归属真源,owner 自治维护)
name: udisk
owners:
- Episkey-G
- pearlinpan
commands:
- udisk
enabled: true
20 changes: 20 additions & 0 deletions products/ufs/internal/ufs/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ufs

import (
"github.com/spf13/cobra"

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

// NewCommand builds the `ufs` root command and mounts the subcommands.
func NewCommand(ctx *cli.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "ufs",
Short: "Manage UFS (UCloud File Storage) volumes",
Long: "Manage UFS (UCloud File Storage) volumes",
}
cmd.AddCommand(newCreate(ctx))
cmd.AddCommand(newDescribe(ctx))
cmd.AddCommand(newDelete(ctx))
return cmd
}
58 changes: 58 additions & 0 deletions products/ufs/internal/ufs/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ufs

import (
"fmt"

"github.com/spf13/cobra"

ufssdk "github.com/ucloud/ucloud-sdk-go/services/ufs"

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

// newCreate ucloud ufs create
func newCreate(ctx *cli.Context) *cobra.Command {
client := cli.NewServiceClient(ctx, ufssdk.NewClient)
req := client.NewCreateUFSVolumeRequest()
cmd := &cobra.Command{
Use: "create",
Short: "Create a UFS volume",
Long: "Create a UFS volume",
Run: func(cmd *cobra.Command, args []string) {
w := ctx.ProgressWriter()
resp, err := client.CreateUFSVolume(req)
if err != nil {
ctx.HandleError(err)
return
}

text := fmt.Sprintf("ufs:%v created", resp.VolumeId)
fmt.Fprintln(w, text)
ctx.EmitResult(cli.OpResultRow{ResourceID: resp.VolumeId, Action: "create", Status: "Created"})
},
}
flags := cmd.Flags()
flags.SortFlags = false
req.VolumeName = flags.String("name", "", "Required. Name of the UFS volume to create")
req.Size = flags.Int("size-gb", 100, "Required. Size of the UFS volume. Unit: GB")
req.StorageType = flags.String("storage-type", "Basic", "Optional. Storage type: 'Basic' (capacity) or 'Advanced' (performance)")
req.ProtocolType = flags.String("protocol-type", "NFS", "Optional. Protocol type: 'NFS' or 'SMB'")
req.ChargeType = flags.String("charge-type", "Dynamic", "Optional. 'Year', pay yearly; 'Month', pay monthly; 'Dynamic', pay hourly")
req.Quantity = flags.Int("quantity", 1, "Optional. The duration of the instance. N years/months")
req.Tag = flags.String("group", "Default", "Optional. Business group")
req.Remark = flags.String("remark", "", "Optional. Remark")

ctx.BindRegion(cmd, req)
ctx.BindZone(cmd, req)
ctx.BindProjectID(cmd, req)

command.SetFlagValues(cmd, "charge-type", "Month", "Year", "Dynamic", "Trial")
command.SetFlagValues(cmd, "storage-type", "Basic", "Advanced")
command.SetFlagValues(cmd, "protocol-type", "NFS", "SMB")

cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("size-gb")

return cmd
}
59 changes: 59 additions & 0 deletions products/ufs/internal/ufs/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ufs

import (
"fmt"

"github.com/spf13/cobra"

ufssdk "github.com/ucloud/ucloud-sdk-go/services/ufs"

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

// newDelete ucloud ufs delete
func newDelete(ctx *cli.Context) *cobra.Command {
var yes *bool
var volumeIDs *[]string
client := cli.NewServiceClient(ctx, ufssdk.NewClient)
req := client.NewRemoveUFSVolumeRequest()
cmd := &cobra.Command{
Use: "delete",
Short: "Delete UFS volume(s)",
Long: "Delete UFS volume(s)",
Run: func(cmd *cobra.Command, args []string) {
ok, err := ctx.Confirm(*yes, "Are you sure to delete UFS volume(s)?")
if err != nil {
ctx.HandleError(err)
return
}
if !ok {
return
}
w := ctx.ProgressWriter()
results := []cli.OpResultRow{}
for _, id := range *volumeIDs {
id := ctx.PickResourceID(id)
req.VolumeId = &id
_, err := client.RemoveUFSVolume(req)
if err != nil {
ctx.HandleError(err)
continue
} else {
fmt.Fprintf(w, "ufs[%s] deleted\n", *req.VolumeId)
results = append(results, cli.OpResultRow{ResourceID: *req.VolumeId, Action: "delete", Status: "Deleted"})
}
}
ctx.EmitResult(results...)
},
}
flags := cmd.Flags()
flags.SortFlags = false
volumeIDs = flags.StringSlice("volume-id", nil, "Required. The Resource ID of UFS volumes to delete")
yes = flags.BoolP("yes", "y", false, "Optional. Do not prompt for confirmation.")

ctx.BindCommonParams(cmd, req)

cmd.MarkFlagRequired("volume-id")

return cmd
}
83 changes: 83 additions & 0 deletions products/ufs/internal/ufs/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ufs

import (
"fmt"

"github.com/spf13/cobra"

ufssdk "github.com/ucloud/ucloud-sdk-go/services/ufs"
"github.com/ucloud/ucloud-sdk-go/ucloud/request"

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

// newDescribe ucloud ufs describe
func newDescribe(ctx *cli.Context) *cobra.Command {
client := cli.NewServiceClient(ctx, ufssdk.NewClient)
req := client.NewDescribeUFSVolume2Request()
cmd := &cobra.Command{
Use: "describe",
Short: "Describe UFS volume(s)",
Long: "Describe UFS volume(s)",
Run: func(cmd *cobra.Command, args []string) {
resp, err := client.DescribeUFSVolume2(req)
if err != nil {
ctx.HandleError(err)
return
}
list := []VolumeRow{}
for _, vol := range resp.DataSet {
row := VolumeRow{
ResourceID: vol.VolumeId,
Name: vol.VolumeName,
Group: vol.Tag,
Size: fmt.Sprintf("%dGB", vol.Size),
UsedSize: fmt.Sprintf("%dGB", vol.UsedSize),
ProtocolType: vol.ProtocolType,
StorageType: vol.StorageType,
MountPoints: fmt.Sprintf("%d/%d", vol.TotalMountPointNum, vol.MaxMountPointNum),
State: vol.IsExpired,
CreationTime: common.FormatDate(vol.CreateTime),
Expiration: common.FormatDate(vol.ExpiredTime),
}
list = append(list, row)
}
ctx.PrintList(list)
},
}
flags := cmd.Flags()
flags.SortFlags = false
req.VolumeId = flags.String("volume-id", "", "Optional. Resource ID of the UFS volume")
req.Limit = flags.Int("limit", 50, "Optional. Limit")
req.Offset = flags.Int("offset", 0, "Optional. Offset")

ctx.BindRegion(cmd, req)
ctx.BindZone(cmd, req)
ctx.BindProjectID(cmd, req)

return cmd
}

// describeUfsByID returns the poller's describe func, closing over ctx so it
// can build an authed ufs client.
func describeUfsByID(ctx *cli.Context) func(volumeID string, commonBase *request.CommonBase) (interface{}, error) {
return func(volumeID string, commonBase *request.CommonBase) (interface{}, error) {
client := cli.NewServiceClient(ctx, ufssdk.NewClient)
req := client.NewDescribeUFSVolume2Request()
if commonBase != nil {
req.CommonBase = *commonBase
}
req.VolumeId = &volumeID
limit := 50
req.Limit = &limit
resp, err := client.DescribeUFSVolume2(req)
if err != nil {
return nil, err
}
if len(resp.DataSet) < 1 {
return nil, nil
}
return &resp.DataSet[0], nil
}
}
16 changes: 16 additions & 0 deletions products/ufs/internal/ufs/rows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ufs

// VolumeRow represents a single row in the ufs volume list output.
type VolumeRow struct {
ResourceID string
Name string
Group string
Size string
UsedSize string
ProtocolType string
StorageType string
MountPoints string
State string
CreationTime string
Expiration string
}
8 changes: 8 additions & 0 deletions products/ufs/internal/ufs/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ufs

// UFS-domain state constants.
const (
VOLUME_CREATING = "Creating"
VOLUME_AVAILABLE = "Available"
VOLUME_FAILED = "Failed"
)
21 changes: 21 additions & 0 deletions products/ufs/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ufs

import (
"github.com/spf13/cobra"

"github.com/ucloud/ucloud-cli/pkg/cli"
internalufs "github.com/ucloud/ucloud-cli/products/ufs/internal/ufs"
)

type product struct{}

// New returns the ufs product (registered via hack/gen-products).
func New() cli.Product { return product{} }

func (product) Metadata() cli.Metadata {
return cli.Metadata{Name: "ufs", Commands: []string{"ufs"}}
}

func (product) NewCommand(ctx *cli.Context) []*cobra.Command {
return []*cobra.Command{internalufs.NewCommand(ctx)}
}
7 changes: 7 additions & 0 deletions products/ufs/product.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# products/ufs/product.yaml — ufs 产品元数据(归属真源,owner 自治维护)
name: ufs
owners:
- pearlinpan
commands:
- ufs
enabled: true
26 changes: 26 additions & 0 deletions products/ufs/testdata/cmdtree.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ucloud ufs use=ufs short=Manage UFS (UCloud File Storage) volumes
ucloud ufs create use=create short=Create a UFS volume
flag=charge-type short= default=Dynamic required=
flag=group short= default=Default required=
flag=name short= default= required=true
flag=project-id short= default= required=
flag=protocol-type short= default=NFS required=
flag=quantity short= default=1 required=
flag=region short= default= required=
flag=remark short= default= required=
flag=size-gb short= default=100 required=true
flag=storage-type short= default=Basic required=
flag=zone short= default= required=
ucloud ufs delete use=delete short=Delete UFS volume(s)
flag=project-id short= default= required=
flag=region short= default= required=
flag=volume-id short= default=[] required=true
flag=yes short=y default=false required=
flag=zone short= default= required=
ucloud ufs describe use=describe short=Describe UFS volume(s)
flag=limit short= default=50 required=
flag=offset short= default=0 required=
flag=project-id short= default= required=
flag=region short= default= required=
flag=volume-id short= default= required=
flag=zone short= default= required=
12 changes: 12 additions & 0 deletions products/ufs/testdata/completion.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ucloud ufs create charge-type static Dynamic,Month,Trial,Year
ucloud ufs create project-id dynamic
ucloud ufs create protocol-type static NFS,SMB
ucloud ufs create region dynamic
ucloud ufs create storage-type static Advanced,Basic
ucloud ufs create zone dynamic
ucloud ufs delete project-id dynamic
ucloud ufs delete region dynamic
ucloud ufs delete zone dynamic
ucloud ufs describe project-id dynamic
ucloud ufs describe region dynamic
ucloud ufs describe zone dynamic
20 changes: 20 additions & 0 deletions products/upfs/internal/upfs/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package upfs

import (
"github.com/spf13/cobra"

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

// NewCommand builds the `upfs` root command and mounts the subcommands.
func NewCommand(ctx *cli.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "upfs",
Short: "Manage UPFS (UCloud Parallel File Storage) volumes",
Long: "Manage UPFS (UCloud Parallel File Storage) volumes",
}
cmd.AddCommand(newCreate(ctx))
cmd.AddCommand(newDescribe(ctx))
cmd.AddCommand(newDelete(ctx))
return cmd
}
Loading
Loading