class Cumulus::S3::BucketConfig

Public: An object representing configuration for an S3 bucket

Attributes

cors[R]
default_encryption[R]
grants[R]
lifecycle[R]
logging[R]
name[R]
notifications[R]
policy[R]
region[R]
replication[R]
tags[R]
versioning[R]
website[R]

Public Class Methods

new(name, json = nil) click to toggle source

Public: Constructor

name - the name of the bucket json - a hash containing the JSON configuration for the bucket

# File lib/s3/models/BucketConfig.rb, line 81
def initialize(name, json = nil)
  @name = name
  if json
    @region = json["region"]
    @tags = json["tags"] || {}
    if json["permissions"]["cors"]
      @cors = Loader.cors_policy(
        json["permissions"]["cors"]["template"],
        json["permissions"]["cors"]["vars"] || {}
      )
    end
    if json["permissions"]["policy"]
      @policy = Loader.bucket_policy(
        json["permissions"]["policy"]["template"],
        json["permissions"]["policy"]["vars"] || {}
      )
    end
    if json["permissions"]["grants"]
      @grants = Hash[json["permissions"]["grants"].map do |g|
        [g["name"], GrantConfig.new(g)]
      end]
    end
    if json["default_encryption"]
      @default_encryption = DefaultEncryptionConfig.new(json["default_encryption"])
    end
    @website = if json["website"] then WebsiteConfig.new(json["website"]) end
    @logging = if json["logging"] then LoggingConfig.new(json["logging"]) end
    @notifications = Hash[(json["notifications"] || []).map { |n| [n["name"], NotificationConfig.new(n)] }]
    @lifecycle = Hash[(json["lifecycle"] || []).map { |l| [l["name"], LifecycleConfig.new(l)] }]
    @versioning = json["versioning"] || false
    @replication = if json["replication"] then ReplicationConfig.new(json["replication"]) end
  end
end

Public Instance Methods

diff(aws) click to toggle source

Public: Produce an array of differences between this local configuration and the configuration in AWS

aws - the AWS resource

Returns an array of the BucketDiffs that were found

# File lib/s3/models/BucketConfig.rb, line 196
def diff(aws)
  diffs = []

  if @tags != Hash[aws.tagging.safe_tags.map { |t| [t.key, t.value] }]
    diffs << BucketDiff.new(BucketChange::TAGS, aws, self)
  end
  if @policy != aws.policy.policy_string and !(@policy.nil? and aws.policy.policy_string == "")
    diffs << BucketDiff.new(BucketChange::POLICY, aws, self)
  end
  if @cors != aws.cors.rules and !(@cors.nil? and aws.cors.rules == [])
    diffs << BucketDiff.new(BucketChange::CORS, aws, self)
  end
  if @website != aws.website.to_cumulus
    diffs << BucketDiff.new(BucketChange::WEBSITE, aws, self)
  end
  if @logging != aws.logging.to_cumulus
    diffs << BucketDiff.new(BucketChange::LOGGING, aws, self)
  end
  if @versioning != aws.versioning.enabled
    diffs << BucketDiff.new(BucketChange::VERSIONING, aws, self)
  end

  grants_diffs = diff_grants(@grants, aws.acl.to_cumulus)
  if !grants_diffs.empty?
    diffs << BucketDiff.grant_changes(grants_diffs, self)
  end

  notification_diffs = diff_notifications(@notifications, aws.notification.to_cumulus)
  if !notification_diffs.empty?
    diffs << BucketDiff.notification_changes(notification_diffs, self)
  end

  lifecycle_diffs = diff_lifecycle(@lifecycle, aws.lifecycle.to_cumulus)
  if !lifecycle_diffs.empty?
    diffs << BucketDiff.lifecycle_changes(lifecycle_diffs, self)
  end

  aws_replication = aws.replication
  if aws_replication then aws_replication = aws_replication.to_cumulus end
  replication_diffs = diff_replication(@replication, aws_replication)
  if !replication_diffs.empty?
    diffs << BucketDiff.replication_changes(replication_diffs, self)
  end

  aws_default_encryption = aws.default_encryption
  if aws_default_encryption then aws_default_encryption = aws_default_encryption.to_cumulus end
  default_encryption_diffs = diff_encryption(@default_encryption, aws_default_encryption)
  if !default_encryption_diffs.empty?
    diffs << BucketDiff.default_encryption_changes(default_encryption_diffs, self)
  end

  diffs
end
populate!(aws, cors, policies) click to toggle source

Public: Populate this BucketConfig from the values in an AWS bucket.

aws - the aws resource cors - a hash of the names of cors policies to the string value of those policies policies - a hash of the names of policies to the string value of those policies

Returns the key names of the new policy or cors policy so they can be written to file immediately

