mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #1598 from bvox/network-create-extensions
[openstack|network] create_network provider extensions
This commit is contained in:
commit
e5e6b2a1b2
3 changed files with 135 additions and 12 deletions
|
@ -6,11 +6,45 @@ module Fog
|
|||
def create_network(options = {})
|
||||
data = { 'network' => {} }
|
||||
|
||||
vanilla_options = [:name, :shared, :admin_state_up, :tenant_id]
|
||||
vanilla_options = [
|
||||
:name,
|
||||
:shared,
|
||||
:admin_state_up,
|
||||
:tenant_id
|
||||
]
|
||||
|
||||
vanilla_options.reject{ |o| options[o].nil? }.each do |key|
|
||||
data['network'][key] = options[key]
|
||||
end
|
||||
|
||||
# Advanced Features through API Extensions
|
||||
#
|
||||
# Not strictly required but commonly found in OpenStack
|
||||
# installs with Quantum networking.
|
||||
#
|
||||
# @see http://docs.openstack.org/trunk/openstack-network/admin/content/provider_attributes.html
|
||||
provider_options = [
|
||||
:router_external,
|
||||
:provider_network_type,
|
||||
:provider_segmentation_id,
|
||||
:provider_physical_network
|
||||
]
|
||||
|
||||
# Map Fog::Network::OpenStack::Network
|
||||
# model attributes to OpenStack provider attributes
|
||||
aliases = {
|
||||
:provider_network_type => 'provider:network_type',
|
||||
# Not applicable to the "local" or "gre" network types
|
||||
:provider_physical_network => 'provider:physical_network',
|
||||
:provider_segmentation_id => 'provider:segmentation_id',
|
||||
:router_external => 'router:external'
|
||||
}
|
||||
|
||||
provider_options.reject{ |o| options[o].nil? }.each do |key|
|
||||
aliased_key = aliases[key] || key
|
||||
data['network'][aliased_key] = options[key]
|
||||
end
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => [201],
|
||||
|
@ -33,6 +67,27 @@ module Fog
|
|||
'admin_state_up' => options[:admin_state_up],
|
||||
'tenant_id' => options[:tenant_id],
|
||||
}
|
||||
|
||||
# Add provider specific attributes when found
|
||||
#
|
||||
provider_options = [
|
||||
:router_external,
|
||||
:provider_network_type,
|
||||
:provider_segmentation_id,
|
||||
:provider_physical_network
|
||||
]
|
||||
aliases = {
|
||||
:provider_network_type => 'provider:network_type',
|
||||
# Not applicable to the "local" or "gre" network types
|
||||
:provider_physical_network => 'provider:physical_network',
|
||||
:provider_segmentation_id => 'provider:segmentation_id',
|
||||
:router_external => 'router:external'
|
||||
}
|
||||
provider_options.reject{ |o| options[o].nil? }.each do |key|
|
||||
aliased_key = aliases[key] || key
|
||||
data[aliased_key] = options[key]
|
||||
end
|
||||
|
||||
self.data[:networks][data['id']] = data
|
||||
response.body = { 'network' => data }
|
||||
response
|
||||
|
@ -41,4 +96,4 @@ module Fog
|
|||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,6 +10,23 @@ Shindo.tests("Fog::Network[:openstack] | network", ['openstack']) do
|
|||
!@instance.id.nil?
|
||||
end
|
||||
|
||||
tests('#create+extensions').succeeds do
|
||||
net = Fog::Network[:openstack].networks.create(
|
||||
:name => 'net_name',
|
||||
:shared => false,
|
||||
:admin_state_up => true,
|
||||
:tenant_id => 'tenant_id',
|
||||
:router_external => true,
|
||||
# local, gre, vlan. Depends on the provider.
|
||||
# May rise an exception if the network_type isn't valid:
|
||||
# QuantumError: "Invalid input for operation: provider:physical_network"
|
||||
:provider_network_type => 'gre',
|
||||
:provider_segmentation_id => 22
|
||||
)
|
||||
net.destroy
|
||||
net.provider_network_type == 'gre'
|
||||
end
|
||||
|
||||
tests('have attributes') do
|
||||
attributes = [
|
||||
:name,
|
||||
|
|
|
@ -1,19 +1,47 @@
|
|||
Shindo.tests('Fog::Network[:openstack] | network requests', ['openstack']) do
|
||||
|
||||
@network_format = {
|
||||
'id' => String,
|
||||
'name' => String,
|
||||
'subnets' => Array,
|
||||
'shared' => Fog::Boolean,
|
||||
'status' => String,
|
||||
'admin_state_up' => Fog::Boolean,
|
||||
'tenant_id' => String,
|
||||
'id' => String,
|
||||
'name' => String,
|
||||
'subnets' => Array,
|
||||
'shared' => Fog::Boolean,
|
||||
'status' => String,
|
||||
'admin_state_up' => Fog::Boolean,
|
||||
'tenant_id' => String
|
||||
}
|
||||
|
||||
@network_format_extensions = {
|
||||
'router:external' => Fog::Boolean,
|
||||
'provider:network_type' => String,
|
||||
'provider:physical_network' => Fog::Nullable::String,
|
||||
'provider:segmentation_id' => Integer
|
||||
}
|
||||
|
||||
tests('success') do
|
||||
tests('#create_network').formats({'network' => @network_format}) do
|
||||
attributes = {:name => 'net_name', :shared => false,
|
||||
:admin_state_up => true, :tenant_id => 'tenant_id'}
|
||||
attributes = {
|
||||
:name => 'net_name',
|
||||
:shared => false,
|
||||
:admin_state_up => true,
|
||||
:tenant_id => 'tenant_id'
|
||||
}
|
||||
Fog::Network[:openstack].create_network(attributes).body
|
||||
end
|
||||
tests('#create_network+provider extensions').formats(
|
||||
{'network' => @network_format.merge(@network_format_extensions)}
|
||||
) do
|
||||
attributes = {
|
||||
:name => 'net_name',
|
||||
:shared => false,
|
||||
:admin_state_up => true,
|
||||
:tenant_id => 'tenant_id',
|
||||
:router_external => true,
|
||||
# local, gre, vlan. Depends on the provider.
|
||||
# May rise an exception if the network_type isn't valid:
|
||||
# QuantumError: "Invalid input for operation: provider:physical_network"
|
||||
:provider_network_type => 'gre',
|
||||
:provider_segmentation_id => 22,
|
||||
}
|
||||
Fog::Network[:openstack].create_network(attributes).body
|
||||
end
|
||||
|
||||
|
@ -40,6 +68,24 @@ Shindo.tests('Fog::Network[:openstack] | network requests', ['openstack']) do
|
|||
end
|
||||
|
||||
tests('failure') do
|
||||
tests('#create_network+provider extensions').raises(
|
||||
Excon::Errors::BadRequest
|
||||
) do
|
||||
pending if Fog.mocking?
|
||||
|
||||
attributes = {
|
||||
:name => 'net_name',
|
||||
:shared => false,
|
||||
:admin_state_up => true,
|
||||
:tenant_id => 'tenant_id',
|
||||
:router_external => true,
|
||||
# QuantumError: "Invalid input for operation: provider:physical_network"
|
||||
:provider_network_type => 'foobar',
|
||||
:provider_segmentation_id => 22,
|
||||
}
|
||||
Fog::Network[:openstack].create_network(attributes)
|
||||
end
|
||||
|
||||
tests('#get_network').raises(Fog::Network::OpenStack::NotFound) do
|
||||
Fog::Network[:openstack].get_network(0)
|
||||
end
|
||||
|
@ -52,5 +98,10 @@ Shindo.tests('Fog::Network[:openstack] | network requests', ['openstack']) do
|
|||
Fog::Network[:openstack].delete_network(0)
|
||||
end
|
||||
end
|
||||
|
||||
# Cleaning up the mess
|
||||
Fog::Network[:openstack].networks.each do |n|
|
||||
Fog::Network[:openstack].delete_network(n.id)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue