Class: TerraspacePluginAws::Interfaces::Backend::Bucket

Inherits:
Base
  • Object
show all
Includes:
Secure
Defined in:
lib/terraspace_plugin_aws/interfaces/backend/bucket.rb,
lib/terraspace_plugin_aws/interfaces/backend/bucket/secure.rb,
lib/terraspace_plugin_aws/interfaces/backend/bucket/tagging.rb

Defined Under Namespace

Modules: Secure Classes: Tagging

Instance Method Summary collapse

Methods included from Secure

#secure

Methods inherited from Base

#initialize

Methods included from Logging

#logger

Methods included from Clients

#dynamodb, #s3, #secretsmanager, #ssm, #sts

Constructor Details

This class inherits a constructor from TerraspacePluginAws::Interfaces::Backend::Base

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/terraspace_plugin_aws/interfaces/backend/bucket.rb', line 5

def create
  bucket = @info["bucket"]
  unless bucket # not bucket provided
    logger.error "ERROR: no bucket value provided in your terraform backend config".color(:red)
    exit 1
  end
  if exist?(bucket)
    logger.debug "Bucket already exist: #{bucket}"
    c = TerraspacePluginAws::Interfaces::Config.instance.config
    secure(bucket) if c.s3.secure_existing
    tag(bucket) if c.tag_existing
  else
    logger.info "Creating bucket: #{bucket}"
    s3.create_bucket(bucket: bucket)
    secure(bucket)
    tag(bucket)
  end
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/terraspace_plugin_aws/interfaces/backend/bucket.rb', line 24

def exist?(name)
  s3.head_bucket(bucket: name, use_accelerate_endpoint: false)
  true  # Bucket exist
rescue Aws::S3::Errors::NotFound
  false # Bucket does not exist
rescue Aws::S3::Errors::Forbidden => e
  logger.error "#{e.class}: #{e.message}"
  logger.error "ERROR: Bucket is not available: #{name}".color(:red)
  logger.error "Bucket might be owned by someone else or is on another one of your AWS accounts."
  exit 1
end

#tag(bucket) ⇒ Object



36
37
38
# File 'lib/terraspace_plugin_aws/interfaces/backend/bucket.rb', line 36

def tag(bucket)
  Tagging.new(@info).tag
end