diff --git a/lib/fog/google/models/compute/address.rb b/lib/fog/google/models/compute/address.rb index 5174100d5..a53fd254c 100755 --- a/lib/fog/google/models/compute/address.rb +++ b/lib/fog/google/models/compute/address.rb @@ -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 diff --git a/lib/fog/google/models/compute/addresses.rb b/lib/fog/google/models/compute/addresses.rb index 7ac1287e6..f79172c05 100755 --- a/lib/fog/google/models/compute/addresses.rb +++ b/lib/fog/google/models/compute/addresses.rb @@ -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 diff --git a/lib/fog/google/models/compute/server.rb b/lib/fog/google/models/compute/server.rb index 89a8ed489..0a5cec298 100644 --- a/lib/fog/google/models/compute/server.rb +++ b/lib/fog/google/models/compute/server.rb @@ -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 diff --git a/lib/fog/google/requests/compute/list_aggregated_addresses.rb b/lib/fog/google/requests/compute/list_aggregated_addresses.rb index 65d2f9580..71e5fafee 100644 --- a/lib/fog/google/requests/compute/list_aggregated_addresses.rb +++ b/lib/fog/google/requests/compute/list_aggregated_addresses.rb @@ -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)