Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactored to work with multiple regions and allow for configuration …

…in the claws.yml file
  • Loading branch information...
commit 33fe494205eea0dfd4bf6166f2e87e5d7b35b6fe 1 parent 1481dd9
@wbailey authored
View
7 lib/claws/collection/base.rb
@@ -3,8 +3,11 @@
module Claws
module Collection
class Base
- def self.connect(credentials)
- AWS.config(credentials)
+ attr_accessor :config
+
+ def initialize( config )
+ self.config = config
+ AWS.config(config.aws)
AWS.start_memoizing
end
end
View
14 lib/claws/collection/ec2.rb
@@ -5,11 +5,19 @@
module Claws
module Collection
class EC2 < Claws::Collection::Base
- def self.get(filters = {})
+ def get(filters = {})
collection = []
- AWS::EC2.new.instances.each do |instance|
- collection << Claws::EC2::Presenter.new(instance)
+
+ AWS::EC2.new.regions.each do |region|
+ if config.ec2.regions
+ next unless config.ec2.regions.include?(region.name)
+ end
+
+ region.instances.each do |instance|
+ collection << Claws::EC2::Presenter.new(instance)
+ end
end
+
collection
end
end
View
4 lib/claws/command/ec2.rb
@@ -10,10 +10,8 @@ def self.exec(options)
exit 1
end
- Claws::Collection::EC2.connect( config.aws )
-
begin
- instances = Claws::Collection::EC2.get
+ instances = Claws::Collection::EC2.new(config).get
rescue Exception => e
puts e.message
end
View
7 lib/claws/configuration.rb
@@ -19,7 +19,12 @@ def initialize(use_path = nil)
self.capistrano = OpenStruct.new( yaml['capistrano'] )
self.ssh = OpenStruct.new( yaml['ssh'] )
self.aws = yaml['aws']
- self.ec2 = OpenStruct.new( { :fields => yaml['ec2']['fields'] } )
+ self.ec2 = OpenStruct.new(
+ {
+ :fields => yaml['ec2']['fields'],
+ :regions => yaml['ec2']['regions'],
+ }
+ )
end
end
end
View
2  lib/claws/version.rb
@@ -1,3 +1,3 @@
module Claws
- VERSION = '1.0.0'
+ VERSION = '1.1.0'
end
View
BIN  pkg/claws-1.0.0.gem
Binary file not shown
View
4 spec/collection/base_spec.rb
@@ -13,7 +13,7 @@
end
let(:config) do
- double('Claws::Configuration', :aws_credentials => credentials)
+ double('Claws::Configuration', :aws => credentials)
end
it 'establishes a connection to the mothership' do
@@ -21,7 +21,7 @@
AWS.should_receive(:start_memoizing).and_return(nil)
expect {
- subject.connect(config.aws_credentials)
+ subject.new(config)
}.to_not raise_exception
end
end
View
81 spec/collection/ec2_spec.rb
@@ -5,14 +5,81 @@
describe Claws::Collection::EC2 do
subject { Claws::Collection::EC2 }
- it 'gets all instances' do
- subject.should_receive(:get).with(no_args).and_return(
- [
- double('AWS::EC2::Instance'),
- double('AWS::EC2::Instance'),
- ]
+ let(:credentials) do
+ {
+ :access_key_id => 'asdf',
+ :secret_access_key => 'qwer'
+ }
+ end
+
+ let(:regions) do
+ [
+ double('AWS::EC2::Region',
+ :name => 'us-east-1',
+ :instances =>
+ [
+ double('AWS::EC2::Instance'),
+ double('AWS::EC2::Instance'),
+ ],
+ ),
+ double('AWS::EC2::Region',
+ :name => 'eu-east-1',
+ :instances =>
+ [
+ double('AWS::EC2::Instance'),
+ double('AWS::EC2::Instance'),
+ ],
+ ),
+ ]
+ end
+
+ context 'gets all instances in regions' do
+ it 'not defined in configuration' do
+ AWS.should_receive(:config).with(credentials).and_return(true)
+ AWS.should_receive(:start_memoizing).and_return(nil)
+
+ AWS::EC2.should_receive(:new).and_return(
+ double('AWS::EC2::RegionsCollection', :regions => regions)
+ )
+
+ config = double('Claws::Configuration',
+ :aws => credentials,
+ :ec2 => OpenStruct.new({:regions => nil}),
+ )
+
+ subject.new(config).get.size.should == 4
+ end
+
+ it 'defined in configuation' do
+ AWS.should_receive(:config).with(credentials).and_return(true)
+ AWS.should_receive(:start_memoizing).and_return(nil)
+
+ AWS::EC2.should_receive(:new).and_return(
+ double('AWS::EC2::RegionsCollection', :regions => regions)
+ )
+
+ config = double('Claws::Configuration',
+ :aws => credentials,
+ :ec2 => OpenStruct.new({:regions => %w(us-east-1 eu-east-1)}),
+ )
+
+ subject.new(config).get.size.should == 4
+ end
+ end
+
+ it 'gets all instances for specified regions' do
+ AWS.should_receive(:config).with(credentials).and_return(true)
+ AWS.should_receive(:start_memoizing).and_return(nil)
+
+ AWS::EC2.should_receive(:new).and_return(
+ double('AWS::EC2::RegionsCollection', :regions => regions)
+ )
+
+ config = double('Claws::Configuration',
+ :aws => credentials,
+ :ec2 => OpenStruct.new({:regions => %w(us-east-1)}),
)
- subject.get.size.should == 2
+ subject.new(config).get.size.should == 2
end
end
View
47 spec/command/ec2_spec.rb
@@ -50,10 +50,14 @@
let(:options) { OpenStruct.new( { :config_file => nil, } ) }
context 'instance collections' do
-
it 'retrieves' do
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
- Claws::Collection::EC2.should_receive(:get).and_return([])
+ Claws::Collection::EC2.should_receive(:new).and_return(
+ double(Claws::Collection::EC2, :get =>
+ [
+ double(AWS::EC2::Instance, :id => 'test', :status => 'running', :dns_name => 'test.com'),
+ ]
+ )
+ )
capture_stdout {
subject.exec options
@@ -61,9 +65,11 @@
end
it 'handles errors retrieving' do
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
- Claws::Collection::EC2.should_receive(:get).and_raise(Exception)
- subject.should_receive(:puts).once
+ Claws::Collection::EC2.should_receive(:new).and_return(
+ double(Claws::Collection::EC2, :get => Exception.new)
+ )
+
+ #subject.should_receive(:puts).once
expect {
subject.exec options
@@ -72,8 +78,13 @@
end
it 'performs report' do
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
- Claws::Collection::EC2.should_receive(:get).and_return([])
+ Claws::Collection::EC2.should_receive(:new).and_return(
+ double(Claws::Collection::EC2, :get =>
+ [
+ double(AWS::EC2::Instance, :id => 'test', :status => 'running', :dns_name => 'test.com'),
+ ]
+ )
+ )
expect {
capture_stdout {
@@ -114,8 +125,9 @@
end
it 'automatically connects to the server' do
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
- Claws::Collection::EC2.should_receive(:get).and_return(instances)
+ Claws::Collection::EC2.should_receive(:new).and_return(
+ double(Claws::Collection::EC2, :get => instances)
+ )
subject.should_receive(:puts).twice
subject.should_receive(:system).with('ssh test@test.com').and_return(0)
@@ -136,8 +148,9 @@
end
it 'handles user inputed selection from the command line' do
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
- Claws::Collection::EC2.should_receive(:get).and_return(instances)
+ Claws::Collection::EC2.should_receive(:new).and_return(
+ double(Claws::Collection::EC2, :get => instances)
+ )
subject.should_receive(:puts).twice
subject.should_receive(:system).with('ssh test@test2.com').and_return(0)
@@ -149,8 +162,9 @@
end
it 'presents a selection and connects to the server' do
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
- Claws::Collection::EC2.should_receive(:get).and_return(instances)
+ Claws::Collection::EC2.should_receive(:new).and_return(
+ double(Claws::Collection::EC2, :get => instances)
+ )
subject.should_receive(:gets).and_return('1\n')
subject.should_receive(:puts).once
@@ -162,8 +176,9 @@
end
it 'presents a selection and allows a user to quit' do
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
- Claws::Collection::EC2.should_receive(:get).and_return(instances)
+ Claws::Collection::EC2.should_receive(:new).and_return(
+ double(Claws::Collection::EC2, :get => instances)
+ )
subject.should_receive(:gets).and_return('q\n')
View
10 spec/configuration_spec.rb
@@ -13,6 +13,10 @@
'aws_user' => 'test',
},
'ec2' => {
+ 'regions' => [
+ 'us-east-1',
+ 'eu-east-1',
+ ],
'fields' => {
'id' => {
'width' => 10,
@@ -80,6 +84,12 @@
YAML.should_receive(:load_file).and_return(yaml)
end
+ it 'defines regions' do
+ regions = config.ec2.regions
+ regions[0].should == 'us-east-1'
+ regions[1].should == 'eu-east-1'
+ end
+
context 'fields' do
it 'defines id hash' do
id = config.ec2.fields['id']
Please sign in to comment.
Something went wrong with that request. Please try again.