Class: CfnGuardian::S3

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cfnguardian/s3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

colors, included, logger, #logger, logger=

Constructor Details

#initialize(bucket, path = '') ⇒ S3

Returns a new instance of S3.



9
10
11
12
# File 'lib/cfnguardian/s3.rb', line 9

def initialize(bucket,path='')
  @bucket = set_bucket_name(bucket)
  @path = path
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



7
8
9
# File 'lib/cfnguardian/s3.rb', line 7

def bucket
  @bucket
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/cfnguardian/s3.rb', line 7

def path
  @path
end

Instance Method Details

#create_bucket_if_not_existsObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cfnguardian/s3.rb', line 23

def create_bucket_if_not_exists()
  s3 = Aws::S3::Client.new
  begin
    s3.head_bucket(bucket: @bucket)
    logger.info("Found bucket #{@bucket}")
  rescue
    logger.info("Creating bucket #{@bucket}")
    s3.create_bucket(bucket: @bucket)
  end
  return bucket
end

#set_bucket_name(bucket) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/cfnguardian/s3.rb', line 14

def set_bucket_name(bucket)
  if bucket.nil?
    sts = Aws::STS::Client.new
     = sts.get_caller_identity().
    return "#{}.#{Aws.config[:region]}.guardian.templates"
  end
  return bucket
end