zerigodns 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tom Wilson
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/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Default: run specs.'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :build do
9
+ %x{gem build zerigodns.gemspec}
10
+ end
11
+
12
+ task :doc do
13
+ %x{yard doc}
14
+ end
15
+
16
+ task :default => :spec
17
+ task :default => :build
18
+ task :default => :doc
19
+
20
+
21
+ # require 'rdoc/task'
22
+ # RDoc::Task.new do |rdoc|
23
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
24
+ #
25
+ # rdoc.rdoc_dir = 'rdoc'
26
+ # rdoc.title = "zerigo_dns #{version}"
27
+ # rdoc.rdoc_files.include('README*')
28
+ # rdoc.rdoc_files.include('lib/**/*.rb')
29
+ # end
@@ -0,0 +1,43 @@
1
+ require 'active_resource'
2
+ require 'active_support/core_ext/module'
3
+
4
+ module ActiveResource
5
+ module CaptureHeaders
6
+ module InstanceMethods
7
+ private
8
+ def request_with_headers(method, path, *arguments)
9
+ request_without_headers(method, path, *arguments).tap do |resp|
10
+ @headers = resp.to_hash
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ def self.included(receiver)
17
+ receiver.class_eval do
18
+ include InstanceMethods
19
+ alias_method_chain :request, :headers
20
+ attr :headers
21
+ end
22
+ end
23
+ end
24
+
25
+ module RetrieveHeaders
26
+ module InstanceMethods
27
+ def last_count
28
+ @last_count ||= (c = connection.headers['x-query-count']) ? c[0].to_i : nil
29
+ end
30
+
31
+ end
32
+
33
+ def self.included(receiver)
34
+ receiver.class_eval do
35
+ include InstanceMethods
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ ActiveResource::Connection.send :include, ActiveResource::CaptureHeaders
43
+ ActiveResource::Base.send :include, ActiveResource::RetrieveHeaders
@@ -0,0 +1,47 @@
1
+ module ZerigoDNS
2
+ class Base < ActiveResource::Base
3
+ class << self
4
+ attr_reader :secure
5
+ alias_method :api_key, :password
6
+ alias_method :api_key=, :password=
7
+ def secure=(bool)
8
+ @secure=bool
9
+ self.site = @secure ? 'https://ns.zerigo.com/api/1.1/' : 'http://ns.zerigo.com/api/1.1/'
10
+ end
11
+ end
12
+
13
+ self.site='https://ns.zerigo.com/api/1.1/'
14
+ self.timeout = 5 # timeout after 5 seconds
15
+ self.format = ActiveResource::Formats::XmlFormat
16
+ @secure = true
17
+
18
+
19
+
20
+ # fix load() so that it no longer clobbers @prefix_options
21
+ # also fix bug exposed by reload() where attributes is effectively parsed twice, causing the first line to raise an exception the 2nd time
22
+ def load(attributes, remove_root = false)
23
+ raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
24
+ new_prefix_options, attributes = split_options(attributes)
25
+ @prefix_options.merge!(new_prefix_options)
26
+ attributes.each do |key, value|
27
+ @attributes[key.to_s] =
28
+ case value
29
+ when Array
30
+ if value.all?{|v2| v2.kind_of?(ActiveResource::Base)}
31
+ value.dup rescue value
32
+ else
33
+ resource = find_or_create_resource_for_collection(key)
34
+ value.map { |attrs| attrs.is_a?(String) ? attrs.dup : resource.new(attrs) }
35
+ end
36
+ when Hash
37
+ resource = find_or_create_resource_for(key)
38
+ resource.new(value)
39
+ else
40
+ value.dup rescue value
41
+ end
42
+ end
43
+ self
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,18 @@
1
+ module ZerigoDNS
2
+ #@attr [String] api_key The Api key generated by Zerigo DNS
3
+ #@attr [String] user Your e-mail address
4
+ #@attr [Boolean] secure Whether to use HTTPS
5
+ class Config
6
+
7
+ BASE_ATTRIBUTES = %w(api_key site secure user password)
8
+ BASE_ATTRIBUTES.each do |attr|
9
+ define_method attr do
10
+ ZerigoDNS::Base.send(attr)
11
+ end
12
+
13
+ define_method "#{attr}=" do |val|
14
+ ZerigoDNS::Base.send("#{attr}=", val)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,59 @@
1
+ class ZerigoDNS::Host < ZerigoDNS::Base
2
+ class << self
3
+
4
+ # Find host record(s) by zone and hostname
5
+ # @param [Symbol, #read] which One of :one, :first, :last, or :all. See http://api.rubyonrails.org/v3.2.1/classes/ActiveResource/Base.html#method-c-find
6
+ # @param [Zone, #read] zone The zone from which to find the host record.
7
+ # @param [String, #read] hostname The hostname to find.
8
+ # @return Host records, or an empty list if no records found.
9
+ def find_by_zone_and_hostname which, zone, hostname
10
+ fqdn = [hostname, zone.domain].select(&:present?).join('.')
11
+ find(which, params: {fqdn: fqdn, zone_id: zone.id})
12
+ end
13
+
14
+ def find_all_by_hostname zone, hostname
15
+ find_by_zone_and_hostname(:all, zone, hostname)
16
+ end
17
+
18
+ # @return [Host] The record found, or nil.
19
+ def find_first_by_hostname zone, hostname
20
+ find_by_zone_and_hostname(:all, zone, hostname).try(:first)
21
+ end
22
+
23
+ # Update or Create Host for a zone
24
+ # This method will only update the _first_ record.
25
+ # @param [Zone, #read] zone The zone to which the host belongs
26
+ # @param [String, #read] hostname The hostname
27
+ # @param [String, #read] type The type of record (e.g 'CNAME')
28
+ # @param [Fixnum, #read] ttl The TTL of the record, in seconds.
29
+ # @param [String, #read] data The data field of the record.
30
+ # @return [Host] The created or updated host.
31
+ def update_or_create(zone, hostname, type, ttl, data)
32
+ host = find_first_by_hostname(zone, hostname)
33
+ if host
34
+ host.update_record(type,ttl,data)
35
+ else
36
+ host = create(
37
+ :zone_id => zone.id,
38
+ :hostname => hostname,
39
+ :host_type => type,
40
+ :data => data,
41
+ :ttl => ttl
42
+ )
43
+ end
44
+ host
45
+ end
46
+ end
47
+
48
+ # Convienence method to update the record.
49
+ # @param [String, #read] type
50
+ # @param [String, #read] ttl
51
+ # @param [String, #read] data
52
+ # @return [Boolean, #read] True if saved, false otherwise.
53
+ def update_record type, ttl, data
54
+ self.host_type = type
55
+ self.data = data
56
+ self.ttl = ttl
57
+ save
58
+ end
59
+ end
@@ -0,0 +1,9 @@
1
+ class ZerigoDNS::HostTemplate < ZerigoDNS::Base
2
+
3
+ # Fetches the zone template to which the host template belongs.
4
+ # @return [ZoneTemplate] The zone template to which the host template belongs.
5
+ # @raise [ActiveResource::ResourceNotFound] if the zone template does not exist.
6
+ def zone_template
7
+ @zone_template ||= ZerigoDNS::ZoneTemplate.find(zone_template_id)
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ class ZerigoDNS::Tools < ZerigoDNS::Base
2
+ class <<self
3
+
4
+ # Fetch current public ipv4 address
5
+ # @return [String] Current public ipv4 address or "unknown"
6
+ def public_ipv4
7
+ get :public_ipv4
8
+ end
9
+
10
+ # Fetch current public ipv6 address
11
+ # @return [String] Current public ipv6 address or "unknown"
12
+ def public_ipv6
13
+ get :public_ipv6
14
+ end
15
+
16
+ # Fetch the current public IP address (either ipv4 or ipv6)
17
+ # @return [String] Current public ip address (ipv4 or ipv6)
18
+ def public_ip
19
+ get :public_ip
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,32 @@
1
+ class ZerigoDNS::Zone < ZerigoDNS::Base
2
+ class <<self
3
+ # Get count of all zones
4
+ # @return [Fixnum] Count of all zones
5
+ def count
6
+ get(:count).to_i
7
+ end
8
+
9
+ # Find zone by domain name
10
+ # @param [String, #read] domain the domain to retrieve
11
+ # @raise ActiveResource::ResourceNotFound if the domain is not present.
12
+ # @return [Zone] the domain retrieved.
13
+ def find_by_domain(domain)
14
+ find(domain)
15
+ end
16
+
17
+ # Find or Create Zone
18
+ # @param [String, #read] domain name of domain to create
19
+ # @return [Zone] the zone found or created.
20
+ def find_or_create(domain)
21
+ find_by_domain(domain)
22
+ rescue ActiveResource::ResourceNotFound
23
+ create(:domain=> domain, :ns_type=>'pri_sec')
24
+ end
25
+ end
26
+
27
+ # Get count of all hosts belonging to this zone
28
+ # @return [Fixnum] Count of all hosts belonging to this zone.
29
+ def count_hosts
30
+ get('hosts/count').to_i
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ class ZerigoDNS::ZoneTemplate < ZerigoDNS::Base
2
+
3
+ # Get count of zone templates
4
+ # @return [Fixnum] the count of zone templates
5
+ def self.count
6
+ get(:count).to_i
7
+ end
8
+
9
+ # Get count of host templates
10
+ # @return [Fixnum] the count of host templates for this zone template
11
+ def count_host_templates
12
+ get('host_templates/count').to_i
13
+ end
14
+
15
+ # Create a zone using the zone template
16
+ # @param [Hash, #read] attrs Attributes of the zone to be created
17
+ # @option attrs domain [String, #read] The domain name
18
+ # @option attrs follow_template [String, #read] ('follow')
19
+ # * 'follow' The zone will reflect updates made to the template.
20
+ # * 'no' The zone will be a one-time copy of the template.
21
+ #
22
+ # Due to a problem with XML conversion, the value assigned to follow_template must be a string and not a symbol.
23
+ # @return [Zone] The created zone
24
+ def create_zone attrs
25
+ ZerigoDNS::Zone.create({follow_template: 'follow', zone_template_id: id}.merge(attrs))
26
+ end
27
+
28
+ # List all host templates of this zone template
29
+ # @return [Array] An array of host templates
30
+ def host_templates
31
+ @host_templates ||= ZerigoDNS::HostTemplate.find(:all, params: {zone_template_id: id})
32
+ end
33
+
34
+ # Create a host template for this template
35
+ # @param [Hash, #read] attrs Attributes of the host template to be created
36
+ def create_host_template attrs={}
37
+ ZerigoDNS::HostTemplate.create(attrs.merge(zone_template_id: id))
38
+ end
39
+ end
data/lib/zerigodns.rb ADDED
@@ -0,0 +1,29 @@
1
+ # Copyright 2009 Zerigo, Inc. See MIT-LICENSE for license information.
2
+ # Visit http://www.zerigo.com/docs/managed-dns for updates and documentation.
3
+ require 'activeresource-ext'
4
+ require 'zerigodns/base'
5
+ require 'zerigodns/config'
6
+ require 'zerigodns/host'
7
+ require 'zerigodns/zone'
8
+ require 'zerigodns/zone_template'
9
+ require 'zerigodns/host_template'
10
+ require 'zerigodns/tools'
11
+
12
+ module ZerigoDNS
13
+ class <<self
14
+ ## Instantiates and memoizes a new +Config+ object
15
+ #
16
+ # @return [Config] the cached configuration instance
17
+ def config
18
+ @config ||= Config.new
19
+ end
20
+
21
+
22
+ # Exposes a block with the +Config+ instance
23
+ #
24
+ # @yield [Config] the config instance
25
+ def configure
26
+ yield config
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zerigodns
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Wilson
9
+ - Zerigo Team
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-11-21 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activeresource
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.2.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 3.2.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: yard
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 0.8.7.6
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 0.8.7.6
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.99'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '2.99'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: simplecov
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: 0.9.1
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: 0.9.1
95
+ - !ruby/object:Gem::Dependency
96
+ name: zerigodns
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Gem for interacting with the Zerigo DNS REST API.
112
+ email: support@zerigo.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - LICENSE
117
+ files:
118
+ - LICENSE
119
+ - Rakefile
120
+ - lib/activeresource-ext.rb
121
+ - lib/zerigodns/base.rb
122
+ - lib/zerigodns/config.rb
123
+ - lib/zerigodns/host.rb
124
+ - lib/zerigodns/host_template.rb
125
+ - lib/zerigodns/tools.rb
126
+ - lib/zerigodns/zone.rb
127
+ - lib/zerigodns/zone_template.rb
128
+ - lib/zerigodns.rb
129
+ homepage: http://github.com/8x8Cloud/zerigo_dns
130
+ licenses: []
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 1.8.25
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Zerigo DNS Gem
153
+ test_files: []
154
+ has_rdoc: