1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[openstack|volume] Volume Snapshot CRUD

This commit is contained in:
Marjun Pagalan 2012-03-01 17:37:40 +08:00 committed by Nelvin Driz
parent f67a186902
commit 1bd045767e
8 changed files with 206 additions and 1 deletions

View file

@ -33,6 +33,8 @@ module Fog
collection :volumes collection :volumes
model :network model :network
collection :networks collection :networks
model :snapshot
collection :snapshots
## REQUESTS ## REQUESTS
# #
@ -123,6 +125,11 @@ module Fog
request :get_volume_details request :get_volume_details
request :delete_volume request :delete_volume
request :create_volume_snapshot
request :list_snapshots
request :get_snapshot_details
request :delete_snapshot
# Usage # Usage
request :list_usages request :list_usages

View file

@ -0,0 +1,43 @@
require 'fog/core/model'
require 'fog/openstack/models/compute/metadata'
module Fog
module Compute
class OpenStack
class Snapshot < Fog::Model
identity :id
attribute :name, :aliases => 'displayName'
attribute :description, :aliases => 'displayDescription'
attribute :volume_id, :aliases => 'volumeId'
attribute :status
attribute :size
attribute :created_at, :aliases => 'createdAt'
def initialize(attributes)
@connection = attributes[:connection]
super
end
def save(force=false)
requires :volume_id, :name, :description
data = connection.create_volume(volume_id, name, description, force)
merge_attributes(data.body['snapshot'])
true
end
def destroy
requires :id
connection.delete_snapshot(id)
true
end
end
end
end
end

View file

@ -0,0 +1,26 @@
require 'fog/core/collection'
require 'fog/openstack/models/compute/snapshot'
module Fog
module Compute
class OpenStack
class Snapshots < Fog::Collection
model Fog::Compute::OpenStack::Snapshot
def all(detailed=true)
load(connection.list_snapshots(detailed).body['snapshots'])
end
def get(snapshot_id)
if snapshot = connection.get_snapshot_details(snapshot_id).body['snapshot']
new(snapshot)
end
rescue Fog::Compute::OpenStack::NotFound
nil
end
end
end
end
end

View file

@ -0,0 +1,32 @@
module Fog
module Compute
class OpenStack
class Real
def create_volume_snapshot(volume_id, name, description, force=false)
data = {
'snapshot' => {
'volume_id' => volume_id,
'display_name' => name,
'display_description' => description,
'force' => force
}
}
request(
:body => MultiJson.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "os-snapshots"
)
end
end
class Mock
end
end
end
end

View file

@ -0,0 +1,26 @@
module Fog
module Compute
class OpenStack
class Real
def delete_snapshot(snapshot_id)
request(
:expects => 202,
:method => 'DELETE',
:path => "os-snapshots/#{snapshot_id}"
)
end
end
class Mock
def delete_snapshot(snapshot_id)
response = Excon::Response.new
response.status = 204
response
end
end
end
end
end

View file

@ -5,7 +5,7 @@ module Fog
def delete_volume(volume_id) def delete_volume(volume_id)
request( request(
:expects => 204, :expects => 202,
:method => 'DELETE', :method => 'DELETE',
:path => "os-volumes/#{volume_id}" :path => "os-volumes/#{volume_id}"
) )

View file

@ -0,0 +1,39 @@
module Fog
module Compute
class OpenStack
class Real
def get_snapshot_details(snapshot_id)
request(
:expects => 200,
:method => 'GET',
:path => "os-snapshots/#{snapshot_id}"
)
end
end
class Mock
def get_snapshot_details(detailed=true)
response = Excon::Response.new
response.status = 200
response.body = {
'snapshot' => {
'id' => '1',
'displayName' => Fog::Mock.random_letters(rand(8) + 5),
'displayDescription' => Fog::Mock.random_letters(rand(12) + 10),
'size' => 3,
'volumeId' => '4',
'status' => 'online',
'createdAt' => Time.now
}
}
response
end
end
end
end
end

View file

@ -0,0 +1,32 @@
module Fog
module Compute
class OpenStack
class Real
def list_snapshots(detailed=true)
path = detailed ? 'os-snapshots/detail' : 'os-snapshots'
request(
:expects => 200,
:method => 'GET',
:path => path
)
end
end
class Mock
def list_snapshots(detailed=true)
response = Excon::Response.new
response.status = 200
response.body = {
'snapshots' => [get_snapshot_details.body["snapshot"]]
}
response
end
end
end
end
end