Class: Cumulus::CloudFront::OriginConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudfront/models/OriginConfig.rb

Overview

Public: An object representing configuration for a origin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json = nil) ⇒ OriginConfig

Public: Constructor

json - a hash containing the JSON configuration for the origin



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cloudfront/models/OriginConfig.rb', line 26

def initialize(json = nil)
  if !json.nil?
    @id = json["id"]
    @domain_name = json["domain-name"]
    @origin_path = json["origin-path"]
    @s3_access_origin_identity = json["s3-origin-access-identity"]
    @custom_origin_config = if json["custom-origin-config"].nil?
      nil
    else
      CustomOriginConfig.new(
        json["custom-origin-config"]["http-port"],
        json["custom-origin-config"]["https-port"],
        json["custom-origin-config"]["protocol-policy"],
        json["custom-origin-config"]["origin-ssl-protocols"] && OriginSslProtocols.new(
          json["custom-origin-config"]["origin-ssl-protocols"]
        ),
        json["custom-origin-config"]["origin-read-timeout"] || 30,
        json["custom-origin-config"]["origin-keepalive-timeout"] || 5
      )
    end
    @custom_headers = if json["custom-headers"].nil?
      []
    else
      json["custom-headers"].map do |name, value|
        CustomHeaderConfig.new(name, value)
      end
    end
    @name = @id
  end
end

Instance Attribute Details

#custom_origin_configObject (readonly)

Returns the value of attribute custom_origin_config.



20
21
22
# File 'lib/cloudfront/models/OriginConfig.rb', line 20

def custom_origin_config
  @custom_origin_config
end

#custom_origin_headersObject (readonly)

Returns the value of attribute custom_origin_headers.



21
22
23
# File 'lib/cloudfront/models/OriginConfig.rb', line 21

def custom_origin_headers
  @custom_origin_headers
end

#domain_nameObject (readonly)

Returns the value of attribute domain_name.



17
18
19
# File 'lib/cloudfront/models/OriginConfig.rb', line 17

def domain_name
  @domain_name
end

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/cloudfront/models/OriginConfig.rb', line 16

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/cloudfront/models/OriginConfig.rb', line 15

def name
  @name
end

#origin_pathObject (readonly)

Returns the value of attribute origin_path.



18
19
20
# File 'lib/cloudfront/models/OriginConfig.rb', line 18

def origin_path
  @origin_path
end

#s3_access_origin_identityObject (readonly)

Returns the value of attribute s3_access_origin_identity.



19
20
21
# File 'lib/cloudfront/models/OriginConfig.rb', line 19

def s3_access_origin_identity
  @s3_access_origin_identity
end

Instance Method Details

#diff(aws) ⇒ Object

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

aws - the AWS resource

Returns an array of the OriginDiffs that were found



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cloudfront/models/OriginConfig.rb', line 129

def diff(aws)
  diffs = []

  if @domain_name != aws.domain_name
    diffs << OriginDiff.new(OriginChange::DOMAIN, aws, self)
  end

  if @origin_path != aws.origin_path
    diffs << OriginDiff.new(OriginChange::PATH, aws, self)
  end

  # If s3 origin is defined here but not aws
  if !aws.s3_origin_config.nil?
    if @s3_access_origin_identity != aws.s3_origin_config.origin_access_identity
      diffs << OriginDiff.new(OriginChange::S3, aws, self)
    end
  else
    if !@s3_access_origin_identity.nil?
      diffs << OriginDiff.new(OriginChange::S3, aws, self)
    end
  end

  if @custom_origin_config.nil?
    if !aws.custom_origin_config.nil?
      custom_diffs = CustomOriginConfig.new(nil, nil, nil).diff(aws.custom_origin_config)
      diffs << OriginDiff.custom(custom_diffs, aws, self) if !custom_diffs.empty?
    end
  else
    custom_diffs = @custom_origin_config.diff(aws.custom_origin_config)
    diffs << OriginDiff.custom(custom_diffs, aws, self) if !custom_diffs.empty?
  end

  header_diffs = diff_custom_headers(aws.custom_headers.items)
  if !header_diffs.empty?
    diffs << OriginDiff.headers(header_diffs, self)
  end

  diffs.flatten
end

#diff_custom_headers(aws_headers) ⇒ Object

Internal : Produce an array of difference between local and remove custom origin headers

aws_headers - the custom origin headers

Returns an array of CustomHeaderDiffs that were found



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/cloudfront/models/OriginConfig.rb', line 174

def diff_custom_headers(aws_headers)
  diffs = []

  #map headers to their names
  aws = Hash[aws_headers.map { |o| [o.header_name, o] }]
  local = Hash[@custom_headers.map { |o| [o.name, o] }]

  # find headers not configured locally
  aws.each do |header_name, header|
    if !local.include?(header_name)
      diffs << CustomHeaderDiff.unmanaged(header)
    end
  end

  local.each do |header_name, header|
    if !aws.include?(header_name)
      diffs << CustomHeaderDiff.added(header)
    else
      diffs += header.diff(aws[header_name])
    end
  end

  diffs
end

#populate!(aws) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloudfront/models/OriginConfig.rb', line 57

def populate!(aws)
  @id = aws.id
  @domain_name = aws.domain_name
  @origin_path = aws.origin_path
  @s3_access_origin_identity = if aws.s3_origin_config then aws.s3_origin_config.origin_access_identity end
  @custom_origin_config = if aws.custom_origin_config
    CustomOriginConfig.new(
      aws.custom_origin_config.http_port,
      aws.custom_origin_config.https_port,
      aws.custom_origin_config.origin_protocol_policy,
      aws.custom_origin_config.origin_ssl_protocols && OriginSslProtocols.new(
        aws.custom_origin_config.origin_ssl_protocols.items
      )
    )
  end
  @custom_headers = (aws.custom_headers.items || []).map do |header|
    CustomHeaderConfig.new(header.header_name, header.header_value)
  end
  @name = @id
end

#to_awsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cloudfront/models/OriginConfig.rb', line 94

def to_aws
  {
    id: @id,
    domain_name: @domain_name,
    origin_path: @origin_path,
    s3_origin_config: if @s3_access_origin_identity.nil? then nil else
      {
        origin_access_identity: @s3_access_origin_identity
      }
    end,
    custom_origin_config: if @custom_origin_config.nil? then nil else
      {
        http_port: @custom_origin_config.http_port,
        https_port: @custom_origin_config.https_port,
        origin_protocol_policy: @custom_origin_config.protocol_policy,
        origin_read_timeout: @custom_origin_config.origin_read_timeout,
        origin_keepalive_timeout: @custom_origin_config.origin_keepalive_timeout,
        origin_ssl_protocols: if @custom_origin_config.origin_ssl_protocols
          {
            quantity: @custom_origin_config.origin_ssl_protocols.quantity,
            items: @custom_origin_config.origin_ssl_protocols.items,
          }
        end
      }
    end,
   custom_headers: AwsUtil.aws_array(@custom_headers.map(&:to_aws))
  }
end

#to_localObject

Public: Get the config as a hash

Returns the hash



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cloudfront/models/OriginConfig.rb', line 81

def to_local
  {
    "id" => @id,
    "domain-name" => @domain_name,
    "origin-path" => @origin_path,
    "s3-origin-access-identity" => @s3_access_origin_identity,
    "custom-origin-config" => if @custom_origin_config.nil? then nil else @custom_origin_config.to_local end,
    "custom-headers" => Hash[@custom_headers.map do |header|
        [header.name, header.value]
      end]
  }.reject { |k, v| v.nil? }
end