Class: Cloudfront::Helpers::Origin

Inherits:
Object
  • Object
show all
Includes:
Utils::ConfigurationChecker, Utils::XmlSerializer
Defined in:
lib/cloudfront/helpers/origin.rb

Instance Attribute Summary collapse

Attributes included from Utils::ConfigurationChecker

#error_messages

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::XmlSerializer

#to_xml

Methods included from Utils::ConfigurationChecker

#check_configuration, #valid?

Constructor Details

#initialize(&block) ⇒ Origin

Returns a new instance of Origin.



16
17
18
19
20
21
22
23
# File 'lib/cloudfront/helpers/origin.rb', line 16

def initialize(&block)
  #set default values
  @http_port = 80
  @https_port = 443
  @origin_protocol_policy = "match-viewer"
  #set value from block
  instance_eval &block if block_given?
end

Instance Attribute Details

#domain_nameObject

Returns the value of attribute domain_name.



9
10
11
# File 'lib/cloudfront/helpers/origin.rb', line 9

def domain_name
  @domain_name
end

#http_portObject

Returns the value of attribute http_port.



9
10
11
# File 'lib/cloudfront/helpers/origin.rb', line 9

def http_port
  @http_port
end

#https_portObject

Returns the value of attribute https_port.



9
10
11
# File 'lib/cloudfront/helpers/origin.rb', line 9

def https_port
  @https_port
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/cloudfront/helpers/origin.rb', line 9

def id
  @id
end

#origin_access_identityObject

Returns the value of attribute origin_access_identity.



9
10
11
# File 'lib/cloudfront/helpers/origin.rb', line 9

def origin_access_identity
  @origin_access_identity
end

#origin_protocol_policyObject

Returns the value of attribute origin_protocol_policy.



9
10
11
# File 'lib/cloudfront/helpers/origin.rb', line 9

def origin_protocol_policy
  @origin_protocol_policy
end

Class Method Details

.from_hash(hash) ⇒ Object

{

"Origin"=> {
    "Id"=>"unique identifier for this origin",
    "DomainName"=>"domain name of origin",
    "S3OriginConfig"=> {
        "OriginAccessIdentity"=> "origin-access-identity/cloudfront/ID"
    },
    "CustomOriginConfig"=> {
        "HTTPPort"=> 80,
        "HTTPSPort"=> 443,
       "OriginProtocolPolicy"=>"match-viewer"
    }
}

}



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cloudfront/helpers/origin.rb', line 46

def self.from_hash(hash)
  hash = hash["Origin"] || hash
  self.new do
    self.id = hash["Id"]
    self.domain_name = hash["DomainName"]
    if hash.has_key? "S3OriginConfig"
      self.origin_access_identity = hash["S3OriginConfig"]["OriginAccessIdentity"]
    else
      custom_origin_config = hash["CustomOriginConfig"]
      if custom_origin_config
        self.http_port = (custom_origin_config["HTTPPort"] || "80").to_i
        self.https_port = (custom_origin_config["HTTPSPort"] || "443").to_i
        self.origin_protocol_policy = custom_origin_config["OriginProtocolPolicy"] || "match-viewer"
      end
    end
  end
end

Instance Method Details

#build_xml(xml) ⇒ Object

The origin class container

<Origin>
  <Id>unique identifier for this origin</Id>
  <DomainName>domain name of origin</DomainName>
  <!-- Include the S3OriginConfig element only if you use an Amazon S3 origin for your distribution. -->
  <S3OriginConfig>
    <OriginAccessIdentity>origin-access-identity/cloudfront/ID</OriginAccessIdentity>
  </S3OriginConfig>
  <!-- Include the CustomOriginConfig element only if you use a custom origin for your distribution. -->
  <CustomOriginConfig>
    <HTTPPort>80</HTTPPort>
    <HTTPSPort>443</HTTPSPort>
    <OriginProtocolPolicy>http-only |
      match-viewer</OriginProtocolPolicy>
  </CustomOriginConfig>
</Origin>


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cloudfront/helpers/origin.rb', line 80

def build_xml(xml)
  check_configuration
  xml.Origin {
    xml.Id @id
    xml.DomainName @domain_name
    if (!@origin_access_identity.nil?)
      xml.S3OriginConfig {
        xml.OriginAccessIdentity @origin_access_identity
      }
    else
      xml.CustomOriginConfig {
        xml.HTTPPort @http_port
        xml.HTTPSPort @https_port
        xml.OriginProtocolPolicy @origin_protocol_policy
      }
    end
  }
end

#validateObject



25
26
27
28
29
30
# File 'lib/cloudfront/helpers/origin.rb', line 25

def validate
  error_messages.push "http_port is invalid" unless @http_port.between?(0, 65535)
  error_messages.push "https_port is invalid" unless @https_port.between?(0, 65535)
  error_messages.push "id can't be null" if @id.nil?
  error_messages.push "origin_protocol_policy is invalid should be in #{Cloudfront::Utils::Util::MATCH_VIEWER_VALUES.join(', ')}" unless Cloudfront::Utils::Util::MATCH_VIEWER_VALUES.include?(@origin_protocol_policy)
end