class Cumulus::CloudFront::DistributionConfig

Public: An object representing configuration for a distribution

Attributes

aliases[R]
cache_behaviors[R]
comment[R]
default_cache_behavior[R]
enabled[R]
id[RW]
name[R]
origins[R]

Public Class Methods

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

Public: Constructor

json - a hash containing the JSON configuration for the distribution

# File lib/cloudfront/models/DistributionConfig.rb, line 24
def initialize(name, json = nil)
  @name = name
  if !json.nil?
    @id = json["id"]
    @aliases = json["aliases"] || []
    @origins = json["origins"].map { |o| OriginConfig.new(o) }
    @default_cache_behavior = CacheBehaviorConfig.new(json["default-cache-behavior"], true)
    @cache_behaviors = (json["cache-behaviors"] || []).map { |cb| CacheBehaviorConfig.new(cb) }
    @comment = json["comment"]
    @enabled = json["enabled"]
  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 DistributionDiffs that were found

# File lib/cloudfront/models/DistributionConfig.rb, line 78
def diff(aws)
  diffs = []

  added_aliases = (@aliases - aws.aliases.items)
  removed_aliases = aws.aliases.items - @aliases
  if !added_aliases.empty? or !removed_aliases.empty?
    diffs << DistributionDiff.aliases(added_aliases, removed_aliases, self)
  end

  origin_diffs = diff_origins(aws.origins.items)
  if !origin_diffs.empty?
    diffs << DistributionDiff.origins(origin_diffs, self)
  end

  default_cache_diffs = @default_cache_behavior.diff(aws.default_cache_behavior)
  if !default_cache_diffs.empty?
    diffs << DistributionDiff.default_cache(default_cache_diffs, self)
  end

  diffs << diff_caches(aws)

  if @comment != aws.comment
    diffs << DistributionDiff.new(DistributionChange::COMMENT, aws, self)
  end

  if @enabled != aws.enabled
    diffs << DistributionDiff.new(DistributionChange::ENABLED, aws, self)
  end

  diffs.flatten
end
populate!(id, aws) click to toggle source
# File lib/cloudfront/models/DistributionConfig.rb, line 37
def populate!(id, aws)
  @id = id
  @name = id
  @aliases = aws.aliases.items
  @origins = aws.origins.items.map do |origin|
    config = OriginConfig.new()
    config.populate!(origin)
    config
  end
  @default_cache_behavior = CacheBehaviorConfig.new()
  @default_cache_behavior.populate!(aws.default_cache_behavior, true)
  @cache_behaviors = aws.cache_behaviors.items.map do |cache_behavior|
    config = CacheBehaviorConfig.new()
    config.populate!(cache_behavior)
    config
  end
  @comment = aws.comment
  @enabled = aws.enabled
end
pretty_json() click to toggle source

Public: Get the config as a prettified JSON string.

Returns the JSON string

# File lib/cloudfront/models/DistributionConfig.rb, line 60
def pretty_json
  JSON.pretty_generate({
    "id" => @id,
    "aliases" => @aliases,
    "origins" => @origins.map(&:to_local),
    "default-cache-behavior" => @default_cache_behavior.to_local,
    "cache-behaviors" => @cache_behaviors.map(&:to_local),
    "comment" => @comment,
    "enabled" => @enabled,
  })
end

Private Instance Methods

diff_caches(aws) click to toggle source

Internal: Produce an array of differences between local cache behaviors and aws cache behaviors

aws - the AWS config

Returns an array of CacheBehaviorDiff

# File lib/cloudfront/models/DistributionConfig.rb, line 147
def diff_caches(aws)
  removed = []
  added = []
  changed = Hash.new

  aws_cache_behaviors = if aws.cache_behaviors.nil? then [] else aws.cache_behaviors.items end

  aws = Hash[aws_cache_behaviors.map { |c| ["#{c.target_origin_id}/#{c.path_pattern}", c]}]
  local = Hash[@cache_behaviors.map { |c| ["#{c.target_origin_id}/#{c.path_pattern}", c]}]

  # find cache behaviors that are not configured locally
  aws.each do |cache_id, cache|
    if !local.include?(cache_id)
      removed << CacheBehaviorDiff.unmanaged(cache)
    end
  end

  local.each do |cache_id, cache|
    if !aws.include?(cache_id)
      added << CacheBehaviorDiff.added(cache)
    else
      diffs = cache.diff(aws[cache_id])
      changed[cache_id] = diffs if !diffs.empty?
    end
  end

  if !removed.empty? or !added.empty? or !changed.empty?
    DistributionDiff.caches(removed, added, changed, self)
  else
    []
  end

end
diff_origins(aws_origins) click to toggle source

Internal: Produce an array of differences between the local origins and the aws origins

aws_origins - the AWS origins from a cloudfront config

Returns an array of OriginDiffs that were found

# File lib/cloudfront/models/DistributionConfig.rb, line 117
def diff_origins(aws_origins)
  diffs = []

  # map the origins to their keys
  aws = Hash[aws_origins.map { |o| [o.id, o] }]
  local = Hash[@origins.map { |o| [o.id, o] }]

  # find origins that are not configured locally
  aws.each do |origin_id, origin|
    if !local.include?(origin_id)
      diffs << OriginDiff.unmanaged(origin)
    end
  end

  local.each do |origin_id, origin|
    if !aws.include?(origin_id)
      diffs << OriginDiff.added(origin)
    else
      diffs << origin.diff(aws[origin_id])
    end
  end

  diffs.flatten
end