Class: Server

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/models/server.rb

Class Method Summary collapse

Class Method Details

.import(aws_host) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/models/server.rb', line 9

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