class Volume < ActiveRecord::Base belongs_to :server, inverse_of: :volumes belongs_to :availability_zone, inverse_of: :volumes has_many :tags, as: :taggable before_validation :set_hourly_rate def self.import(aws_volume) volume = Volume.where(remote_id: aws_volume.id).first || Volume.new(remote_id: aws_volume.id) volume.attributes = { name: aws_volume.tags["Name"], remote_id: aws_volume.id, state: aws_volume.state, volume_type: aws_volume.type, iops: aws_volume.iops, size: aws_volume.size, server: Server.where(remote_id: aws_volume.server_id).first, availability_zone: AvailabilityZone.find_or_create_by(name: aws_volume.availability_zone), created_at: aws_volume.created_at, deleted_at: nil } volume.save! aws_volume.tags.each do |key, value| volume.tags.find_or_create_by(name: key.downcase).update_attributes(value: value.downcase) end volume end def set_hourly_rate self.hourly_rate = if volume_type == "standard" (0.10 * self.size) / 720 elsif volume_type == "io1" ((0.125 * self.size) + (0.10 * self.iops)) / 720 else raise "Unknown volume_type : #{self.volume_type}" end end end