Class: Fog::Rackspace::BlockStorage::Mock

Inherits:
Service
  • Object
show all
Includes:
MockData
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

Constant Summary

Constants included from MockData

MockData::NOT_FOUND_ID

Instance Method Summary collapse

Methods included from MockData

#data, ipv4_address, ipv6_address, keep, slice, uuid, zulu_time

Methods inherited from Service

#authenticate, #endpoint_uri, #region, #service_name

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



51
52
53
# File 'lib/fog/rackspace/block_storage.rb', line 51

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

Instance Method Details

#create_snapshot(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
# File 'lib/fog/rackspace/requests/block_storage/create_snapshot.rb', line 46

def create_snapshot(volume_id, options = {})
  volume = self.data[:volumes][volume_id]
  if volume.nil?
    raise Fog::Rackspace::BlockStorage::NotFound
  else
    snapshot_id         = Fog::Rackspace::MockData.uuid
    display_name        = options[:display_name] || "test snapshot"
    display_description = options[:display_description] || "test snapshot description"

    snapshot = {
      "id"                  => snapshot_id,
      "display_name"        => display_name,
      "display_description" => display_description,
      "volume_id"           => volume_id,
      "status"              => "available",
      "size"                => volume["size"],
      "created_at"          => Fog::Rackspace::MockData.zulu_time,
      "availability_zone"   => "nova",
    }

    self.data[:snapshots][snapshot_id] = snapshot

    response(:body => {"snapshot" => snapshot})
  end
end

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



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
79
80
81
82
83
84
85
# File 'lib/fog/rackspace/requests/block_storage/create_volume.rb', line 52

def create_volume(size, options = {})
  if size < 1 && !options[:snapshot_id]
    raise Fog::Rackspace::BlockStorage::BadRequest
  elsif size < 100 || size > 1024
    raise Fog::Rackspace::BlockStorage::BadRequest.new("'size' parameter must be between 100 and 1024")
  else
    volume_id         = Fog::Rackspace::MockData.uuid
    name              = options[:display_name] || "test volume"
    description       = options[:display_description] || "description goes here"
    volume_type       = options[:volume_type] || "SATA"

    volume = {
      "id"                  => volume_id,
      "display_name"        => name,
      "display_description" => description,
      "size"                => size,
      "status"              => "available",
      "volume_type"         => volume_type,
      "snapshot_id"         => nil,
      "attachments"         => [],
      "created_at"          => Fog::Rackspace::MockData.zulu_time,
      "availability_zone"   => "nova",
      "metadata"            => {},
    }
    if options[:snapshot_id]
      snapshot = self.data[:snapshots][snapshot_id]
      volume.merge!("size" => snapshot["size"])
    end

    self.data[:volumes][volume_id] = volume

    response(:body => {"volume" => volume})
  end
end

#delete_snapshot(snapshot_id) ⇒ Object



21
22
23
24
# File 'lib/fog/rackspace/requests/block_storage/delete_snapshot.rb', line 21

def delete_snapshot(snapshot_id)
  self.data[:snapshots].delete(snapshot_id)
  response(:status => 202)
end

#delete_volume(volume_id) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/rackspace/requests/block_storage/delete_volume.rb', line 22

def delete_volume(volume_id)
  volume = self.data[:volumes][volume_id]
  snapshots = self.data[:snapshots].values.select { |s| s["volume_id"] == volume_id }
  raise Fog::Rackspace::BlockStorage::BadRequest.new("Volume still has #{snapshots.count} dependent snapshots") if !snapshots.empty?
  unless volume.nil?
    unless volume["attachments"].empty?
      raise Fog::Rackspace::BlockStorage::BadRequest.new("Volume status must be available or error")
    end
    self.data[:volumes].delete(volume_id)
    response(:status => 202)
  else
    raise Fog::Rackspace::BlockStorage::NotFound
  end
end

#get_snapshot(snapshot_id) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/fog/rackspace/requests/block_storage/get_snapshot.rb', line 31

def get_snapshot(snapshot_id)
  snapshot = self.data[:snapshots][snapshot_id]
  if snapshot.nil?
    raise Fog::Rackspace::BlockStorage::NotFound
  else
    response(:body => {"snapshot" => snapshot})
  end
end

#get_volume(volume_id) ⇒ Object



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

def get_volume(volume_id)
  possible_states = ["available", "in-use"]
  volume = self.data[:volumes][volume_id]
  if volume.nil?
    raise Fog::Rackspace::BlockStorage::NotFound
  else
    volume["status"] = possible_states[rand(possible_states.length)]
    if volume.nil?
      raise Fog::Rackspace::BlockStorage::NotFound
    else
      response(:body => {"volume" => volume})
    end
  end
end

#get_volume_type(volume_type_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/rackspace/requests/block_storage/get_volume_type.rb', line 25

def get_volume_type(volume_type_id)
  type = self.data[:volume_types][volume_type_id]
  if type.nil?
    raise Fog::Rackspace::BlockStorage::NotFound
  else
    type = type.dup
    type["id"] = type["id"].to_s
    response(:body => {"volume_type" => type})
  end
end

#list_snapshotsObject



28
29
30
31
32
# File 'lib/fog/rackspace/requests/block_storage/list_snapshots.rb', line 28

def list_snapshots
  snapshots = self.data[:snapshots].values

  response(:body => {"snapshots" => snapshots})
end

#list_volume_typesObject



24
25
26
27
# File 'lib/fog/rackspace/requests/block_storage/list_volume_types.rb', line 24

def list_volume_types
  types = self.data[:volume_types].values
  response(:body => {"volume_types" => types})
end

#list_volumesObject



32
33
34
35
# File 'lib/fog/rackspace/requests/block_storage/list_volumes.rb', line 32

def list_volumes
  volumes = self.data[:volumes].values
  response(:body => {"volumes" => volumes})
end

#request(params) ⇒ Object



55
56
57
# File 'lib/fog/rackspace/block_storage.rb', line 55

def request(params)
  Fog::Mock.not_implemented
end

#response(params = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fog/rackspace/block_storage.rb', line 59

def response(params={})
  body    = params[:body] || {}
  status  = params[:status] || 200
  headers = params[:headers] || {}

  response = Excon::Response.new(:body => body, :headers => headers, :status => status)
  if params.has_key?(:expects) && ![*params[:expects]].include?(response.status)
    raise(Excon::Errors.status_error(params, response))
  else response
  end
end