create_network(options = {})
click to toggle source
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
request(
:body => Fog::JSON.encode(data),
:expects => [201],
:method => 'POST',
:path => 'networks'
)
end
create_port(network_id, options = {})
click to toggle source
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_subnet(network_id, cidr, ip_version, options = {})
click to toggle source
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
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 }
end
delete_network(network_id)
click to toggle source
def delete_network(network_id)
request(
:expects => 204,
:method => 'DELETE',
:path => "networks/#{network_id}"
)
end
delete_port(port_id)
click to toggle source
def delete_port(port_id)
request(
:expects => 204,
:method => 'DELETE',
:path => "ports/#{port_id}"
)
end
delete_subnet(subnet_id)
click to toggle source
def delete_subnet(subnet_id)
request(
:expects => 204,
:method => 'DELETE',
:path => "subnets/#{subnet_id}"
)
end
get_network(network_id)
click to toggle source
def get_network(network_id)
request(
:expects => [200],
:method => 'GET',
:path => "networks/#{network_id}"
)
end
get_port(port_id)
click to toggle source
def get_port(port_id)
request(
:expects => [200],
:method => 'GET',
:path => "ports/#{port_id}"
)
end
get_subnet(subnet_id)
click to toggle source
def get_subnet(subnet_id)
request(
:expects => [200],
:method => 'GET',
:path => "subnets/#{subnet_id}"
)
end
list_networks(filters = {})
click to toggle source
def list_networks(filters = {})
request(
:expects => 200,
:method => 'GET',
:path => 'networks',
:query => filters
)
end
list_ports(filters = {})
click to toggle source
def list_ports(filters = {})
request(
:expects => 200,
:method => 'GET',
:path => 'ports',
:query => filters
)
end
list_subnets(filters = {})
click to toggle source
def list_subnets(filters = {})
request(
:expects => 200,
:method => 'GET',
:path => 'subnets',
:query => filters
)
end
reload()
click to toggle source
def reload
@connection.reset
end
request(params)
click to toggle source
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'
@openstack_must_reauthenticate = true
authenticate
retry
else
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
def set_tenant(tenant)
@openstack_must_reauthenticate = true
@openstack_tenant = tenant.to_s
authenticate
end
update_network(network_id, options = {})
click to toggle source
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
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_subnet(subnet_id, options = {})
click to toggle source
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