zerigo-designate 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in capify-ec2.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 James Miller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = designate
2
+
3
+ Designate is a simple Ruby wrapper for the Zerigo API v1.1 specification.
4
+
5
+ = Usage
6
+
7
+ d = Designate::Client.new("example@email.com", "30f1b76a010d062c22b07bda66c1d9f0")
8
+
9
+ Get all zones:
10
+
11
+ d.zones
12
+
13
+ Create a new zone:
14
+
15
+ d.create_zone("example.com")
16
+
17
+ Find a specific zone:
18
+
19
+ d.find_zone_by_id(12345)
20
+ d.find_zone_by_domain("example.com")
21
+
22
+ Get all zone templates:
23
+
24
+ d.templates
25
+
26
+ Find a template:
27
+
28
+ d.find_template(12345)
29
+
30
+ Get all hosts in a zone:
31
+
32
+ d.find_zone_by_domain("example.com").hosts
33
+
34
+ Create host:
35
+
36
+ d.find_zone_by_domain("example.com").create_host("MX", "mx1.example.com.")
37
+
38
+ The next features have only been tested in conjunction with capify-ec2.
39
+ I recommend further testing before adding them to your projects.
40
+ Also works via Capistrano, using zerigo.yml:
41
+ :username: "username"
42
+ :key: "API Key"
43
+ :domain: "example.com"
44
+
45
+ Within your project:
46
+ application = "test"
47
+ cap <server_name> zerigo:first_time
48
+
49
+ retrieves the first part of the <hostname> from the remote machine and creates:
50
+ CNAME, <hostname>.example.com => actual DNS name, as specified in your server address.
51
+ CNAME, <hostname>-test.example.com => web.example.com
52
+
53
+ Nothing will be created if the DNS names are already in use.
54
+
55
+ == Copyright
56
+
57
+ Copyright (c) 2010 James Miller. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'bundler'
4
+
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/test_*.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ task :default => :test
13
+
14
+ Bundler::GemHelper.install_tasks
data/designate.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "designate/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "zerigo-designate"
6
+ s.version = Designate::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["James Miller", "Noah Cantor"]
9
+ s.summary = %q{Ruby gem wrapper for the Zerigo DNS API}
10
+ s.description = %q{Designate is a simple Ruby gem wrapper for the Zerigo DNS API v1.1 specification.}
11
+ s.homepage = "http://github.com/ncantor/designate"
12
+ s.email = ["bensie@gmail.com", "noah@forward.co.uk"]
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.has_rdoc = false
20
+ s.add_dependency("rest-client", "= 1.6.7")
21
+ s.add_dependency("crack", "= 0.3.1")
22
+ s.add_development_dependency('shoulda')
23
+ end
data/lib/designate.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rest_client'
3
+ require 'crack/xml'
4
+ require File.expand_path(File.dirname(__FILE__) + '/designate/client')
5
+ require File.expand_path(File.dirname(__FILE__) + '/designate/zone')
6
+ require File.expand_path(File.dirname(__FILE__) + '/designate/template')
7
+ require File.expand_path(File.dirname(__FILE__) + '/designate/host')
8
+
9
+ module Designate
10
+ class InvalidHostType < StandardError; end
11
+ end
@@ -0,0 +1,21 @@
1
+ require File.join(File.dirname(__FILE__), '../designate')
2
+
3
+ Capistrano::Configuration.instance(:must_exist).load do
4
+ namespace :zerigo do
5
+ desc "Run only on individual servers, not entire roles."
6
+ task :first_time do
7
+ hostname = capture('hostname -s').chomp
8
+ dns_names = []
9
+ roles.each do |role|
10
+ dns_names << role[1].instance_variable_get("@static_servers").first.instance_variable_get("@host")
11
+ end
12
+ zone = Designate::Client.new().find_zone_by_domain()
13
+ zone.create_host('CNAME', "#{hostname}.#{zerigo_config[:domain]}", {:hostname => "#{hostname}-#{application}"})
14
+ zone.create_host('CNAME', dns_names.first, {:hostname => hostname})
15
+ end
16
+
17
+ def zerigo_config()
18
+ YAML.load(File.new("config/zerigo.yml"))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,91 @@
1
+ require 'cgi'
2
+ module Designate
3
+
4
+ class Client
5
+
6
+ API_VERSION = "1.1"
7
+ DOMAIN = "ns.zerigo.com"
8
+
9
+ def initialize()
10
+ @@auth_string = CGI.escape(zerigo_config[:username]) + ':' + CGI.escape(zerigo_config[:key])
11
+ @domain = zerigo_config[:domain]
12
+ end
13
+
14
+ def zerigo_config()
15
+ YAML.load(File.new("config/zerigo.yml"))
16
+ end
17
+
18
+ def zones
19
+ zones = []
20
+ get('zones.xml')['zones'].each { |zone| zones << Zone.new(zone) }
21
+ return zones
22
+ end
23
+
24
+ def find_zone_by_id(id)
25
+ if zone = get("zones/#{id}.xml")['zone']
26
+ Zone.new(zone)
27
+ end
28
+ end
29
+
30
+ def find_zone_by_domain(domain = @domain)
31
+ if zone = get("zones/#{domain}.xml")['zone']
32
+ Zone.new(zone)
33
+ end
34
+ end
35
+
36
+ def create_zone(domain = @domain, options = {})
37
+ options[:domain] = domain
38
+ options[:default_ttl] ||= 14400
39
+ options[:nx_ttl] ||= 300
40
+ if options[:zone_template_id]
41
+ options[:follow_template] ||= 'no'
42
+ end
43
+ if zone = post("zones.xml", { :zone => options })['zone']
44
+ Zone.new(zone)
45
+ end
46
+ end
47
+
48
+ def find_or_create_zone(domain = @domain)
49
+ if zone = find_zone_by_domain(domain)
50
+ return zone
51
+ else
52
+ create_zone(domain)
53
+ end
54
+ end
55
+
56
+ def templates
57
+ templates = []
58
+ get('zone_templates.xml')['zone_templates'].each { |template| templates << Template.new(template) }
59
+ return templates
60
+ end
61
+
62
+ def find_template_by_id(id)
63
+ if template = get("zone_templates/#{id}.xml")['zone_template']
64
+ Template.new(template)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def get(path)
71
+ Crack::XML.parse(RestClient.get(url(path)))
72
+ end
73
+
74
+ def post(path, params = {})
75
+ Crack::XML.parse(RestClient.post(url(path), params))
76
+ end
77
+
78
+ def put(path, params = {})
79
+ Crack::XML.parse(RestClient.put(url(path), params))
80
+ end
81
+
82
+ def delete(path)
83
+ Crack::XML.parse(RestClient.delete(url(path)))
84
+ end
85
+
86
+ def url(path)
87
+ "https://#{@@auth_string}@#{DOMAIN}/api/#{API_VERSION}/#{path}"
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,22 @@
1
+ module Designate
2
+ class Host < Client
3
+
4
+ def initialize(data)
5
+ data.each do |key, value|
6
+ instance_variable_set("@#{key}", value)
7
+ Host.instance_eval do
8
+ attr_reader key.to_sym
9
+ end
10
+ end
11
+ end
12
+
13
+ def update(options = {})
14
+ put("hosts/#{id}.xml", { :host => options })
15
+ end
16
+
17
+ def destroy
18
+ delete("hosts/#{id}.xml")
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module Designate
2
+ class Template < Client
3
+
4
+ def initialize(data)
5
+ data.each do |key, value|
6
+ instance_variable_set("@#{key}", value)
7
+ Template.instance_eval do
8
+ attr_reader key.to_sym
9
+ end
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Designate
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,47 @@
1
+ module Designate
2
+ class Zone < Client
3
+
4
+ def initialize(data)
5
+ data.each do |key, value|
6
+ instance_variable_set("@#{key}", value)
7
+ Zone.instance_eval do
8
+ attr_reader key.to_sym
9
+ end
10
+ end
11
+ hosts = []
12
+ data['hosts'].each { |host| hosts << Host.new(host) } unless data['hosts'].nil?
13
+ @hosts = hosts
14
+ end
15
+
16
+ def update(options = {})
17
+ options[:default_ttl] ||= 14400
18
+ options[:nx_ttl] ||= 60
19
+ if options[:zone_template_id]
20
+ options[:follow_template] ||= 'no'
21
+ end
22
+ put("zones/#{id}.xml", { :zone => options })
23
+ end
24
+
25
+ def destroy
26
+ delete("zones/#{id}.xml")
27
+ end
28
+
29
+ def host(host_id)
30
+ if host = get("hosts/#{host_id}.xml")['host']
31
+ Host.new(host)
32
+ end
33
+ end
34
+
35
+ def create_host(host_type, data, options = {})
36
+ raise InvalidHostType unless %w(A AAAA CNAME MX NS SRV TXT).include?(host_type)
37
+ return if hosts.find {|host| host.hostname == options[:hostname]}
38
+ options[:host_type] = host_type
39
+ options[:hostname] ||= nil
40
+ options[:data] = data
41
+ if host = post("zones/#{id}/hosts.xml", { :host => options })
42
+ return Host.new(host)
43
+ end
44
+ end
45
+
46
+ end
47
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'designate'
8
+
9
+ class Test::Unit::TestCase
10
+ end
data/test/test_zdns.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'helper'
2
+
3
+ class TestZDNS < Test::Unit::TestCase
4
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zerigo-designate
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - James Miller
14
+ - Noah Cantor
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-10-06 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rest-client
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - "="
29
+ - !ruby/object:Gem::Version
30
+ hash: 1
31
+ segments:
32
+ - 1
33
+ - 6
34
+ - 7
35
+ version: 1.6.7
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: crack
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ hash: 17
47
+ segments:
48
+ - 0
49
+ - 3
50
+ - 1
51
+ version: 0.3.1
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: shoulda
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ type: :development
67
+ version_requirements: *id003
68
+ description: Designate is a simple Ruby gem wrapper for the Zerigo DNS API v1.1 specification.
69
+ email:
70
+ - bensie@gmail.com
71
+ - noah@forward.co.uk
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files: []
77
+
78
+ files:
79
+ - .gitignore
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.rdoc
83
+ - Rakefile
84
+ - designate.gemspec
85
+ - lib/designate.rb
86
+ - lib/designate/capistrano.rb
87
+ - lib/designate/client.rb
88
+ - lib/designate/host.rb
89
+ - lib/designate/template.rb
90
+ - lib/designate/version.rb
91
+ - lib/designate/zone.rb
92
+ - test/helper.rb
93
+ - test/test_zdns.rb
94
+ has_rdoc: true
95
+ homepage: http://github.com/ncantor/designate
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.3.7
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Ruby gem wrapper for the Zerigo DNS API
128
+ test_files:
129
+ - test/helper.rb
130
+ - test/test_zdns.rb