-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathalicloud_oss_bucket.rb
102 lines (81 loc) · 2.37 KB
/
alicloud_oss_bucket.rb
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
require 'alicloud_backend'
require 'alicloud/oss/bucket'
class AliCloudOssBucket < AliCloudResourceBase
name 'alicloud_oss_bucket'
desc 'Verifies settings for an AliCloud OSS Bucket.'
example <<-EXAMPLE
describe alicloud_oss_bucket(bucket_name: 'test-oss-bucket') do
it { should exist }
end
EXAMPLE
attr_reader :region, :bucket_name
def initialize(opts = {})
opts = { bucket_name: opts } if opts.is_a?(String)
super(opts)
validate_parameters(required: %i(bucket_name region))
@bucket_name = opts[:bucket_name]
catch_alicloud_errors do
@bucket = @alicloud.aliyun_oss_client.get_bucket(opts[:bucket_name])
end
end
def exists?
# @bucket object itself will not be nil if the bucket doesn't exist
# need to check some property of the bucket and rescue the Bucket doesn't exist error
@bucket.acl
true
rescue Aliyun::OSS::ServerError
false
end
def public?
return false unless exists?
catch_alicloud_errors do
@bucket_policy_status_public ||= @bucket.acl != 'private'
end
end
def has_access_logging_enabled?
return false unless exists?
catch_alicloud_errors do
@has_access_logging_enabled ||= @bucket.logging.enable == true
end
end
def has_default_encryption_enabled?
return false unless exists?
@has_default_encryption_enabled ||= catch_alicloud_errors do
@has_default_encryption_enabled = [email protected]_algorithm.nil?
rescue Aliyun::OSS::ServerError
false
rescue StandardError => e
fail_resource("Unexpected error thrown: #{e}")
end
end
def has_versioning_enabled?
return false unless exists?
catch_alicloud_errors do
@has_versioning_enabled ||= @bucket.versioning.status == 'Enabled'
end
end
def has_website_enabled?
return false unless exists?
catch_alicloud_errors do
@has_website_enabled ||= @bucket.website.enable == true
end
end
def bucket_lifecycle_rules
return false unless exists?
catch_alicloud_errors do
@bucket_lifecycle_rules ||= @bucket.lifecycle
end
end
def tags
catch_alicloud_errors do
@bucket_custom = @alicloud.alicloud_oss_client_custom.get_bucket(opts[:bucket_name])
end
@bucket_custom.tagging.as_json
end
def resource_id
@bucket ? @bucket.name : @bucket_name
end
def to_s
"OSS Bucket #{@bucket_name}"
end
end