lib/dcmgr/models/volume.rb in wakame-vdc-dcmgr-11.06.0 vs lib/dcmgr/models/volume.rb in wakame-vdc-dcmgr-11.12.0
- old
+ new
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
-require 'statemachine'
module Dcmgr::Models
class Volume < AccountResource
taggable 'vol'
@@ -19,35 +18,11 @@
STATE_TYPE_FAILED = "failed"
STATE_TYPE_DEREGISTERING = "deregistering"
STATE_TYPE_DELETING = "deleting"
STATE_TYPE_DELETED = "deleted"
- inheritable_schema do
- Fixnum :storage_pool_id, :null=>false
- String :status, :null=>false, :default=>STATUS_TYPE_REGISTERING
- String :state, :null=>false, :default=>STATE_TYPE_REGISTERING
- Fixnum :size, :null=>false
- Fixnum :instance_id
- Fixnum :boot_dev, :null=>false, :default=>0
- String :snapshot_id
- String :host_device_name
- String :guest_device_name
- String :export_path, :null=>false
-# String :intermediate_path, :null=>false
- Text :transport_information
- Time :deleted_at
- Time :attached_at
- Time :detached_at
-
- index :storage_pool_id
- index :instance_id
- index :snapshot_id
- index :deleted_at
- end
- with_timestamps
-
- many_to_one :storage_pool
+ many_to_one :storage_node, :after_set=>:validate_storage_node_assigned
many_to_one :instance
plugin ArchiveChangedColumn, :histories
subset(:lives, {:deleted_at => nil})
@@ -63,29 +38,42 @@
# method calls.
# Possible column data:
# iscsi:
# {:iqn=>'iqn.1986-03.com.sun:02:a1024afa-775b-65cf-b5b0-aa17f3476bfc', :lun=>0}
plugin :serialization, :yaml, :transport_information
+ plugin :serialization, :yaml, :request_params
- class DiskError < RuntimeError; end
+ class CapacityError < RuntimeError; end
class RequestError < RuntimeError; end
- def before_create
- sp = self.storage_pool
+ def validate_storage_node_assigned(sp)
+ unless sp.is_a?(StorageNode)
+ raise "unknown class: #{sp.class}"
+ end
volume_size = sp.volumes_dataset.lives.sum(:size).to_i
# check if the sum of available volume and new volume is under
# the limit of offering capacity.
total_size = sp.offering_disk_space - volume_size.to_i
if self.size > total_size
- raise DiskError, "out of disk space"
+ raise CapacityError, "Allocation exceeds storage node blank size: #{}"
end
+ end
+
+ def validate
+ # do not run validation if the row is maked as deleted.
+ return true if self.deleted_at
- # TODO: Here may not be the right place for capacity validation.
- per_account_totoal = self.class.filter(:account_id=>self.account_id).lives.sum(:size).to_i
- if self.account.quota.volume_total_size < per_account_totoal + self.size.to_i
- raise DiskError, "Out of account quota: #{self.account.quota.volume_total_size}, #{self.size.to_i}, #{per_account_totoal}"
+ if new?
+ # TODO: Here may not be the right place for capacity validation.
+ per_account_total = self.class.filter(:account_id=>self.account_id).lives.sum(:size).to_i
+ if self.account.quota.volume_total_size < per_account_total + self.size.to_i
+ raise CapacityError, "Allocation exceeds account's quota: #{self.account.quota.volume_total_size}, #{self.size.to_i}, #{per_account_total}"
+ end
end
+
+ errors.add(:size, "Invalid volume size.") if self.size == 0
+
super
end
def self.get_list(account_id, *args)
data = args.first
@@ -123,11 +111,11 @@
v
end
def merge_pool_data
v = self.to_hash
- v.merge(:storage_pool=>storage_pool.to_hash)
+ v.merge(:storage_node=>storage_node.to_hash)
end
def to_hash
h = super
# yaml -> hash translation
@@ -155,11 +143,35 @@
%w(available attached).member?(self.state)
end
def create_snapshot(account_id)
vs = VolumeSnapshot.create(:account_id=>account_id,
- :storage_pool_id=>self.storage_pool_id,
+ :storage_node_id=>self.storage_node_id,
:origin_volume_id=>self.canonical_uuid,
:size=>self.size)
end
+
+ # override Sequel::Model#delete not to delete rows but to set
+ # delete flags.
+ def delete
+ self.deleted_at ||= Time.now
+ self.state = :deleted if self.state != :deleted
+ self.status = :offline if self.status != :offline
+ self.save
+ end
+
+ def snapshot
+ VolumeSnapshot[self.snapshot_id]
+ end
+
+ def self.entry_new(account, size, params, &blk)
+ # Mash is passed in some cases.
+ raise ArgumentError unless params.class == ::Hash
+ v = self.new &blk
+ v.account_id = account.canonical_uuid
+ v.size = size
+ v.request_params = params
+ v
+ end
+
end
end