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

added floatingip

This commit is contained in:
kanetann 2012-12-14 18:35:37 +09:00
parent f653aee448
commit 8329f16f8d
13 changed files with 506 additions and 0 deletions

View file

@ -0,0 +1,68 @@
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_floatingip(self.floating_network_id,
self.attributes).body['floatingip'])
self
end
def update
requires :id, :floating_network_id
merge_attributes(connection.create_floatingip(self.floating_network_id,
self.attributes).body['floatingip'])
end
def destroy
requires :id
connection.delete_floatingip(self.id)
true
end
def assosiate
requires :floating_network_id
merge_attributes(connection.assosiate_floatingip(self.floating_network_id,
self.port_id,
self.fixed_ip_address,
self.attributes).body['floatingip'])
true
end
def disassosiate
requires :floating_network_id
connection.disassosiate_floatingip(self.floating_network_id)
true
end
end
end
end
end

View file

@ -0,0 +1,34 @@
require 'fog/core/collection'
require 'fog/openstack/models/network/floatingip'
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_floatingips(filters).body['floatingips'])
end
def get(floating_network_id)
if floatingip = connection.get_floatingip(floating_network_id).body['floatingip']
new(floatingip)
end
rescue Fog::Network::OpenStack::NotFound
nil
end
end
end
end
end

View file

@ -19,6 +19,8 @@ module Fog
collection :ports
model :subnet
collection :subnets
model :floatingip
collection :floatingips
## REQUESTS
#
@ -45,6 +47,15 @@ module Fog
request :get_subnet
request :update_subnet
# Floatingip CRUD
request :list_floatingips
request :create_floatingip
request :delete_floatingip
request :get_floatingip
request :update_floatingip
request :associate_floatingip
request :disassociate_floatingip
# Tenant
request :set_tenant
@ -55,6 +66,7 @@ module Fog
:networks => {},
:ports => {},
:subnets => {},
:floatingips => {},
}
end
end

View file

@ -0,0 +1,42 @@
module Fog
module Network
class OpenStack
class Real
def associate_floatingip(floating_network_id, options = {})
data = {
'floatingip' => {
'network_id' => floating_network_id,
}
}
vanilla_options = [:port_id, :fixed_ip_address ]
vanilla_options.reject{ |o| options[o].nil? }.each do |key|
data['floatingip'][key] = options[key]
end
request(
:body => Fog::JSON.encode(data),
:expects => [201],
:method => 'POST',
:path => 'floatingips'
)
end
end
class Mock
def associatee_floatingip(floating_network_id, options = {})
response = Excon::Response.new
response.status = 201
data = {
'id' => '00000000-0000-0000-0000-000000000000',
}
self.data[:floatingips][data['id']] = data
response.body = { 'floatingip' => data }
response
end
end
end
end
end

View file

