2013-03-21 13:58:11 -04:00
|
|
|
require 'fog/hp'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module HP
|
|
|
|
class Network < Fog::Service
|
|
|
|
|
2013-03-21 18:54:24 -04:00
|
|
|
requires :hp_access_key, :hp_secret_key, :hp_tenant_id, :hp_avl_zone
|
2013-04-12 19:32:22 -04:00
|
|
|
recognizes :hp_auth_uri, :credentials
|
2013-03-21 13:58:11 -04:00
|
|
|
recognizes :persistent, :connection_options
|
|
|
|
recognizes :hp_use_upass_auth_style, :hp_auth_version, :user_agent
|
|
|
|
|
|
|
|
secrets :hp_secret_key
|
|
|
|
|
2013-03-28 18:13:49 -04:00
|
|
|
model_path 'fog/hp/models/network'
|
2013-04-01 18:06:21 -04:00
|
|
|
model :floating_ip
|
|
|
|
collection :floating_ips
|
2013-03-28 18:13:49 -04:00
|
|
|
model :network
|
|
|
|
collection :networks
|
2013-04-01 15:27:48 -04:00
|
|
|
model :port
|
|
|
|
collection :ports
|
2013-04-02 12:04:48 -04:00
|
|
|
model :router
|
|
|
|
collection :routers
|
2013-04-08 16:53:05 -04:00
|
|
|
model :security_group
|
|
|
|
collection :security_groups
|
2013-04-12 00:52:06 -04:00
|
|
|
model :security_group_rule
|
|
|
|
collection :security_group_rules
|
2013-03-29 14:37:05 -04:00
|
|
|
model :subnet
|
|
|
|
collection :subnets
|
2013-03-28 18:13:49 -04:00
|
|
|
|
2013-03-21 13:58:11 -04:00
|
|
|
request_path 'fog/hp/requests/network'
|
2013-03-27 18:07:39 -04:00
|
|
|
request :add_router_interface
|
2013-03-26 17:50:15 -04:00
|
|
|
request :associate_floating_ip
|
|
|
|
request :create_floating_ip
|
2013-03-21 16:06:58 -04:00
|
|
|
request :create_network
|
2013-03-27 18:07:39 -04:00
|
|
|
request :create_port
|
|
|
|
request :create_router
|
2013-04-04 14:29:44 -04:00
|
|
|
request :create_security_group
|
2013-04-04 16:57:31 -04:00
|
|
|
request :create_security_group_rule
|
2013-03-22 16:16:25 -04:00
|
|
|
request :create_subnet
|
2013-03-26 17:50:15 -04:00
|
|
|
request :disassociate_floating_ip
|
|
|
|
request :delete_floating_ip
|
2013-03-21 16:37:27 -04:00
|
|
|
request :delete_network
|
2013-03-27 18:07:39 -04:00
|
|
|
request :delete_port
|
|
|
|
request :delete_router
|
2013-04-04 14:29:44 -04:00
|
|
|
request :delete_security_group
|
2013-04-04 16:57:31 -04:00
|
|
|
request :delete_security_group_rule
|
2013-03-22 16:16:25 -04:00
|
|
|
request :delete_subnet
|
2013-03-26 17:50:15 -04:00
|
|
|
request :get_floating_ip
|
2013-03-21 14:54:07 -04:00
|
|
|
request :get_network
|
2013-03-27 18:07:39 -04:00
|
|
|
request :get_port
|
|
|
|
request :get_router
|
2013-04-04 14:29:44 -04:00
|
|
|
request :get_security_group
|
2013-04-04 16:57:31 -04:00
|
|
|
request :get_security_group_rule
|
2013-03-22 16:16:25 -04:00
|
|
|
request :get_subnet
|
2013-03-26 17:50:15 -04:00
|
|
|
request :list_floating_ips
|
2013-03-21 13:58:11 -04:00
|
|
|
request :list_networks
|
2013-03-27 18:07:39 -04:00
|
|
|
request :list_ports
|
|
|
|
request :list_routers
|
2013-04-04 16:57:31 -04:00
|
|
|
request :list_security_group_rules
|
2013-04-04 14:29:44 -04:00
|
|
|
request :list_security_groups
|
2013-03-22 16:16:25 -04:00
|
|
|
request :list_subnets
|
2013-03-27 18:07:39 -04:00
|
|
|
request :remove_router_interface
|
2013-03-21 16:55:42 -04:00
|
|
|
request :update_network
|
2013-03-27 18:07:39 -04:00
|
|
|
request :update_port
|
|
|
|
request :update_router
|
2013-03-22 16:16:25 -04:00
|
|
|
request :update_subnet
|
2013-03-21 13:58:11 -04:00
|
|
|
|
|
|
|
module Utils
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
include Utils
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {
|
2013-03-27 18:07:39 -04:00
|
|
|
:floating_ips => {},
|
2013-03-28 14:13:28 -04:00
|
|
|
:networks => {
|
|
|
|
'X0X0X0ExtNetX0X0X0' =>
|
|
|
|
{
|
|
|
|
'id' => 'X0X0X0ExtNetX0X0X0',
|
|
|
|
'name' => 'Mock-Ext-Net',
|
|
|
|
'tenant_id' => Fog::Mock.random_numbers(14).to_s,
|
|
|
|
'status' => 'ACTIVE',
|
|
|
|
'subnets' => [],
|
|
|
|
'router:external' => true,
|
|
|
|
'admin_state_up' => true,
|
|
|
|
'shared' => true
|
|
|
|
}
|
|
|
|
},
|
2013-03-26 17:50:15 -04:00
|
|
|
:ports => {},
|
2013-03-27 18:07:39 -04:00
|
|
|
:routers => {},
|
2013-04-04 14:29:44 -04:00
|
|
|
:security_groups => {},
|
2013-04-04 16:57:31 -04:00
|
|
|
:security_group_rules => {},
|
2013-03-27 18:07:39 -04:00
|
|
|
:subnets => {}
|
2013-03-21 13:58:11 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
@hp_access_key = options[:hp_access_key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data[@hp_access_key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@hp_access_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
include Utils
|
2013-04-12 19:32:22 -04:00
|
|
|
attr_reader :credentials
|
2013-03-21 13:58:11 -04:00
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
@hp_access_key = options[:hp_access_key]
|
|
|
|
@hp_secret_key = options[:hp_secret_key]
|
|
|
|
@hp_auth_uri = options[:hp_auth_uri]
|
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
### Set an option to use the style of authentication desired; :v1 or :v2 (default)
|
|
|
|
auth_version = options[:hp_auth_version] || :v2
|
2013-04-12 19:32:22 -04:00
|
|
|
### Pass the service name for network to the authentication call
|
2013-03-21 13:58:11 -04:00
|
|
|
options[:hp_service_type] = "Networking"
|
|
|
|
@hp_tenant_id = options[:hp_tenant_id]
|
|
|
|
@hp_avl_zone = options[:hp_avl_zone]
|
|
|
|
|
|
|
|
### Make the authentication call
|
|
|
|
if (auth_version == :v2)
|
|
|
|
# Call the control services authentication
|
|
|
|
credentials = Fog::HP.authenticate_v2(options, @connection_options)
|
2013-04-12 19:32:22 -04:00
|
|
|
# the CS service catalog returns the network endpoint
|
2013-03-21 13:58:11 -04:00
|
|
|
@hp_network_uri = credentials[:endpoint_url]
|
2013-04-12 19:32:22 -04:00
|
|
|
@credentials = credentials
|
2013-03-21 13:58:11 -04:00
|
|
|
else
|
|
|
|
# Call the legacy v1.0/v1.1 authentication
|
|
|
|
credentials = Fog::HP.authenticate_v1(options, @connection_options)
|
2013-04-12 19:32:22 -04:00
|
|
|
# the user sends in the network endpoint
|
2013-03-21 13:58:11 -04:00
|
|
|
@hp_network_uri = options[:hp_auth_uri]
|
|
|
|
end
|
|
|
|
|
|
|
|
@auth_token = credentials[:auth_token]
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
|
|
|
|
uri = URI.parse(@hp_network_uri)
|
|
|
|
@host = uri.host
|
|
|
|
@path = uri.path
|
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
|
|
|
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params, parse_json = true, &block)
|
|
|
|
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}/v2.0/#{params[:path]}"
|
|
|
|
}), &block)
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
|
|
|
Fog::HP::Network::NotFound.slurp(error)
|
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
|
2013-04-15 17:47:06 -04:00
|
|
|
if response.body.nil? || response.body == 'null'
|
|
|
|
response.body = ''
|
|
|
|
else
|
|
|
|
response.body = Fog::JSON.decode(response.body)
|
|
|
|
end
|
2013-03-21 13:58:11 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|