Class: Fog::HP::BlockStorageV2::Mock

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fog/hp/requests/block_storage_v2/list_volumes.rb,
lib/fog/hp/block_storage_v2.rb,
lib/fog/hp/requests/block_storage_v2/create_volume.rb,
lib/fog/hp/requests/block_storage_v2/delete_volume.rb,
lib/fog/hp/requests/block_storage_v2/update_volume.rb,
lib/fog/hp/requests/block_storage_v2/list_snapshots.rb,
lib/fog/hp/requests/block_storage_v2/create_snapshot.rb,
lib/fog/hp/requests/block_storage_v2/delete_snapshot.rb,
lib/fog/hp/requests/block_storage_v2/update_snapshot.rb,
lib/fog/hp/requests/block_storage_v2/get_volume_details.rb,
lib/fog/hp/requests/block_storage_v2/list_volume_backups.rb,
lib/fog/hp/requests/block_storage_v2/list_volumes_detail.rb,
lib/fog/hp/requests/block_storage_v2/create_volume_backup.rb,
lib/fog/hp/requests/block_storage_v2/delete_volume_backup.rb,
lib/fog/hp/requests/block_storage_v2/get_snapshot_details.rb,
lib/fog/hp/requests/block_storage_v2/list_snapshots_detail.rb,
lib/fog/hp/requests/block_storage_v2/restore_volume_backup.rb,
lib/fog/hp/requests/block_storage_v2/get_volume_backup_details.rb,
lib/fog/hp/requests/block_storage_v2/list_volume_backups_detail.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.



77
78
79
# File 'lib/fog/hp/block_storage_v2.rb', line 77

def initialize(options={})
  @hp_access_key = options[:hp_access_key]
end

Class Method Details

.dataObject



63
64
65
66
67
68
69
70
71
# File 'lib/fog/hp/block_storage_v2.rb', line 63

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

.resetObject



73
74
75
# File 'lib/fog/hp/block_storage_v2.rb', line 73

def self.reset
  @data = nil
end

Instance Method Details

#copy_volume_data(volume_id, backup_volume) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fog/hp/requests/block_storage_v2/restore_volume_backup.rb', line 76

def copy_volume_data(volume_id, backup_volume)
  data = {
      'id'                  => volume_id,
      'status'              => 'available',
      'display_name'        => backup_volume['display_name'],
      'attachments'         => backup_volume['attachments'],
      'availability_zone'   => backup_volume['availability_zone'],
      'bootable'            => backup_volume['bootable'],
      'created_at'          => Time.now.to_s,
      'display_description' => backup_volume['display_description'],
      'volume_type'         => backup_volume['volume_type'],
      'snapshot_id'         => backup_volume['snapshot_id'],
      'source_volid'        => backup_volume['source_volid'],
      'metadata'            => backup_volume['metadata'],
      'size'                => backup_volume['size']
  }
end

#create_snapshot(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_v2/create_snapshot.rb', line 52

def create_snapshot(volume_id, options={})
  response = Excon::Response.new
  if self.data[:volumes][volume_id]
    response.status = 200
    data = {
      'id'                  => Fog::HP::Mock.uuid.to_s,
      'display_name'        => options['display_name'] || '',
      'display_description' => options['display_description'] || '',
      'size'                => self.data[:volumes][volume_id]['size'],
      'status'              => 'available',
      'volume_id'           => volume_id,
      'created_at'          => Time.now.to_s
    }
    self.data[:snapshots][data['id']] = data
    response.body = { 'snapshot' => data }
  else
    raise Fog::HP::BlockStorageV2::NotFound
  end
  response
end

#create_volume(options = {}) ⇒ Object



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

def create_volume(options={})
  if options['snapshot_id'] && options['imageRef'] && options['source_volid']
    raise Fog::Errors::BadRequest.new('The snapshot_id, imageRef and the source_volid parameters are mutually exclusive, and only one should be specified in the request.')
  else
    response = Excon::Response.new
    response.status = 200
    data = {
      'id'                  => Fog::HP::Mock.uuid.to_s,
      'status'              => 'available',
      'display_name'        => options['display_name'] || '',
      'attachments'         => [{}],
      'availability_zone'   => options['availability_zone'] || 'az1',
      'bootable'            => false,
      'created_at'          => Time.now.to_s,
      'display_description' => options['display_description'] || '',
      'volume_type'         => 'None',
      'snapshot_id'         => options['snapshot_id'] || '',
      'source_volid'        => options['source_volid'] || '',
      'metadata'            => options['metadata'] || {},
      'size'                => options['size']
    }
    self.data[:volumes][data['id']] = data
    response.body = { 'volume' => data }
    response
  end
end

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fog/hp/requests/block_storage_v2/create_volume_backup.rb', line 46

def create_volume_backup(volume_id, options={})
  response = Excon::Response.new
  tenant_id = Fog::Mock.random_numbers(14).to_s
  id = Fog::HP::Mock.uuid.to_s

  if volume = self.data[:volumes][volume_id]
    data = {
      'id'                  => id,
      'status'              => 'available',
      'name'                => options['name'] || '',
      'description'         => options['description'] || '',
      'container'           => options['container'] || 'volumebackups',
      'availability_zone'   => volume['availability_zone'],
      'created_at'          => Time.now.to_s,
      'volume_id'           => volume_id,
      'size'                => volume['size'],
      'links'               => [{'href'=>"http://cinder:8776/v1/#{tenant_id}/backups/#{id}", 'rel'=>'self'}, {'href'=>"http://cinder:8776/v1/#{tenant_id}/backups/#{id}", 'rel'=>'bookmark'}],
      'fail_reason'         => '',
      'object_count'        => 1
    }
    resp_data = {
      'id'                  => id,
      'name'                => options['name'] || '',
      'links'               => [{'href'=>"http://cinder:8776/v1/#{tenant_id}/backups/#{id}", 'rel'=>'self'}, {'href'=>"http://cinder:8776/v1/#{tenant_id}/backups/#{id}", 'rel'=>'bookmark'}]
    }
    self.data[:volume_backups][data['id']] = data
    response.status = 202
    response.body = { 'backup' => resp_data }
    response
  else
    raise Fog::HP::BlockStorageV2::NotFound
  end
