Class: Fog::HP::BlockStorage::Mock

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fog/hp/requests/block_storage/list_volumes.rb,
lib/fog/hp/block_storage.rb,
lib/fog/hp/requests/block_storage/create_volume.rb,
lib/fog/hp/requests/block_storage/delete_volume.rb,
lib/fog/hp/requests/block_storage/list_snapshots.rb,
lib/fog/hp/requests/block_storage/create_snapshot.rb,
lib/fog/hp/requests/block_storage/delete_snapshot.rb,
lib/fog/hp/requests/block_storage/get_volume_details.rb,
lib/fog/hp/requests/block_storage/get_snapshot_details.rb,
lib/fog/hp/requests/block_storage/list_bootable_volumes.rb,
lib/fog/hp/requests/block_storage/get_bootable_volume_details.rb

Overview

:nodoc:all

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#compute

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fog/hp/block_storage.rb', line 66

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.")
    @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
end

Class Method Details

.dataObject



53
54
55
56
57
58
59
60
# File 'lib/fog/hp/block_storage.rb', line 53

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

.resetObject



62
63
64
# File 'lib/fog/hp/block_storage.rb', line 62

def self.reset
  @data = nil
end

Instance Method Details

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/hp/requests/block_storage/create_snapshot.rb', line 49

def create_snapshot(name, description, volume_id, options={})
  response = Excon::Response.new
  if self.data[:volumes][volume_id]
    response.status = 200
    data = {
      'id'                 => Fog::Mock.random_numbers(3).to_i,
      'displayName'        => name,
      'displayDescription' => description,
      'size'               => self.data[:volumes][volume_id]['size'],
      'status'             => 'available',
      'volumeId'           => volume_id,
      'createdAt'          => Time.now.to_s
    }
    self.data[:snapshots][data['id']] = data
    response.body = { 'snapshot' => data }
  else
    raise Fog::HP::BlockStorage::NotFound
  end
  response
end

#create_volume(name, description, size, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/hp/requests/block_storage/create_volume.rb', line 55

def create_volume(name, description, size, options={})
  if options['snapshotId'] && options['imageRef']
    raise Fog::Errors::BadRequest.new("Snapshot and image cannot be specified together.")
  else
    response = Excon::Response.new
    response.status = 200
    data = {
      'id'                 => Fog::Mock.random_numbers(3).to_i,
      'displayName'        => name,
      'displayDescription' => description,
      'size'               => size,
      'status'             => 'available',
      'snapshotId'         => options['snapshot_id'] || "",
      #'imageRef'           => options['imageRef'] || "", # TODO: not implemented to preserve backward compatibility
      'volumeType'         => nil,
      'availabilityZone'   => 'nova',
      'createdAt'          => Time.now.to_s,
      'metadata'           => options['metadata'] || {},
      'attachments'        => [{}]
    }
    self.data[:volumes][data['id']] = data
    response.body = { 'volume' => data }
    response
  end
end

#dataObject



78
79
80
# File 'lib/fog/hp/block_storage.rb', line 78

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

#delete_snapshot(snapshot_id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/hp/requests/block_storage/delete_snapshot.rb', line 21

def delete_snapshot(snapshot_id)
  response = Excon::Response.new
  if self.data[:snapshots][snapshot_id]
    self.data[:snapshots].delete(snapshot_id)
    response.status = 202
  else
    raise Fog::HP::BlockStorage::NotFound
  end
  response
end

#delete_volume(volume_id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/hp/requests/block_storage/delete_volume.rb', line 21

def delete_volume(volume_id)
  response = Excon::Response.new
  if self.data[:volumes][volume_id]
    self.data[:volumes].delete(volume_id)
    response.status = 202
  else
    raise Fog::HP::BlockStorage::NotFound
  end
  response
end

#get_bootable_volume_details(volume_id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fog/hp/requests/block_storage/get_bootable_volume_details.rb', line 38

def get_bootable_volume_details(volume_id)
  unless volume_id
    raise ArgumentError.new('volume_id is required')
  end
  response = Excon::Response.new
  if volume = self.data[:volumes][volume_id]
    response.status = 200
    response.body = { 'volume' => volume }
    response
  else
    raise Fog::HP::BlockStorage::NotFound
  end
end

#get_snapshot_details(snapshot_id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/hp/requests/block_storage/get_snapshot_details.rb', line 33

def get_snapshot_details(snapshot_id)
  unless snapshot_id
    raise ArgumentError.new('snapshot_id is required')
  end
  response = Excon::Response.new
  if snapshot = self.data[:snapshots][snapshot_id]
    response.status = 200
    response.body = { 'snapshot' => snapshot }
    response
  else
    raise Fog::HP::BlockStorage::NotFound
  end
end

#get_volume_details(volume_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/hp/requests/block_storage/get_volume_details.rb', line 37

def get_volume_details(volume_id)
  unless volume_id
    raise ArgumentError.new('volume_id is required')
  end
  response = Excon::Response.new
  if volume = self.data[:volumes][volume_id]
    response.status = 200
    response.body = { 'volume' => volume }
    response
  else
    raise Fog::HP::BlockStorage::NotFound
  end
end

#list_bootable_volumesObject



37
38
39
40
41
42
43
44
45
# File 'lib/fog/hp/requests/block_storage/list_bootable_volumes.rb', line 37

def list_bootable_volumes
  response = Excon::Response.new
  volumes = []
  volumes = self.data[:volumes].values unless self.data[:volumes].nil?

  response.status = 200
  response.body = { 'volumes' => volumes }
  response
end

#list_snapshotsObject



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

def list_snapshots
  response = Excon::Response.new
  snapshots = []
  snapshots = self.data[:snapshots].values unless self.data[:snapshots].nil?

  response.status = 200
  response.body = { 'snapshots' => snapshots }
  response
end

#list_volumesObject



36
37
38
39
40
41
42
43
44
# File 'lib/fog/hp/requests/block_storage/list_volumes.rb', line 36

def list_volumes
  response = Excon::Response.new
  volumes = []
  volumes = self.data[:volumes].values unless self.data[:volumes].nil?

  response.status = 200
  response.body = { 'volumes' => volumes }
  response
end

#reset_dataObject



82
83
84
# File 'lib/fog/hp/block_storage.rb', line 82

def reset_data
  self.class.data.delete(@hp_access_key)
end