2011-08-10 09:03:51 -04:00
|
|
|
require 'fog/core/collection'
|
2011-09-08 17:07:04 -04:00
|
|
|
require 'fog/glesys/models/compute/ip'
|
2011-08-10 09:03:51 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Glesys
|
|
|
|
|
|
|
|
class Ips < Fog::Collection
|
|
|
|
|
2013-02-01 08:36:57 -05:00
|
|
|
model Fog::Compute::Glesys::Ip
|
2011-08-10 09:03:51 -04:00
|
|
|
|
|
|
|
attribute :serverid
|
2013-02-01 14:36:44 -05:00
|
|
|
attribute :server
|
2011-08-10 09:03:51 -04:00
|
|
|
|
|
|
|
def all
|
2013-02-01 14:36:44 -05:00
|
|
|
attributes = {}
|
|
|
|
attributes[:serverid] = serverid unless serverid.nil?
|
|
|
|
ips = service.ip_list_own(attributes).body['response']['iplist']
|
|
|
|
load(ips)
|
2011-08-10 09:03:51 -04:00
|
|
|
end
|
|
|
|
|
2013-02-01 14:36:44 -05:00
|
|
|
def get(ip)
|
|
|
|
data = service.ip_details( :ipaddress => ip).body['response']['details']
|
|
|
|
new data
|
|
|
|
end
|
|
|
|
|
|
|
|
def attached
|
|
|
|
all.select { |ip| !ip.serverid.nil? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def available
|
|
|
|
all.select { |ip| ip.serverid.nil? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def free(options = {})
|
|
|
|
default_options = {
|
|
|
|
:version => 4
|
|
|
|
}
|
|
|
|
|
|
|
|
if !server.nil?
|
|
|
|
default_options[:datacenter] = server.datacenter
|
|
|
|
default_options[:platform] = server.platform
|
|
|
|
end
|
|
|
|
|
|
|
|
options = default_options.merge!(options)
|
|
|
|
|
|
|
|
%w{platform datacenter version}.each do |attr|
|
|
|
|
raise Fog::Errors::Error.new("You need to specify ':#{attr}'") if !options.has_key?(attr.to_sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
options[:ipversion] = options[:version]
|
|
|
|
options.delete(:version)
|
|
|
|
|
|
|
|
service.ip_list_free(options).body["response"]["iplist"]["ipaddresses"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def take(ip, options = {})
|
|
|
|
|
|
|
|
default_options = {
|
|
|
|
:attach => false
|
|
|
|
}
|
|
|
|
|
|
|
|
options = default_options.merge!(options)
|
2011-08-10 09:03:51 -04:00
|
|
|
|
2013-02-01 14:36:44 -05:00
|
|
|
data = service.ip_take(
|
|
|
|
:ipaddress => ip_from_object(ip)
|
2013-02-05 14:44:45 -05:00
|
|
|
).body["response"]["details"]
|
2012-12-22 18:27:08 -05:00
|
|
|
|
2013-02-01 14:36:44 -05:00
|
|
|
ip = new data
|
|
|
|
|
|
|
|
if options[:attach] && serverid
|
|
|
|
server.attach_ip ip
|
|
|
|
ip.serverid = serverid
|
|
|
|
end
|
|
|
|
|
|
|
|
ip
|
|
|
|
end
|
|
|
|
|
|
|
|
def release(ip)
|
|
|
|
service.ip_release(
|
|
|
|
:ipaddress => ip_from_object(ip)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attach(ip, server_id=nil)
|
|
|
|
|
|
|
|
if server_id.nil?
|
|
|
|
server_id = serverid
|
|
|
|
end
|
|
|
|
|
|
|
|
if server_id.nil?
|
|
|
|
raise Fog::Errors::Error.new("You need to specify a server id")
|
|
|
|
end
|
|
|
|
|
|
|
|
server_id = server_id.serverid if server_id.is_a?(Fog::Compute::Glesys::Server)
|
|
|
|
|
|
|
|
service.ip_add(
|
|
|
|
:ipaddress => ip_from_object(ip),
|
|
|
|
:serverid => server_id
|
|
|
|
)
|
|
|
|
|
|
|
|
if server.nil?
|
|
|
|
true
|
2011-08-10 09:03:51 -04:00
|
|
|
else
|
2013-02-01 14:36:44 -05:00
|
|
|
server.reload
|
2012-12-22 18:27:08 -05:00
|
|
|
end
|
|
|
|
end
|
2011-08-10 09:03:51 -04:00
|
|
|
|
2013-02-01 14:36:44 -05:00
|
|
|
def remove(ip, options = {})
|
|
|
|
new service.ip_remove({:ipaddress => ip_from_object(ip)}.merge!(options)).data.body["response"]["details"]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ip_from_object(ip)
|
|
|
|
ip.is_a?(Fog::Compute::Glesys::Ip) ? ip.ip : ip
|
2011-08-10 09:03:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|