Skip to content
This repository was archived by the owner on Jun 5, 2020. It is now read-only.

Commit ddd5fb8

Browse files
committed
Trying to implement ability to set lifecycle_configuration.
1 parent c9a1cc8 commit ddd5fb8

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

lib/puppet/provider/s3_bucket/v2.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ def self.instances
2424
Puppet.debug("An error occurred reading the policy on S3 bucket #{s3_bucket.name}: " + e.message)
2525
end
2626

27+
begin
28+
results = s3_client.get_bucket_lifecycle_configuration({bucket: s3_bucket.name})
29+
data[:lifecycle_configuration] = { lifecycle_configuration: results.to_h }
30+
rescue Exception => e
31+
Puppet.debug("An error occurred reading the lifecycle configuration on S3 bucket #{s3_bucket.name}: " + e.message)
32+
end
33+
2734
new(data)
2835
end
2936
bucket_list.reject {|b| b.nil? }
@@ -61,4 +68,12 @@ def policy=(value)
6168
})
6269
end
6370

71+
def lifecycle_configuration=(value)
72+
Puppet.debug('Replacing bucket lifecycle configuration')
73+
s3_client.put_bucket_lifecycle_configuration({
74+
bucket: @property_hash[:name],
75+
lifecycle_configuration: value[:lifecycle_configuration]
76+
})
77+
end
78+
6479
end

lib/puppet/type/s3_bucket.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,36 @@
1717
newproperty(:policy) do
1818
desc 'The policy document JSON string to apply'
1919
validate do |value|
20-
fail Puppet::Error, 'Policy documents must be JSON strings' unless value.is_a? String
20+
fail Puppet::Error, 'Policy documents must be JSON strings' unless is_valid_json?(value)
2121
end
2222

2323
munge do |value|
24-
begin
25-
data = JSON.parse(value)
26-
JSON.pretty_generate(data)
27-
rescue
28-
fail('Policy string is not valid JSON')
29-
end
24+
JSON.pretty_generate(JSON.parse(value))
25+
rescue
26+
fail('Policy string is not valid JSON')
3027
end
3128
end
3229

30+
newproperty(:lifecycle_configuration) do
31+
desc 'The lifecycle configuration document JSON string to apply'
32+
validate do |value|
33+
fail Puppet::Error, 'Lifecycle configuration documents must be JSON strings' unless is_valid_json?(value)
34+
end
35+
36+
munge do |value|
37+
JSON.pretty_generate(JSON.parse(value))
38+
rescue
39+
fail('Lifecycle configuration string is not valid JSON')
40+
end
41+
end
42+
43+
private
44+
45+
def is_valid_json?(string)
46+
!!JSON.parse(string)
47+
rescue JSON::ParserError => _e
48+
false
49+
end
50+
3351
end
3452

0 commit comments

Comments
 (0)