/
service_alicloud_oss.go
104 lines (91 loc) · 2.93 KB
/
service_alicloud_oss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package alicloud
import (
"strconv"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
)
// OssService *connectivity.AliyunClient
type OssService struct {
client *connectivity.AliyunClient
}
func (s *OssService) DescribeOssBucket(id string) (response oss.GetBucketInfoResult, err error) {
request := map[string]string{"bucketName": id}
var requestInfo *oss.Client
raw, err := s.client.WithOssClient(func(ossClient *oss.Client) (interface{}, error) {
requestInfo = ossClient
return ossClient.GetBucketInfo(request["bucketName"])
})
if err != nil {
if ossNotFoundError(err) {
return response, WrapErrorf(err, NotFoundMsg, AliyunOssGoSdk)
}
return response, WrapErrorf(err, DefaultErrorMsg, id, "GetBucketInfo", AliyunOssGoSdk)
}
addDebug("GetBucketInfo", raw, requestInfo, request)
response, _ = raw.(oss.GetBucketInfoResult)
return
}
func (s *OssService) WaitForOssBucket(id string, status Status, timeout int) error {
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
for {
object, err := s.DescribeOssBucket(id)
if err != nil {
if NotFoundError(err) {
if status == Deleted {
return nil
}
// for delete bucket replication
} else if status == Deleted && IsExpectedErrors(err, []string{"AccessDenied"}) {
return nil
} else {
return WrapError(err)
}
}
if object.BucketInfo.Name != "" && status != Deleted {
return nil
}
if time.Now().After(deadline) {
return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, object.BucketInfo.Name, status, ProviderERROR)
}
}
}
func (s *OssService) WaitForOssBucketObject(bucket *oss.Bucket, id string, status Status, timeout int) error {
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
for {
exist, err := bucket.IsObjectExist(id)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, id, "IsObjectExist", AliyunOssGoSdk)
}
addDebug("IsObjectExist", exist)
if !exist {
return nil
}
if time.Now().After(deadline) {
return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, strconv.FormatBool(exist), status, ProviderERROR)
}
}
}
func (s *OssService) DescribeOssBucketReplication(id string) (response string, err error) {
parts, err := ParseResourceId(id, 2)
if err != nil {
return response, WrapError(err)
}
bucket := parts[0]
ruleId := parts[1]
request := map[string]string{"bucketName": bucket, "ruleId": ruleId}
var requestInfo *oss.Client
raw, err := s.client.WithOssClient(func(ossClient *oss.Client) (interface{}, error) {
requestInfo = ossClient
return ossClient.GetBucketReplication(bucket)
})
if err != nil {
if ossNotFoundError(err) {
return response, WrapErrorf(err, NotFoundMsg, AliyunOssGoSdk)
}
return response, WrapErrorf(err, DefaultErrorMsg, id, "GetBucketReplication", AliyunOssGoSdk)
}
addDebug("GetBucketReplication", raw, requestInfo, request)
response, _ = raw.(string)
return
}