From 78b8d4a12509f48d1049e96b42b47fa08bc8fdb5 Mon Sep 17 00:00:00 2001 From: Athir Nuaimi Date: Fri, 10 Dec 2010 17:33:36 -0500 Subject: [PATCH] can now create DNS zones on linode added support for domain.create method. continuing work to support rest of DNS zone methods added a sample in examples directory. Right now includes code for slicehost. will add linode once API fully supported --- examples/dns_methods.rb | 103 ++++++++++++++++++ lib/fog/linode/compute.rb | 2 +- .../linode/requests/compute/domain_create.rb | 14 ++- .../linode/requests/compute/domain_delete.rb | 35 ++++++ 4 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 examples/dns_methods.rb create mode 100644 lib/fog/linode/requests/compute/domain_delete.rb diff --git a/examples/dns_methods.rb b/examples/dns_methods.rb new file mode 100644 index 000000000..3fc71988c --- /dev/null +++ b/examples/dns_methods.rb @@ -0,0 +1,103 @@ +#!/usr/bin/env ruby + +require 'rubygems' +# require 'fog' +require '/Users/anuaimi/code/fog/lib/fog' + +LINODE_API_KEY = '--put-your-key-here--' +SLICEHOST_PASSWORD = '--put-your-key-here--' + + +# example of how to use Linode DNS calls +def show_linode_dns_usage( api_key) + + if api_key == '--put-your-key-here--' + return false + end + + begin + options = { :linode_api_key => api_key } + cloud= Fog::Linode::Compute.new( options) + + # will add samples once finished adding linode DNS API to fog + + rescue + #opps, ran into a problem + puts $!.message + return false + end + + true +end + +# example of how to use Slicehost DNS calls +def show_slicehost_dns_usage( password) + + #check if we have a value api key for this cloud + if password == '--put-your-key-here--' + return false + end + + begin + #connect to Slicehost + options = { :slicehost_password => password } + cloud= Fog::Slicehost::Compute.new( options) + + #start by getting a list of existing zones Slicehost hosted for account + response = cloud.get_zones() + if response.status == 200 + num_zones= response.body['zones'].count + puts "Slicehost is hosting #{num_zones} DNS zones for this account" + end + + #create a zone for a domain + zone_id = 0 + options = { :ttl => 1800, :active => 'N' } + response= cloud.create_zone( "sample-domain.com", options) + if response.status == 200 + zone_id= response.body['id'] + end + + #add an A record for website + record_id = 0 + options = { :ttl => 3600, :active => 'N' } + response= cloud.create_record( 'A', 159631, "www.sample-domain.com", "1.2.3.4", options) + if response.status == 200 + record_id= response.body['id'] + end + + #now get details on zone and A record for www + if record_id > 0 + response = cloud.get_record( record_id) + if response.status == 200 + record = response.body['records'][0] + name = record['name'] + type = record['record-type'] + puts "got #{type} record for #{name} domain" + end + end + + #finally cleanup by deleting the zone we created + if zoned_id > 0 + response = cloud.delete_zone( zone_id) + if response.status == 200 + puts "sample-domain.com removed from Slicehost DNS" + end + end + + rescue + #opps, ran into a problem + puts $!.message + return false + end + + true +end + + +###################################### + +# note, if you have not added your key for a given provider, the related function will do nothing + +show_linode_dns_usage( LINODE_API_KEY) +show_slicehost_dns_usage( SLICEHOST_PASSWORD) diff --git a/lib/fog/linode/compute.rb b/lib/fog/linode/compute.rb index 331f80a8b..69c7ff956 100644 --- a/lib/fog/linode/compute.rb +++ b/lib/fog/linode/compute.rb @@ -14,7 +14,7 @@ module Fog request :avail_linodeplans request :avail_stackscripts request :domain_create - # request :domain_delete + request :domain_delete request :domain_list # request :domain_update # request :domain_resource_create diff --git a/lib/fog/linode/requests/compute/domain_create.rb b/lib/fog/linode/requests/compute/domain_create.rb index a3401c023..36620dc6e 100644 --- a/lib/fog/linode/requests/compute/domain_create.rb +++ b/lib/fog/linode/requests/compute/domain_create.rb @@ -6,7 +6,8 @@ module Fog # Creates a domain record # # ==== Parameters - # * domain<~String>: The zone's name + # * domain<~String>: The zone's name. Note, if master zone, SOA_email is required and if slave + # master_ips is/are required # * type<~String>: master or slave # * options<~Hash> # * description<~String> Currently undisplayed @@ -20,9 +21,12 @@ module Fog # # ==== Returns # * response<~Excon::Response>: - # * body<~Array>: - # TODO: docs - def domain_create( domain, type, options) + # * body<~Hash>: + # * DATA<~Hash>: + # * 'DomainID'<~Integer>: domain ID + def domain_create( domain, type, options = {}) + + query= {} request( :expects => 200, :method => 'GET', @@ -30,7 +34,7 @@ module Fog :api_action => 'domain.create', :domain => domain, :type => type - } + }.merge!( options) ) end diff --git a/lib/fog/linode/requests/compute/domain_delete.rb b/lib/fog/linode/requests/compute/domain_delete.rb new file mode 100644 index 000000000..5c3d0d032 --- /dev/null +++ b/lib/fog/linode/requests/compute/domain_delete.rb @@ -0,0 +1,35 @@ +module Fog + module Linode + class Compute + class Real + + # Delete the given domain from the list Linode hosts + # + # ==== Parameters + # * domain_id<~Integer>: id of domain to delete + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * DATA<~Hash>: + # TODO: docs + def domain_delete(domain_id) + request( + :expects => 200, + :method => 'GET', + :query => { :api_action => 'domain.delete', :domainId => domain_id } + ) + end + + end + + class Mock + + def linode_delete(linode_id, options={}) + Fog::Mock.not_implemented + end + + end + end + end +end