Sha256: cc8aa20eef6c55c10e4bbd29f88f8ba3af85a6b845c6a97817d533c80f0d58e9

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

class Server < ActiveRecord::Base

  belongs_to :availability_zone, inverse_of: :servers
  belongs_to :flavor, inverse_of: :servers

  has_many :volumes, inverse_of: :server
  has_many :tags, as: :taggable

  def self.import(aws_host)
    server = Server.where(remote_id: aws_host.id).first || Server.new(remote_id: aws_host.id)
    server.attributes = {
      name: aws_host.tags["Name"],
      server_type: aws_host.tags["type"],
      account_id: aws_host.tags["account_id"],
      state: aws_host.state,
      remote_id: aws_host.id,
      ebs_optimized: aws_host.ebs_optimized,
      availability_zone: AvailabilityZone.find_or_create_by(name: aws_host.availability_zone),
      flavor: Flavor.find_or_create_by(name: aws_host.flavor_id),
      created_at: aws_host.created_at,
      deleted_at: nil
    }

    print "."
    server.save!

    aws_host.tags.each do |key, value|
      next if value.nil?
      server.tags.find_or_create_by(name: key.downcase).update_attributes(value: value.downcase)
    end

    server
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
crop-duster-0.0.14 lib/models/server.rb
crop-duster-0.0.13 lib/models/server.rb
crop-duster-0.0.12 lib/models/server.rb
crop-duster-0.0.11 lib/models/server.rb