Class: Fog::Storage::HP::Real

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fog/hp/storage.rb,
lib/fog/hp/requests/storage/get_object.rb,
lib/fog/hp/requests/storage/put_object.rb,
lib/fog/hp/requests/storage/head_object.rb,
lib/fog/hp/requests/storage/delete_object.rb,
lib/fog/hp/requests/storage/get_container.rb,
lib/fog/hp/requests/storage/put_container.rb,
lib/fog/hp/requests/storage/get_containers.rb,
lib/fog/hp/requests/storage/head_container.rb,
lib/fog/hp/requests/storage/head_containers.rb,
lib/fog/hp/requests/storage/delete_container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#acl_to_header, #cdn, #header_to_acl, #url

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



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
168
# File 'lib/fog/hp/storage.rb', line 129

def initialize(options={})
  require 'mime/types'
  @hp_secret_key = options[:hp_secret_key]
  @hp_account_id = options[:hp_account_id]
  @hp_auth_uri   = options[:hp_auth_uri]
  @hp_cdn_ssl    = options[:hp_cdn_ssl]
  @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 type for object storage to the authentication call
  options[:hp_service_type] = "object-store"
  @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)
    # the CS service catalog returns the cdn endpoint
    @hp_storage_uri = credentials[:endpoint_url]
    @hp_cdn_uri  = credentials[:cdn_endpoint_url]
  else
    # Call the legacy v1.0/v1.1 authentication
    credentials = Fog::HP.authenticate_v1(options, @connection_options)
    # the user sends in the cdn endpoint
    @hp_storage_uri = options[:hp_auth_uri]
    @hp_cdn_uri  = options[:hp_cdn_uri]
  end

  @auth_token = credentials[:auth_token]

  uri = URI.parse(@hp_storage_uri)
  @host   = options[:hp_servicenet] == true ? "snet-#{uri.host}" : uri.host
  @path   = uri.path
  @persistent = options[:persistent] || false
  @port   = uri.port
  @scheme = uri.scheme
  Excon.ssl_verify_peer = false if options[:hp_servicenet] == true

  @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Instance Attribute Details

#hp_cdn_sslObject (readonly)

Returns the value of attribute hp_cdn_ssl.



127
128
129
# File 'lib/fog/hp/storage.rb', line 127

def hp_cdn_ssl
  @hp_cdn_ssl
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/storage/delete_container.rb', line 11

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

#delete_object(container, object) ⇒ Object

Delete an existing object

Parameters

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

  • object<~String> - Name of object to delete



12
13
14
15
16
17
18
19
# File 'lib/fog/hp/requests/storage/delete_object.rb', line 12

def delete_object(container, object)
  response = request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "#{Fog::HP.escape(container)}/#{Fog::HP.escape(object)}"
  )
  response
end

#get_container(container, options = {}) ⇒ Object

Get details for container and total bytes stored

Parameters

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

  • options<~String>:

    • ‘limit’<~String> - Maximum number of objects to return

    • ‘marker’<~String> - Only return objects whose name is greater than marker

    • ‘prefix’<~String> - Limits results to those starting with prefix

    • ‘path’<~String> - Return objects nested in the pseudo path

Returns

  • response<~Excon::Response>:

    • headers<~Hash>:

      • ‘X-Account-Container-Count’<~String> - Count of containers

      • ‘X-Account-Bytes-Used’<~String> - Bytes used

    • body<~Array>:

      • ‘bytes’<~Integer> - Number of bytes used by container

      • ‘count’<~Integer> - Number of items in container

      • ‘name’<~String> - Name of container

      • item<~Hash>:

        • ‘bytes’<~String> - Size of object

        • ‘content_type’<~String> Content-Type of object

        • ‘hash’<~String> - Hash of object (etag?)

        • ‘last_modified’<~String> - Last modified timestamp

        • ‘name’<~String> - Name of object



31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/hp/requests/storage/get_container.rb', line 31

def get_container(container, options = {})
  options = options.reject {|key, value| value.nil?}
  response = request(
    :expects  => 200,
    :method   => 'GET',
    :path     => Fog::HP.escape(container),
    :query    => {'format' => 'json'}.merge!(options)
  )
  response
end

#get_containers(options = {}) ⇒ Object

List existing storage containers

Parameters

  • options<~Hash>:

    • ‘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<~Hash>:

        • ‘bytes’<~Integer>: - Number of bytes used by container

        • ‘count’<~Integer>: - Number of items in container

        • ‘name’<~String>: - Name of container



20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/hp/requests/storage/get_containers.rb', line 20

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

#get_object(container, object, &block) ⇒ Object

Get details for an object

Parameters

  • container<~String> - Name of container to look in

  • object<~String> - Name of object to look for



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/hp/requests/storage/get_object.rb', line 12

def get_object(container, object, &block)
  params = {}

  if block_given?
    params[:response_block] = Proc.new
  end

  response = request(params.merge!({
    :block    => block,
    :expects  => 200,
    :method   => 'GET',
    :path     => "#{Fog::HP.escape(container)}/#{Fog::HP.escape(object)}"
  }), false)
  response
end

#head_container(container) ⇒ Object

List number of objects and total bytes stored

Parameters

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

Returns

  • response<~Excon::Response>:

    • headers<~Hash>:

      • ‘X-Container-Object-Count’<~String> - Count of containers

      • ‘X-Container-Bytes-Used’<~String> - Bytes used



16
17
18
19
20
21
22
23
24
# File 'lib/fog/hp/requests/storage/head_container.rb', line 16

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

#head_containersObject

List number of containers and total bytes stored

Returns

  • response<~Excon::Response>:

    • headers<~Hash>:

      • ‘X-Account-Container-Count’<~String> - Count of containers

      • ‘X-Account-Bytes-Used’<~String> - Bytes used



13
14
15
16
17
18
19
20
21
# File 'lib/fog/hp/requests/storage/head_containers.rb', line 13

def head_containers
  response = request(
    :expects  => 204,
    :method   => 'HEAD',
    :path     => '',
    :query    => {'format' => 'json'}
  )
  response
end

#head_object(container, object) ⇒ Object

Get headers for object

Parameters

  • container<~String> - Name of container to look in

  • object<~String> - Name of object to look for



12
13
14
15
16
17
18
19
# File 'lib/fog/hp/requests/storage/head_object.rb', line 12

def head_object(container, object)
  response = request({
    :expects  => 200,
    :method   => 'HEAD',
    :path     => "#{Fog::HP.escape(container)}/#{Fog::HP.escape(object)}"
  }, false)
  response
end

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

Create a new container

Parameters

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



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

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

#put_object(container, object, data, options = {}) ⇒ Object

Create a new object

Parameters

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/hp/requests/storage/put_object.rb', line 11

def put_object(container, object, data, options = {})
  data = Fog::Storage.parse_data(data)
  headers = data[:headers].merge!(options)
  if headers.has_key?('Transfer-Encoding')
    headers.delete('Content-Length')
  end
  response = request(
    :body     => data[:body],
    :expects  => 201,
    :headers  => headers,
    :method   => 'PUT',
    :path     => "#{Fog::HP.escape(container)}/#{Fog::HP.escape(object)}"
  )
  response
end

#reloadObject



170
171
172
# File 'lib/fog/hp/storage.rb', line 170

def reload
  @connection.reset
end

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



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

def request(params, parse_json = true, &block)
  begin
    response = @connection.request(params.merge!({
      :headers  => {
        'Content-Type' => '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::Storage::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