Class: ConfigureS3Website::CloudFrontClient

Inherits:
Object
  • Object
show all
Defined in:
lib/configure-s3-website/cloudfront_client.rb

Class Method Summary collapse

Class Method Details

.apply_distribution_config(options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/configure-s3-website/cloudfront_client.rb', line 6

def self.apply_distribution_config(options)
  config_source = options[:config_source]
  puts "Detected an existing CloudFront distribution (id #{config_source.cloudfront_distribution_id}) ..."

  # Get caller reference and ETag (will be required by the PUT config resource)
  response = HttpHelper.call_cloudfront_api(
    path = "/2012-07-01/distribution/#{config_source.cloudfront_distribution_id}/config",
    method = Net::HTTP::Get,
    body = '',
    config_source
  )
  etag = response['ETag']
  caller_reference = REXML::XPath.first(
    REXML::Document.new(response.body),
    '/DistributionConfig/CallerReference'
  ).get_text.to_s

  # Call the PUT config resource with the caller reference and ETag
  custom_distribution_config = config_source.cloudfront_distribution_config || {}
  custom_distribution_config_with_caller_ref = custom_distribution_config.merge({
    'caller_reference' => caller_reference,
    'comment' => 'Updated by the configure-s3-website gem'
  })
  HttpHelper.call_cloudfront_api(
    path = "/2012-07-01/distribution/#{options[:config_source].cloudfront_distribution_id}/config",
    method = Net::HTTP::Put,
    body = distribution_config_xml(
      config_source,
      custom_distribution_config_with_caller_ref
    ),
    config_source,
    headers = { 'If-Match' => etag }
  )

  # Report
  unless custom_distribution_config.empty?
    print_report_on_custom_distribution_config custom_distribution_config
  end
end

.create_distribution_if_user_agrees(options, standard_input) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/configure-s3-website/cloudfront_client.rb', line 46

def self.create_distribution_if_user_agrees(options, standard_input)
  if options['autocreate-cloudfront-dist'] and options[:headless]
    puts 'Creating a CloudFront distribution for your S3 website ...'
    create_distribution options
  elsif options[:headless]
    # Do nothing
  else
    puts 'Do you want to deliver your website via CloudFront, the CDN of Amazon? [y/N]'
    case standard_input.gets.chomp
    when /(y|Y)/ then create_distribution options
    end
  end
end