From 87de932252d8b0e6d08420083ea11f8617a2f2b2 Mon Sep 17 00:00:00 2001 From: howete Date: Fri, 3 May 2013 12:31:08 -0600 Subject: [PATCH] monkey patch start --- lib/fog/hp/dns.rb | 153 ++++++++++++++++++ lib/fog/hp/requests/dns/create_domain.rb | 37 +++++ lib/fog/hp/requests/dns/create_record.rb | 44 +++++ lib/fog/hp/requests/dns/delete_domain.rb | 42 +++++ lib/fog/hp/requests/dns/delete_record.rb | 42 +++++ lib/fog/hp/requests/dns/get_domain.rb | 39 +++++ lib/fog/hp/requests/dns/get_record.rb | 34 ++++ .../dns/get_servers_hosting_domain.rb | 40 +++++ lib/fog/hp/requests/dns/list_domains.rb | 33 ++++ .../requests/dns/list_records_in_a_domain.rb | 77 +++++++++ lib/fog/hp/requests/dns/update_domain.rb | 33 ++++ lib/fog/hp/requests/dns/update_record.rb | 39 +++++ 12 files changed, 613 insertions(+) create mode 100644 lib/fog/hp/dns.rb create mode 100644 lib/fog/hp/requests/dns/create_domain.rb create mode 100644 lib/fog/hp/requests/dns/create_record.rb create mode 100644 lib/fog/hp/requests/dns/delete_domain.rb create mode 100644 lib/fog/hp/requests/dns/delete_record.rb create mode 100644 lib/fog/hp/requests/dns/get_domain.rb create mode 100644 lib/fog/hp/requests/dns/get_record.rb create mode 100644 lib/fog/hp/requests/dns/get_servers_hosting_domain.rb create mode 100644 lib/fog/hp/requests/dns/list_domains.rb create mode 100644 lib/fog/hp/requests/dns/list_records_in_a_domain.rb create mode 100644 lib/fog/hp/requests/dns/update_domain.rb create mode 100644 lib/fog/hp/requests/dns/update_record.rb diff --git a/lib/fog/hp/dns.rb b/lib/fog/hp/dns.rb new file mode 100644 index 000000000..c7d7f7060 --- /dev/null +++ b/lib/fog/hp/dns.rb @@ -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 diff --git a/lib/fog/hp/requests/dns/create_domain.rb b/lib/fog/hp/requests/dns/create_domain.rb new file mode 100644 index 000000000..4fbb2f758 --- /dev/null +++ b/lib/fog/hp/requests/dns/create_domain.rb @@ -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 diff --git a/lib/fog/hp/requests/dns/create_record.rb b/lib/fog/hp/requests/dns/create_record.rb new file mode 100644 index 000000000..03c7c617a --- /dev/null +++ b/lib/fog/hp/requests/dns/create_record.rb @@ -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 + diff --git a/lib/fog/hp/requests/dns/delete_domain.rb b/lib/fog/hp/requests/dns/delete_domain.rb new file mode 100644 index 000000000..86dfc0f30 --- /dev/null +++ b/lib/fog/hp/requests/dns/delete_domain.rb @@ -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 \ No newline at end of file diff --git a/lib/fog/hp/requests/dns/delete_record.rb b/lib/fog/hp/requests/dns/delete_record.rb new file mode 100644 index 000000000..b081efecc --- /dev/null +++ b/lib/fog/hp/requests/dns/delete_record.rb @@ -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 \ No newline at end of file diff --git a/lib/fog/hp/requests/dns/get_domain.rb b/lib/fog/hp/requests/dns/get_domain.rb new file mode 100644 index 000000000..2d907f970 --- /dev/null +++ b/lib/fog/hp/requests/dns/get_domain.rb @@ -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 \ No newline at end of file diff --git a/lib/fog/hp/requests/dns/get_record.rb b/lib/fog/hp/requests/dns/get_record.rb new file mode 100644 index 000000000..95a9c0821 --- /dev/null +++ b/lib/fog/hp/requests/dns/get_record.rb @@ -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 \ No newline at end of file diff --git a/lib/fog/hp/requests/dns/get_servers_hosting_domain.rb b/lib/fog/hp/requests/dns/get_servers_hosting_domain.rb new file mode 100644 index 000000000..ba398ea85 --- /dev/null +++ b/lib/fog/hp/requests/dns/get_servers_hosting_domain.rb @@ -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 diff --git a/lib/fog/hp/requests/dns/list_domains.rb b/lib/fog/hp/requests/dns/list_domains.rb new file mode 100644 index 000000000..5c295ee20 --- /dev/null +++ b/lib/fog/hp/requests/dns/list_domains.rb @@ -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 \ No newline at end of file diff --git a/lib/fog/hp/requests/dns/list_records_in_a_domain.rb b/lib/fog/hp/requests/dns/list_records_in_a_domain.rb new file mode 100644 index 000000000..cbe3308cc --- /dev/null +++ b/lib/fog/hp/requests/dns/list_records_in_a_domain.rb @@ -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 diff --git a/lib/fog/hp/requests/dns/update_domain.rb b/lib/fog/hp/requests/dns/update_domain.rb new file mode 100644 index 000000000..b537b74fa --- /dev/null +++ b/lib/fog/hp/requests/dns/update_domain.rb @@ -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 \ No newline at end of file diff --git a/lib/fog/hp/requests/dns/update_record.rb b/lib/fog/hp/requests/dns/update_record.rb new file mode 100644 index 000000000..54ab1437b --- /dev/null +++ b/lib/fog/hp/requests/dns/update_record.rb @@ -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 \ No newline at end of file