1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

monkey patch start

This commit is contained in:
howete 2013-05-03 12:31:08 -06:00 committed by Rupak Ganguly
parent 5b3ba81469
commit 87de932252
12 changed files with 613 additions and 0 deletions

153
lib/fog/hp/dns.rb Normal file
View file

@ -0,0 +1,153 @@
require 'fog/hp'
module Fog
module HP
class DNS < Fog::Service
requires :hp_secret_key, :hp_tenant_id, :hp_avl_zone
recognizes :hp_auth_uri, :credentials
recognizes :persistent, :connection_options
recognizes :hp_use_upass_auth_style, :hp_auth_version, :user_agent
recognizes :hp_access_key, :hp_account_id # :hp_account_id is deprecated use hp_access_key instead
secrets :hp_secret_key
# model_path 'fog/hp/models/dns'
# model :domain
# collection :domains
# model :record
# collection :records
# model :server
request_path 'fog/hp/requests/dns'
request :create_domain
request :create_record
request :delete_domain
request :delete_record
request :get_domain
request :get_record
request :get_servers_hosting_domain
request :list_domains
request :list_records_in_a_domain
request :update_domain
request :update_record
class Mock
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
}
end
end
def self.reset
@data = nil
end
def initialize(options={})
# deprecate hp_account_id
if options[:hp_account_id]
Fog::Logger.deprecation(":hp_account_id is deprecated, please use :hp_access_key instead.")
@hp_access_key = options.delete(:hp_account_id)
end
@hp_access_key = options[:hp_access_key]
unless @hp_access_key
raise ArgumentError.new("Missing required arguments: hp_access_key. :hp_account_id is deprecated, please use :hp_access_key instead.")
end
end
def data
self.class.data[@hp_access_key]
end
def reset_data
self.class.data.delete(@hp_access_key)
end
end
class Real
attr_reader :credentials
def initialize(options={})
# deprecate hp_account_id
if options[:hp_account_id]
Fog::Logger.deprecation(":hp_account_id is deprecated, please use :hp_access_key instead.")
options[:hp_access_key] = options.delete(:hp_account_id)
end
@hp_access_key = options[:hp_access_key]
unless @hp_access_key
raise ArgumentError.new("Missing required arguments: hp_access_key. :hp_account_id is deprecated, please use :hp_access_key instead.")
end
@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
### Pass the service name for object storage to the authentication call
options[:hp_service_type] = "DNS"
@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)
# the CS service catalog returns the block storage endpoint
@hp_block_uri = credentials[:endpoint_url]
@credentials = credentials
else
# Call the legacy v1.0/v1.1 authentication
credentials = Fog::HP.authenticate_v1(options, @connection_options)
# the user sends in the block storage endpoint
@hp_block_uri = options[:hp_auth_uri]
end
@auth_token = credentials[:auth_token]
@persistent = options[:persistent] || false
uri = URI.parse(@hp_block_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',
'X-Auth-Token' => @auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}",
}), &block)
rescue Excon::Errors::HTTPStatusError => error
raise case error
when Excon::Errors::NotFound
Fog::HP::BlockStorage::NotFound.slurp(error)
else
error
end
end
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
response.body = Fog::JSON.decode(response.body)
end
response
end
end
end
end
end

View file

@ -0,0 +1,37 @@
module Fog
module HP
class DNS
class Real
def create_domain(name, options)
data = options.dup
data[:name] = name
request(
:body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => 'domains'
)
end
end
class Mock
def create_domain(name, options)
response = Excon::Response.new
response.status = 200
data = {
id: SecureRandom.uuid,
name: "domain1.com.",
ttl: 3600,
serial: 1351800588,
email: "nsadmin@example.org",
created_at: "2012-11-01T20:09:48.094457"
}
response.body = data
response
end
end
end
end
end

View file

