This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added server create & delete

  • Loading branch information...
itsderek23 authored and technicalpickles committed Aug 12, 2010
1 parent 5c01f42 commit 80635be3c2126a3b5853fd60897f4b2bc50ca416
Showing with 61 additions and 5 deletions.
  1. +7 −1 README.rdoc
  2. +31 −0 lib/scout_scout/server.rb
  3. +19 −0 spec/scout_scout_spec.rb
  4. +4 −4 spec/spec_helper.rb
View
@@ -1,6 +1,6 @@
= scout_scout
-A library for extracting data out of Scout[http//:scoutapp.com], a hosted monitoring service. This is experimental and likely to change.
+A library for interacting with Scout[http//:scoutapp.com], a hosted monitoring service.
require 'scout_scout'
scout = ScoutScout.new('youraccountname', 'your@awesome.com', 'sekret')
@@ -35,6 +35,12 @@ A library for extracting data out of Scout[http//:scoutapp.com], a hosted monito
#cluster metrics
ScoutScout::Cluster.average('mem_used')
+
+ #create a server using the server with id=6 as the template.
+ ScoutScout::Server.create('heavy metal',:id => 6) => <#ScoutScout::Server>
+
+ #delete the server with id=10
+ ScoutScout::Server.delete(10) => true
== Metrics
View
@@ -20,6 +20,37 @@ def self.first(server_id_or_hostname)
ScoutScout::Server.new(response['clients'].first)
end
end
+
+ # Creates a new server. If an error occurs, a +ScoutScout::Error+ is raised.
+ #
+ # An optional existing server id can be used as a template:
+ # ScoutScout::Server.create('web server 12',:id => 99999)
+ #
+ # @return [ScoutScout::Server]
+ def self.create(name,options = {})
+ id = options[:id]
+ response = ScoutScout.post("/#{ScoutScout.account}/clients.xml",
+ :query => {:client => {:name => name, :copy_plugins_from_client_id => id}})
+
+ raise ScoutScout::Error, response['errors']['error'] if response['errors']
+
+ first(response.headers['id'].first.to_i)
+ end
+
+ # Delete a server by id. If an error occurs, a +ScoutScout::Error+ is raised.
+ #
+ # @return [true]
+ def self.delete(id)
+ response = ScoutScout.delete("/#{ScoutScout.account}/clients/#{id}.xml")
+
+ if response.headers['status'].first.match('404')
+ raise ScoutScout::Error, "Server Not Found"
+ elsif !response.headers['status'].first.match('200')
+ raise ScoutScout::Error, "An error occured"
+ else
+ return true
+ end
+ end
# Search for servers by matching hostname via :host.
#
View
@@ -97,6 +97,25 @@
end
end
end
+ describe '' do
+ before(:each) do
+ @scout_scout.stub_post('clients.xml?client[copy_plugins_from_client_id]=&client[name]=sweet%20new%20server',
+ 'client.xml', {:id => '1234'})
+ @scout_scout.stub_get('clients/1234.xml', 'client.xml')
+ end
+ it 'can be created' do
+ @server = ScoutScout::Server.create('sweet new server')
+ @server.id.should == 13431
+ end
+ end
+ describe '' do
+ before(:each) do
+ @scout_scout.stub_delete('clients/1234.xml','client.xml', {'status' => '200 OK'})
+ end
+ it 'can be deleted' do
+ ScoutScout::Server.delete(1234).should == true
+ end
+ end
describe 'alert log' do
before(:each) do
@scout_scout.stub_get('clients/13431.xml', 'client.xml')
View
@@ -24,16 +24,16 @@ def stub_get(path, filename = nil, status=nil)
FakeWeb.register_uri(:get, scout_url(path), options)
end
- def stub_post(path, filename)
- FakeWeb.register_uri(:post, scout_url(path), :body => file_fixture(filename))
+ def stub_post(path, filename, headers = {})
+ FakeWeb.register_uri(:post, scout_url(path), {:body => file_fixture(filename)}.merge(headers))
end
def stub_put(path, filename)
FakeWeb.register_uri(:put, scout_url(path), :body => file_fixture(filename))
end
- def stub_delete(path, filename)
- FakeWeb.register_uri(:delete, scout_url(path), :body => file_fixture(filename))
+ def stub_delete(path, filename, headers = {})
+ FakeWeb.register_uri(:delete, scout_url(path), {:body => file_fixture(filename)}.merge(headers))
end
def stub_http_response_with(filename)

0 comments on commit 80635be

Please sign in to comment.