File: network_subnets_routers.rb

package info (click to toggle)
ruby-fog-openstack 1.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,784 kB
  • sloc: ruby: 47,937; makefile: 5; sh: 4
file content (62 lines) | stat: -rw-r--r-- 2,288 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'fog/openstack'

#
# Quantum demo
#
# Create some routers, networks and subnets for
# a couple of tenants.
#
# Needs Fog >= 1.11.0
# Needs OpenStack credentials in ~/.fog
#

def create_tenant_network( tenant_name,
                           external_net,
                           router_name = 'router1',
                           subnet_range = '10.0.0.0/21',
                           subnet_gateway = '10.0.0.1',
                           private_network_name = 'private' )

  network = Fog::OpenStack::Network.new
  id = Fog::Identity[:openstack]

  tenant = id.tenants.find { |t| t.name == tenant_name }

  # Create a router for the tenant
  router = network.routers.create :name => router_name,
                                  :tenant_id => tenant.id,
                                  :external_gateway_info => {
                                    'network_id' => external_net.id
                                  }

  # Create a private network for the tenant
  net = network.networks.create :name => private_network_name,
                                :tenant_id => tenant.id

  # Create a subnet for the previous network and associate it
  # with the tenant
  subnet = network.subnets.create :name => 'net_10',
                                  :network_id  => net.id,
                                  :ip_version  => 4,
                                  :gateway_ip  => subnet_gateway,
                                  :cidr        => subnet_range,
                                  :tenant_id => tenant.id,
                                  :enable_dhcp => true

  network.add_router_interface router.id, subnet.id
end

# Create a public shared network
public_net = network.networks.create :name => 'nova',
                                     :router_external => true

# Create the public subnet
public_subnet = network.subnets.create :name => 'floating_ips_net',
                                       :network_id  => public_net.id,
                                       :ip_version  => 4,
                                       :cidr        => '1.2.3.0/24',
                                       :enable_dhcp => false

# Create tenant networks
create_tenant_network 'admin@example.net', public_net
create_tenant_network 'demo@example.net', public_net