Class: Fog::Storage::IBM::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/ibm/storage.rb,
lib/fog/ibm/requests/storage/get_volume.rb,
lib/fog/ibm/requests/storage/list_volumes.rb,
lib/fog/ibm/requests/storage/delete_volume.rb,
lib/fog/ibm/requests/storage/create_volume.rb,
lib/fog/ibm/requests/storage/list_offerings.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock



68
69
70
71
72
# File 'lib/fog/ibm/storage.rb', line 68

def initialize(options={})
  @ibm_username = options[:ibm_username]
  @ibm_password = options[:ibm_password]
  @data = self.class.data[@ibm_username]
end

Class Method Details

.dataObject



47
48
49
50
51
52
53
# File 'lib/fog/ibm/storage.rb', line 47

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :volumes      => {},
    }
  end
end

.resetObject



55
56
57
# File 'lib/fog/ibm/storage.rb', line 55

def self.reset
  @data = nil
end

Instance Method Details

#create_volume(name, offering_id, format, location_id, size) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/fog/ibm/requests/storage/create_volume.rb', line 45

def create_volume(name, offering_id, format, location_id, size)
  volume          = Fog::IBM::Mock.create_volume(name, offering_id, format, location_id, size)
  self.data[:volumes][volume['id']] = volume
  response        = Excon::Response.new
  response.status = 200
  response.body   = format_create_volume_response_for(volume['id'])
  response
end

#dataObject



59
60
61
# File 'lib/fog/ibm/storage.rb', line 59

def data
  self.class.data[@ibm_username]
end

#delete_volume(volume_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/ibm/requests/storage/delete_volume.rb', line 24

def delete_volume(volume_id)
  response = Excon::Response.new
  if volume_exists? volume_id
    self.data[:volumes].delete volume_id
    response.status = 200
    response.body = {"success"=>true}
  else
    response.status = 404
  end
  response
end

#format_create_volume_response_for(volume_id) ⇒ Object

The create_volume response doesn't contain ioPrice either



55
56
57
58
59
# File 'lib/fog/ibm/requests/storage/create_volume.rb', line 55

def format_create_volume_response_for(volume_id)
  # If we aren't attached/ready, make us ready
  ready_volume(volume_id) unless volume_attached? volume_id
  self.data[:volumes][volume_id].reject { |k,v| k == 'ioPrice' }
end

#format_get_volume_response_for(volume_id) ⇒ Object

get_volume response doesn't contain instanceId



36
37
38
39
40
# File 'lib/fog/ibm/requests/storage/get_volume.rb', line 36

def format_get_volume_response_for(volume_id)
  # If we aren't attached/ready, make us ready
  ready_volume(volume_id) unless volume_attached? volume_id
  self.data[:volumes][volume_id].reject { |k,v| k == 'instanceId' }
end

#format_list_volumes_responseObject

The list_volumes response doesn't contain ioPrice



43
44
45
# File 'lib/fog/ibm/requests/storage/get_volume.rb', line 43

def format_list_volumes_response
  self.data[:volumes].values.dup.map { |volume| volume.reject { |k,v| k == 'ioPrice'} }
end

#get_volume(volume_id) ⇒ Object

For whatever reason, get_volume returns different data than an entry in list_volumes



24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/ibm/requests/storage/get_volume.rb', line 24

def get_volume(volume_id)
  response = Excon::Response.new
  if volume_exists? volume_id
    response.status = 200
    response.body   = format_get_volume_response_for(volume_id)
  else
    response.status = 404
  end
  response
end

#list_offeringsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/ibm/requests/storage/list_offerings.rb', line 37

def list_offerings
  response = Excon::Response.new
  response.status = 200
  response.body = {"volumes"=>
      [{"name"=>"Small",
        "price"=>
         {"pricePerQuantity"=>1,
          "effectiveDate"=>-1,
          "rate"=>0.0384,
          "countryCode"=>"897",
          "unitOfMeasure"=>"UHR",
          "currencyCode"=>"USD"},
        "location"=>"61",
        "id"=>"20001208",
        "formats"=>
         [{"label"=>"ext3", "id"=>"EXT3"}, {"label"=>"raw", "id"=>"RAW"}],
        "capacity"=>256},
       {"name"=>"Small",
        "price"=>
         {"pricePerQuantity"=>1,
          "effectiveDate"=>-1,
          "rate"=>0.0384,
          "countryCode"=>"897",
          "unitOfMeasure"=>"UHR",
          "currencyCode"=>"USD"},
        "location"=>"141",
        "id"=>"20001208",
        "formats"=>
         [{"label"=>"ext3", "id"=>"EXT3"}, {"label"=>"raw", "id"=>"RAW"}],
        "capacity"=>256}]},
  response
end

#list_volumesObject



34
35
36
37
38
39
# File 'lib/fog/ibm/requests/storage/list_volumes.rb', line 34

def list_volumes
  response = Excon::Response.new
  response.status = 200
  response.body = { 'volumes' => format_list_volumes_response }
  response
end

#ready_volume(volume_id) ⇒ Object

Sets volume status to Detached if it's not already set, and or attached



61
62
63
64
# File 'lib/fog/ibm/requests/storage/get_volume.rb', line 61

def ready_volume(volume_id)
  # If not ready, make ready
  self.data[:volumes][volume_id]['state'] = 4
end

#reset_dataObject



63
64
65
66
# File 'lib/fog/ibm/storage.rb', line 63

def reset_data
  self.class.data.delete(@ibm_username)
  @data = self.class.data[@ibm_username]
end

#volume_attached?(volume_id) ⇒ Boolean



56
57
58
# File 'lib/fog/ibm/requests/storage/get_volume.rb', line 56

def volume_attached?(volume_id)
  self.data[:volumes][volume_id]['instanceId'] != "0"
end

#volume_exists?(volume_id) ⇒ Boolean



47
48
49
# File 'lib/fog/ibm/requests/storage/get_volume.rb', line 47

def volume_exists?(volume_id)
  self.data[:volumes].key? volume_id
end

#volume_ready?(volume_id) ⇒ Boolean

Checks if an volume is Active



52
53
54
# File 'lib/fog/ibm/requests/storage/get_volume.rb', line 52

def volume_ready?(volume_id)
  self.data[:volumes][volume_id]['state'] == 4
end