class Fog::Rackspace::BlockStorage::Real

Public Class Methods

new(options = {}) click to toggle source
# File lib/fog/rackspace/block_storage.rb, line 52
def initialize(options = {})
  @rackspace_api_key = options[:rackspace_api_key]
  @rackspace_username = options[:rackspace_username]
  @rackspace_auth_url = options[:rackspace_auth_url]
  @rackspace_must_reauthenticate = false
  @connection_options = options[:connection_options] || {}

  endpoint = options[:rackspace_endpoint] || DFW_ENDPOINT
  uri = URI.parse(endpoint)

  @host = uri.host
  @persistent = options[:persistent] || false
  @path = uri.path
  @port = uri.port
  @scheme = uri.scheme

  authenticate

  @connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)
end

Public Instance Methods

create_snapshot(volume_id, options = {}) click to toggle source
# File lib/fog/rackspace/requests/block_storage/create_snapshot.rb, line 5
def create_snapshot(volume_id, options = {})
  data = {
    'snapshot' => {
      'volume_id' => volume_id
    }
  }

  data['snapshot']['display_name'] = options[:display_name] unless options[:display_name].nil?
  data['snapshot']['display_description'] = options[:display_description] unless options[:display_description].nil?
  data['snapshot']['force'] = options[:force] unless options[:force].nil?

  request(
    :body => Fog::JSON.encode(data),
    :expects => [200],
    :method => 'POST',
    :path => "snapshots"
  )
end
create_volume(size, options = {}) click to toggle source
# File lib/fog/rackspace/requests/block_storage/create_volume.rb, line 5
def create_volume(size, options = {})
  data = {
    'volume' => {
      'size' => size
    }
  }

  data['volume']['display_name'] = options[:display_name] unless options[:display_name].nil?
  data['volume']['display_description'] = options[:display_description] unless options[:display_description].nil?
  data['volume']['volume_type'] = options[:volume_type] unless options[:volume_type].nil?
  data['volume']['availability_zone'] = options[:availability_zone] unless options[:availability_zone].nil?

  request(
    :body => Fog::JSON.encode(data),
    :expects => [200],
    :method => 'POST',
    :path => "volumes"
  )
end
delete_snapshot(snapshot_id) click to toggle source
# File lib/fog/rackspace/requests/block_storage/delete_snapshot.rb, line 5
def delete_snapshot(snapshot_id)
  request(
    :expects => [202],
    :method => 'DELETE',
    :path => "snapshots/#{snapshot_id}"
  )
end
delete_volume(volume_id) click to toggle source
# File lib/fog/rackspace/requests/block_storage/delete_volume.rb, line 5
def delete_volume(volume_id)
  request(
    :expects => [202],
    :method => 'DELETE',
    :path => "volumes/#{volume_id}"
  )
end
get_snapshot(snapshot_id) click to toggle source
# File lib/fog/rackspace/requests/block_storage/get_snapshot.rb, line 5
def get_snapshot(snapshot_id)
  request(
    :expects => [200],
    :method => 'GET',
    :path => "snapshots/#{snapshot_id}"
  )
end
get_volume(volume_id) click to toggle source
# File lib/fog/rackspace/requests/block_storage/get_volume.rb, line 5
def get_volume(volume_id)
  request(
    :expects => [200],
    :method => 'GET',
    :path => "volumes/#{volume_id}"
  )
end
get_volume_type(volume_type_id) click to toggle source
# File lib/fog/rackspace/requests/block_storage/get_volume_type.rb, line 5
def get_volume_type(volume_type_id)
  request(
    :expects => [200],
    :method => 'GET',
    :path => "types/#{volume_type_id}"
  )
end
list_snapshots() click to toggle source
# File lib/fog/rackspace/requests/block_storage/list_snapshots.rb, line 5
def list_snapshots
  request(
    :expects => [200],
    :method => 'GET',
    :path => 'snapshots'
  )
end
list_volume_types() click to toggle source
# File lib/fog/rackspace/requests/block_storage/list_volume_types.rb, line 5
def list_volume_types
  request(
    :expects => [200],
    :method => 'GET',
    :path => 'types'
  )
end
list_volumes() click to toggle source
# File lib/fog/rackspace/requests/block_storage/list_volumes.rb, line 5
def list_volumes
  request(
    :expects => [200],
    :method => 'GET',
    :path => 'volumes'
  )
end
request(params) click to toggle source
# File lib/fog/rackspace/block_storage.rb, line 73
def request(params)
  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]}"
    }))
  rescue Excon::Errors::NotFound => error
    raise NotFound.slurp error
  rescue Excon::Errors::BadRequest => error
    raise BadRequest.slurp error
  rescue Excon::Errors::InternalServerError => error
    raise InternalServerError.slurp error
  rescue Excon::Errors::HTTPStatusError => error
    raise ServiceError.slurp error
  end
  unless response.body.empty?
    response.body = Fog::JSON.decode(response.body)
  end
  response
end

Private Instance Methods

authenticate() click to toggle source
# File lib/fog/rackspace/block_storage.rb, line 100
def authenticate
  options = {
    :rackspace_api_key  => @rackspace_api_key,
    :rackspace_username => @rackspace_username,
    :rackspace_auth_url => @rackspace_auth_url
  }
  credentials = Fog::Rackspace.authenticate(options, @connection_options)
  @auth_token = credentials['X-Auth-Token']
  account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
  @path = "#{@path}/#{account_id}"
end