1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[google|compute] Update Addresses suport

- Modify "list_aggregated_addresses" to support filters
- Add "get_by_ip_address" model method
- Add ability to assign/deassign an IP to a server
This commit is contained in:
Ferran Rodenas 2014-04-16 23:20:10 -07:00
parent 2c189bfafb
commit 52abc9b499
4 changed files with 49 additions and 2 deletions

View file

@ -24,6 +24,17 @@ module Fog
IN_USE_STATE = 'IN_USE'
RESERVED_STATE = 'RESERVED'
def server
return nil if !in_use? || self.users.nil? || self.users.empty?
service.servers.get(self.users.first.split('/')[-1])
end
def server=(server)
requires :identity, :region
server ? associate(server) : disassociate
end
def save
requires :identity, :region
@ -55,6 +66,29 @@ module Fog
def in_use?
self.status == IN_USE_STATE
end
private
def associate(server)
nic = server.network_interfaces.first['name']
data = service.add_server_access_config(server.name, server.zone_name, nic, :address => self.address)
Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end
def disassociate
return nil if !in_use? || self.users.nil? || self.users.empty?
# An address can only be associated with one server at a time
server = service.servers.get(self.users.first.split('/')[-1])
nic = server.network_interfaces.first['name']
unless server.network_interfaces.first['accessConfigs'].nil? ||
server.network_interfaces.first['accessConfigs'].empty?
access_config = server.network_interfaces.first['accessConfigs'].first['name']
data = service.delete_server_access_config(server.name, server.zone_name, nic,
:access_config => access_config)
Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end
end
end
end

View file

@ -27,6 +27,14 @@ module Fog
rescue Fog::Errors::NotFound
nil
end
def get_by_ip_address(ip_address)
addresses = service.list_aggregated_addresses(:filter => "address eq .*#{ip_address}").body['items']
address = addresses.each_value.select { |region| region.has_key?('addresses') }
return nil if address.empty?
new(address.first['addresses'].first)
end
end
end

View file

@ -105,6 +105,10 @@ module Fog
ip
end
def addresses
[private_ip_address, public_ip_address]
end
def attach_disk(disk, options = {})
requires :identity, :zone

View file

@ -3,17 +3,18 @@ module Fog
class Google
class Mock
def list_aggregated_addresses
def list_aggregated_addresses(options = {})
Fog::Mock.not_implemented
end
end
class Real
def list_aggregated_addresses
def list_aggregated_addresses(options = {})
api_method = @compute.addresses.aggregated_list
parameters = {
'project' => @project,
}
parameters['filter'] = options[:filter] if options[:filter]
result = self.build_result(api_method, parameters)
response = self.build_response(result)