Class: BuildCloud::S3Bucket

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/build-cloud/s3bucket.rb

Constant Summary collapse

@@objects =
[]

Instance Method Summary collapse

Methods included from Component

included

Constructor Details

#initialize(fog_interfaces, log, options = {}) ⇒ S3Bucket

Returns a new instance of S3Bucket.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/build-cloud/s3bucket.rb', line 7

def initialize ( fog_interfaces, log, options = {} )

    @s3      = fog_interfaces[:s3]
    @log     = log
    @options = options

    @log.debug( options.inspect )

    required_options(:key, :location)

end

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/build-cloud/s3bucket.rb', line 19

def create
    
    policy = @options.delete(:policy)
    
    unless exists?

        @log.info( "Creating new S3 bucket #{@options[:key]}" )

        bucket = @s3.directories.new( @options )
        bucket.save

        @log.debug( bucket.inspect )
    end
    
    rationalise_policies( policy )

end

#deleteObject



43
44
45
46
47
48
49
50
51
# File 'lib/build-cloud/s3bucket.rb', line 43

def delete

    return unless exists?

    @log.info( "Deleting S3 bucket #{@options[:key]}" )

    fog_object.destroy

end

#rationalise_policies(policy) ⇒ Object



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
# File 'lib/build-cloud/s3bucket.rb', line 53

def rationalise_policies( policy )

    policy = JSON.parse(policy) unless policy.nil?
    @log.debug("Policy inspect #{policy.inspect}")
    
    begin
        @log.debug("Inspect #{@s3.get_bucket_policy(fog_object.key)}")
        current_policy = @s3.get_bucket_policy(fog_object.key)
    rescue Excon::Errors::NotFound
        current_policy = nil
    end

    @log.debug("Current Policy inspect #{current_policy.inspect}")
    
    if policy.nil? and current_policy.nil?
        return
    elsif policy.nil? and current_policy.any?
        @log.info("Existing policy here, deleting it")
        @s3.delete_bucket_policy(fog_object.key)
    elsif policy != current_policy
        @log.info( "For bucket #{fog_object.key} adding/updating policy #{p}" )
        @s3.put_bucket_policy( fog_object.key, policy )
    end
    
end

#readObject Also known as: fog_object



37
38
39
# File 'lib/build-cloud/s3bucket.rb', line 37

def read
    @s3.directories.select { |d| d.key == @options[:key] }.first
end