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.



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

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

Instance Method Details

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



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/rackspace/requests/block_storage/create_snapshot.rb', line 50

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



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
86
87
88
89
# File 'lib/fog/rackspace/requests/block_storage/create_volume.rb', line 56

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



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

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

#delete_volume(volume_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/rackspace/requests/block_storage/delete_volume.rb', line 26

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



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

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



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

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



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

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



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

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

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

#list_volume_typesObject



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

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

#list_volumesObject



36
37
38
39
# File 'lib/fog/rackspace/requests/block_storage/list_volumes.rb', line 36

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

#request(params) ⇒ Object



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

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

#response(params = {}) ⇒ Object



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

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