dynect_rest 0.1.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,23 @@
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
+ doc
21
+ .yardoc
22
+
23
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = dynect-rest
2
+
3
+ Interact with the Dynect REST API.
4
+
5
+ == Resource Records
6
+
7
+ Since this is 90% of what you'll be doing, we make it easy.
8
+
9
+ require 'dynect_rest'
10
+ dyn = DynectRest.new("CUSTOMER", "USERNAME", "PASSWORD", "ZONE")
11
+ dyn.a.fqdn("sunshine.example.com").ttl(3600).address("192.168.1.1").save
12
+ dyn.cname.fqdn("something.example.com").cname("ec2-10-10-10-10.amazonaws.com").save
13
+ dyn.publish
14
+
15
+ Will add an A record and a CNAME. You can use the chained method syntax for every resource record - we map unknown methods to the resource data you make in the call.
16
+
17
+ == Everything else
18
+
19
+ We wrap up the lower-level functionality of the API, so you can focus on just making the calls. For example, to get a list of contacts:
20
+
21
+ dyn = DynectRest.new("CUSTOMER", "USERNAME", "PASSWORD", "ZONE")
22
+ dyn.get('Contact')
23
+
24
+ To create a new contact:
25
+
26
+ dyn = DynectRest.new("CUSTOMER", "USERNAME", "PASSWORD", "ZONE")
27
+ dyn.post('Contact', { "first_name" => "Bobo", "last_name" => "Clown", "organization" => 'The Circus', "email" => "bobo@example.com" })
28
+
29
+ == Use the API docs
30
+
31
+ For more - you should be able to find the latest at:
32
+
33
+ https://manage.dynect.net/help/docs/api2/rest/resources/
34
+
35
+ If you are a Dynect customer.
36
+
37
+ == Note on Patches/Pull Requests
38
+
39
+ * Fork the project.
40
+ * Make your feature addition or bug fix.
41
+ * Commit, do not mess with rakefile, version, or history.
42
+ (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)
43
+ * Send me a pull request. Bonus points for topic branches.
44
+
45
+ == Copyright
46
+
47
+ Copyright (c) 2010 Opscode, Inc. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dynect_rest"
8
+ gem.summary = %Q{Dynect REST API library}
9
+ gem.description = %Q{Use the Dynect services REST API}
10
+ gem.email = "adam@opscode.com"
11
+ gem.homepage = "http://github.com/adamhjk/dynect-rest"
12
+ gem.authors = ["Adam Jacob"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ gem.add_dependency('json')
16
+ gem.add_dependency('rest-client')
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ begin
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
43
+ rescue LoadError
44
+ task :yardoc do
45
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
46
+ end
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,23 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2010 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ class DynectRest
20
+ class Exceptions
21
+ class RequestFailed < RuntimeError; end
22
+ end
23
+ end
@@ -0,0 +1,125 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2010 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ class DynectRest
20
+ class Resource
21
+
22
+ attr_accessor :dynect, :fqdn, :record_type, :record_id, :ttl, :zone, :rdata
23
+
24
+ def initialize(dynect, record_type, zone, fqdn=nil, record_id=nil, ttl=nil, rdata={})
25
+ @dynect = dynect
26
+ @record_type = record_type
27
+ @fqdn = fqdn
28
+ @record_id = record_id
29
+ @ttl = ttl
30
+ @zone = zone
31
+ @rdata = rdata
32
+ end
33
+
34
+ def [](rdata_key)
35
+ @rdata[rdata_key]
36
+ end
37
+
38
+ def []=(rdata_key, rdata_value)
39
+ @rdata[rdata_key] = rdata_value
40
+ end
41
+
42
+ def fqdn(value=nil)
43
+ value ? (@fqdn = value; self) : @fqdn
44
+ end
45
+
46
+ def record_id(value=nil)
47
+ value ? (@record_id = value; self) : @record_id
48
+ end
49
+
50
+ def ttl(value=nil)
51
+ value ? (@ttl = value; self) : @ttl
52
+ end
53
+
54
+ def resource_path(full=false)
55
+ if (full == true || full == :full)
56
+ "/REST/#{@record_type}Record/#{@zone}"
57
+ else
58
+ "#{@record_type}Record/#{@zone}"
59
+ end
60
+ end
61
+
62
+ def get(fqdn, record_id=nil)
63
+ if record_id
64
+ raw_rr = @dynect.get("#{resource_path}/#{fqdn}/#{record_id}")
65
+ DynectRest::Resource.new(dynect, raw_rr["record_type"], raw_rr["zone"], raw_rr["fqdn"], raw_rr["record_id"], raw_rr["ttl"], raw_rr["rdata"])
66
+ else
67
+ raw_rr_list = []
68
+ @dynect.get("#{resource_path}/#{fqdn}").each do |record|
69
+ record =~ /^#{resource_path(:full)}\/#{fqdn}\/(\d+)$/
70
+ raw_rr_list << self.get(fqdn, $1)
71
+ end
72
+ raw_rr_list.length == 1 ? raw_rr_list[0] : raw_rr_list
73
+ end
74
+ end
75
+
76
+ def find(fqdn, query_hash)
77
+ results = []
78
+ get(fqdn).each do |rr|
79
+ query_hash.each do |key, value|
80
+ results << rr if rr[key.to_s] == value
81
+ end
82
+ end
83
+ results
84
+ end
85
+
86
+ def save(replace=false)
87
+ if record_id
88
+ @dynect.put("#{resource_path}/#{@fqdn}/#{@record_id}", self)
89
+ else
90
+ if replace == true || replace == :replace
91
+ @dynect.put("#{resource_path}/#{@fqdn}", self)
92
+ else
93
+ @dynect.post("#{resource_path}/#{@fqdn}", self)
94
+ end
95
+ end
96
+ self
97
+ end
98
+
99
+ def delete
100
+ @dynect.delete("#{resource_path}/#{fqdn}/#{record_id}")
101
+ end
102
+
103
+ def to_json
104
+ {
105
+ "rdata" => @rdata,
106
+ "ttl" => @ttl
107
+ }.to_json
108
+ end
109
+
110
+ def method_missing(method_symbol, *args, &block)
111
+ method_string = method_symbol.to_s
112
+ if (args.length > 0 && method_string !~ /=$/)
113
+ @rdata[method_string] = *args
114
+ self
115
+ elsif @rdata.has_key?(method_string)
116
+ @rdata[method_string]
117
+ else
118
+ raise NoMethodError, "undefined method `#{method_symbol.to_s}' for #{self.class.to_s}"
119
+ end
120
+ end
121
+
122
+ end
123
+
124
+ end
125
+
@@ -0,0 +1,194 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2010 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ class DynectRest
20
+
21
+ require 'dynect_rest/exceptions'
22
+ require 'dynect_rest/resource'
23
+ require 'rest_client'
24
+ require 'json'
25
+
26
+ attr_accessor :customer_name, :user_name, :password, :rest, :zone
27
+
28
+ # Creates a new base object for interacting with Dynect's REST API
29
+ #
30
+ # @param [String] Your dynect customer name
31
+ # @param [String] Your dnyect user name
32
+ # @param [String] Your dynect password
33
+ # @param [String] The zone you are going to be editing
34
+ # @param [Boolean] Whether to connect immediately or not - runs login for you
35
+ def initialize(customer_name, user_name, password, zone=nil, connect=true)
36
+ @customer_name = customer_name
37
+ @user_name = user_name
38
+ @password = password
39
+ @rest = RestClient::Resource.new('https://api2.dynect.net/REST/', :headers => { :content_type => 'application/json' })
40
+ @zone = zone
41
+ login if connect
42
+ end
43
+
44
+ ##
45
+ # Session Management
46
+ ##
47
+
48
+ # Login to Dynect - must be done before any other methods called.
49
+ #
50
+ # See: https://manage.dynect.net/help/docs/api2/rest/resources/Session.html
51
+ #
52
+ # @return [Hash] The dynect API response
53
+ def login
54
+ response = post('Session', { 'customer_name' => @customer_name, 'user_name' => @user_name, 'password' => @password })
55
+ @rest.headers[:auth_token] = response["token"]
56
+ response
57
+ end
58
+
59
+ # Logout of Dynect - must be done before any other methods called.
60
+ #
61
+ # See: https://manage.dynect.net/help/docs/api2/rest/resources/Session.html
62
+ #
63
+ # @return [Hash] The dynect API response
64
+ def logout
65
+ delete('Session')
66
+ end
67
+
68
+ ##
69
+ # Zone
70
+ ##
71
+ # Get a zone from dynect
72
+ #
73
+ # See: https://manage.dynect.net/help/docs/api2/rest/resources/Zone.html
74
+ #
75
+ # @param [String] The zone to fetch - if one is provided when instantiated, we use that.
76
+ # @return [Hash] The dynect API response
77
+ def get_zone(zone=nil)
78
+ zone ||= @zone
79
+ get("Zone/#{zone}")
80
+ end
81
+
82
+ # Publish any pending changes to the zone - required to make any alterations permanent.
83
+ #
84
+ # See: https://manage.dynect.net/help/docs/api2/rest/resources/Zone.html
85
+ #
86
+ # @param [String] The zone to publish - if one is provided when instantiated, we use that.
87
+ # @return [Hash] The dynect API response
88
+ def publish(zone=nil)
89
+ zone ||= @zone
90
+ put("Zone/#{zone}", { "publish" => true })
91
+ end
92
+
93
+ # Freeze the zone.
94
+ #
95
+ # See: https://manage.dynect.net/help/docs/api2/rest/resources/Zone.html
96
+ #
97
+ # @param [String] The zone to freeze - if one is provided when instantiated, we use that.
98
+ # @return [Hash] The dynect API response
99
+ def freeze(zone=nil)
100
+ zone ||= @zone
101
+ put("Zone/#{zone}", { "freeze" => true })
102
+ end
103
+
104
+ # Thaw the zone.
105
+ #
106
+ # See: https://manage.dynect.net/help/docs/api2/rest/resources/Zone.html
107
+ #
108
+ # @param [String] The zone to thaw - if one is provided when instantiated, we use that.
109
+ # @return [Hash] The dynect API response
110
+ def thaw(zone=nil)
111
+ zone ||= @zone
112
+ put("Zone/#{zone}", { "freeze" => true })
113
+ end
114
+
115
+ ##
116
+ # Resource Records
117
+ ##
118
+ %w{AAAA A CNAME DNSKEY DS KEY LOC MX NS PTR RP SOA SRV TXT}.each do |resource_type|
119
+ define_method resource_type.downcase.to_sym do
120
+ DynectRest::Resource.new(self, resource_type, @zone)
121
+ end
122
+ end
123
+
124
+ # Raw GET request, formatted for Dyn. See list of endpoints at:
125
+ #
126
+ # https://manage.dynect.net/help/docs/api2/rest/resources/
127
+ #
128
+ # @param [String] The partial path to GET - for example, 'User' or 'Zone'.
129
+ # @param [Hash] Additional HTTP headers
130
+ def get(path_part, additional_headers = {}, &block)
131
+ api_request { @rest[path_part].get(additional_headers, &block) }
132
+ end
133
+
134
+ # Raw DELETE request, formatted for Dyn. See list of endpoints at:
135
+ #
136
+ # https://manage.dynect.net/help/docs/api2/rest/resources/
137
+ #
138
+ # @param [String] The partial path to DELETE - for example, 'User' or 'Zone'.
139
+ # @param [Hash] Additional HTTP headers
140
+ def delete(path_part, additional_headers = {}, &block)
141
+ api_request { @rest[path_part].delete(additional_headers, &block) }
142
+ end
143
+
144
+ # Raw POST request, formatted for Dyn. See list of endpoints at:
145
+ #
146
+ # https://manage.dynect.net/help/docs/api2/rest/resources/
147
+ #
148
+ # Read the API documentation, and submit the proper data structure from here.
149
+ #
150
+ # @param [String] The partial path to POST - for example, 'User' or 'Zone'.
151
+ # @param [Hash] The data structure to submit as the body, is automatically turned to JSON.
152
+ # @param [Hash] Additional HTTP headers
153
+ def post(path_part, payload, additional_headers = {}, &block)
154
+ api_request { @rest[path_part].post(payload.to_json, additional_headers, &block) }
155
+ end
156
+
157
+ # Raw PUT request, formatted for Dyn. See list of endpoints at:
158
+ #
159
+ # https://manage.dynect.net/help/docs/api2/rest/resources/
160
+ #
161
+ # Read the API documentation, and submit the proper data structure from here.
162
+ #
163
+ # @param [String] The partial path to PUT - for example, 'User' or 'Zone'.
164
+ # @param [Hash] The data structure to submit as the body, is automatically turned to JSON.
165
+ # @param [Hash] Additional HTTP headers
166
+ def put(path_part, payload, additional_headers = {}, &block)
167
+ api_request { @rest[path_part].put(payload.to_json, additional_headers, &block) }
168
+ end
169
+
170
+ # Handles making Dynect API requests and formatting the responses properly.
171
+ def api_request(&block)
172
+ response_body = begin
173
+ response = block.call
174
+ response.body
175
+ rescue RestClient::Exception => e
176
+ e.response
177
+ end
178
+ parse_response(JSON.parse(response_body))
179
+ end
180
+
181
+ def parse_response(response)
182
+ case response["status"]
183
+ when "success"
184
+ response["data"]
185
+ when "failure", "incomplete"
186
+ error_messages = []
187
+ response["msgs"].each do |error_message|
188
+ error_messages << "#{error_message["LVL"]} #{error_message["ERR_CD"]} #{error_message["SOURCE"]} - #{error_message["INFO"]}"
189
+ end
190
+ raise DynectRest::Exceptions::RequestFailed, "Request failed: #{error_messages.join("\n")}"
191
+ end
192
+ end
193
+
194
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "DynectRest" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'dynect-rest'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynect_rest
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Adam Jacob
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-06 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: yard
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :runtime
57
+ version_requirements: *id003
58
+ - !ruby/object:Gem::Dependency
59
+ name: rest-client
60
+ prerelease: false
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ type: :runtime
69
+ version_requirements: *id004
70
+ description: Use the Dynect services REST API
71
+ email: adam@opscode.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - LICENSE
78
+ - README.rdoc
79
+ files:
80
+ - .document
81
+ - .gitignore
82
+ - LICENSE
83
+ - README.rdoc
84
+ - Rakefile
85
+ - VERSION
86
+ - lib/dynect_rest.rb
87
+ - lib/dynect_rest/exceptions.rb
88
+ - lib/dynect_rest/resource.rb
89
+ - spec/dynect-rest_spec.rb
90
+ - spec/spec.opts
91
+ - spec/spec_helper.rb
92
+ has_rdoc: true
93
+ homepage: http://github.com/adamhjk/dynect-rest
94
+ licenses: []
95
+
96
+ post_install_message:
97
+ rdoc_options:
98
+ - --charset=UTF-8
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project:
118
+ rubygems_version: 1.3.6
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Dynect REST API library
122
+ test_files:
123
+ - spec/dynect-rest_spec.rb
124
+ - spec/spec_helper.rb