From 14683910e14d6fa469b6535bcc4a614f41e98422 Mon Sep 17 00:00:00 2001 From: jojoliang Date: Mon, 14 Sep 2020 21:56:24 +0800 Subject: [PATCH] add bucket intelligenttiering --- bucket_intelligenttiering.go | 47 ++++++++++++++++++++++ bucket_intelligenttiering_test.go | 76 ++++++++++++++++++++++++++++++++++++ example/bucket/intelligenttiering.go | 64 ++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 bucket_intelligenttiering.go create mode 100644 bucket_intelligenttiering_test.go create mode 100644 example/bucket/intelligenttiering.go diff --git a/bucket_intelligenttiering.go b/bucket_intelligenttiering.go new file mode 100644 index 0000000..bb47d78 --- /dev/null +++ b/bucket_intelligenttiering.go @@ -0,0 +1,47 @@ +package cos + +import ( + "context" + "encoding/xml" + "net/http" +) + +type BucketIntelligentTieringTransition struct { + Days int `xml:"Days,omitempty"` + RequestFrequent int `xml:"RequestFrequent,omitempty"` +} + +type BucketPutIntelligentTieringOptions struct { + XMLName xml.Name `xml:"IntelligentTieringConfiguration"` + Status string `xml:"Status,omitempty"` + Transition *BucketIntelligentTieringTransition `xml:"Transition,omitempty"` +} + +type BucketGetIntelligentTieringResult BucketPutIntelligentTieringOptions + +func (s *BucketService) PutIntelligentTiering(ctx context.Context, opt *BucketPutIntelligentTieringOptions) (*Response, error) { + if opt != nil && opt.Transition != nil { + opt.Transition.RequestFrequent = 1 + } + sendOpt := sendOptions{ + baseURL: s.client.BaseURL.BucketURL, + uri: "/?intelligenttiering", + method: http.MethodPut, + body: opt, + } + resp, err := s.client.send(ctx, &sendOpt) + return resp, err +} + +func (s *BucketService) GetIntelligentTiering(ctx context.Context) (*BucketGetIntelligentTieringResult, *Response, error) { + var res BucketGetIntelligentTieringResult + sendOpt := sendOptions{ + baseURL: s.client.BaseURL.BucketURL, + uri: "/?intelligenttiering", + method: http.MethodGet, + result: &res, + } + resp, err := s.client.send(ctx, &sendOpt) + return &res, resp, err + +} diff --git a/bucket_intelligenttiering_test.go b/bucket_intelligenttiering_test.go new file mode 100644 index 0000000..04f4791 --- /dev/null +++ b/bucket_intelligenttiering_test.go @@ -0,0 +1,76 @@ +package cos + +import ( + "context" + "encoding/xml" + "fmt" + "net/http" + "reflect" + "testing" +) + +func TestBucketService_PutIntelligentTiering(t *testing.T) { + setup() + defer teardown() + opt := &BucketPutIntelligentTieringOptions{ + Status: "Enabled", + Transition: &BucketIntelligentTieringTransition{ + Days: 30, + }, + } + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPut) + vs := values{ + "intelligenttiering": "", + } + testFormValues(t, r, vs) + + body := &BucketPutIntelligentTieringOptions{} + xml.NewDecoder(r.Body).Decode(body) + want := opt + want.XMLName = xml.Name{Local: "IntelligentTieringConfiguration"} + if !reflect.DeepEqual(want, body) { + t.Fatalf("Bucket.PutIntelligentTiering request\n body: %+v\n, want %+v\n", body, want) + } + }) + + _, err := client.Bucket.PutIntelligentTiering(context.Background(), opt) + if err != nil { + t.Fatalf("Bucket.PutIntelligentTiering failed, error: %v", err) + } +} + +func TestBucketService_GetIntelligentTiering(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + vs := values{ + "intelligenttiering": "", + } + testFormValues(t, r, vs) + + fmt.Fprint(w, ` + Enabled + + 30 + + `) + }) + res, _, err := client.Bucket.GetIntelligentTiering(context.Background()) + if err != nil { + t.Fatalf("Bucket.GetIntelligentTiering failed, error: %v", err) + } + want := &BucketGetIntelligentTieringResult{ + XMLName: xml.Name{Local: "IntelligentTieringConfiguration"}, + Status: "Enabled", + Transition: &BucketIntelligentTieringTransition{ + Days: 30, + }, + } + + if !reflect.DeepEqual(res, want) { + t.Errorf("Bucket.GetIntelligentTiering returned\n%+v, want\n%+v", res, want) + } +} diff --git a/example/bucket/intelligenttiering.go b/example/bucket/intelligenttiering.go new file mode 100644 index 0000000..0256843 --- /dev/null +++ b/example/bucket/intelligenttiering.go @@ -0,0 +1,64 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "net/url" + "os" + + "github.com/tencentyun/cos-go-sdk-v5" + "github.com/tencentyun/cos-go-sdk-v5/debug" +) + +func log_status(err error) { + if err == nil { + return + } + if cos.IsNotFoundError(err) { + // WARN + fmt.Println("Resource is not existed") + } else if e, ok := cos.IsCOSError(err); ok { + fmt.Printf("Code: %v\n", e.Code) + fmt.Printf("Message: %v\n", e.Message) + fmt.Printf("Resource: %v\n", e.Resource) + fmt.Printf("RequestId: %v\n", e.RequestID) + // ERROR + } else { + fmt.Println(err) + // ERROR + } +} + +func main() { + u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") + b := &cos.BaseURL{ + BucketURL: u, + } + c := cos.NewClient(b, &http.Client{ + Transport: &cos.AuthorizationTransport{ + SecretID: os.Getenv("COS_SECRETID"), + SecretKey: os.Getenv("COS_SECRETKEY"), + Transport: &debug.DebugRequestTransport{ + RequestHeader: true, + RequestBody: false, + ResponseHeader: true, + ResponseBody: false, + }, + }, + }) + + opt := &cos.BucketPutIntelligentTieringOptions { + Status: "Enabled", + Transition: &cos.BucketIntelligentTieringTransition { + Days: 30, + }, + } + _, err := c.Bucket.PutIntelligentTiering(context.Background(), opt) + log_status(err) + res, _, err := c.Bucket.GetIntelligentTiering(context.Background()) + log_status(err) + fmt.Printf("%+v\n", res) + fmt.Printf("%+v\n", res.Status) + fmt.Printf("%+v\n", res.Transition.Days) +}