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.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/hp/block_storage.rb', line 69

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



56
57
58
59
60
61
62
63
# File 'lib/fog/hp/block_storage.rb', line 56

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

.resetObject



65
66
67
# File 'lib/fog/hp/block_storage.rb', line 65

def self.reset
  @data = nil
end

Instance Method Details

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



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

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



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

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



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

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

#delete_snapshot(snapshot_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/hp/requests/block_storage/delete_snapshot.rb', line 24

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



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

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/hp/requests/block_storage/get_bootable_volume_details.rb', line 41

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



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

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/hp/requests/block_storage/get_volume_details.rb', line 40

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



40
41
42
43
44
45
46
47
48
# File 'lib/fog/hp/requests/block_storage/list_bootable_volumes.rb', line 40

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



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

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



39
40
41
42
43
44
45
46
47
# File 'lib/fog/hp/requests/block_storage/list_volumes.rb', line 39

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



85
86
87
# File 'lib/fog/hp/block_storage.rb', line 85

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