Class: Fog::Model

Inherits:
Object
  • Object
show all
Extended by:
Attributes::ClassMethods
Includes:
Attributes::InstanceMethods
Defined in:
lib/fog/core/model.rb

Direct Known Subclasses

AWS::AutoScaling::Activity, AWS::AutoScaling::Configuration, AWS::AutoScaling::Group, AWS::AutoScaling::Instance, AWS::CloudWatch::Alarm, AWS::CloudWatch::AlarmDatum, AWS::CloudWatch::AlarmHistory, AWS::CloudWatch::Metric, AWS::CloudWatch::MetricStatistic, AWS::ELB::Listener, AWS::ELB::LoadBalancer, AWS::ELB::Policy, AWS::ElasticBeanstalk::Application, AWS::ElasticBeanstalk::Environment, AWS::ElasticBeanstalk::Event, AWS::ElasticBeanstalk::Template, AWS::ElasticBeanstalk::Version, AWS::Elasticache::Cluster, AWS::Elasticache::ParameterGroup, AWS::Elasticache::SecurityGroup, AWS::RDS::Parameter, AWS::RDS::ParameterGroup, AWS::RDS::SecurityGroup, AWS::RDS::Server, AWS::RDS::Snapshot, Compute::AWS::Address, Compute::AWS::Flavor, Compute::AWS::Image, Compute::AWS::KeyPair, Compute::AWS::NetworkInterface, Compute::AWS::SecurityGroup, Compute::AWS::Snapshot, Compute::AWS::SpotRequest, Compute::AWS::Subnet, Compute::AWS::Tag, Compute::AWS::VPC, Compute::AWS::Volume, Compute::Bluebox::Flavor, Compute::Bluebox::Image, Compute::Bluebox::Location, Compute::Brightbox::Account, Compute::Brightbox::CloudIp, Compute::Brightbox::FirewallPolicy, Compute::Brightbox::FirewallRule, Compute::Brightbox::Flavor, Compute::Brightbox::Image, Compute::Brightbox::LoadBalancer, Compute::Brightbox::ServerGroup, Compute::Brightbox::User, Compute::Brightbox::Zone, Compute::Clodo::Image, Compute::Clodo::Server, Compute::GoGrid::Image, Compute::GoGrid::Password, Compute::IBM::Address, Compute::IBM::Image, Compute::IBM::InstanceType, Compute::IBM::Key, Compute::IBM::Location, Compute::IBM::Vlan, Compute::Joyent::Flavor, Compute::Joyent::Image, Compute::Joyent::Key, Compute::Joyent::Snapshot, Compute::Libvirt::Interface, Compute::Libvirt::Network, Compute::Libvirt::Nic, Compute::Libvirt::Node, Compute::Libvirt::Pool, Compute::Libvirt::Volume, Compute::Linode::DataCenter, Compute::Linode::Disk, Compute::Linode::Flavor, Compute::Linode::Image, Compute::Linode::Ip, Compute::Linode::Kernel, Compute::Linode::StackScript, Compute::Ninefold::Address, Compute::Ninefold::Flavor, Compute::Ninefold::Image, Compute::Ninefold::IpForwardingRule, Compute::OpenStack::Flavor, Compute::OpenStack::Image, Compute::OpenStack::Meta, Compute::Ovirt::Cluster, Compute::Ovirt::Interface, Compute::Ovirt::Template, Compute::Ovirt::Volume, Compute::Rackspace::Flavor, Compute::Rackspace::Image, Compute::Server, Compute::Slicehost::Flavor, Compute::Slicehost::Image, Compute::StormOnDemand::Balancer, Compute::StormOnDemand::Config, Compute::StormOnDemand::Image, Compute::StormOnDemand::PrivateIp, Compute::StormOnDemand::Stat, Compute::StormOnDemand::Template, Compute::VirtualBox::Medium, Compute::VirtualBox::MediumFormat, Compute::VirtualBox::NATEngine, Compute::VirtualBox::NATRedirect, Compute::VirtualBox::NetworkAdapter, Compute::VirtualBox::Server, Compute::VirtualBox::StorageController, Compute::Vmfusion::Server, Compute::Voxel::Image, DNS::AWS::Record, DNS::AWS::Zone, DNS::Bluebox::Record, DNS::Bluebox::Zone, DNS::DNSMadeEasy::Record, DNS::DNSMadeEasy::Zone, DNS::DNSimple::Record, DNS::DNSimple::Zone, DNS::Dynect::Record, DNS::Dynect::Zone, DNS::Linode::Record, DNS::Linode::Zone, DNS::Rackspace::Record, DNS::Rackspace::Zone, DNS::Slicehost::Record, DNS::Slicehost::Zone, DNS::Zerigo::Record, DNS::Zerigo::Zone, Ecloud::Model, Glesys::Compute::Ip, Glesys::Compute::Template, Rackspace::LoadBalancers::AccessRule, Rackspace::LoadBalancers::LoadBalancer, Rackspace::LoadBalancers::Node, Rackspace::LoadBalancers::VirtualIp, Storage::AWS::Directory, Storage::AWS::File, Storage::AWS::Version, Storage::Google::Directory, Storage::Google::File, Storage::IBM::Offering, Storage::IBM::Volume, Storage::Local::Directory, Storage::Local::File, Storage::Ninefold::Directory, Storage::Ninefold::File, Storage::Rackspace::Directory, Storage::Rackspace::File, Terremark::Shared::Address, Terremark::Shared::Network, Terremark::Shared::Server, Terremark::Shared::Task, Terremark::Shared::Vdc, Vcloud::Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Attributes::ClassMethods

