mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #2009 from rvrignaud/new-network
[google|compute] Add support for network and external_ip during server creation
This commit is contained in:
commit
69c521160b
3 changed files with 58 additions and 13 deletions
18
lib/fog/google/examples/network.rb
Normal file
18
lib/fog/google/examples/network.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
def test
|
||||
connection = Fog::Compute.new({ :provider => "Google" })
|
||||
|
||||
# we create a new private network
|
||||
connection.insert_network('my-private-network','10.240.0.0/16')
|
||||
|
||||
server = connection.servers.bootstrap(defaults = {
|
||||
:name => "fog-smoke-test-#{Time.now.to_i}",
|
||||
:image_name => "debian-7-wheezy-v20130522",
|
||||
:machine_type => "n1-standard-1",
|
||||
:zone_name => "us-central1-a",
|
||||
:private_key_path => File.expand_path("~/.ssh/id_rsa"),
|
||||
:public_key_path => File.expand_path("~/.ssh/id_rsa.pub"),
|
||||
:network => 'my-private-network',
|
||||
:external_ip => false,
|
||||
:user => ENV['USER'],
|
||||
})
|
||||
end
|
|
@ -10,6 +10,8 @@ module Fog
|
|||
identity :name
|
||||
attribute :image_name, :aliases => 'image'
|
||||
attribute :network_interfaces, :aliases => 'networkInterfaces'
|
||||
attribute :network, :aliases => 'network'
|
||||
attribute :external_ip, :aliases => 'externalIP'
|
||||
attribute :state, :aliases => 'status'
|
||||
attribute :zone_name, :aliases => 'zone'
|
||||
attribute :machine_type, :aliases => 'machineType'
|
||||
|
@ -28,11 +30,13 @@ module Fog
|
|||
|
||||
def public_ip_address
|
||||
ip = nil
|
||||
if self.network_interfaces
|
||||
if self.network_interfaces.respond_to? :each
|
||||
self.network_interfaces.each do |netif|
|
||||
netif["accessConfigs"].each do |access_config|
|
||||
if access_config["name"] == "External NAT"
|
||||
ip = access_config['natIP']
|
||||
if netif["accessConfigs"].respond_to? :each
|
||||
netif["accessConfigs"].each do |access_config|
|
||||
if access_config["name"] == "External NAT"
|
||||
ip = access_config['natIP']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -41,6 +45,14 @@ module Fog
|
|||
ip
|
||||
end
|
||||
|
||||
def private_ip_address
|
||||
ip = nil
|
||||
if self.network_interfaces.respond_to? :first
|
||||
ip = self.network_interfaces.first['networkIP']
|
||||
end
|
||||
ip
|
||||
end
|
||||
|
||||
def ready?
|
||||
self.state == RUNNING
|
||||
end
|
||||
|
@ -77,6 +89,8 @@ module Fog
|
|||
'image' => image_name,
|
||||
'machineType' => machine_type,
|
||||
'networkInterfaces' => network_interfaces,
|
||||
'network' => network,
|
||||
'externalIp' => external_ip,
|
||||
'disks' => disks,
|
||||
'kernel' => kernel,
|
||||
'metadata' => metadata
|
||||
|
|
|
@ -35,16 +35,29 @@ module Fog
|
|||
body_object['image'] = @image_url
|
||||
end
|
||||
body_object['machineType'] = @api_url + @project + "/zones/#{zone_name}/machineTypes/#{options.delete 'machineType'}"
|
||||
networkInterfaces = []
|
||||
if @default_network
|
||||
networkInterfaces << {
|
||||
'network' => @api_url + @project + "/global/networks/#{@default_network}",
|
||||
'accessConfigs' => [
|
||||
{'type' => 'ONE_TO_ONE_NAT',
|
||||
'name' => 'External NAT'}
|
||||
]
|
||||
}
|
||||
network = nil
|
||||
if options.has_key? 'network'
|
||||
network = options.delete 'network'
|
||||
elsif @default_network
|
||||
network = @default_network
|
||||
end
|
||||
|
||||
# ExternalIP is default value for server creation
|
||||
if options.has_key? 'externalIp'
|
||||
external_ip = options.delete 'externalIp'
|
||||
else
|
||||
external_ip = true
|
||||
end
|
||||
|
||||
networkInterfaces = []
|
||||
if ! network.nil?
|
||||
networkInterface = { 'network' => @api_url + @project + "/global/networks/#{network}" }
|
||||
if external_ip
|
||||
networkInterface['accessConfigs'] = [{'type' => 'ONE_TO_ONE_NAT', 'name' => 'External NAT'}]
|
||||
end
|
||||
networkInterfaces << networkInterface
|
||||
end
|
||||
|
||||
# TODO: add other networks
|
||||
body_object['networkInterfaces'] = networkInterfaces
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue