class Object

Public Instance Methods

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' ) click to toggle source

Quantum demo

Create some routers, networks and subnets for a couple of tenants.

Needs Fog >= 1.11.0 Needs OpenStack credentials in ~/.fog

# File lib/fog/openstack/examples/network/network_subnets_routers.rb, line 13
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::Network[:openstack]
  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_virtual_address_pairing(username, password, auth_url, tenant, device_id, device_ip_address, network_id) click to toggle source

Add additional port to an Openstack node

# File lib/fog/openstack/examples/network/network_add_port.rb, line 5
def create_virtual_address_pairing(username, password, auth_url, tenant, device_id, device_ip_address, network_id)
  network_driver = Fog::Network.new(:provider           => :openstack,
                                    :openstack_api_key  => password,
                                    :openstack_username => username,
                                    :openstack_auth_url => auth_url,
                                    :openstack_tenant   => tenant)

  virtual_ip_address = network_driver.create_port(network_id)

  server_nics = network_driver.list_ports('device_id' => device_id).data[:body]['ports']
  port = (server_nics.select do |network_port|
    network_port['mac_address'] == server.attributes['macaddress']
  end).first

  network_driver.update_port(port['id'], :allowed_address_pairs => [{:ip_address => device_ip_address},
                                                                    {:ip_address => virtual_ip_address}])
end