Class: Cfdef::Driver

Inherits:
Object
  • Object
show all
Includes:
Logger::Helper, Utils::Helper
Defined in:
lib/cfdef/driver.rb

Instance Method Summary collapse

Methods included from Logger::Helper

#log

Methods included from Utils::Helper

#diff, #matched?

Constructor Details

#initialize(client, options = {}) ⇒ Driver

Returns a new instance of Driver.



5
6
7
8
# File 'lib/cfdef/driver.rb', line 5

def initialize(client, options = {})
  @client = client
  @options = options
end

Instance Method Details

#create_distribution(dist_id, distribution) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cfdef/driver.rb', line 10

def create_distribution(dist_id, distribution)
  log(:info, "Create Distribution `#{dist_id}`", color: :cyan)

  unless @options[:dry_run]
    caller_reference = "#{dist_id} #{SecureRandom.uuid}"

    params = {
      distribution_config: {
        caller_reference: caller_reference,
      }.merge(distribution),
    }

    @client.create_distribution(params)
  end
end

#delete_distribution(dist_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cfdef/driver.rb', line 26

def delete_distribution(dist_id)
  log(:info, "Delete Distribution `#{dist_id}`", color: :red)

  unless @options[:dry_run]
    etag = @client.get_distribution(id: dist_id).etag

    params = {
      id: dist_id,
      if_match: etag,
    }

    @client.delete_distribution(params)
  end
end

#update_distribution(dist_id, distribution, old_distribution) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cfdef/driver.rb', line 41

def update_distribution(dist_id, distribution, old_distribution)
  log(:info, "Update Distribution `#{dist_id}`", color: :green)
  log(:info, diff(old_distribution, distribution, color: @options[:color]), color: false)

  unless @options[:dry_run]
    resp = @client.get_distribution(id: dist_id)
    etag = resp.etag
    caller_reference = resp.distribution.distribution_config.caller_reference

    dist_conf = {
      caller_reference: caller_reference,
    }.merge(distribution)

    unless dist_conf[:default_root_object]
      dist_conf[:default_root_object] = ""
    end

    unless dist_conf[:logging]
      dist_conf[:logging] = {
        enabled: false,
        include_cookies: false,
        bucket: '',
        prefix: '',
      }
    end

    params = {
      distribution_config: dist_conf,
      id: dist_id,
      if_match: etag,
    }

    @client.update_distribution(params)
  end
end