lib/fog/google/models/compute/server.rb in fog-1.12.1 vs lib/fog/google/models/compute/server.rb in fog-1.13.0
- old
+ new
@@ -11,68 +11,81 @@
attribute :image_name, :aliases => 'image'
attribute :network_interfaces, :aliases => 'networkInterfaces'
attribute :state, :aliases => 'status'
attribute :zone_name, :aliases => 'zone'
attribute :machine_type, :aliases => 'machineType'
+ attribute :disks, :aliases => 'disks'
+ attribute :kernel, :aliases => 'kernel'
attribute :metadata
def destroy
- requires :name
- service.delete_server(name)
+ requires :name, :zone
+ service.delete_server(name, zone)
end
def image
service.get_image(self.image_name.split('/')[-1])
end
def public_ip_address
- if self.network_interfaces.count
- self.network_interfaces[0]["networkIP"]
- else
- nil
+ ip = nil
+ if self.network_interfaces
+ self.network_interfaces.each do |netif|
+ netif["accessConfigs"].each do |access_config|
+ if access_config["name"] == "External NAT"
+ ip = access_config['natIP']
+ end
+ end
+ end
end
+
+ ip
end
def ready?
- data = service.get_server(self.name, self.zone_name).body
- data['zone_name'] = self.zone_name
- self.merge_attributes(data)
- self.state == RUNNING_STATE
+ self.state == RUNNING
end
def zone
- service.get_zone(self.zone_name.split('/')[-1])
+ if self.zone_name.is_a? String
+ service.get_zone(self.zone_name.split('/')[-1]).body["name"]
+ elsif zone_name.is_a? Excon::Response
+ service.get_zone(zone_name.body["name"]).body["name"]
+ else
+ self.zone_name
+ end
end
+ def reload
+ data = service.get_server(self.name, self.zone).body
+ self.merge_attributes(data)
+ end
+
def save
requires :name
- requires :image_name
requires :machine_type
requires :zone_name
- data = service.insert_server(
- name,
- image_name,
- zone_name,
- machine_type)
+ if self.metadata.nil?
+ self.metadata = {}
+ end
- data = service.get_server(self.name, self.zone_name).body
- service.servers.merge_attributes(data)
- end
+ self.metadata.merge!({
+ "sshKeys" => "#{username}:#{File.read(public_key_path).strip}"
+ }) if public_key_path
- def setup(credentials = {})
- requires :public_ip_address, :public_key, :username
- service.set_metadata(self.instance, self.zone, {'sshKeys' => self.public_key })
- rescue Errno::ECONNREFUSED
- sleep(1)
- retry
- end
-
- def sshable?(options={})
- service.set_metadata(self.instance, self.zone, {'sshKeys' => self.public_key })
- ready? && !public_ip_address.nil? && public_key && metadata['sshKeys']
- rescue SystemCallError, Net::SSH::AuthenticationFailed, Timeout::Error
- false
+ options = {
+ 'image' => image_name,
+ 'machineType' => machine_type,
+ 'networkInterfaces' => network_interfaces,
+ 'disks' => disks,
+ 'kernel' => kernel,
+ 'metadata' => metadata
+ }
+ options.delete_if {|key, value| value.nil?}
+ service.insert_server(name, zone_name, options)
+ data = service.backoff_if_unfound {service.get_server(self.name, self.zone_name).body}
+ service.servers.merge_attributes(data)
end
end
end
end