Skip to content
Browse files

first commit

  • Loading branch information...
1 parent d1e9126 commit 52846593f8a2c7a37dee5fcc6027e6be8ab094f5 @twilson63 committed Feb 6, 2010
Showing with 259 additions and 11 deletions.
  1. +11 −10 README.rdoc
  2. +2 −1 Rakefile
  3. +194 −0 example/sample.rb
  4. 0 lib/zerigo_dns.rb
  5. +52 −0 lib/zerigo_ns.rb
View
21 README.rdoc
@@ -1,17 +1,18 @@
= zerigo_dns
-Description goes here.
+Zerigo DNS Gem
-== Note on Patches/Pull Requests
+== Credit goes to Zerigo DNS
-* Fork the project.
-* Make your feature addition or bug fix.
-* Add tests for it. This is important so I don't break it in a
- future version unintentionally.
-* Commit, do not mess with rakefile, version, or history.
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-* Send me a pull request. Bonus points for topic branches.
+Copyright 2009 Zerigo, Inc. See MIT-LICENSE for license information.
+Visit http://www.zerigo.com/docs/managed-dns for updates and documentation.
+
+I just put there sample code into a gem library for deployment ease. And chef
+integration.
+
+If you have any questions contact zerigo_dns_gem@jackhq.com
+
== Copyright
-Copyright (c) 2010 Tom Wilson. See LICENSE for details.
+Copyright 2009 Zerigo, Inc. See MIT-LICENSE for license information.
View
3 Rakefile
@@ -6,10 +6,11 @@ begin
Jeweler::Tasks.new do |gem|
gem.name = "zerigo_dns"
gem.summary = %Q{Zerigo DNS Gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.description = %Q{This gem is a resource wrapper of the example provide by zerigo dns}
gem.email = "tom@jackhq.com"
gem.homepage = "http://github.com/twilson63/zerigo_dns"
gem.authors = ["Tom Wilson"]
+ gem.add_dependency "active_resource"
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
View
194 example/sample.rb
@@ -0,0 +1,194 @@
+# Copyright 2009 Zerigo, Inc. See MIT-LICENSE for license information.
+# Visit http://www.zerigo.com/docs/managed-dns for updates and documentation.
+
+# First, require the Zerigo DNS library for ActiveResource.
+
+require 'zerigo_ns'
+
+# All API request require a Zerigo account and an Account API key. We'll set
+# them here for later reference. The 'user' is your regular login email.
+# The 'password' is your API key and comes from the Preferences page
+# (Manage Account -> NS -> Preferences).
+
+Zerigo::NS::Base.user = 'you@email.com'
+
+Zerigo::NS::Base.password = 'api_token'
+
+
+
+# Note: This example assumes that there is at least one domain/zone already
+# existing for the above referenced account.
+
+
+
+# We'll start by retrieving a list of domains for this account. Note that the
+# API refers to domains as zones. Note that API attributes are addressed as
+# instance methods: the_zone.domain
+# All attributes will have underscores in the name instead of dashes.
+# eg: zone.default_ttl, not zone.default-ttl
+
+puts '', "Retrieving list of first 20 zones..."
+zones = Zerigo::NS::Zone.find(:all, :params=>{:per_page=>20, :page=>1})
+
+# Now print a list of those zones
+zones.each do |zone|
+ puts " #{zone.domain} (id: #{zone.id})"
+end
+
+# And show exactly how many results there were. Note that total_count is
+# attached to each zone object, so we just pick the first one here.
+if zones.empty?
+ puts " (0 of 0 displayed)"
+else
+ puts " (1-#{zones.size} of #{zones.first.last_count} displayed)"
+end
+
+
+# We'll list all hosts for the first zone from the last request.
+
+zone = zones.first
+puts '', "Hosts for zone #{zone.domain} (id: #{zone.id})"
+
+hosts = Zerigo::NS::Host.find(:all, :params=>{:zone_id=>zone.id, :per_page=>20, :page=>1})
+hosts.each do |host|
+ puts " #{host.hostname} (id: #{host.id})"
+end
+
+# While not demonstrated here, host.last_count works just like for
+# zones.
+
+
+# Now we'll load a single zone. In this case, it's the first zone returned in
+# the last request.
+
+puts '', "Loading a single zone..."
+zone = Zerigo::NS::Zone.find(zones.first.id)
+
+puts " Loaded zone #{zone.id} (#{zone.domain})"
+
+
+# Now we'll try to load a non-existent zone and catch the error.
+
+puts '', "Loading a non-existent zone..."
+begin
+ zone2 = Zerigo::NS::Zone.find(987654321)
+ puts " Loaded zone #{zone2.id} (#{zone2.domain})"
+rescue ActiveResource::ResourceNotFound
+ puts " Zone not found"
+end
+
+
+# Let's create a random zone with a validation error
+
+puts '', "Creating a random zone that is invalid..."
+
+now = Time.now.to_i
+vals = {:domain=>"example-#{now}.org", :ns_type=>'not_valid' }
+
+newzone = Zerigo::NS::Zone.create(vals)
+if newzone.errors.empty?
+ puts " Zone #{newzone.domain} created successfully with id #{newzone.id}."
+else
+ puts " There was an error saving the new zone."
+ newzone.errors.each {|attr, msg| puts " #{attr} #{msg}"}
+end
+
+# now do it right
+
+puts '', "Fixing and resubmitting that random zone..."
+
+vals[:ns_type] = 'pri_sec' # options for this are 'pri_sec' (the default and most common), 'pri', and 'sec' -- see the API docs for details
+
+newzone = Zerigo::NS::Zone.create(vals)
+if newzone.errors.empty?
+ puts " Zone #{newzone.domain} created successfully with id #{newzone.id}."
+else
+ puts " There was an error saving the new zone."
+ newzone.errors.each {|attr, msg| puts " #{attr} #{msg}"}
+end
+
+
+# Then we'll update that same zone.
+
+puts '', "Now adding slave_nameservers and changing to 'pri'..."
+
+newzone.ns_type = 'pri'
+newzone.slave_nameservers = "ns8.example-#{now}.org,ns9.example-#{now}.org"
+if newzone.save
+ puts " Changes saved successfully."
+else
+ puts " There was an error saving the changes."
+ newzone.errors.each {|attr, msg| puts " #{attr} #{msg}"}
+end
+
+
+# Add a host to the zone.
+
+puts '', "Adding a host to the zone."
+
+vals2 = {:hostname=>'www',
+ :host_type=>'A',
+ :data=>'10.10.10.10',
+ :ttl=>86400,
+ :zone_id=>newzone.id
+ }
+
+# A host has to be assigned to a zone. This is done by including 'zone_id'
+# in the vals2 hash.
+newhost = Zerigo::NS::Host.create(vals2)
+if newhost.errors.empty?
+ puts " Host #{newhost.hostname} created successfully with id #{newhost.id}."
+else
+ puts " There was an error saving the new host."
+ newhost.errors.each {|attr, msg| puts " #{attr} #{msg}"}
+end
+
+
+# To make the new host show up in the zone, either the zone needs to be
+# reloaded or just the hosts can be reloaded. We'll also reload the host
+# just for fun.
+
+puts '', "Reloading the host..."
+
+newhost.reload
+
+puts '', "Reloading the zone..."
+
+newzone.reload
+
+
+# Update the host.
+
+puts '', "Changing the host ttl to use the zone's default..."
+
+host = newzone.hosts.first
+host.ttl = nil
+if host.save
+ puts " Changes saved successfully."
+else
+ puts " There was an error saving the changes."
+ host.errors.each {|attr, msg| puts " #{attr} #{msg}"}
+end
+
+
+# Delete the host.
+
+puts '', "Deleting this host..."
+
+if newhost.destroy
+ puts " Successful."
+else
+ puts " Failed."
+end
+
+
+# Now delete this zone.
+
+puts '', "Deleting the zone..."
+
+if newzone.destroy
+ puts " Successful."
+else
+ puts " Failed."
+end
+
View
0 lib/zerigo_dns.rb
No changes.
View
52 lib/zerigo_ns.rb
@@ -0,0 +1,52 @@
+# Copyright 2009 Zerigo, Inc. See MIT-LICENSE for license information.
+# Visit http://www.zerigo.com/docs/managed-dns for updates and documentation.
+
+require 'activeresource-ext'
+
+module Zerigo
+ module NS
+
+ class Base < ActiveResource::Base
+ self.site='http://ns.zerigo.com/api/1.1/'
+ self.user = 'test@example.com'
+ self.password = 'ca01ffae311a7854ea366b05cd02bd50'
+ self.timeout = 5 # timeout after 5 seconds
+
+
+ # fix load() so that it no longer clobbers @prefix_options
+ # also fix bug exposed by reload() where attributes is effectively parsed twice, causing the first line to raise an exception the 2nd time
+ def load(attributes)
+ raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
+ new_prefix_options, attributes = split_options(attributes)
+ @prefix_options.merge!(new_prefix_options)
+ attributes.each do |key, value|
+ @attributes[key.to_s] =
+ case value
+ when Array
+ if value.all?{|v2| v2.kind_of?(ActiveResource::Base)}
+ value.dup rescue value
+ else
+ resource = find_or_create_resource_for_collection(key)
+ value.map { |attrs| attrs.is_a?(String) ? attrs.dup : resource.new(attrs) }
+ end
+ when Hash
+ resource = find_or_create_resource_for(key)
+ resource.new(value)
+ else
+ value.dup rescue value
+ end
+ end
+ self
+ end
+
+ end
+
+
+ class Zone < Base
+ end
+
+ class Host < Base
+ end
+
+ end
+end

0 comments on commit 5284659

Please sign in to comment.
Something went wrong with that request. Please try again.