mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
supported OpenStack Quantum Router Operation. not include mock code.
This commit is contained in:
parent
67650e2b3e
commit
9e7b767fea
10 changed files with 388 additions and 0 deletions
53
lib/fog/openstack/models/network/router.rb
Normal file
53
lib/fog/openstack/models/network/router.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
class Router < Fog::Model
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :network_id
|
||||
attribute :fixed_ips
|
||||
attribute :mac_address
|
||||
attribute :status
|
||||
attribute :admin_state_up
|
||||
attribute :device_owner
|
||||
attribute :device_id
|
||||
attribute :tenant_id
|
||||
|
||||
def initialize(attributes)
|
||||
# Old 'connection' is renamed as service and should be used instead
|
||||
prepare_service_value(attributes)
|
||||
super
|
||||
end
|
||||
|
||||
def save
|
||||
requires :name
|
||||
identity ? update : create
|
||||
end
|
||||
|
||||
def create
|
||||
requires :name
|
||||
merge_attributes(service.create_router(self.name,
|
||||
self.attributes).body['router'])
|
||||
self
|
||||
end
|
||||
|
||||
def update
|
||||
requires :id
|
||||
merge_attributes(service.update_router(self.id,
|
||||
self.attributes).body['router'])
|
||||
self
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
service.delete_router(self.id)
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
34
lib/fog/openstack/models/network/routers.rb
Normal file
34
lib/fog/openstack/models/network/routers.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/openstack/models/network/router'
|
||||
|
||||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
class Routers < Fog::Collection
|
||||
|
||||
attribute :filters
|
||||
|
||||
model Fog::Network::OpenStack::Router
|
||||
|
||||
def initialize(attributes)
|
||||
self.filters ||= {}
|
||||
super
|
||||
end
|
||||
|
||||
def all(filters = filters)
|
||||
self.filters = filters
|
||||
load(service.list_routers(filters).body['routers'])
|
||||
end
|
||||
|
||||
def get(router_id)
|
||||
if router = service.get_router(router_id).body['router']
|
||||
new(router)
|
||||
end
|
||||
rescue Fog::Network::OpenStack::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -40,6 +40,15 @@ module Fog
|
|||
request :get_port
|
||||
request :update_port
|
||||
|
||||
# Router CRUD
|
||||
request :list_routers
|
||||
request :create_router
|
||||
request :delete_router
|
||||
request :get_router
|
||||
request :update_router
|
||||
request :add_router_interface
|
||||
request :remove_router_interface
|
||||
|
||||
# Subnet CRUD
|
||||
request :list_subnets
|
||||
request :create_subnet
|
||||
|
@ -66,6 +75,7 @@ module Fog
|
|||
:ports => {},
|
||||
:subnets => {},
|
||||
:floating_ips => {},
|
||||
:routers => {},
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
47
lib/fog/openstack/requests/network/add_router_interface.rb
Normal file
47
lib/fog/openstack/requests/network/add_router_interface.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def add_router_interface(router_id, subnet_id, options = {})
|
||||
data = {
|
||||
'subnet_id' => subnet_id,
|
||||
}
|
||||
|
||||
vanilla_options = [:name]
|
||||
vanilla_options.reject{ |o| options[o].nil? }.each do |key|
|
||||
data['subnet_id'][key] = options[key]
|
||||
end
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => [200],
|
||||
:method => 'PUT',
|
||||
:path => "routers/#{router_id}/add_router_interface"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def add_router_interface(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
|
46
lib/fog/openstack/requests/network/create_router.rb
Normal file
46
lib/fog/openstack/requests/network/create_router.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def create_router(name, options = {})
|
||||
data = {
|
||||
'router' => {
|
||||
'name' => name,
|
||||
}
|
||||
}
|
||||
|
||||
vanilla_options = [:admin_state_up, :tenand_id]
|
||||
vanilla_options.reject{ |o| options[o].nil? }.each do |key|
|
||||
data['router'][key] = options[key]
|
||||
end
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => [201],
|
||||
:method => 'POST',
|
||||
:path => 'routers'
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def create_router(name, options = {})
|
||||
response = Excon::Response.new
|
||||
response.status = 201
|
||||
data = {
|
||||
'id' => Fog::Mock.random_numbers(6).to_s,
|
||||
'name' => options[:name],
|
||||
'status' => 'ACTIVE',
|
||||
'admin_state_up' => options[:admin_state_up],
|
||||
'tenant_id' => options[:tenant_id],
|
||||
}
|
||||
self.data[:routers][data['id']] = data
|
||||
response.body = { 'router' => data }
|
||||
response
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
30
lib/fog/openstack/requests/network/delete_router.rb
Normal file
30
lib/fog/openstack/requests/network/delete_router.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def delete_router(router_id)
|
||||
request(
|
||||
:expects => 204,
|
||||
:method => 'DELETE',
|
||||
:path => "routers/#{router_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def delete_router(router_id)
|
||||
response = Excon::Response.new
|
||||
if list_routers.body['routers'].map { |r| r['id'] }.include? router_id
|
||||
self.data[:routers].delete(router_id)
|
||||
response.status = 204
|
||||
response
|
||||
else
|
||||
raise Fog::Network::OpenStack::NotFound
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
48
lib/fog/openstack/requests/network/get_router.rb
Normal file
48
lib/fog/openstack/requests/network/get_router.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def get_router(router_id)
|
||||
request(
|
||||
:expects => [200],
|
||||
:method => 'GET',
|
||||
:path => "routers/#{router_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def get_router(router_id)
|
||||
response = Excon::Response.new
|
||||
if data = self.data[:routers][router_id]
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'router' => {
|
||||
'id' => '5c81d975-5fea-4674-9c1f-b8aa10bf9a79',
|
||||
'name' => 'router_1',
|
||||
'network_id' => 'e624a36d-762b-481f-9b50-4154ceb78bbb',
|
||||
'fixed_ips' => [
|
||||
{
|
||||
'ip_address' => '10.2.2.2',
|
||||
'subnet_id' => '2e4ec6a4-0150-47f5-8523-e899ac03026e',
|
||||
}
|
||||
],
|
||||
'mac_address' => 'fa:16:3e:62:91:7f',
|
||||
'status' => 'ACTIVE',
|
||||
'admin_state_up' => true,
|
||||
'device_id' => 'dhcp724fc160-2b2e-597e-b9ed-7f65313cd73f-e624a36d-762b-481f-9b50-4154ceb78bbb',
|
||||
'device_owner' => 'network:dhcp',
|
||||
'tenant_id' => 'f8b26a6032bc47718a7702233ac708b9',
|
||||
}
|
||||
}
|
||||
response
|
||||
else
|
||||
raise Fog::Network::OpenStack::NotFound
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
27
lib/fog/openstack/requests/network/list_routers.rb
Normal file
27
lib/fog/openstack/requests/network/list_routers.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def list_routers(filters = {})
|
||||
request(
|
||||
:expects => 200,
|
||||
:method => 'GET',
|
||||
:path => 'routers',
|
||||
:query => filters
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def list_routers(filters = {})
|
||||
Excon::Response.new(
|
||||
:body => { 'routers' => self.data[:routers].values },
|
||||
:status => 200
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,47 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def remove_router_interface(router_id, subnet_id, options = {})
|
||||
data = {
|
||||
'subnet_id' => subnet_id,
|
||||
}
|
||||
|
||||
vanilla_options = [:name]
|
||||
vanilla_options.reject{ |o| options[o].nil? }.each do |key|
|
||||
data['subnet_id'][key] = options[key]
|
||||
end
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => [200],
|
||||
:method => 'PUT',
|
||||
:path => "routers/#{router_id}/remove_router_interface"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def remove_router_interface(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
|
46
lib/fog/openstack/requests/network/update_router.rb
Normal file
46
lib/fog/openstack/requests/network/update_router.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
module Fog
|
||||
module Network
|
||||
class OpenStack
|
||||
|
||||
class Real
|
||||
def update_router(router_id, network_id, options = {})
|
||||
data = {
|
||||
'router' => {
|
||||
'external_gateway_info' => {
|
||||
'network_id' => network_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
vanilla_options = [:name, :admin_state_up, :tenand_id]
|
||||
vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
|
||||
data['router'][key] = options[key]
|
||||
end
|
||||
|
||||
request(
|
||||
:body => Fog::JSON.encode(data),
|
||||
:expects => 200,
|
||||
:method => 'PUT',
|
||||
:path => "routers/#{router_id}.json"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def update_router(router_id, options = {})
|
||||
response = Excon::Response.new
|
||||
if router = list_routers.body['routers'].detect { |_| _['id'] == router_id }
|
||||
router['name'] = options[:name]
|
||||
router['admin_state_up'] = options[:admin_state_up]
|
||||
router['tenant_id'] = options[:tenant_id]
|
||||
response.body = { 'router' => router }
|
||||
response.status = 200
|
||||
response
|
||||
else
|
||||
raise Fog::Network::OpenStack::NotFound
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue