Class: RainForest::CloudFront

Inherits:
Object
  • Object
show all
Defined in:
lib/rain_forest/cloud_front.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCloudFront

Returns a new instance of CloudFront.



8
9
10
11
12
# File 'lib/rain_forest/cloud_front.rb', line 8

def initialize
  akid = ENV['RAIN_FOREST_AWS_AKID']
  secret = ENV['RAIN_FOREST_AWS_SECRET']
  @client = ::Aws::CloudFront::Client.new(access_key_id: akid, secret_access_key: secret, region: 'cloudfront.amazonaws.com')
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/rain_forest/cloud_front.rb', line 6

def client
  @client
end

Class Method Details

.create_invalidation(distribution_id, invalidate_paths_array) ⇒ Object



32
33
34
# File 'lib/rain_forest/cloud_front.rb', line 32

def self.create_invalidation(distribution_id, invalidate_paths_array)
  self.new.create_invalidation(distribution_id, invalidate_paths_array)
end

.get_distribution(distribution_id) ⇒ Object



14
15
16
# File 'lib/rain_forest/cloud_front.rb', line 14

def self.get_distribution(distribution_id)
  self.new.get_distribution(distribution_id)
end

.get_distribution_status(distribution_id) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/rain_forest/cloud_front.rb', line 18

def self.get_distribution_status(distribution_id)
  success, status_or_message = self.new.get_distribution(distribution_id)

  if success
    return success, status_or_message.distribution.status
  else
    return success, status_or_message
  end
end

.update_origin(distribution_id, new_origin_key) ⇒ Object



28
29
30
# File 'lib/rain_forest/cloud_front.rb', line 28

def self.update_origin(distribution_id, new_origin_key)
  self.new.update_origin(distribution_id, new_origin_key)
end

Instance Method Details

#create_invalidation(distribution_id, invalidate_paths_array, caller_reference = ::SecureRandom.hex(16)) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rain_forest/cloud_front.rb', line 70

def create_invalidation(distribution_id, invalidate_paths_array, caller_reference=::SecureRandom.hex(16))
  begin
    parameters = {
      distribution_id: distribution_id,
      invalidation_batch: {
        paths: {
          quantity: invalidate_paths_array.count,
          items: invalidate_paths_array
        },
        caller_reference: caller_reference
      }
    }

    resp = @client.create_invalidation(parameters)

    return true, resp
  rescue Exception => e
    return false, e.message
  end
end

#get_distribution(distribution_id) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/rain_forest/cloud_front.rb', line 36

def get_distribution(distribution_id)
  begin
    dist = @client.get_distribution(id: distribution_id)
    return true, dist
  rescue Exception => e
    return false, e.message
  end
end

#update_origin(distribution_id, new_origin_key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rain_forest/cloud_front.rb', line 45

def update_origin(distribution_id, new_origin_key)
  begin
    dist = @client.get_distribution(id: distribution_id)
    
    dist_config = dist[:distribution][:distribution_config]
    dist_config[:comment] = dist_config[:comment].to_s #ensure not nil or will get an error
    dist_config[:logging][:bucket] = dist_config[:logging][:bucket].to_s #ensure not nil or will get an error
    dist_config[:logging][:prefix] = dist_config[:logging][:prefix].to_s #ensure not nil or will get an error
    
    dist_config[:origins][:items][0][:origin_path] = new_origin_key # IMPORTANT: set new origin!
    
    options = {
      id: distribution_id,
      if_match: dist[:etag],
      distribution_config: dist_config
    }

    @client.update_distribution(options)
  rescue Exception => e
    return false, e.message
  end
  
  return true, nil
end