mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[glesys] Refactor the ip interface, did not work earlier
Some requests were ok but they never returned a ip. Now you can
manipulate ips in a whole new way. 😎
This commit is contained in:
parent
843a03962c
commit
c60fea80bc
4 changed files with 135 additions and 56 deletions
|
@ -15,6 +15,8 @@ module Fog
|
|||
model :ip
|
||||
|
||||
request_path 'fog/glesys/requests/compute'
|
||||
|
||||
# Server
|
||||
request :create
|
||||
request :destroy
|
||||
request :list_servers
|
||||
|
@ -23,9 +25,11 @@ module Fog
|
|||
request :start
|
||||
request :reboot
|
||||
request :stop
|
||||
|
||||
# Templates
|
||||
request :template_list
|
||||
# IP operations
|
||||
|
||||
# IP
|
||||
request :ip_list_free
|
||||
request :ip_list_own
|
||||
request :ip_details
|
||||
|
|
|
@ -23,59 +23,43 @@ module Fog
|
|||
attribute :ptr
|
||||
attribute :cost
|
||||
|
||||
def list_own
|
||||
service.list_own
|
||||
end
|
||||
|
||||
def list_free
|
||||
requires :version, :datacenter, :platform
|
||||
service.ip_list_free(
|
||||
:ipversion => version,
|
||||
:platform => platform,
|
||||
:datacenter => datacenter
|
||||
).body['response']['iplist']
|
||||
end
|
||||
|
||||
def details
|
||||
requires :version, :ip
|
||||
service.ip_details(
|
||||
:ipversion => version,
|
||||
:ipaddress => ip
|
||||
)
|
||||
end
|
||||
|
||||
def take
|
||||
requires :version, :ip
|
||||
service.ip_take(
|
||||
:ipversion => version,
|
||||
:ipaddress => ip
|
||||
)
|
||||
def attached?
|
||||
!serverid.nil?
|
||||
end
|
||||
|
||||
# Return an unused ip-address to the pool of free ips.
|
||||
def release
|
||||
requires :version, :ip
|
||||
requires :ip
|
||||
raise Fog::Errors::Error.new('You can\'t release a ip that is attached to a server') if attached?
|
||||
service.ip_release(
|
||||
:ipversion => version,
|
||||
:ipaddress => ip
|
||||
:ipaddress => identity
|
||||
)
|
||||
end
|
||||
|
||||
def add
|
||||
requires :serverid, :version, :ip
|
||||
service.ip_add(
|
||||
:serverid => serverid,
|
||||
:ipversion => version,
|
||||
:ipaddress => ip
|
||||
)
|
||||
# Add an ip-adress to the server.
|
||||
def attach(server)
|
||||
requires :ip
|
||||
server = server.serverid if server.is_a?(Fog::Compute::Glesys::Server)
|
||||
raise Fog::Errors::Error.new("Ip is already attached to a server, #{serverid}") unless serverid.nil?
|
||||
data = service.ip_add(
|
||||
:ipaddress => identity,
|
||||
:serverid => server
|
||||
).body["response"]["details"]
|
||||
merge_attributes data
|
||||
end
|
||||
|
||||
def remove
|
||||
requires :serverid, :version, :ip
|
||||
service.ip_remove(
|
||||
:serverid => serverid,
|
||||
:ipversion => version,
|
||||
:ipaddress => ip
|
||||
)
|
||||
# Remove an ip from the server
|
||||
def remove(options = {})
|
||||
requires :ip
|
||||
raise Fog::Errors::Error.new('Ip is not attached to a server.') if serverid.nil?
|
||||
data = service.ip_remove({:ipaddress => ip}.merge!(options)).body["response"]["details"]
|
||||
merge_attributes data
|
||||
end
|
||||
|
||||
# Remove the ip from a server and release it
|
||||
def destroy
|
||||
requires :ip
|
||||
remove(:release => true)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -10,27 +10,110 @@ module Fog
|
|||
model Fog::Compute::Glesys::Ip
|
||||
|
||||
attribute :serverid
|
||||
attribute :server
|
||||
|
||||
def all
|
||||
data = service.ip_list_own.body['response']['iplist']
|
||||
load(data)
|
||||
attributes = {}
|
||||
attributes[:serverid] = serverid unless serverid.nil?
|
||||
ips = service.ip_list_own(attributes).body['response']['iplist']
|
||||
load(ips)
|
||||
end
|
||||
|
||||
def get(identifier)
|
||||
return nil if identifier.nil? || identifier == ""
|
||||
def get(ip)
|
||||
data = service.ip_details( :ipaddress => ip).body['response']['details']
|
||||
new data
|
||||
end
|
||||
|
||||
self.new( :serverid => identifier )
|
||||
def attached
|
||||
all.select { |ip| !ip.serverid.nil? }
|
||||
end
|
||||
|
||||
data = service.ip_list_own(:serverid => identifier).body['response']
|
||||
if data['iplist'].empty?
|
||||
nil
|
||||
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)
|
||||
|
||||
data = service.ip_take(
|
||||
:ipaddress => ip_from_object(ip)
|
||||
).response.body["response"]["details"]
|
||||
|
||||
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
|
||||
else
|
||||
new(data['iplist'].first)
|
||||
server.reload
|
||||
end
|
||||
end
|
||||
|
||||
def new(attributes = {})
|
||||
super({ :serverid => serverid }.merge!(attributes))
|
||||
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
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -106,6 +106,14 @@ module Fog
|
|||
super(command, options, &block)
|
||||
end
|
||||
|
||||
def ips
|
||||
Fog::Compute::Glesys::Ips.new(:serverid => identity, :server => self, :service => service).all
|
||||
end
|
||||
|
||||
def ip(ip)
|
||||
Fog::Compute::Glesys::Ips.new(:serverid => identity, :server => self, :service => service).get(ip)
|
||||
end
|
||||
|
||||
def public_ip_address(options = {})
|
||||
|
||||
return nil if iplist.nil?
|
||||
|
|
Loading…
Add table
Reference in a new issue