Class: Fog::CDN::HP::Real

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fog/hp/cdn.rb,
lib/fog/hp/requests/cdn/put_container.rb,
lib/fog/hp/requests/cdn/get_containers.rb,
lib/fog/hp/requests/cdn/head_container.rb,
lib/fog/hp/requests/cdn/post_container.rb,
lib/fog/hp/requests/cdn/delete_container.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fog/hp/cdn.rb', line 69

def initialize(options={})
  # deprecate hp_account_id
  if options[:hp_account_id]
    Fog::Logger.deprecation(":hp_account_id is deprecated, please use :hp_access_key instead.")
    options[:hp_access_key] = options.delete(:hp_account_id)
  end
  @hp_access_key = options[:hp_access_key]
  unless @hp_access_key
    raise ArgumentError.new("Missing required arguments: hp_access_key. :hp_account_id is deprecated, please use :hp_access_key instead.")
  end
  @hp_secret_key = options[:hp_secret_key]
  @connection_options = options[:connection_options] || {}
  ### Set an option to use the style of authentication desired; :v1 or :v2 (default)
  auth_version = options[:hp_auth_version] || :v2
  ### Pass the service name for object storage to the authentication call
  options[:hp_service_type] = "CDN"
  @hp_tenant_id = options[:hp_tenant_id]

  ### Make the authentication call
  if (auth_version == :v2)
    # Call the control services authentication
    credentials = Fog::HP.authenticate_v2(options, @connection_options)
    ### When using the v2 CS authentication, the CDN Mgmt comes from the service catalog
    @hp_cdn_uri = credentials[:endpoint_url]
    cdn_mgmt_url = @hp_cdn_uri
  else
    # Call the legacy v1.0/v1.1 authentication
    credentials = Fog::HP.authenticate_v1(options, @connection_options)
    # In case of legacy authentication systems, the user can pass the CDN Mgmt Uri
    @hp_cdn_uri = options[:hp_cdn_uri] || "https://region-a.geo-1.cdnmgmt.hpcloudsvc.com/v1.0"
    # In case of legacy authentication systems, the :cdn_endpoint_url will carry the cdn storage url
    cdn_mgmt_url = "#{@hp_cdn_uri}#{URI.parse(credentials[:cdn_endpoint_url]).path}"
  end

  @auth_token = credentials[:auth_token]
  @enabled = false
  @persistent = options[:persistent] || false

  if cdn_mgmt_url
    uri = URI.parse(cdn_mgmt_url)
    @host   = uri.host
    @path   = uri.path.chomp("/")
    @port   = uri.port
    @scheme = uri.scheme
    @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
    @enabled = true
  end
end

Instance Method Details

#delete_container(name) ⇒ Object

Delete an existing container

Parameters

  • name<~String> - Name of container to delete



11
12
13
14
15
16
17
18
# File 'lib/fog/hp/requests/cdn/delete_container.rb', line 11

def delete_container(name)
  response = request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => Fog::HP.escape(name)
  )
  response
end

#enabled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/fog/hp/cdn.rb', line 118

def enabled?
  @enabled
end

#get_containers(options = {}) ⇒ Object

List existing cdn-enabled storage containers

Parameters

  • options<~Hash>:

    • ‘enabled_only’<~Boolean> - Set to true to limit results to cdn enabled containers

    • ‘limit’<~Integer> - Upper limit to number of results returned

    • ‘marker’<~String> - Only return objects with name greater than this value

Returns

  • response<~Excon::Response>:

    • body<~Array>:

      • container<~String>: Name of container



18
19
20
21
22
23
24
25
26
# File 'lib/fog/hp/requests/cdn/get_containers.rb', line 18

def get_containers(options = {})
  response = request(
    :expects  => [200, 204],
    :method   => 'GET',
    :path     => '',
    :query    => {'format' => 'json'}.merge!(options)
  )
  response
end

#head_container(name) ⇒ Object

List cdn properties for a container

Parameters

  • name<~String> - Name of container to retrieve info for

Returns

  • response<~Excon::Response>:

    • headers<~Hash>:

      • ‘X-Cdn-Enabled’<~Boolean> - cdn status for container

      • ‘X-Cdn-Uri’<~String> - cdn url for this container

      • ‘X-Ttl’<~String> - integer seconds before data expires, defaults to 86400 (1 day)

      • ‘X-Log-Retention’<~Boolean> - ?



18
19
20
21
22
23
24
25
26
# File 'lib/fog/hp/requests/cdn/head_container.rb', line 18

def head_container(name)
  response = request(
    :expects  => 204,
    :method   => 'HEAD',
    :path     => Fog::HP.escape(name),
    :query    => {'format' => 'json'}
  )
  response
end

#post_container(name, options = {}) ⇒ Object

modify CDN properties for a container

Parameters

  • name<~String> - Name for container, should be < 256 bytes and must not contain ‘/’

# options<~Hash>:

* 'X-CDN-Enabled'<~Boolean> - cdn status for container
* 'X-CDN-URI'<~String> - cdn url for this container
* 'X-TTL'<~String> - integer seconds before data expires, defaults to 86400 (1 day), in 900 (15 min.) to 1577836800 (50 years)
* 'X-Log-Retention'<~Boolean> - ?


15
16
17
18
19
20
21
22
23
# File 'lib/fog/hp/requests/cdn/post_container.rb', line 15

def post_container(name, options = {})
  response = request(
    :expects  => [201, 202],
    :headers  => options,
    :method   => 'POST',
    :path     => Fog::HP.escape(name)
  )
  response
end

#put_container(name, options = {}) ⇒ Object

enable CDN for a container

Parameters

  • name<~String> - Name for container, should be < 256 bytes and must not contain ‘/’

# options<~Hash>:

* 'X-CDN-Enabled'<~Boolean> - cdn status for container
* 'X-CDN-URI'<~String> - cdn url for this container
* 'X-TTL'<~String> - integer seconds before data expires, defaults to 86400 (1 day), in 900 (15 min.) to 1577836800 (50 years)
* 'X-Log-Retention'<~Boolean> - ?


15
16
17
18
19
20
21
22
23
# File 'lib/fog/hp/requests/cdn/put_container.rb', line 15

def put_container(name, options = {})
  response = request(
    :expects  => [201, 202],
    :headers  => options,
    :method   => 'PUT',
    :path     => Fog::HP.escape(name)
  )
  response
end

#reloadObject



122
123
124
# File 'lib/fog/hp/cdn.rb', line 122

def reload
  @cdn_connection.reset
end

#request(params, parse_json = true, &block) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fog/hp/cdn.rb', line 126

def request(params, parse_json = true, &block)
  begin
    response = @connection.request(params.merge!({
      :headers  => {
        'Content-Type' => 'application/json',
        'Accept'       => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :host     => @host,
      :path     => "#{@path}/#{params[:path]}",
    }), &block)
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::CDN::HP::NotFound.slurp(error)
    else
      error
    end
  end
  if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
    response.body = Fog::JSON.decode(response.body)
  end
  response
end