@ -0,0 +1,49 @@
module Fog
module Network
class OpenStack
class Real
def create_floatingip(floating_network_id, options = {})
data = {
'floatingip' => {
'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['floatingip'][key] = options[key]
end
request(
:body => Fog::JSON.encode(data),
:expects => [201],
:method => 'POST',
:path => 'floatingips'
)
end
end
class Mock
def create_floatingip(floating_network_id, options = {})
response = Excon::Response.new
response.status = 201
data = {
'id' => Fog::Mock.random_numbers(6).to_s,
'floating_network_id' => floating_network_id,
'port_id' => options[:port_id],
'tenant_id' => options[:tenant_id],
'fixed_ip_address' => options[:fixed_ip_address],
}
self.data[:floatingips][data['id']] = data
response.body = { 'floatingip' => data }
response
end
end
end
end
end

View file

@ -0,0 +1,30 @@
module Fog
module Network
class OpenStack
class Real
def delete_floatingip(floating_network_id)
request(
:expects => 204,
:method => 'DELETE',
:path => "floatingips/#{floating_network_id}"
)
end
end
class Mock
def delete_floatingip(floating_network_id)
response = Excon::Response.new
if list_floatingips.body['floatingips'].map { |r| r['id'] }.include? floating_network_id
self.data[:floatingips].delete(floating_network_id)
response.status = 204
response
else
raise Fog::Network::OpenStack::NotFound
end
end
end
end
end
end

View file

@ -0,0 +1,42 @@
module Fog
module Network
class OpenStack
class Real
def disassociate_floatingip(floating_network_id, options = {})
data = {
'floatingip' => {
'network_id' => floating_network_id,
}
}
# vanilla_options = [:port_id, :tenant_id, :fixed_ip_address ]
# vanilla_options.reject{ |o| options[o].nil? }.each do |key|
# data['floatingip'][key] = options[key]
# end
request(
:body => Fog::JSON.encode(data),
:expects => [201],
:method => 'POST',
:path => 'floatingips'
)
end
end
class Mock
def disassociate_floatingip(floating_network_id, options = {})
response = Excon::Response.new
response.status = 201
data = {
'id' => '00000000-0000-0000-0000-000000000000',
}
self.data[:floatingips][data['id']] = data
response.body = { 'floatingip' => data }
response
end
end
end
end
end

View file

@ -0,0 +1,39 @@
module Fog
module Network
class OpenStack
class Real
def get_floatingip(floating_network_id)
request(
:expects => [200],
:method => 'GET',
:path => "floatingips/#{floating_network_id}"
)
end
end
class Mock
def get_floatingip(floating_network_id)
response = Excon::Response.new
if data = self.data[:floatingips][floating_network_id]
response.status = 200
response.body = {
"floatingip" => {
"id" => "00000000-0000-0000-0000-000000000000",
"floating_network_id" => floating_network_id,
"port_id" => data["port_id"],
"tenant_id" => data["tenant_id"],
"fixed_ip_address" => data["fixed_ip_address"],
}
}
response
else
raise Fog::Network::OpenStack::NotFound
end
end
end
end
end
end

View file

@ -0,0 +1,27 @@
module Fog
module Network
class OpenStack
class Real
def list_floatingips(filters = {})
request(
:expects => 200,
:method => 'GET',
:path => 'floatingips',
:query => filters
)
end
end
class Mock
def list_floatingips(filters = {})
Excon::Response.new(
:body => { 'floatingips' => self.data[:floatingips].values },
:status => 200
)
end
end
end
end
end

View file

@ -0,0 +1,41 @@
module Fog
module Network
class OpenStack
class Real
def update_floatingip(floating_network_id, options = {})
data = { 'floatingip' => {} }
vanilla_options = [:port_id, :tenant_id, :fixed_ip_address ]
vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
data['floatingip'][key] = options[key]
end
request(
:body => Fog::JSON.encode(data),
:expects => 200,
:method => 'PUT',
:path => "floatingips/#{floating_network_id}"
)
end
end
class Mock
def update_floatingip(floating_network_id, options = {})
response = Excon::Response.new
if floatingip = list_floatingips.body['floatingips'].detect { |_| _['id'] == floating_network_id }
floatingip['port_id'] = options[:port_id]
floatingip['tenant_id'] = options[:tenant_id]
floatingip['fixed_ip_address'] = options[:fixed_ip_address]
response.body = { 'floatingip' => floatingip }
response.status = 200
response
else
raise Fog::Network::OpenStack::NotFound
end
end
end
end
end
end

View file

@ -0,0 +1,30 @@
Shindo.tests("Fog::Network[:openstack] | floatingip", ['openstack']) do
tests('success') do
tests('#create').succeeds do
@instance = Fog::Network[:openstack].floatingips.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

View file

@ -0,0 +1,27 @@
Shindo.tests("Fog::Network[:openstack] | floatingips", ['openstack']) do
@floatingip = Fog::Network[:openstack].floatingips.create(:floating_network_id => 'f0000000-0000-0000-0000-000000000000')
@floatingips = Fog::Network[:openstack].floatingips
tests('success') do
tests('#all').succeeds do
@floatingips.all
end
tests('#get').succeeds do
@floatingips.get @floatingip.id
end
end
@floatingip.destroy
end

View file

@ -0,0 +1,65 @@
Shindo.tests('Fog::Network[:openstack] | floatingip requests', ['openstack']) do
@floatingip_format = {
'id' => String,
'floating_network_id' => String,
'port_id' => Fog::Nullable::String,
'tenant_id' => Fog::Nullable::String,
'fixed_ip_address' => Fog::Nullable::String,
}
require '/home/kaneko/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/awesome_print-1.1.0/lib/awesome_print.rb'
tests('success') do
tests('#create_floatingip').formats({'floatingip' => @floatingip_format}) do
floating_network_id = 'f0000000-0000-0000-0000-000000000000'
attributes = {:port_id => 'p0000000-0000-0000-0000-000000000000',
:tenant_id => 't0000000-0000-0000-0000-000000000000',
:fixed_ip_address => '192.168.0.1' }
Fog::Network[:openstack].create_floatingip(floating_network_id, attributes).body
end
tests('#list_floatingip').formats({'floatingips' => [@floatingip_format]}) do
Fog::Network[:openstack].list_floatingips.body
end
tests('#get_floatingip').formats({'floatingip' => @floatingip_format}) do
floating_network_id = Fog::Network[:openstack].floatingips.all.first.id
Fog::Network[:openstack].get_floatingip(floating_network_id).body
end
tests('#update_floatingip').formats({'floatingip' => @floatingip_format}) do
floating_network_id = Fog::Network[:openstack].floatingips.all.first.id
attributes = {:port_id => 'p0000000-0000-0000-0000-000000000000',
:tenant_id => 't0000000-0000-0000-0000-000000000000',
:fixed_ip_address => '192.168.0.1' }
Fog::Network[:openstack].update_floatingip(floating_network_id, attributes).body
end
tests('#delete_floatingip').succeeds do
floating_network_id = Fog::Network[:openstack].floatingips.all.first.id
Fog::Network[:openstack].delete_floatingip(floating_network_id)
end
end
tests('failure') do
tests('#get_floatingip').raises(Fog::Network::OpenStack::NotFound) do
Fog::Network[:openstack].get_floatingip(0)
end
tests('#update_floatingip').raises(Fog::Network::OpenStack::NotFound) do
Fog::Network[:openstack].update_floatingip(0, {})
end
tests('#delete_floatingip').raises(Fog::Network::OpenStack::NotFound) do
Fog::Network[:openstack].delete_floatingip(0)
end
end
end