_load, aliases, attribute, attributes, identity, ignore_attributes, ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one

Constructor Details

#initialize(new_attributes = {}) ⇒ Model

Returns a new instance of Model.



9
10
11
# File 'lib/fog/core/model.rb', line 9

def initialize(new_attributes = {})
  merge_attributes(new_attributes)
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



7
8
9
# File 'lib/fog/core/model.rb', line 7

def collection
  @collection
end

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/fog/core/model.rb', line 7

def connection
  @connection
end

Instance Method Details

#inspectObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/core/model.rb', line 13

def inspect
  Thread.current[:formatador] ||= Formatador.new
  data = "#{Thread.current[:formatador].indentation}<#{self.class.name}"
  Thread.current[:formatador].indent do
    unless self.class.attributes.empty?
      data << "\n#{Thread.current[:formatador].indentation}"
      data << self.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
    end
  end
  data << "\n#{Thread.current[:formatador].indentation}>"
  data
end

#reloadObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/core/model.rb', line 26

def reload
  requires :identity

  return unless data = begin
    collection.get(identity)
  rescue Excon::Errors::SocketError
    nil
  end

  new_attributes = data.attributes
  merge_attributes(new_attributes)
  self
end

#symbolize_keys(hash) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/fog/core/model.rb', line 45

def symbolize_keys(hash)
  return nil if hash.nil?
  hash.inject({}) do |options, (key, value)|
    options[(key.to_sym rescue key) || key] = value
    options
  end
end

#to_json(options = {}) ⇒ Object



40
41
42
43
# File 'lib/fog/core/model.rb', line 40

def to_json(options = {})
  require 'multi_json'
  MultiJson.encode(attributes)
end

#wait_for(timeout = Fog.timeout, interval = 1, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/core/model.rb', line 53

def wait_for(timeout=Fog.timeout, interval=1, &block)
  reload
  retries = 3
  Fog.wait_for(timeout, interval) do
    if reload
      retries = 3
    elsif retries > 0
      retries -= 1
      sleep(1)
    elsif retries == 0
      raise Fog::Errors::Error.new("Reload failed, #{self.class} #{self.identity} went away.")
    end
    instance_eval(&block)
  end
end