mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #1396 from kanetann/master
added floatingip methods to Fog::OpenStack::Network
This commit is contained in:
commit
7778df034e
12 changed files with 19495 additions and 0 deletions
52
lib/fog/openstack/models/network/floating_ip.rb
Normal file
52
lib/fog/openstack/models/network/floating_ip.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
class FloatingIp < Fog::Model
|
||||
identity :id
|
||||
|
||||
attribute :floating_network_id
|
||||
attribute :port_id
|
||||
attribute :tenant_id
|
||||
attribute :fixed_ip_address
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def initialize(attributes)
|
||||
@connection = attributes[:connection]
|
||||
super
|
||||
end
|
||||
|
||||
def save
|
||||
requires :floating_network_id
|
||||
identity ? update : create
|
||||
end
|
||||
|
||||
def create
|
||||
requires :floating_network_id
|
||||
merge_attributes(connection.create_floating_ip(self.floating_network_id,
|
||||
|
||||
|
||||
self.attributes).body['floating_ip'])
|
||||
self
|
||||
end
|
||||
|
||||
def update
|
||||
self
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
connection.delete_floating_ip(self.id)
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
34
lib/fog/openstack/models/network/floating_ips.rb
Normal file
34
lib/fog/openstack/models/network/floating_ips.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/openstack/models/network/floating_ip'
|
||||
|
||||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
class FloatingIps < Fog::Collection
|
||||
|
||||
attribute :filters
|
||||
|
||||
model Fog::Network::OpenStack::FloatingIp
|
||||
|
||||
def initialize(attributes)
|
||||
self.filters ||= {}
|
||||
super
|
||||
end
|
||||
|
||||
def all(filters = filters)
|
||||
self.filters = filters
|
||||
load(connection.list_floating_ips(filters).body['floating_ips'])
|
||||
end
|
||||
|
||||
def get(floating_network_id)
|
||||
if floating_ip = connection.get_floating_ip(floating_network_id).body['floating_ip']
|
||||
new(floating_ip)
|
||||
end
|
||||
rescue Fog::Network::OpenStack::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -19,6 +19,8 @@ module Fog
|
|||
collection :ports
|
||||
model :subnet
|
||||
collection :subnets
|
||||
model :floating_ip
|
||||
collection :floating_ips
|
||||
|
||||
## REQUESTS
|
||||
#
|
||||
|
@ -45,6 +47,14 @@ module Fog
|
|||
request :get_subnet
|
||||
request :update_subnet
|
||||
|
||||
# FloatingIp CRUD
|
||||
request :list_floating_ips
|
||||
request :create_floating_ip
|
||||
request :delete_floating_ip
|
||||
request :get_floating_ip
|
||||
request :associate_floating_ip
|
||||
request :disassociate_floating_ip
|
||||
|
||||
# Tenant
|
||||
request :set_tenant
|
||||
|
||||
|
@ -55,6 +65,7 @@ module Fog
|
|||
:networks => {},
|
||||
:ports => {},
|
||||
:subnets => {},
|
||||
:floating_ips => {},
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
49
lib/fog/openstack/requests/network/associate_floating_ip.rb
Normal file
49
lib/fog/openstack/requests/network/associate_floating_ip.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def associate_floating_ip(floating_ip_id, port_id, options = {})
|
||||
data = {
|
||||
'floating_ip' => {
|
||||
'port_id' => port_id,
|
||||
}
|
||||
}
|
||||
|
||||
vanilla_options = [:fixed_ip_address]
|
||||
vanilla_options.reject{ |o| options[o].nil? }.each do |key|
|
||||
data['floating_ip'][key] = options[key]
|
||||
end
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => [200],
|
||||
:method => 'PUT',
|
||||
:path => "floatingips/#{floating_ip_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def associate_floating_ip(floating_ip_id, port_id, options = {})
|
||||
response = Excon::Response.new
|
||||
response.status = 201
|
||||
data = {
|
||||
'id' => '00000000-0000-0000-0000-000000000000',
|
||||
'router_id' => '00000000-0000-0000-0000-000000000000',
|
||||
'tenant_id' => options["tenant_id"],
|
||||
'floating_network_id' => options["floating_network_id"],
|
||||
'fixed_ip_address' => options["fixed_ip_address"],
|
||||
'floating_ip_address' => options["floating_ip_address"],
|
||||
'port_id' => port_id,
|
||||
}
|
||||
|
||||
self.data[:floating_ips][data['floating_ip_id']] = data
|
||||
response.body = { 'floating_ip' => data }
|
||||
response
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
50
lib/fog/openstack/requests/network/create_floating_ip.rb
Normal file
50
lib/fog/openstack/requests/network/create_floating_ip.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def create_floating_ip(floating_network_id, options = {})
|
||||
data = {
|
||||
'floating_ip' => {
|
||||
'floating_network_id' => floating_network_id,
|
||||
'port_id' => options[:port_id],
|
||||
'tenant_id' => options[:tenant_id],
|
||||
'fixed_ip_address' => options[:fixed_ip_address],
|
||||
}
|
||||
}
|
||||
|
||||
vanilla_options = [:port_id, :tenant_id, :fixed_ip_address ]
|
||||
vanilla_options.reject{ |o| options[o].nil? }.each do |key|
|
||||
data['floating_ip'][key] = options[key]
|
||||
end
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => [201],
|
||||
:method => 'POST',
|
||||
:path => 'floatingips'
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def create_floating_ip(floating_network_id, options = {})
|
||||
response = Excon::Response.new
|
||||
response.status = 201
|
||||
data = {
|
||||
'id' => floating_network_id,
|
||||
'floating_network_id' => floating_network_id,
|
||||
'port_id' => options[:port_id],
|
||||
'tenant_id' => options[:tenant_id],
|
||||
'fixed_ip_address' => options[:fixed_ip_address],
|
||||
'router_id' => nil,
|
||||
}
|
||||
self.data[:floating_ips][data['id']] = data
|
||||
response.body = { 'floating_ip' => data }
|
||||
response
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
30
lib/fog/openstack/requests/network/delete_floating_ip.rb
Normal file
30
lib/fog/openstack/requests/network/delete_floating_ip.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def delete_floating_ip(floating_ip_id)
|
||||
request(
|
||||
:expects => 204,
|
||||
:method => 'DELETE',
|
||||
:path => "floatingips/#{floating_ip_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def delete_floating_ip(floating_ip_id)
|
||||
response = Excon::Response.new
|
||||
if list_floating_ips.body['floating_ips'].map { |r| r['id'] }.include? floating_ip_id
|
||||
self.data[:floating_ips].delete(floating_ip_id)
|
||||
response.status = 204
|
||||
response
|
||||
else
|
||||
raise Fog::Network::OpenStack::NotFound
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,49 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def disassociate_floating_ip(floating_ip_id, options = {})
|
||||
data = {
|
||||
'floating_ip' => {
|
||||
'port_id' => nil,
|
||||
}
|
||||
}
|
||||
|
||||
vanilla_options = [:fixed_ip_address]
|
||||
vanilla_options.reject{ |o| options[o].nil? }.each do |key|
|
||||
data['floating_ip'][key] = options[key]
|
||||
end
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => [200],
|
||||
:method => 'PUT',
|
||||
:path => "floatingips/#{floating_ip_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def disassociate_floating_ip(floating_ip_id, options = {})
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
data = {
|
||||
'id' => '00000000-0000-0000-0000-000000000000',
|
||||
'router_id' => nil,
|
||||
'tenant_id' => options["tenant_id"],
|
||||
'floating_network_id' => options["floating_network_id"],
|
||||
'fixed_ip_address' => nil,
|
||||
'floating_ip_address' => options["floating_ip_address"],
|
||||
'port_id' => options["port_id"],
|
||||
}
|
||||
|
||||
self.data[:floating_ips][data['floating_ip_id']] = data
|
||||
response.body = { 'floating_ip' => data }
|
||||
response
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
42
lib/fog/openstack/requests/network/get_floating_ip.rb
Normal file
42
lib/fog/openstack/requests/network/get_floating_ip.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def get_floating_ip(floating_ip_id)
|
||||
request(
|
||||
:expects => [200],
|
||||
:method => 'GET',
|
||||
:path => "floatingips/#{floating_ip_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def get_floating_ip(floating_ip_id)
|
||||
response = Excon::Response.new
|
||||
if data = self.data[:floating_ips][floating_ip_id]
|
||||
response.status = 200
|
||||
response.body = {
|
||||
"floating_ip" => {
|
||||
"id" => "00000000-0000-0000-0000-000000000000",
|
||||
# changed
|
||||
# "floating_ip_id" => floating_ip_id,
|
||||
"port_id" => data["port_id"],
|
||||
"tenant_id" => data["tenant_id"],
|
||||
"fixed_ip_address" => data["fixed_ip_address"],
|
||||
"router_id" => "00000000-0000-0000-0000-000000000000",
|
||||
"floating_ip_address" => data["floating_ip_address"],
|
||||
}
|
||||
}
|
||||
response
|
||||
else
|
||||
raise Fog::Network::OpenStack::NotFound
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
end
|
27
lib/fog/openstack/requests/network/list_floating_ips.rb
Normal file
27
lib/fog/openstack/requests/network/list_floating_ips.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def list_floating_ips(filters = {})
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => 'floatingips',
|
||||
:query => filters
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def list_floating_ips(filters = {})
|
||||
Excon::Response.new(
|
||||
:body => { 'floating_ips' => self.data[:floating_ips].values },
|
||||
:status => 200
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
30
tests/openstack/models/network/floating_ip_tests.rb
Normal file
30
tests/openstack/models/network/floating_ip_tests.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
Shindo.tests("Fog::Network[:openstack] | floating_ip", ['openstack']) do
|
||||
|
||||
tests('success') do
|
||||
|
||||
tests('#create').succeeds do
|
||||
@instance = Fog::Network[:openstack].floating_ips.create(:floating_network_id => 'f0000000-0000-0000-0000-000000000000')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
!@instance.id.nil?
|
||||
end
|
||||
|
||||
tests('#update').succeeds do
|
||||
@instance.port_id = 'p0000000-0000-0000-0000-000000000000'
|
||||
@instance.update
|
||||
end
|
||||
|
||||
tests('#destroy').succeeds do
|
||||
@instance.destroy == true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
27
tests/openstack/models/network/floating_ips_tests.rb
Normal file
27
tests/openstack/models/network/floating_ips_tests.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
Shindo.tests("Fog::Network[:openstack] | floating_ips", ['openstack']) do
|
||||
@floating_ip = Fog::Network[:openstack].floating_ips.create(:floating_network_id => 'f0000000-0000-0000-0000-000000000000')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@floating_ips = Fog::Network[:openstack].floating_ips
|
||||
|
||||
tests('success') do
|
||||
|
||||
tests('#all').succeeds do
|
||||
@floating_ips.all
|
||||
end
|
||||
|
||||
tests('#get').succeeds do
|
||||
@floating_ips.get @floating_ip.id
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@floating_ip.destroy
|
||||
end
|
Loading…
Reference in a new issue