Skip to content

Commit

Permalink
feat: add huawei obs ls function
Browse files Browse the repository at this point in the history
  • Loading branch information
teamssix committed Dec 13, 2022
1 parent 31cc766 commit e783772
Show file tree
Hide file tree
Showing 15 changed files with 439 additions and 48 deletions.
16 changes: 16 additions & 0 deletions cmd/huawei/huawei.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package huawei

import (
"github.com/spf13/cobra"
"github.com/teamssix/cf/cmd"
)

func init() {
cmd.RootCmd.AddCommand(huaweiCmd)
}

var huaweiCmd = &cobra.Command{
Use: "huawei",
Short: "执行与华为云相关的操作 (Perform Huawei Cloud related operations)",
Long: "执行与华为云相关的操作 (Perform Huawei Cloud related operations)",
}
39 changes: 39 additions & 0 deletions cmd/huawei/obs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package huawei

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/teamssix/cf/pkg/cloud/huawei/huaweiobs"
)

var (
obsLsRegion string
obsLsFlushCache bool
obsLsObjectNumber string
)

func init() {
huaweiCmd.AddCommand(obsCmd)
obsCmd.AddCommand(obsLsCmd)

obsLsCmd.Flags().StringVarP(&obsLsObjectNumber, "number", "n", "all", "指定列出对象的数量 (Specify the number of objects to list)")
obsLsCmd.Flags().StringVarP(&obsLsRegion, "region", "r", "all", "指定区域 ID (Specify region ID)")
obsLsCmd.Flags().BoolVar(&obsLsFlushCache, "flushCache", false, "刷新缓存,不使用缓存数据 (Refresh the cache without using cached data)")

}

var obsCmd = &cobra.Command{
Use: "obs",
Short: "执行与对象存储相关的操作 (Perform obs-related operations)",
Long: "执行与对象存储相关的操作 (Perform obs-related operations)",
}

var obsLsCmd = &cobra.Command{
Use: "ls",
Short: "列出所有的存储桶 (List all buckets)",
Long: "列出所有的存储桶 (List all buckets)",
Run: func(cmd *cobra.Command, args []string) {
log.Debugf("obsLsRegion: %s, obsLsFlushCache: %v", obsLsRegion, obsLsFlushCache)
huaweiobs.PrintBucketsList(obsLsRegion, obsLsFlushCache, obsLsObjectNumber)
},
}
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ require (
github.com/google/flatbuffers v1.12.1 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.22.11+incompatible // indirect
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.15 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand All @@ -66,7 +68,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.opencensus.io v0.22.5 // indirect
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
Expand All @@ -75,7 +77,7 @@ require (
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
golang.org/x/tools v0.1.11 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.36.0 // indirect
modernc.org/ccgo/v3 v3.16.8 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gookit/color v1.5.1 h1:Vjg2VEcdHpwq+oY63s/ksHrgJYCTo0bwWvmmYWdE9fQ=
Expand All @@ -86,6 +87,10 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.22.11+incompatible h1:bSww59mgbqFRGCRvlvfQutsptE3lRjNiU5C0YNT/bWw=
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.22.11+incompatible/go.mod h1:l7VUhRbTKCzdOacdT4oWCwATKyvZqUOlOqr0Ous3k4s=
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.15 h1:CpgaMg4GMIEl5TU+ts65j4QvXSgHvFaaNDnFVaUEr8s=
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.15/go.mod h1:QpZ96CRqyqd5fEODVmnzDNp3IWi5W95BFmWz1nfkq+s=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/ivanpirog/coloredcobra v1.0.1 h1:aURSdEmlR90/tSiWS0dMjdwOvCVUeYLfltLfbgNxrN4=
Expand All @@ -102,6 +107,7 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
Expand Down Expand Up @@ -134,10 +140,12 @@ github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2Em
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand Down Expand Up @@ -227,6 +235,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
Expand Down Expand Up @@ -337,6 +347,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.66.6 h1:LATuAqN/shcYAOkv3wl2L4rkaKqkcgTBQjOyYDvcPKI=
gopkg.in/ini.v1 v1.66.6/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/teamssix/cf/cmd"
_ "github.com/teamssix/cf/cmd/alibaba"
_ "github.com/teamssix/cf/cmd/aws"
_ "github.com/teamssix/cf/cmd/huawei"
_ "github.com/teamssix/cf/cmd/tencent"
)

Expand Down
26 changes: 12 additions & 14 deletions pkg/cloud/aws/awss3/s3ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,16 @@ func FindBucketAcl(bucket string, region string) string {
result, err := svc.GetBucketAcl(input)
errutil.HandleErr(err)
for _, v := range result.Grants {
if *v.Grantee.Type == "Group" {
if *v.Grantee.URI == "http://acs.amazonaws.com/groups/global/AllUsers" {
switch *v.Permission {
case "READ":
read = 1
case "WRITE":
write = 2
case "READ_ACP":
readACP = 4
case "WRITE_ACP":
writeACP = 8
}
if *v.Grantee.Type == "Group" && *v.Grantee.URI == "http://acs.amazonaws.com/groups/global/AllUsers" {
switch *v.Permission {
case "READ":
read = 1
case "WRITE":
write = 2
case "READ_ACP":
readACP = 4
case "WRITE_ACP":
writeACP = 8
}
}
}
Expand Down Expand Up @@ -132,7 +130,7 @@ func PrintBucketsListRealTime(region string, s3LsObjectNumber string) {
dataLen int
)
buckets := ListBuckets()
log.Infof("获取到 %d 条 S3 Bucket 信息 (Obtained %d pieces of S3 Bucket information)", len(buckets), len(buckets))
log.Infof("在全部区域下获取到 %d 条 S3 Bucket 信息 (Find %d S3 Bucket under all areas)", len(buckets), len(buckets))
var data = make([][]string, len(buckets))
for i, o := range buckets {
SN := strconv.Itoa(i + 1)
Expand All @@ -154,7 +152,7 @@ func PrintBucketsListRealTime(region string, s3LsObjectNumber string) {
if dataLen == 0 {
log.Info("没发现存储桶 (No Buckets Found)")
} else {
Caption := "AWS 资源 (AWS resources)"
Caption := "S3 资源 (S3 resources)"
cloud.PrintTable(td, Caption)
cmdutil.WriteCacheFile(td, "aws", "s3", "all", "all")
util.WriteTimestamp(TimestampType)
Expand Down
37 changes: 37 additions & 0 deletions pkg/cloud/huawei/huaweiobs/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package huaweiobs

import (
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
log "github.com/sirupsen/logrus"
"github.com/teamssix/cf/pkg/util/cmdutil"
"github.com/teamssix/cf/pkg/util/errutil"
"os"
)

func obsClient(region string) *obs.ObsClient {
var (
obsClient *obs.ObsClient
err error
)
config := cmdutil.GetConfig("huawei")
if config.AccessKeyId == "" {
log.Warnln("需要先配置访问密钥 (Access Key need to be configured first)")
os.Exit(0)
return nil
} else {
if region == "all" {
region = "cn-north-1"
}
if config.STSToken == "" {
obsClient, err = obs.New(config.AccessKeyId, config.AccessKeySecret, "https://obs."+region+".myhuaweicloud.com")
} else {
obsClient, err = obs.New(config.AccessKeyId, config.AccessKeySecret, "https://obs."+region+".myhuaweicloud.com", obs.WithSecurityToken(config.STSToken))
}
if err == nil {
log.Traceln("obs Client 连接成功 (obs Client connection successful)")
} else {
errutil.HandleErr(err)
}
return obsClient
}
}
Loading

0 comments on commit e783772

Please sign in to comment.