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:
parent
2c189bfafb
commit
52abc9b499
4 changed files with 49 additions and 2 deletions
|
@ -24,6 +24,17 @@ module Fog
|
||||||
IN_USE_STATE = 'IN_USE'
|
IN_USE_STATE = 'IN_USE'
|
||||||
RESERVED_STATE = 'RESERVED'
|
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
|
def save
|
||||||
requires :identity, :region
|
requires :identity, :region
|
||||||
|
|
||||||
|
@ -55,6 +66,29 @@ module Fog
|
||||||
def in_use?
|
def in_use?
|
||||||
self.status == IN_USE_STATE
|
self.status == IN_USE_STATE
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,6 +27,14 @@ module Fog
|
||||||
rescue Fog::Errors::NotFound
|
rescue Fog::Errors::NotFound
|
||||||
nil
|
nil
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -105,6 +105,10 @@ module Fog
|
||||||
ip
|
ip
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def addresses
|
||||||
|
[private_ip_address, public_ip_address]
|
||||||
|
end
|
||||||
|
|
||||||
def attach_disk(disk, options = {})
|
def attach_disk(disk, options = {})
|
||||||
requires :identity, :zone
|
requires :identity, :zone
|
||||||
|
|
||||||
|
|
|
@ -3,17 +3,18 @@ module Fog
|
||||||
class Google
|
class Google
|
||||||
|
|
||||||
class Mock
|
class Mock
|
||||||
def list_aggregated_addresses
|
def list_aggregated_addresses(options = {})
|
||||||
Fog::Mock.not_implemented
|
Fog::Mock.not_implemented
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Real
|
class Real
|
||||||
def list_aggregated_addresses
|
def list_aggregated_addresses(options = {})
|
||||||
api_method = @compute.addresses.aggregated_list
|
api_method = @compute.addresses.aggregated_list
|
||||||
parameters = {
|
parameters = {
|
||||||
'project' => @project,
|
'project' => @project,
|
||||||
}
|
}
|
||||||
|
parameters['filter'] = options[:filter] if options[:filter]
|
||||||
|
|
||||||
result = self.build_result(api_method, parameters)
|
result = self.build_result(api_method, parameters)
|
||||||
response = self.build_response(result)
|
response = self.build_response(result)
|
||||||
|
|
Loading…
Reference in a new issue