class Fog::Network::OpenStack::Real

Attributes

current_tenant[R]
current_user[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/fog/openstack/network.rb, line 181
def initialize(options={})
  require 'multi_json'

  @openstack_auth_token = options[:openstack_auth_token]

  unless @openstack_auth_token
    missing_credentials = Array.new
    @openstack_api_key  = options[:openstack_api_key]
    @openstack_username = options[:openstack_username]

    missing_credentials << :openstack_api_key  unless @openstack_api_key
    missing_credentials << :openstack_username unless @openstack_username
    raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
  end

  @openstack_tenant               = options[:openstack_tenant]
  @openstack_auth_uri             = URI.parse(options[:openstack_auth_url])
  @openstack_management_url       = options[:openstack_management_url]
  @openstack_must_reauthenticate  = false
  @openstack_service_type         = options[:openstack_service_type] || ['network']
  @openstack_service_name         = options[:openstack_service_name]
  @openstack_endpoint_type        = options[:openstack_endpoint_type] || 'publicURL'
  @openstack_region               = options[:openstack_region]

  @connection_options = options[:connection_options] || {}

  @current_user = options[:current_user]
  @current_tenant = options[:current_tenant]

  authenticate

  @persistent = options[:persistent] || false
  @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Public Instance Methods

add_router_interface(router_id, subnet_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/add_router_interface.rb, line 6
def add_router_interface(router_id, subnet_id, options = {})
  data = {
      'subnet_id' => subnet_id,
  }

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [200],
    :method   => 'PUT',
    :path     => "routers/#{router_id}/add_router_interface"
  )
end
associate_floating_ip(floating_ip_id, port_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/associate_floating_ip.rb, line 6
def associate_floating_ip(floating_ip_id, port_id, options = {})
  data = {
    'floatingip' => {
      'port_id'    => port_id,
    }
  }

  vanilla_options = [: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  => [200],
    :method   => 'PUT',
    :path     => "floatingips/#{floating_ip_id}"
  )
end
associate_lb_health_monitor(pool_id, health_monitor_id) click to toggle source
# File lib/fog/openstack/requests/network/associate_lb_health_monitor.rb, line 6
def associate_lb_health_monitor(pool_id, health_monitor_id)
  data = {
    'health_monitor' => {
      'id' => health_monitor_id,
    }
  }

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [201],
    :method   => 'POST',
    :path     => "lb/pools/#{pool_id}/health_monitors"
  )
end
create_floating_ip(floating_network_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/create_floating_ip.rb, line 6
def create_floating_ip(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
create_lb_health_monitor(type, delay, timeout, max_retries, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/create_lb_health_monitor.rb, line 6
def create_lb_health_monitor(type, delay, timeout, max_retries, options = {})
  data = {
    'health_monitor' => {
      'type'        => type,
      'delay'       => delay,
      'timeout'     => timeout,
      'max_retries' => max_retries
    }
  }

  vanilla_options = [:http_method, :url_path, :expected_codes, :admin_state_up, :tenant_id]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['health_monitor'][key] = options[key]
  end

  request(
    :body    => Fog::JSON.encode(data),
    :expects => [201],
    :method  => 'POST',
    :path    => 'lb/health_monitors'
  )
end
create_lb_member(pool_id, address, protocol_port, weight, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/create_lb_member.rb, line 6
def create_lb_member(pool_id, address, protocol_port, weight, options = {})
  data = {
    'member' => {
      'pool_id'       => pool_id,
      'address'       => address,
      'protocol_port' => protocol_port,
      'weight'        => weight
    }
  }

  vanilla_options = [:admin_state_up, :tenant_id]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['member'][key] = options[key]
  end

  request(
    :body    => Fog::JSON.encode(data),
    :expects => [201],
    :method  => 'POST',
    :path    => 'lb/members'
  )
end
create_lb_pool(subnet_id, protocol, lb_method, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/create_lb_pool.rb, line 6
def create_lb_pool(subnet_id, protocol, lb_method, options = {})
  data = {
    'pool' => {
      'subnet_id' => subnet_id,
      'protocol'  => protocol,
      'lb_method' => lb_method
    }
  }

  vanilla_options = [:name, :description, :admin_state_up, :tenant_id]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['pool'][key] = options[key]
  end

  request(
    :body    => Fog::JSON.encode(data),
    :expects => [201],
    :method  => 'POST',
    :path    => 'lb/pools'
  )
end
create_lb_vip(subnet_id, pool_id, protocol, protocol_port, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/create_lb_vip.rb, line 6
def create_lb_vip(subnet_id, pool_id, protocol, protocol_port, options = {})
  data = {
    'vip' => {
      'subnet_id'     => subnet_id,
      'pool_id'       => pool_id,
      'protocol'      => protocol,
      'protocol_port' => protocol_port
    }
  }

  vanilla_options = [:name, :description, :address, :session_persistence, :connection_limit,
                     :admin_state_up, :tenant_id]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['vip'][key] = options[key]
  end

  request(
    :body    => Fog::JSON.encode(data),
    :expects => [201],
    :method  => 'POST',
    :path    => 'lb/vips'
  )
end
create_network(options = {}) click to toggle source
# File lib/fog/openstack/requests/network/create_network.rb, line 6
def create_network(options = {})
  data = { 'network' => {} }

  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],
    :method   => 'POST',
    :path     => 'networks'
  )
end
create_port(network_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/create_port.rb, line 6
def create_port(network_id, options = {})
  data = {
    'port' => {
      'network_id' => network_id,
    }
  }

  vanilla_options = [:name, :fixed_ips, :mac_address, :admin_state_up,
                     :device_owner, :device_id, :tenant_id]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['port'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [201],
    :method   => 'POST',
    :path     => 'ports'
  )
end
create_router(name, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/create_router.rb, line 6
def create_router(name, options = {})
  data = {
    'router' => {
      'name' => name,
    }
  }

  vanilla_options = [
    :admin_state_up, 
    :tenant_id,
    :network_id,
    :external_gateway_info,
    :status,
    :subnet_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
create_subnet(network_id, cidr, ip_version, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/create_subnet.rb, line 6
def create_subnet(network_id, cidr, ip_version, options = {})
  data = {
    'subnet' => {
      'network_id' => network_id,
      'cidr'       => cidr,
      'ip_version' => ip_version,
    }
  }

  vanilla_options = [:name, :gateway_ip, :allocation_pools,
                     :dns_nameservers, :host_routes, :enable_dhcp,
                     :tenant_id]
  vanilla_options.reject{ |o| options[o].nil? }.each do |key|
    data['subnet'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [201],
    :method   => 'POST',
    :path     => 'subnets'
  )
end
credentials() click to toggle source
# File lib/fog/openstack/network.rb, line 216
def credentials
  { :provider                 => 'openstack',
    :openstack_auth_url       => @openstack_auth_uri.to_s,
    :openstack_auth_token     => @auth_token,
    :openstack_management_url => @openstack_management_url,
    :current_user             => @current_user,
    :current_tenant           => @current_tenant,
    :openstack_region         => @openstack_region }
end
delete_floating_ip(floating_ip_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_floating_ip.rb, line 6
def delete_floating_ip(floating_ip_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "floatingips/#{floating_ip_id}"
  )
end
delete_lb_health_monitor(health_monitor_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_lb_health_monitor.rb, line 6
def delete_lb_health_monitor(health_monitor_id)
  request(
    :expects => 204,
    :method  => 'DELETE',
    :path    => "lb/health_monitors/#{health_monitor_id}"
  )
end
delete_lb_member(member_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_lb_member.rb, line 6
def delete_lb_member(member_id)
  request(
    :expects => 204,
    :method  => 'DELETE',
    :path    => "lb/members/#{member_id}"
  )
end
delete_lb_pool(pool_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_lb_pool.rb, line 6
def delete_lb_pool(pool_id)
  request(
    :expects => 204,
    :method  => 'DELETE',
    :path    => "lb/pools/#{pool_id}"
  )
end
delete_lb_vip(vip_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_lb_vip.rb, line 6
def delete_lb_vip(vip_id)
  request(
    :expects => 204,
    :method  => 'DELETE',
    :path    => "lb/vips/#{vip_id}"
  )
end
delete_network(network_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_network.rb, line 6
def delete_network(network_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "networks/#{network_id}"
  )
end
delete_port(port_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_port.rb, line 6
def delete_port(port_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "ports/#{port_id}"
  )
end
delete_quota(tenant_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_quota.rb, line 6
def delete_quota(tenant_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "/quotas/#{tenant_id}"
  )
end
delete_router(router_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_router.rb, line 6
def delete_router(router_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "routers/#{router_id}"
  )
end
delete_subnet(subnet_id) click to toggle source
# File lib/fog/openstack/requests/network/delete_subnet.rb, line 6
def delete_subnet(subnet_id)
  request(
    :expects  => 204,
    :method   => 'DELETE',
    :path     => "subnets/#{subnet_id}"
  )
end
disassociate_floating_ip(floating_ip_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/disassociate_floating_ip.rb, line 6
def disassociate_floating_ip(floating_ip_id, options = {})
  data = {
    'floatingip' => {
      'port_id'    => nil,
    }
  }

  vanilla_options = [: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  => [200],
    :method   => 'PUT',
    :path     => "floatingips/#{floating_ip_id}"
  )
end
disassociate_lb_health_monitor(pool_id, health_monitor_id) click to toggle source
# File lib/fog/openstack/requests/network/disassociate_lb_health_monitor.rb, line 6
def disassociate_lb_health_monitor(pool_id, health_monitor_id)
  request(
    :expects  => [204],
    :method   => 'DELETE',
    :path     => "lb/pools/#{pool_id}/health_monitors/#{health_monitor_id}"
  )
end
get_floating_ip(floating_ip_id) click to toggle source
# File lib/fog/openstack/requests/network/get_floating_ip.rb, line 6
def get_floating_ip(floating_ip_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "floatingips/#{floating_ip_id}"
  )
end
get_lb_health_monitor(health_monitor_id) click to toggle source
# File lib/fog/openstack/requests/network/get_lb_health_monitor.rb, line 6
def get_lb_health_monitor(health_monitor_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "lb/health_monitors/#{health_monitor_id}"
  )
end
get_lb_member(member_id) click to toggle source
# File lib/fog/openstack/requests/network/get_lb_member.rb, line 6
def get_lb_member(member_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "lb/members/#{member_id}"
  )
end
get_lb_pool(pool_id) click to toggle source
# File lib/fog/openstack/requests/network/get_lb_pool.rb, line 6
def get_lb_pool(pool_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "lb/pools/#{pool_id}"
  )
end
get_lb_pool_stats(pool_id) click to toggle source
# File lib/fog/openstack/requests/network/get_lb_pool_stats.rb, line 6
def get_lb_pool_stats(pool_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "lb/pools/#{pool_id}/stats"
  )
end
get_lb_vip(vip_id) click to toggle source
# File lib/fog/openstack/requests/network/get_lb_vip.rb, line 6
def get_lb_vip(vip_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "lb/vips/#{vip_id}"
  )
end
get_network(network_id) click to toggle source
# File lib/fog/openstack/requests/network/get_network.rb, line 6
def get_network(network_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "networks/#{network_id}"
  )
end
get_port(port_id) click to toggle source
# File lib/fog/openstack/requests/network/get_port.rb, line 6
def get_port(port_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "ports/#{port_id}"
  )
end
get_quota(tenant_id) click to toggle source
# File lib/fog/openstack/requests/network/get_quota.rb, line 6
def get_quota(tenant_id)
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/quotas/#{tenant_id}"
  )
end
get_quotas() click to toggle source
# File lib/fog/openstack/requests/network/get_quotas.rb, line 6
def get_quotas
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "/quotas"
  )
end
get_router(router_id) click to toggle source
# File lib/fog/openstack/requests/network/get_router.rb, line 6
def get_router(router_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "routers/#{router_id}"
  )
end
get_subnet(subnet_id) click to toggle source
# File lib/fog/openstack/requests/network/get_subnet.rb, line 6
def get_subnet(subnet_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "subnets/#{subnet_id}"
  )
end
list_floating_ips(filters = {}) click to toggle source
# File lib/fog/openstack/requests/network/list_floating_ips.rb, line 6
def list_floating_ips(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'floatingips',
    :query   => filters
  )
end
list_lb_health_monitors(filters = {}) click to toggle source
# File lib/fog/openstack/requests/network/list_lb_health_monitors.rb, line 6
def list_lb_health_monitors(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'lb/health_monitors',
    :query   => filters
  )
end
list_lb_members(filters = {}) click to toggle source
# File lib/fog/openstack/requests/network/list_lb_members.rb, line 6
def list_lb_members(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'lb/members',
    :query   => filters
  )
end
list_lb_pools(filters = {}) click to toggle source
# File lib/fog/openstack/requests/network/list_lb_pools.rb, line 6
def list_lb_pools(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'lb/pools',
    :query   => filters
  )
end
list_lb_vips(filters = {}) click to toggle source
# File lib/fog/openstack/requests/network/list_lb_vips.rb, line 6
def list_lb_vips(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'lb/vips',
    :query   => filters
  )
end
list_networks(filters = {}) click to toggle source
# File lib/fog/openstack/requests/network/list_networks.rb, line 6
def list_networks(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'networks',
    :query   => filters
  )
end
list_ports(filters = {}) click to toggle source
# File lib/fog/openstack/requests/network/list_ports.rb, line 6
def list_ports(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'ports',
    :query   => filters
  )
end
list_routers(filters = {}) click to toggle source
# File lib/fog/openstack/requests/network/list_routers.rb, line 6
def list_routers(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'routers',
    :query   => filters
  )
end
list_subnets(filters = {}) click to toggle source
# File lib/fog/openstack/requests/network/list_subnets.rb, line 6
def list_subnets(filters = {})
  request(
    :expects => 200,
    :method  => 'GET',
    :path    => 'subnets',
    :query   => filters
  )
end
reload() click to toggle source
# File lib/fog/openstack/network.rb, line 226
def reload
  @connection.reset
end
remove_router_interface(router_id, subnet_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/remove_router_interface.rb, line 6
def remove_router_interface(router_id, subnet_id, options = {})
  data = {
      'subnet_id' => subnet_id,
  }

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => [200],
    :method   => 'PUT',
    :path     => "routers/#{router_id}/remove_router_interface"
  )
end
request(params) click to toggle source
# File lib/fog/openstack/network.rb, line 230
def request(params)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :host     => @host,
      :path     => "#{@path}/#{params[:path]}"#,
    }))
  rescue Excon::Errors::Unauthorized => error
    if error.response.body != 'Bad username or password' # token expiration
      @openstack_must_reauthenticate = true
      authenticate
      retry
    else # bad credentials
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Network::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    response.body = MultiJson.decode(response.body)
  end
  response
end
set_tenant(tenant) click to toggle source
# File lib/fog/openstack/requests/network/set_tenant.rb, line 6
def set_tenant(tenant)
  @openstack_must_reauthenticate = true
  @openstack_tenant = tenant.to_s
  authenticate
end
update_lb_health_monitor(health_monitor_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/update_lb_health_monitor.rb, line 6
def update_lb_health_monitor(health_monitor_id, options = {})
  data = { 'health_monitor' => {} }

  vanilla_options = [:delay, :timeout, :max_retries, :http_method, :url_path, :expected_codes, :admin_state_up]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['health_monitor'][key] = options[key]
  end

  request(
    :body    => Fog::JSON.encode(data),
    :expects => 200,
    :method  => 'PUT',
    :path    => "lb/health_monitors/#{health_monitor_id}"
  )
end
update_lb_member(member_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/update_lb_member.rb, line 6
def update_lb_member(member_id, options = {})
  data = { 'member' => {} }

  vanilla_options = [:pool_id, :weight, :admin_state_up]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['member'][key] = options[key]
  end

  request(
    :body    => Fog::JSON.encode(data),
    :expects => 200,
    :method  => 'PUT',
    :path    => "lb/members/#{member_id}"
  )
end
update_lb_pool(pool_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/update_lb_pool.rb, line 6
def update_lb_pool(pool_id, options = {})
  data = { 'pool' => {} }

  vanilla_options = [:name, :description, :lb_method, :admin_state_up]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['pool'][key] = options[key]
  end

  request(
    :body    => Fog::JSON.encode(data),
    :expects => 200,
    :method  => 'PUT',
    :path    => "lb/pools/#{pool_id}"
  )
end
update_lb_vip(vip_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/update_lb_vip.rb, line 6
def update_lb_vip(vip_id, options = {})
  data = { 'vip' => {} }

  vanilla_options = [:pool_id, :name, :description, :session_persistence, :connection_limit, :admin_state_up]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['vip'][key] = options[key]
  end

  request(
    :body    => Fog::JSON.encode(data),
    :expects => 200,
    :method  => 'PUT',
    :path    => "lb/vips/#{vip_id}"
  )
end
update_network(network_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/update_network.rb, line 6
def update_network(network_id, options = {})
  data = { 'network' => {} }

  vanilla_options = [:name, :shared, :admin_state_up]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['network'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "networks/#{network_id}.json"
  )
end
update_port(port_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/update_port.rb, line 6
def update_port(port_id, options = {})
  data = { 'port' => {} }

  vanilla_options = [:name, :fixed_ips, :admin_state_up, :device_owner,
                     :device_id]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['port'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "ports/#{port_id}.json"
  )
end
update_quota(tenant_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/update_quota.rb, line 6
def update_quota(tenant_id, options = {})
  request(
    :body     => Fog::JSON.encode({ 'quota' => options} ),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "/quotas/#{tenant_id}"
  )
end
update_router(router_id, options = {}) click to toggle source

Update Router

Beyond the name and the administrative state, the only parameter which can be updated with this operation is the external gateway.

router = Fog::Network[:openstack].routers.first
net = Fog::Network[:openstack].networks.first

# :external_gateway_info can be either a
# Fog::Network::OpenStack::Network or a Hash
# like { 'network_id' => network.id }
Fog::Network[:openstack].update_router router.id,
                                       :name => 'foo',
                                       :external_gateway_info => net,
                                       :admin_state_up => true

@see docs.openstack.org/api/openstack-network/2.0/content/router_update.html

# File lib/fog/openstack/requests/network/update_router.rb, line 25
def update_router(router_id, options = {})
  data = { 'router' => {} }
  
  vanilla_options = [:name, :admin_state_up]

  egi = options[:external_gateway_info]
  if egi
    if egi.is_a?(Fog::Network::OpenStack::Network)
      data['router']['external_gateway_info'] = { 'network_id' => egi.id }
    elsif egi.is_a?(Hash) and egi['network_id']
      data['router']['external_gateway_info'] = egi
    else
      raise ArgumentError.new('Invalid external_gateway_info attribute')
    end
  end

  vanilla_options.reject{ |o| options[o].nil? }.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
update_subnet(subnet_id, options = {}) click to toggle source
# File lib/fog/openstack/requests/network/update_subnet.rb, line 6
def update_subnet(subnet_id, options = {})
  data = { 'subnet' => {} }

  vanilla_options = [:name, :gateway_ip, :dns_nameservers,
                     :host_routes, :enable_dhcp]
  vanilla_options.select{ |o| options.has_key?(o) }.each do |key|
    data['subnet'][key] = options[key]
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'PUT',
    :path     => "subnets/#{subnet_id}"
  )
end

Private Instance Methods

authenticate() click to toggle source
# File lib/fog/openstack/network.rb, line 265
def authenticate
  if @openstack_must_reauthenticate || @openstack_auth_token.nil?
    options = {
      :openstack_tenant   => @openstack_tenant,
      :openstack_api_key  => @openstack_api_key,
      :openstack_username => @openstack_username,
      :openstack_auth_uri => @openstack_auth_uri,
      :openstack_auth_token => @openstack_auth_token,
      :openstack_service_type => @openstack_service_type,
      :openstack_service_name => @openstack_service_name,
      :openstack_endpoint_type => @openstack_endpoint_type,
      :openstack_region => @openstack_region
    }

    credentials = Fog::OpenStack.authenticate_v2(options, @connection_options)

    @current_user = credentials[:user]
    @current_tenant = credentials[:tenant]

    @openstack_must_reauthenticate = false
    @auth_token = credentials[:token]
    @openstack_management_url = credentials[:server_management_url]
    uri = URI.parse(@openstack_management_url)
  else
    @auth_token = @openstack_auth_token
    uri = URI.parse(@openstack_management_url)
  end

  @host   = uri.host
  @path   = uri.path
  @path.sub!(/\/$/, '')
  unless @path.match(SUPPORTED_VERSIONS)
    @path = "/" + Fog::OpenStack.get_supported_version(SUPPORTED_VERSIONS,
                                                       uri,
                                                       @auth_token,
                                                       @connection_options)
  end
  @port   = uri.port
  @scheme = uri.scheme
  true
end