end

#dataObject



81
82
83
# File 'lib/fog/hp/block_storage_v2.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_v2/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::BlockStorageV2::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_v2/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::BlockStorageV2::NotFound
  end
  response
end

#delete_volume_backup(backup_id) ⇒ Object



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

def delete_volume_backup(backup_id)
  response = Excon::Response.new
  if self.data[:volume_backups][backup_id]
    self.data[:volume_backups].delete(backup_id)
    response.status = 202
  else
    raise Fog::HP::BlockStorageV2::NotFound
  end
  response
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_v2/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::BlockStorageV2::NotFound
  end

end

#get_volume_backup_details(backup_id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fog/hp/requests/block_storage_v2/get_volume_backup_details.rb', line 40

def get_volume_backup_details(backup_id)
  response = Excon::Response.new
  if backup = self.data[:volume_backups][backup_id]
    response.status = 200
    response.body = { 'backup' => backup }
    response
  else
    raise Fog::HP::BlockStorageV2::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_v2/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::BlockStorageV2::NotFound
  end

end

#list_snapshots(options = {}) ⇒ Object



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

def list_snapshots(options={})
  response = Excon::Response.new
  snapshots = []
  data = list_snapshots_detail.body['snapshots']
  for snapshot in data
    snapshots << snapshot.reject { |key, _| ['volume_image_metadata'].include?(key) }
  end

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

#list_snapshots_detail(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/fog/hp/requests/block_storage_v2/list_snapshots_detail.rb', line 41

def list_snapshots_detail(options={})
  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_volume_backups(options = {}) ⇒ Object



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

def list_volume_backups(options = {})
  response = Excon::Response.new
  backups = []
  data = list_volume_backups_detail.body['backups']
  for backup in data
    backups << backup.reject { |key, _| !['id', 'name', 'links'].include?(key) }
  end

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

#list_volume_backups_detail(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/fog/hp/requests/block_storage_v2/list_volume_backups_detail.rb', line 45

def list_volume_backups_detail(options = {})
  response = Excon::Response.new
  backups = []
  backups = self.data[:volume_backups].values unless self.data[:volume_backups].nil?

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

#list_volumes(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/hp/requests/block_storage_v2/list_volumes.rb', line 45

def list_volumes(options = {})
  response = Excon::Response.new
  volumes = []
  data = list_volumes_detail.body['volumes']
  for volume in data
    volumes << volume.reject { |key, _| ['volume_image_metadata'].include?(key) }
  end

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

#list_volumes_detail(options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/fog/hp/requests/block_storage_v2/list_volumes_detail.rb', line 46

def list_volumes_detail(options = {})
  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_v2.rb', line 85

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

#restore_volume_backup(backup_id, options = {}) ⇒ Object



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
69
70
71
72
73
74
# File 'lib/fog/hp/requests/block_storage_v2/restore_volume_backup.rb', line 40

def restore_volume_backup(backup_id, options={})
  volume_id = options['volume_id']

  response = Excon::Response.new
  if backup_volume = self.data[:volume_backups][backup_id]
    # get the volume from the backup that will be used to restore
    volume_to_restore = self.data[:volumes][backup_volume['volume_id']]
    if volume_id
      # overwrite the volume specified by the backup
      if self.data[:volumes][volume_id]
        data = copy_volume_data(volume_id, volume_to_restore)
        resp_volume_id = volume_id
      else
        raise Fog::HP::BlockStorageV2::NotFound.new("Invalid volume: '#{volume_id}' specified")
      end
    else
      # create a new volume and restore the backup
      new_vol = create_volume('display_name' => 'restore_backup', 'size' => 1).body
      new_vol_id = new_vol['volume']['id']
      data = copy_volume_data(new_vol_id, volume_to_restore)
      resp_volume_id = new_vol_id
    end
    # update the existing volume or create a new volume
    self.data[:volumes][resp_volume_id] = data
    resp_data = {
      'backup_id' => backup_id,
      'volume_id' => resp_volume_id
    }
    response.status = 202
    response.body = { 'restore' => resp_data }
  else
    raise Fog::HP::BlockStorageV2::NotFound
  end
  response
end

#update_snapshot(snapshot_id, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fog/hp/requests/block_storage_v2/update_snapshot.rb', line 48

def update_snapshot(snapshot_id, options={})
  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
    snapshot['display_name'] = options['display_name'] if options['display_name']
    snapshot['display_description'] = options['display_description'] if options['display_description']
    snapshot['metadata'] = options['metadata'] if options['metadata']
    response.body = { 'snapshot' => snapshot }
    response
  else
    raise Fog::HP::BlockStorageV2::NotFound
  end

end

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fog/hp/requests/block_storage_v2/update_volume.rb', line 53

def update_volume(volume_id, options={})
  response = Excon::Response.new
  if volume = self.data[:volumes][volume_id]
    response.status = 200
    volume['display_name'] = options['display_name'] if options['display_name']
    volume['display_description'] = options['display_description'] if options['display_description']
    volume['metadata'] = options['metadata'] if options['metadata']
    response.body = { 'volume' => volume }
    response
  else
    raise Fog::HP::BlockStorageV2::NotFound
  end
end