@ -0,0 +1,44 @@
module Fog
module HP
class DNS
class Real
def create_record(domain_id, name, type, data)
data = {
name: name,
type: type,
data: data
}
request(
:body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => "domains/#{domain_id}/records"
)
end
end
class Mock
def create_record(domain_id, name, type, data)
response = Excon::Response.new
response.status = 200
data = {
id: "2e32e609-3a4f-45ba-bdef-e50eacd345ad",
name: "www.example.com.",
type: "A",
created_at: "2012-11-02T19:56:26.366792",
updated_at: null,
domain_id: "89acac79-38e7-497d-807c-a011e1310438",
ttl: 3600,
data: "15.185.172.152"
}
response.body = data
response
response
end
end
end
end
end

View file

@ -0,0 +1,42 @@
module Fog
module HP
class DNS
class Real
# Delete a Domain
#
# ==== Parameters
# * domain_id<~Integer> - Id of domain to delete
#
def delete_domain(domain_id)
request(
:expects => 200,
:method => 'DELETE',
:path => "domains/#{domain_id}"
)
end
end
class Mock
def delete_domain(domain_id)
response = Excon::Response.new
if image = find_domain(domain_id)
response.status = 202
response
else
raise Fog::HP::DNS::NotFound
end
response
end
def find_domain(domain_id)
list_domains.body['domains'].detect { |_| _['id'] == domain_id }
end
end
end
end
end

View file

@ -0,0 +1,42 @@
module Fog
module HP
class DNS
class Real
# Delete a Record
#
# ==== Parameters
# * domain_id<~Integer> - Id of domain for record
# * record_id<~Integer> - Id of record to delete
#
def delete_record(domain_id, record_id)
request(
:expects => 200,
:method => 'DELETE',
:path => "domains/#{domain_id}/records/#{record_id}"
)
end
end
class Mock
def delete_record(domain_id, record_id)
response = Excon::Response.new
if image = find_record(domain_id, record_id)
response.status = 200
response
else
response.status = 404
raise(Excon::Errors.status_error({:expects => 200}, response))
end
response
end
def find_record(domain_id, record_id)
list_records_in_a_domain(domain_id).body['records'].detect { |_| _['id'] == record_id }
end
end
end
end
end

View file

@ -0,0 +1,39 @@
module Fog
module HP
class DNS
# Get details for existing domain
#
# ==== Parameters
# * instance_id<~Integer> - Id of the domain to get
#
# ==== Returns
# * response<~Excon::Response>:
# *TBD
class Real
def get_domain(instance_id)
response = request(
:expects => 200,
:method => 'GET',
:path => "domains/#{instance_id}",
)
response
end
end
class Mock
def get_domain(instance_id)
response = Excon::Response.new
if domain = find_domain(instance_id)
response.status = 200
response.body = {'domain' => domain}
response
else
raise Fog::HP::DNS::NotFound
end
response
end
end
end
end
end

View file

@ -0,0 +1,34 @@
module Fog
module HP
class DNS
class Real
def get_record(domain_id, record_id)
response = request(
:expects => 200,
:method => 'GET',
:path => "domains/#{domain_id}/records/#{record_id}",
)
response
end
end
class Mock
def get_record(domain_id, record_id)
if record = find_record(domain_id, record_id)
response.status = 200
response.body = record
response
else
response.status = 400
raise(Excon::Errors.status_error({:expects => 200}, response))
end
response
end
def find_record(domain_id, record_id)
list_records_in_a_domain(domain_id).body['records'].detect { |_| _['id'] == record_id }
end
end
end
end
end

View file

@ -0,0 +1,40 @@
module Fog
module HP
class DNS
class Real
# Get servers for existing domain
#
# ==== Parameters
# * instance_id<~Integer> - Id of the domain with servers
#
# ==== Returns
# * response<~Excon::Response>:
# *TBD
def get_servers_hosting_domain(instance_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "domains/#{instance_id}/servers"
)
end
end
class Mock
def get_servers_hosting_domain(instance_id)
if domain = find_domain(instance_id)
response.status = 200
response.body = {'domain' => domain}
response
else
response.status = 400
raise(Excon::Errors.status_error({:expects => 200}, response))
end
response
end
end
end
end
end

