voxxit-whm_xml 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
1
+ = WHM XML-API Ruby Wrapper
2
+
3
+ This is a Ruby wrapper for the cPanel WHM XML-API interface. It will allow you to perform multiple functions available on your cPanel WHM server. For more information on the XML-API, see the cPanel website at http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi.
4
+
5
+ == Requirements
6
+
7
+ The following Ruby gems are required to be installed in order to make use this gem:
8
+
9
+ xml-simple (>= 1.0.12):: http://xml-simple.rubyforge.org
10
+ activesupport (>= 2.3.2):: http://as.rubyonrails.org
11
+ curb (>= 0.3.2):: http://curb.rubyforge.org
12
+ validatable (>= 1.6.7):: http://validatable.rubyforge.org
13
+
14
+ == Usage
15
+
16
+ First off, you must have root access to a cPanel WHM server. With this gem, you can either use <b>password</b> or <b>remote access key</b> authentication. To get to the remote access key in WHM (which is likely the more secure method of connecting), go under "Cluster/Remote Access", and click on "Setup Remote Access Key". You can either copy the pre-generated one, or re-generate it.
17
+
18
+ ==== Installation
19
+
20
+ You can install this gem simply by doing the following:
21
+
22
+ $ gem sources -a http://gems.github.com
23
+ $ gem install ivanoats-whm_xml_api_ruby
24
+
25
+ You can also include it in a Rails project:
26
+
27
+ $ script/plugin install git://github.com/ivanoats/whm_xml_api_ruby.git
28
+
29
+ ==== Connecting
30
+
31
+ To access the functions of the server, all you need to do is initialize a new Server class:
32
+
33
+ server = Whm::Server.new(
34
+ :username => "root",
35
+ :remote_access_key => "sd00fsd2i3rj...",
36
+ :host => "dedicated.server.com"
37
+ )
38
+
39
+ server.version
40
+ => "11.24.2"
41
+
42
+ == Currently Available Methods
43
+
44
+ As of WHM version 11.24.2, these are the methods that are available via this gem:
45
+
46
+ * <tt>account_summary</tt>
47
+ * <tt>change_account_password</tt>
48
+ * <tt>change_package</tt>
49
+ * <tt>create_account</tt>
50
+ * <tt>generate_ssl_certificate</tt>
51
+ * <tt>hostname</tt>
52
+ * <tt>limit_bandwidth_usage</tt>
53
+ * <tt>list_accounts</tt>
54
+ * <tt>list_packages</tt>
55
+ * <tt>modify_account</tt>
56
+ * <tt>suspend_account</tt>
57
+ * <tt>terminate_account</tt>
58
+ * <tt>unsuspend_account</tt>
59
+ * <tt>version</tt>
60
+
61
+ == Authors and credits
62
+
63
+ Authors:: Ivan Storck, Josh Delsman, Padraic McGee
64
+ Home page:: http://github.com/ivanoats/whm_xml_api_ruby
65
+
66
+ == License
67
+
68
+ Copyright (c) 2008-2009 Ivan Storck
69
+
70
+ Permission is hereby granted, free of charge, to any person obtaining
71
+ a copy of this software and associated documentation files (the
72
+ 'Software'), to deal in the Software without restriction, including
73
+ without limitation the rights to use, copy, modify, merge, publish,
74
+ distribute, sublicense, and/or sell copies of the Software, and to
75
+ permit persons to whom the Software is furnished to do so, subject to
76
+ the following conditions:
77
+
78
+ The above copyright notice and this permission notice shall be
79
+ included in all copies or substantial portions of the Software.
80
+
81
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
82
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
83
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
84
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
85
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
86
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
87
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rake/rdoctask'
3
+
4
+ desc 'Generate documentation for whm_xml_api_ruby'
5
+ Rake::RDocTask.new do |rd|
6
+ rd.rdoc_dir = 'html'
7
+ rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
8
+ rd.main = "README.rdoc"
9
+ rd.title = "whm_xml_api_ruby -- A Ruby wrapper for cPanel's WHM"
10
+ rd.options << "--all"
11
+ end
@@ -0,0 +1,67 @@
1
+ module Whm #:nodoc:
2
+ # Allows for parameter requirements and validations for methods
3
+ module Parameters
4
+ # Check the included hash for the included parameters, and ensure they aren't blank.
5
+ #
6
+ # ==== Example
7
+ #
8
+ # class User
9
+ # def initialize
10
+ # requires!(options, :username, :password)
11
+ # end
12
+ # end
13
+ #
14
+ # >> User.new
15
+ # ArgumentError: Missing required parameter: username
16
+ #
17
+ # >> User.new(:username => "john")
18
+ # ArgumentError: Missing required parameter: password
19
+ def requires!(hash, *params)
20
+ params.each do |param|
21
+ if param.is_a?(Array)
22
+ raise ArgumentError.new("Missing required parameter: #{param.first}") unless hash.has_key?(param.first)
23
+ raise ArgumentError.new("Required parameter cannot be blank: #{param.first}") if hash[param.first].blank?
24
+ else
25
+ raise ArgumentError.new("Missing required parameter: #{param}") unless hash.has_key?(param)
26
+ raise ArgumentError.new("Required parameter cannot be blank: #{param}") if hash[param].blank?
27
+ end
28
+ end
29
+ end
30
+
31
+ # Checks to see if supplied params (which are booleans) contain
32
+ # either a 1 ("Yes") or 0 ("No") value.
33
+ def booleans!(hash, *params)
34
+ params.each do |param|
35
+ if param.is_a?(Array)
36
+ raise ArgumentError.new("Boolean parameter must be \"1\" or \"0\": #{param.first}") unless hash[param.first].to_s.match(/(1|0)/)
37
+ else
38
+ raise ArgumentError.new("Boolean parameter must be \"1\" or \"0\": #{param}") unless hash[param].to_s.match(/(1|0)/)
39
+ end
40
+ end
41
+ end
42
+
43
+ # Checks the hash to see if the hash includes any parameter
44
+ # which is not included in the list of valid parameters.
45
+ #
46
+ # ==== Example
47
+ #
48
+ # class User
49
+ # def initialize
50
+ # valid_options!(options, :username)
51
+ # end
52
+ # end
53
+ #
54
+ # >> User.new(:username => "josh")
55
+ # => #<User:0x18a1190 @username="josh">
56
+ #
57
+ # >> User.new(:username => "josh", :credit_card => "5105105105105100")
58
+ # ArgumentError: Not a valid parameter: credit_card
59
+ def valid_options!(hash, *params)
60
+ keys = hash.keys
61
+
62
+ keys.each do |key|
63
+ raise ArgumentError.new("Not a valid parameter: #{key}") unless params.include?(key)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,16 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'curb' # As opposed to Net::HTTP (for faster requests)
5
+ require 'xmlsimple' # For simple XML parsing
6
+ require 'active_support' # For stringifying keys, etc.
7
+ require 'parameters' # For parameter requirements in methods
8
+ require 'validatable' # For object validation
9
+
10
+ require 'whm_xml/exceptions'
11
+ require 'whm_xml/server'
12
+ require 'whm_xml/account'
13
+
14
+ module Whm
15
+ VERSION = '0.3.0'
16
+ end
@@ -0,0 +1,4 @@
1
+ module Whm #:nodoc:
2
+ class CommandFailedError < StandardError #:nodoc:
3
+ end
4
+ end
@@ -0,0 +1,326 @@
1
+ module Whm #:nodoc:
2
+ # The Server class initializes a new connection with the cPanel WHM server, and
3
+ # contains all functions that can be run on a cPanel WHM server as of version 11.24.2.
4
+ class Server
5
+ include Parameters
6
+
7
+ # Hostname of the WHM server (e.g., <tt>dedicated.server.com</tt>)
8
+ attr_reader :host
9
+
10
+ # WHM XML-API username
11
+ attr_reader :username
12
+
13
+ # WHM XML-API password. Use this, or the remote key, to authenticate with the server
14
+ attr_reader :password
15
+
16
+ # WHM XML-API remote key. Use this, or the password, to authenticate with the server
17
+ attr_reader :remote_key
18
+
19
+ # If you'd like increased verbosity of commands, set this to <tt>true</tt>. Defaults to <tt>false</tt>
20
+ attr_accessor :debug
21
+
22
+ attr_accessor :attributes
23
+
24
+ # Initialize the connection with WHM using the hostname,
25
+ # user and password/remote key. Will default to asking for
26
+ # a password, but remote_key is required if a password
27
+ # is not used.
28
+ #
29
+ # ==== Example
30
+ #
31
+ # Password authentication with debugging enabled:
32
+ #
33
+ # Whm::Server.new(
34
+ # :host => "dedicated.server.com",
35
+ # :username => "root",
36
+ # :password => "s3cUr3!p@5sw0rD",
37
+ # :debug => true
38
+ # )
39
+ #
40
+ # Remote key authentication with port 8000, and SSL to off (defaults to port 2087, and SSL on):
41
+ #
42
+ # Whm::Server.new(
43
+ # :host => "dedicated.server.com",
44
+ # :username => "root",
45
+ # :remote_key => "cf975b8930b0d0da69764c5d8dc8cf82 ...",
46
+ # :port => 8000,
47
+ # :ssl => false
48
+ # )
49
+ def initialize(options = {})
50
+ requires!(options, :host, :username)
51
+ requires!(options, :password) unless options[:remote_key]
52
+
53
+ @host = options[:host]
54
+ @username = options[:username] || "root"
55
+ @remote_key = options[:remote_key].gsub(/(\r|\n)/, "") unless options[:password]
56
+ @password = options[:password] unless options[:remote_key]
57
+ @debug = options[:debug] || false
58
+ @port = options[:port] || 2087
59
+ @ssl = options[:ssl] || true
60
+ end
61
+
62
+ # Displays pertient account information for a specific account.
63
+ #
64
+ # ==== Options
65
+ # * <tt>:user</tt> - Username associated with the acount to display (string)
66
+ def account_summary(options = {})
67
+ requires!(options, :user)
68
+
69
+ data = get_xml(:url => "accountsummary", :params => options)
70
+ check_for_cpanel_errors_on(data)["acct"]
71
+ end
72
+
73
+ # Changes the password of a domain owner (cPanel) or reseller (WHM) account.
74
+ #
75
+ # ==== Options
76
+ # * <tt>:user</tt> - Username of the user whose password should be changed (string)
77
+ # * <tt>:pass</tt> - New password for that user (string)
78
+ def change_account_password(options = {})
79
+ requires!(options, :user, :pass)
80
+
81
+ data = get_xml(:url => "passwd", :params => options)
82
+ check_for_cpanel_errors_on(data)["passwd"]
83
+ end
84
+
85
+ # Changes the hosting package associated with an account.
86
+ # Returns <tt>true</tt> if it is successful, or
87
+ # <tt>false</tt> if it is not.
88
+ #
89
+ # ==== Options
90
+ # * <tt>:user</tt> - Username of the account to change the package for (string)
91
+ # * <tt>:pkg</tt> - Name of the package that the account should use (string)
92
+ def change_package(options = {})
93
+ requires!(options, :user, :pkg)
94
+
95
+ data = get_xml(:url => "changepackage", :params => options)
96
+ check_for_cpanel_errors_on(data)
97
+
98
+ data["status"] == "1" ? true : false
99
+ end
100
+
101
+ # Creates a hosting account and sets up it's associated domain information.
102
+ #
103
+ # ==== Options
104
+ # * <tt>:username</tt> - Username of the account (string)
105
+ # * <tt>:domain</tt> - Domain name (string)
106
+ # * <tt>:pkgname</tt> - Name of a new package to be created based on the settings used (string)
107
+ # * <tt>:savepkg</tt> - Save the settings used as a new package (boolean)
108
+ # * <tt>:featurelist</tt> - Name of the feature list to be used when creating a new package (string)
109
+ # * <tt>:quota</tt> - Disk space quota in MB. Must be between 0-999999, with 0 being unlimited (integer)
110
+ # * <tt>:password</tt> - User's password to access cPanel (string)
111
+ # * <tt>:ip</tt> - Whether or not the domain has a dedicated IP address, either <tt>"y"</tt> (Yes) or <tt>"n"</tt> (No) (string)
112
+ # * <tt>:cgi</tt> - Whether or not the domain has CGI access (boolean)
113
+ # * <tt>:frontpage</tt> - Whether or not the domain has FrontPage extensions installed (boolean)
114
+ # * <tt>:hasshell</tt> - Whether or not the domain has shell/SSH access (boolean)
115
+ # * <tt>:contactemail</tt> - Contact email address for the account (string)
116
+ # * <tt>:cpmod</tt> - cPanel theme name (string)
117
+ # * <tt>:maxftp</tt> - Maximum number of FTP accounts the user can create. Must be between 0-999999, with 0 being unlimited (integer)
118
+ # * <tt>:maxsql</tt> - Maximum number of SQL databases the user can create. Must be between 0-999999, with 0 being unlimited (integer)
119
+ # * <tt>:maxpop</tt> - Maximum number of email accounts the user can create. Must be between 0-999999, with 0 being unlimited (integer)
120
+ # * <tt>:maxlst</tt> - Maximum number of mailing lists the user can create. Must be between 0-999999, with 0 being unlimited (integer)
121
+ # * <tt>:maxsub</tt> - Maximum number of subdomains the user can create. Must be between 0-999999, with 0 being unlimited (integer)
122
+ # * <tt>:maxpark</tt> - Maximum number of parked domains the user can create. Must be between 0-999999, with 0 being unlimited (integer)
123
+ # * <tt>:maxaddon</tt> - Maximum number of addon domains the user can create. Must be between 0-999999, with 0 being unlimited (integer)
124
+ # * <tt>:bwlimit</tt> - Bandwidth limit in MB. Must be between 0-999999, with 0 being unlimited (integer)
125
+ # * <tt>:customip</tt> - Specific IP for the site (string)
126
+ # * <tt>:language</tt> - Language to use in the account's cPanel interface (string)
127
+ # * <tt>:useregns</tt> - Use the registered nameservers for the domain instead of the ones configured on the server (boolean)
128
+ # * <tt>:hasuseregns</tt> - Must be set to <tt>1</tt> if the above <tt>:useregns</tt> is set to <tt>1</tt> (boolean)
129
+ # * <tt>:reseller</tt> - Give reseller privileges to the account (boolean)
130
+ def create_account(options = {})
131
+ requires!(options, :domain, :username)
132
+
133
+ data = get_xml(:url => "createacct", :params => options)
134
+ check_for_cpanel_errors_on(data)
135
+ end
136
+
137
+ # Generates an SSL certificate
138
+ #
139
+ # ==== Options
140
+ # * <tt>:xemail</tt> - Email address of the domain owner (string)
141
+ # * <tt>:host</tt> - Domain the SSL certificate is for, or the SSL host (string)
142
+ # * <tt>:country</tt> - Country the organization is located in (string)
143
+ # * <tt>:state</tt> - State the organization is located in (string)
144
+ # * <tt>:city</tt> - City the organization is located in (string)
145
+ # * <tt>:co</tt> - Name of the organization/company (string)
146
+ # * <tt>:cod</tt> - Name of the department (string)
147
+ # * <tt>:email</tt> - Email to send the certificate to (string)
148
+ # * <tt>:pass</tt> - Certificate password (string)
149
+ def generate_ssl_certificate(options = {})
150
+ requires!(options, :city, :co, :cod, :country, :email, :host, :pass, :state, :xemail)
151
+ data = get_xml(:url => "generatessl", :params => options)
152
+ check_for_cpanel_errors_on(data)
153
+ end
154
+
155
+ # Displays the server's hostname.
156
+ def hostname
157
+ data = get_xml(:url => "gethostname")
158
+ check_for_cpanel_errors_on(data)["hostname"]
159
+ end
160
+
161
+ # Modifies the bandwidth usage (transfer) limit for a specific account.
162
+ #
163
+ # ==== Options
164
+ # * <tt>:user</tt> - Name of user to modify the bandwidth usage (transfer) limit for (string)
165
+ # * <tt>:bwlimit</tt> - Bandwidth usage (transfer) limit in MB (string)
166
+ def limit_bandwidth_usage(options = {})
167
+ requires!(options, :user, :bwlimit)
168
+
169
+ data = get_xml(:url => "limitbw", :params => options)
170
+ check_for_cpanel_errors_on(data)["bwlimit"]
171
+ end
172
+
173
+ # Lists all accounts on the server, or allows you to search for
174
+ # a specific account or set of accounts.
175
+ #
176
+ # ==== Options
177
+ # * <tt>:searchtype</tt> - Type of account search (<tt>"domain"</tt>, <tt>"owner"</tt>, <tt>"user"</tt>, <tt>"ip"</tt> or <tt>"package"</tt>)
178
+ # * <tt>:search</tt> - Search criteria, in Perl regular expression format (string)
179
+ def list_accounts(options = {})
180
+ data = get_xml(:url => "listaccts", :params => options)
181
+ check_for_cpanel_errors_on(data)["acct"]
182
+ end
183
+
184
+ # Lists all hosting packages that are available for use by
185
+ # the current WHM user. If the current user is a reseller,
186
+ # they may not see some packages that exist if those packages
187
+ # are not available to be used for account creation at this time.
188
+ def list_packages
189
+ data = get_xml(:url => "listpkgs")
190
+ check_for_cpanel_errors_on(data)["package"]
191
+ end
192
+
193
+ # Modifies account specific settings. We recommend changing the
194
+ # account's package instead with change_package. If the account
195
+ # is associated with a package, the account's settings will be
196
+ # changed whenever the package is changed. That may overwrite
197
+ # any changes you make with this function.
198
+ #
199
+ # ==== Options
200
+ # * <tt>:CPTHEME</tt> - cPanel theme name (string)
201
+ # * <tt>:domain</tt> - Domain name (string)
202
+ # * <tt>:HASCGI</tt> - Whether or not the domain has CGI access (boolean)
203
+ # * <tt>:LANG</tt> - Language to use in the account's cPanel interface (boolean)
204
+ # * <tt>:MAXFTP</tt> - Maximum number of FTP accounts the user can create. Must be between 0-999999, with 0 being unlimited (integer)
205
+ # * <tt>:MAXSQL</tt> - Maximum number of SQL databases the user can create. Must be between 0-999999, with 0 being unlimited (integer)
206
+ # * <tt>:MAXPOP</tt> - Maximum number of email accounts the user can create. Must be between 0-999999, with 0 being unlimited (integer)
207
+ # * <tt>:MAXLST</tt> - Maximum number of mailing lists the user can create. Must be between 0-999999, with 0 being unlimited (integer)
208
+ # * <tt>:MAXSUB</tt> - Maximum number of subdomains the user can create. Must be between 0-999999, with 0 being unlimited (integer)
209
+ # * <tt>:MAXPARK</tt> - Maximum number of parked domains the user can create. Must be between 0-999999, with 0 being unlimited (integer)
210
+ # * <tt>:MAXADDON</tt> - Maximum number of addon domains the user can create. Must be between 0-999999, with 0 being unlimited (integer)
211
+ # * <tt>:shell</tt> - Whether or not the domain has shell/SSH access (boolean)
212
+ # * <tt>:user</tt> - User name of the account (string)
213
+ def modify_account(options = {})
214
+ booleans!(options, :HASCGI, :LANG, :shell)
215
+ requires!(options, :user, :domain, :HASCGI, :CPTHEME, :LANG, :MAXPOP, :MAXFTP, :MAXLST, :MAXSUB,
216
+ :MAXPARK, :MAXADDON, :MAXSQL, :shell)
217
+
218
+ data = get_xml(:url => "modifyacct", :params => options)
219
+
220
+ check_for_cpanel_errors_on(data)
221
+ end
222
+
223
+ # Suspend an account. Returns <tt>true</tt> if it is successful,
224
+ # or <tt>false</tt> if it is not.
225
+ #
226
+ # ==== Options
227
+ # * <tt>:user</tt> - Username to suspend (string)
228
+ # * <tt>:reason</tt> - Reason for suspension (string)
229
+ def suspend_account(options = {})
230
+ requires!(options, :user, :reason)
231
+
232
+ data = get_xml(:url => "suspendacct", :params => options)
233
+ check_for_cpanel_errors_on(data)
234
+
235
+ data["status"] == "1" ? true : false
236
+ end
237
+
238
+ # Terminates a hosting account. <b>Please note that this action is irreversible!</b>
239
+ #
240
+ # ==== Options
241
+ # * <tt>:user</tt> - Username to terminate (string)
242
+ # * <tt>:keepdns</tt> - Keep DNS entries for the domain ("y" or "n")
243
+ def terminate_account(options = {})
244
+ requires!(options, :user)
245
+
246
+ data = get_xml(:url => "removeacct", :params => {
247
+ :user => options[:user],
248
+ :keepdns => options[:keepdns] || "n"
249
+ })
250
+
251
+ check_for_cpanel_errors_on(data)
252
+ end
253
+
254
+ # Unsuspend a suspended account. Returns <tt>true</tt> if it
255
+ # is successful, or <tt>false</tt> if it is not.
256
+ #
257
+ # ==== Options
258
+ # * <tt>:user</tt> - Username to unsuspend (string)
259
+ def unsuspend_account(options = {})
260
+ requires!(options, :user)
261
+
262
+ data = get_xml(:url => "unsuspendacct", :params => options)
263
+ check_for_cpanel_errors_on(data)
264
+
265
+ data["status"] == "1" ? true : false
266
+ end
267
+
268
+ # Returns the cPanel WHM version.
269
+ def version
270
+ data = get_xml(:url => 'version')
271
+ check_for_cpanel_errors_on(data)["version"]
272
+ end
273
+
274
+ private
275
+
276
+ # Grabs the XML response for a command to the server, and parses it.
277
+ #
278
+ # ==== Options
279
+ # * <tt>:url</tt> - URL of the XML API function (string)
280
+ # * <tt>:params</tt> - Passed in parameter hash (hash)
281
+ def get_xml(options = {})
282
+ requires!(options, :url)
283
+
284
+ prefix = @ssl ? "https" : "http"
285
+ params = []
286
+
287
+ unless options[:params].nil?
288
+ for key, value in options[:params]
289
+ params << Curl::PostField.content(key.to_s, value)
290
+ end
291
+ end
292
+
293
+ request = Curl::Easy.new("#{prefix}://#{@host}:#{@port}/xml-api/#{options[:url]}") do |connection|
294
+ puts "WHM: Requesting #{options[:url]}..." if @debug
295
+
296
+ connection.userpwd = "#{@username}:#{@password}" if @password
297
+ connection.headers["Authorization"] = "WHM #{@username}:#{@remote_key}" if @remote_key
298
+ connection.verbose = true if @debug
299
+ connection.timeout = 60
300
+ end
301
+
302
+ if request.http_post(params)
303
+ xml = XmlSimple.xml_in(request.body_str, { 'ForceArray' => false })
304
+ xml["result"].nil? ? xml : xml["result"]
305
+ end
306
+ end
307
+
308
+ # Returns the data, unless a <tt>status</tt> is equal to <tt>0</tt>,
309
+ # in which case a CommandFailedError exception is
310
+ # thrown with the <tt>statusmsg</tt> from the fetched XML.
311
+ #
312
+ # If the command does not support status messaging,
313
+ # then just return the raw data.
314
+ def check_for_cpanel_errors_on(data)
315
+ if data["status"]
316
+ if data["status"] == 1
317
+ data
318
+ else
319
+ raise CommandFailedError, data["statusmsg"]
320
+ end
321
+ else
322
+ data
323
+ end
324
+ end
325
+ end
326
+ end
@@ -0,0 +1,128 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require 'whm_xml'
3
+ require 'pp'
4
+
5
+ describe "A WHM Server" do
6
+
7
+ before do
8
+ @xml = Whm::Xml.new(
9
+ :username => "username",
10
+ :password => "password",
11
+ :host => "www.example.com"
12
+ )
13
+ end
14
+
15
+ it "should list accounts" do
16
+ data = open('fixtures/listaccts.xml').read
17
+ @xml.should_receive(:get_xml).with({:url=>"listaccts", :params=>{}}).and_return(XmlSimple.xml_in(data))
18
+ @xml.list_accounts.should_not be_nil
19
+ end
20
+
21
+ it "should list packages" do
22
+ data = open('fixtures/listpkgs.xml').read
23
+ @xml.should_receive(:get_xml).with(:url => 'listpkgs').and_return(XmlSimple.xml_in(data))
24
+ @xml.list_packages.should_not be_nil
25
+ end
26
+
27
+ it "should display hostname" do
28
+ data = open(File.dirname(__FILE__) + '/fixtures/gethostname.xml').read
29
+ @xml.should_receive(:get_xml).with('gethostname').and_return(XmlSimple.xml_in(data))
30
+ @xml.hostname.should_not be_nil
31
+ end
32
+
33
+ it "should display version" do
34
+ data = open(File.dirname(__FILE__) + '/fixtures/version.xml').read
35
+ @xml.should_receive(:get_xml).with('version').and_return(XmlSimple.xml_in(data))
36
+ @xml.version.should_not be_nil
37
+ end
38
+
39
+ it "should generate a ssl certificate" do
40
+ data = open(File.dirname(__FILE__) + '/fixtures/generatessl.xml').read
41
+ @xml.should_receive(:get_xml).with('generatessl',{}).and_return(XmlSimple.xml_in(data))
42
+ @xml.generate_certificate.should_not be_nil
43
+ end
44
+
45
+ it "should generate a ssl certificate using an alias" do
46
+ data = open(File.dirname(__FILE__) + '/fixtures/generatessl.xml').read
47
+ @xml.should_receive(:get_xml).with('generatessl',{:xemail => "xemail"}).and_return(XmlSimple.xml_in(data))
48
+ @xml.generate_ssl_certificate({:xemail => "xemail"}).should_not be_nil
49
+ end
50
+
51
+ describe "working with Accounts" do
52
+ before do
53
+ @account_options = {:username => 'test_account'}
54
+ end
55
+
56
+ it "should create an account" do
57
+ data = open(File.dirname(__FILE__) + '/fixtures/createacct.xml').read
58
+ @xml.should_receive(:get_xml).with('createacct',@account_options).and_return(XmlSimple.xml_in(data))
59
+ @xml.create_account(@account_options).should_not be_nil
60
+ end
61
+
62
+ it "should change account password" do
63
+ data = open(File.dirname(__FILE__) + '/fixtures/passwd.xml').read
64
+ @xml.should_receive(:get_xml).with('passwd',{:user => 'test_account', :pass => 'new_password'}).and_return(XmlSimple.xml_in(data))
65
+ @xml.change_account_password('test_account', 'new_password').should_not be_nil
66
+ end
67
+
68
+ it "should limit bandwidth usage" do
69
+ data = open(File.dirname(__FILE__) + '/fixtures/limitbw.xml').read
70
+ @xml.should_receive(:get_xml).with('limitbw',{:user => 'test_account', :bwlimit => '100'}).and_return(XmlSimple.xml_in(data))
71
+ @xml.limit_bandwidth('test_account', '100').should_not be_nil
72
+ end
73
+
74
+ it "should display account summary" do
75
+ data = open(File.dirname(__FILE__) + '/fixtures/accountsummary.xml').read
76
+ @xml.should_receive(:get_xml).with('accountsummary',{:user =>'test_account'}).and_return(XmlSimple.xml_in(data))
77
+ @xml.account_summary('test_account').should_not be_nil
78
+ end
79
+
80
+ it "should suspend account" do
81
+ data = open(File.dirname(__FILE__) + '/fixtures/suspendacct.xml').read
82
+ @xml.should_receive(:get_xml).with('suspendacct',{:user =>'test_account', :reason => 'General Misbehaving'}).and_return(XmlSimple.xml_in(data))
83
+ @xml.suspend_account('test_account', 'General Misbehaving').should_not be_nil
84
+ end
85
+
86
+ it "should unsuspend account" do
87
+ data = open(File.dirname(__FILE__) + '/fixtures/unsuspendacct.xml').read
88
+ @xml.should_receive(:get_xml).with('unsuspendacct',{:user =>'test_account'}).and_return(XmlSimple.xml_in(data))
89
+ @xml.unsuspend_account('test_account').should_not be_nil
90
+ end
91
+
92
+ it "should terminate account" do
93
+ data = open(File.dirname(__FILE__) + '/fixtures/removeacct.xml').read
94
+ @xml.should_receive(:get_xml).with('removeacct',{:user =>'test_account', :keepdns => 'n'}).and_return(XmlSimple.xml_in(data))
95
+ @xml.terminate_account('test_account').should_not be_nil
96
+ end
97
+
98
+ it "should change account package" do
99
+ data = open(File.dirname(__FILE__) + '/fixtures/changepackage.xml').read
100
+ @xml.should_receive(:get_xml).with('changepackage',{:user =>'test_account', :pkg => 'new_package'}).and_return(XmlSimple.xml_in(data))
101
+ @xml.change_package('test_account', 'new_package').should_not be_nil
102
+ end
103
+
104
+ it "should display an error message" do
105
+ data = open(File.dirname(__FILE__) + '/fixtures/error.xml').read
106
+ @xml.connection.should_receive(:get_xml).with('listaccts',{}).and_return(data)
107
+
108
+ lambda { @xml.list_accounts }.should raise_error( Whm::CommandFailed )
109
+ end
110
+
111
+ it "should throw an exception when an account doesn't exist" do
112
+ data = open(File.dirname(__FILE__) + '/fixtures/error2.xml').read
113
+ @xml.connection.should_receive(:get_xml).with('accountsummary',{:user => 'bad_account'}).and_return(data)
114
+ lambda { @xml.account_summary('bad_account') }.should raise_error( Whm::CommandFailed )
115
+ end
116
+
117
+ end
118
+ end
119
+
120
+ describe "A Nonexistant Server" do
121
+ before do
122
+ @xml = Whm::Xml.new('localhost',2087,'username','password')
123
+ end
124
+
125
+ it "should exceptionize on connection failure" do
126
+ lambda { @xml.version }.should raise_error( Whm::CantConnect )
127
+ end
128
+ end
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ dir = File.dirname(__FILE__)
10
+
11
+ $:.unshift(File.join(dir, '/../lib/'))
12
+
13
+ def stdout_for(&block)
14
+ # Inspired by http://www.ruby-forum.com/topic/58647
15
+ old_stdout = $stdout
16
+ $stdout = StringIO.new
17
+ yield
18
+ output = $stdout.string
19
+ $stdout = old_stdout
20
+ output
21
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: voxxit-whm_xml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Storck
8
+ - Padraic McGee
9
+ - Josh Delsman
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-04-17 00:00:00 -07:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: xml-simple
19
+ type: :runtime
20
+ version_requirement:
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 1.0.12
26
+ version:
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ type: :runtime
30
+ version_requirement:
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.3.2
36
+ version:
37
+ - !ruby/object:Gem::Dependency
38
+ name: curb
39
+ type: :runtime
40
+ version_requirement:
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 0.3.2
46
+ version:
47
+ - !ruby/object:Gem::Dependency
48
+ name: validatable
49
+ type: :runtime
50
+ version_requirement:
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.7
56
+ version:
57
+ description: The whm_xml library provides a Ruby wrapper for the cPanel Web Host Manager (WHM) XML-API
58
+ email: ivanoats+whm_xml@gmail.com
59
+ executables: []
60
+
61
+ extensions: []
62
+
63
+ extra_rdoc_files:
64
+ - README.rdoc
65
+ files:
66
+ - Rakefile
67
+ - README.rdoc
68
+ - lib/parameters.rb
69
+ - lib/whm_xml/exceptions.rb
70
+ - lib/whm_xml/server.rb
71
+ - lib/whm_xml.rb
72
+ - spec/server_spec.rb
73
+ - spec/spec_helper.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/ivanoats/whm_xml_api_ruby
76
+ post_install_message:
77
+ rdoc_options:
78
+ - --main
79
+ - README.rdoc
80
+ - --inline-source
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.2.0
100
+ signing_key:
101
+ specification_version: 2
102
+ summary: Web Host Manager (WHM) XML-API Ruby library
103
+ test_files:
104
+ - spec/server_spec.rb