Class: Fog::Rackspace::BlockStorage::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/block_storage.rb,
lib/fog/rackspace/requests/block_storage/get_volume.rb,
lib/fog/rackspace/requests/block_storage/get_snapshot.rb,
lib/fog/rackspace/requests/block_storage/list_volumes.rb,
lib/fog/rackspace/requests/block_storage/create_volume.rb,
lib/fog/rackspace/requests/block_storage/delete_volume.rb,
lib/fog/rackspace/requests/block_storage/list_snapshots.rb,
lib/fog/rackspace/requests/block_storage/create_snapshot.rb,
lib/fog/rackspace/requests/block_storage/delete_snapshot.rb,
lib/fog/rackspace/requests/block_storage/get_volume_type.rb,
lib/fog/rackspace/requests/block_storage/list_volume_types.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

Instance Method Details

#create_snapshot(volume_id, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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 = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object



5
6
7
8
9
10
11
# 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) ⇒ Object



5
6
7
8
9
10
11
# 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) ⇒ Object



5
6
7
8
9
10
11
# 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) ⇒ Object



5
6
7
8
9
10
11
# 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) ⇒ Object



5
6
7
8
9
10
11
# 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_snapshotsObject



5
6
7
8
9
10
11
# 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_typesObject



5
6
7
8
9
10
11
# 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_volumesObject



5
6
7
8
9
10
11
# 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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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