# File lib/s3/models/BucketConfig.rb, line 123
def populate!(aws, cors, policies)
  @region = aws.location
  @grants = aws.acl.to_cumulus
  @website = aws.website.to_cumulus
  @logging = aws.logging.to_cumulus
  @notifications = aws.notification.to_cumulus
  @lifecycle = aws.lifecycle.to_cumulus
  @versioning = aws.versioning.enabled
  @replication = aws.replication.to_cumulus rescue nil
  @tags = Hash[aws.tagging.safe_tags.map { |t| [t.key, t.value] }]
  default_encryption = aws.default_encryption
  if default_encryption
    @default_encryption = default_encryption.to_cumulus
  end

  policy = aws.policy.policy_string
  if policy and policy != ""
    policy = JSON.pretty_generate(JSON.parse(policy))
    if policies.has_value? policy
      @policy_name = policies.key(policy)
    else
      @policy_name = "#{@name}-policy"
      policies[@policy_name] = policy
      @new_policy_key = @policy_name
    end
  end

  cors_string = JSON.pretty_generate(aws.cors.rules.map(&:to_h))
  if cors_string and !aws.cors.rules.empty?
    if cors.has_value? cors_string
      @cors_name = cors.key(cors_string)
    else
      @cors_name = "#{@name}-cors"
      cors[@cors_name] = cors_string
      @new_cors_key = @cors_name
    end
  end

  return @new_policy_key, @new_cors_key
end
pretty_json() click to toggle source

Public: Produce a pretty JSON version of this BucketConfig.

Returns the pretty JSON string.

# File lib/s3/models/BucketConfig.rb, line 167
def pretty_json
  JSON.pretty_generate({
    region: @region,
    permissions: {
      policy: if @policy_name then {
        template: @policy_name,
      } end,
      cors: if @cors_name then {
        template: @cors_name,
      } end,
      grants: @grants.values.map(&:to_h)
    }.reject { |k, v| v.nil? },
    website: if @website then @website.to_h end,
    logging: if @logging then @logging.to_h end,
    notifications: if !@notifications.empty? then @notifications.values.map(&:to_h) end,
    lifecycle: if !@lifecycle.empty? then @lifecycle.values.map(&:to_h) end,
    versioning: @versioning,
    replication: if @replication then @replication.to_h end,
    default_encryption: if @default_encryption then @default_encryption end,
    tags: @tags,
  }.reject { |k, v| v.nil? })
end

Private Instance Methods

diff_configs(local, aws, options) click to toggle source

Internal: Determine changes in sub configurations.

local - the local configurations (hash from name to config) aws - the aws configurations (hash from name to config) options - a hash that contains the following operations to run

unmanaged - a function that creates the unmanaged diff
added     - a function that creates the added diff

Returns an array of diffs representing the differences between local and AWS configuration

# File lib/s3/models/BucketConfig.rb, line 345
def diff_configs(local, aws, options)
  diffs = []

  diffs << aws.reject { |k, v| local.include?(k) }.map { |k, v| options[:unmanaged].call(v) }
  local.each do |k, v|
    if aws.include?(k)
      if v != aws[k]
        diffs << v.diff(aws[k])
      end
    else
      diffs << options[:added].call(v)
    end
  end

  diffs.flatten
end
diff_encryption(local, aws) click to toggle source

Internal: Determine changes in default encryption.

local - the local default encryption configuration aws - the aws default encryption configuration

Returns an array of DefaultEncryptionDiffs representing the differences between local and AWS configuration.

# File lib/s3/models/BucketConfig.rb, line 322
def diff_encryption(local, aws)
  diffs = []
  if local and aws
    diffs << local.diff(aws)
  elsif local
    diffs << DefaultEncryptionDiff.added(local)
  elsif aws
    diffs << DefaultEncryptionDiff.unmanaged(aws)
  end

  diffs.flatten
end
diff_grants(local, aws) click to toggle source

Internal: Determine changes in grants.

local - the grants defined locally (hash from name to config) aws - the grants defined in aws (hash from name to config)

Returns an array of GrantDiffs represeting the differences between local AWS configuration

# File lib/s3/models/BucketConfig.rb, line 259
def diff_grants(local, aws)
  diff_configs(local, aws, {
    unmanaged: GrantDiff.method(:unmanaged),
    added: GrantDiff.method(:added)
  })
end
diff_lifecycle(local, aws) click to toggle source

Internal: Determine changes in lifecycle rules.

local - the lifecycle rules defined locally (hash from name to config) aws - the lifecycle rules defined in aws (hash from name to config)

Returns an array of LifecycleDiffs representing the differences between local and AWS configuration.

# File lib/s3/models/BucketConfig.rb, line 287
def diff_lifecycle(local, aws)
  diff_configs(local, aws, {
    unmanaged: LifecycleDiff.method(:unmanaged),
    added: LifecycleDiff.method(:added)
  })
end
diff_notifications(local, aws) click to toggle source

Internal: Determine changes in notifications.

local - the notifications defined locally (hash from name to config) aws - the notifications defined in aws (hash from name to config)

Returns an array of NotificationDiffs representing the differences between local and AWS configuration

# File lib/s3/models/BucketConfig.rb, line 273
def diff_notifications(local, aws)
  diff_configs(local, aws, {
    unmanaged: NotificationDiff.method(:unmanaged),
    added: NotificationDiff.method(:added)
  })
end
diff_replication(local, aws) click to toggle source

Internal: Determine changes in replication.

local - the local replication configuration aws - the aws replication configuration

Returns an array of ReplicationDiffs representing the differences between local and AWS configuration.

# File lib/s3/models/BucketConfig.rb, line 301
def diff_replication(local, aws)
  diffs = []

  if local and aws
    diffs << local.diff(aws)
  elsif local
    diffs << ReplicationDiff.added(local)
  elsif aws
    diffs << ReplicationDiff.unmanaged(local)
  end

  diffs.flatten
end