View file

@ -0,0 +1,33 @@
module Fog
module HP
class DNS
class Real
# List all flavors (IDs and names only)
def list_domains
request(
:expects => [200],
:method => 'GET',
:path => 'domains',
)
end
end
class Mock
def list_domains
response = Excon::Response.new
response.status = 200
response.body = {
"domains" => [
{"name" => "domain1.com.", "created_at" => "2012-11-01T20:11:08.000000", "email" => "nsadmin@example.org", "ttl" => 3600, "serial" => 1351800668, "id" => "09494b72-b65b-4297-9efb-187f65a0553e"},
{"name" => "domain2.com.", "created_at" => "2012-11-01T20:09:48.000000", "email" => "nsadmin@example.org", "ttl" => 3600, "serial" => 1351800588, "id" => "89acac79-38e7-497d-807c-a011e1310438"}
]
}
response
end
end
end
end
end

View file

@ -0,0 +1,77 @@
module Fog
module HP
class DNS
class Real
def list_records_in_a_domain(domain_id)
request(
:expects => [200],
:method => 'GET',
:path => "domains/#{domain_id}/records"
)
end
end
class Mock
def list_records_in_a_domain(domain_id)
response = Excon::Response.new
if records = find_domain(domain_id)
response.status = 200
response.body = records_for_domain(domain_id)
else
raise Fog::HP::DNS::NotFound
end
response
end
def find_domain(domain_id)
list_domains.body['domains'].detect { |_| _['id'] == domain_id }
end
def records_for_domain(domain_id)
data = {
"records" => [
{
"id" => "2e32e609-3a4f-45ba-bdef-e50eacd345ad",
"name" => "www.example.com.",
"type" => "A",
"ttl" => 3600,
"created_at" => "2012-11-02T19:56:26.000000",
"updated_at" => "2012-11-04T13:22:36.000000",
"data" => "15.185.172.153",
"domain_id" => "89acac79-38e7-497d-807c-a011e1310438",
"tenant_id" => nil,
"priority" => nil,
"version" => 1,
},
{
"id" => "8e9ecf3e-fb92-4a3a-a8ae-7596f167bea3",
"name" => "host1.example.com.",
"type" => "A",
"ttl" => 3600,
"created_at" => "2012-11-04T13:57:50.000000",
"updated_at" => nil,
"data" => "15.185.172.154",
"domain_id" => "89acac79-38e7-497d-807c-a011e1310438",
"tenant_id" => nil,
"priority" => nil,
"version" => 1
}
]
}
if domain_id == list_domains.body["domains"].first["id"]
data
else
{
"records" => []
}
end
end
end
end
end
end

View file

@ -0,0 +1,33 @@
module Fog
module HP
class DNS
class Real
def update_domain(domain_id, options)
request(
:body => Fog::JSON.encode(options),
:expects => 200,
:method => 'PUT',
:path => "domains/#{domain_id}"
)
end
end
class Mock
def update_domain(domain_id, options)
response = Excon::Response.new
if domain = list_domains.body['domains'].detect { |_| _['id'] == domain_id }
if options['name']
domain['name'] = options['name']
end
response.status = 200
response.body = domain
response
else
raise Fog::HP::DNS::NotFound
end
end
end
end
end
end

View file

@ -0,0 +1,39 @@
module Fog
module HP
class DNS
class Real
def update_record(domain_id, record_id, options)
request(
:body => Fog::JSON.encode(options),
:expects => 200,
:method => 'PUT',
:path => "domains/#{domain_id}/records/#{record_id}"
)
end
end
class Mock
def update_record(domain_id, record_id, options)
if record = find_record(domain_id, record_id)
if options['name']
domain['name'] = options['name']
end
response.status = 200
response.body = record
response
else
raise Fog::HP::DNS::NotFound
end
response
end
def find_record(domain_id, record_id)
list_records_in_a_domain(domain_id).body['records'].detect { |_| _['id'] == record_id }
end
end
end
end
end