fog_tracker 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile CHANGED
@@ -1,8 +1,9 @@
1
1
  # A sample Guardfile
2
2
  # More info at https://github.com/guard/guard#readme
3
3
 
4
- guard 'rspec', :version => 2, :cli => "--color --format documentation" do
5
- watch(%r{^spec/.+_spec\.rb$})
6
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
- watch('spec/spec_helper.rb') { "spec" }
4
+ guard 'rspec', :version => 2,
5
+ :cli => "--color --format documentation -r ./spec/spec_helper.rb" do
6
+ watch(%r{^spec/.+_spec\.rb$})
7
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
8
+ watch('spec/spec_helper.rb') { "spec" }
8
9
  end
data/README.md CHANGED
@@ -68,25 +68,28 @@ How is it [done]? (Usage)
68
68
  # get all S3 objects in a given account
69
69
  tracker["my production account::Storage::AWS::files"]
70
70
 
71
- ----------------
72
- *Usage Tips*
71
+ ----------------
72
+ *Usage Tips*
73
73
 
74
- Instead of calling `each` on the results of every query, you can pass a single-argument block, and it will be invoked once with each resulting resource:
74
+ * Instead of calling `each` on the results of every query, you can pass a single-argument block, and it will be invoked once with each resulting resource:
75
75
 
76
- tracker.query("*::*::*::*"){|r| puts "Found #{r.class} #{r.identity}"}
76
+ tracker.query("*::*::*::*"){|r| puts "Found #{r.class} #{r.identity}"}
77
77
 
78
- You can also pass a Proc to the Tracker at initialization, which will be invoked whenever an account's Resources have been updated. It should accept an Array containing the updated Resources as its first argument:
78
+ * You can also pass a Proc to the Tracker at initialization, which will be invoked whenever an account's Resources have been updated. It should accept an Array containing the updated Resources as its first argument:
79
79
 
80
- FogTracker::Tracker.new(YAML::load(File.read 'accounts.yml'),
81
- :callback => Proc.new do |resources|
82
- puts "Got #{resources.count} resources from account "+
83
- resources.first.tracker_account[:name]
84
- end
85
- ).start
80
+ FogTracker::Tracker.new(YAML::load(File.read 'accounts.yml'),
81
+ :callback => Proc.new do |resources|
82
+ puts "Got #{resources.count} resources from account "+
83
+ resources.first.tracker_account[:name]
84
+ end
85
+ ).start
86
86
 
87
- To get a Resource's Hash of account information, call its `tracker_account` method _(credentials are removed)_.
87
+ * The resources returned from a query are all Fog::Model objects, but they are "decorated" with some extra methods for your convenience. This simplifies the code that consumes the resources, because it does not have to know anything about the tracker. Here are the methods added:
88
+ 1. To get a Resource's Hash of account information, call its `tracker_account` method _(credentials are removed)_.
89
+ 2. To query for more resources, you can call `resource.tracker_query(query_string)`, though you cannot yet pass a block to this method.
90
+ 3. To get a collection of resources from the same account, call `resource.account_resources(collection_name)`.
88
91
 
89
- Any Exceptions that occur in the Tracker's polling threads are rescued and logged. If you want to take further action, you can initialize the Tracker with an `:error_callback` Proc, similar to the Account update `:callback` -- except that the `:error_callback` should accept an Exception instead of an Array of Resources.
92
+ * Any Exceptions that occur in the Tracker's polling threads are rescued and logged. If you want to take further action, you can initialize the Tracker with an `:error_callback` Proc, similar to the Account update `:callback` -- except that the `:error_callback` should accept an Exception instead of an Array of Resources.
90
93
 
91
94
 
92
95
  ----------------
@@ -4,10 +4,10 @@
4
4
  # Uses the Fog gem to track the status of cloud computing resources
5
5
  #
6
6
  # == Usage
7
- # tracker.rb [options] ACCOUNTS_CONFIG_FILE.YML
7
+ # fog_tracker.rb [options] ACCOUNTS_CONFIG_FILE.YML
8
8
  #
9
9
  # == Options (all options can be put into the config file)
10
- # -d, --delay [INTEGER] Seconds between status updates. default = 180
10
+ # -d, --delay [INTEGER] Seconds between status updates.
11
11
  # -l, --log-level [LEVEL] Sets Log4r level for console output. default = INFO
12
12
  # -h, --help Displays help message
13
13
  #
@@ -18,7 +18,7 @@ module FogTracker
18
18
  # @param [Hash] options optional additional parameters:
19
19
  # - :delay (Integer) - Default time between polling of accounts
20
20
  # - :callback (Proc) - A Method or Proc to call each time an account is polled.
21
- # (should take the name of the account as its only required parameter)
21
+ # (should take an Array of resources as its only required parameter)
22
22
  # - :error_callback (Proc) - A Method or Proc to call if polling errors occur.
23
23
  # (should take a single Exception as its only required parameter)
24
24
  # - :logger - a Ruby Logger-compatible object
@@ -29,6 +29,7 @@ module FogTracker
29
29
  @log = options[:logger] || FogTracker.default_logger
30
30
  @delay = options[:delay] || account[:polling_time] ||
31
31
  FogTracker::DEFAULT_POLLING_TIME
32
+ @account[:polling_time] = @delay # TODO - test this
32
33
  @error_proc = options[:error_callback]
33
34
  @log.debug "Creating tracker for account #{@name}."
34
35
  create_collection_trackers
@@ -5,7 +5,7 @@ module FogTracker
5
5
  class CollectionTracker
6
6
 
7
7
  # An Array of Fog::Model objects, all of the same resource type (class)
8
- attr_accessor :collection
8
+ attr_reader :collection
9
9
 
10
10
  # Creates an object for tracking a single Fog collection in a single account
11
11
  # @param [String] resource_type the Fog collection name for this resource type
@@ -0,0 +1,46 @@
1
+ module FogTracker
2
+ module Extensions
3
+ # Adds convenience methods to Fog::Model instances for gathering
4
+ # information about its account, and about other Fog::Model resources
5
+ module FogModel
6
+ extend Forwardable # Resources need to be queriable
7
+
8
+ # a FogTracker::CollectionTracker - *do not modify* - used for {#tracker_account}
9
+ attr_accessor :_fog_collection_tracker
10
+
11
+ # a FogTracker::QueryParser - *do not modify* - used for tracker_query
12
+ attr_accessor :_query_processor
13
+ def_delegator :@_query_processor, :execute, :tracker_query
14
+
15
+ # Returns a cleaned copy of the resource's account information
16
+ # from the its collection tracker (credentials are removed).
17
+ # @return [Hash] a cleaned copy of the resource's account information
18
+ def tracker_account
19
+ (not _fog_collection_tracker) ? Hash.new :
20
+ _fog_collection_tracker.clean_account_data
21
+ end
22
+
23
+ # Returns Fog::Model resources from this Resource's account only
24
+ # @param [String] collection_name a String which is converted to
25
+ # a RegEx, and used to match collection names for resources
26
+ # in the same account as the current resource.
27
+ # @return [Array <Fog::Model>] an Array of resources from this Model's
28
+ # accout, whose collection matches collection_name.
29
+ def account_resources(collection_name)
30
+ (not @_query_processor) ? Array.new :
31
+ @_query_processor.execute(
32
+ "#{tracker_account[:name]}::"+
33
+ "#{tracker_account[:service]}::"+
34
+ "#{tracker_account[:provider]}::"+
35
+ "#{collection_name}"
36
+ )
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ module Fog
43
+ class Model
44
+ include FogTracker::Extensions::FogModel
45
+ end
46
+ end
@@ -16,7 +16,7 @@ module FogTracker
16
16
  @log = options[:logger] || FogTracker.default_logger
17
17
  end
18
18
 
19
- # Uses the query string to filter this account's resources
19
+ # Uses the query string to filter this account's resources
20
20
  # for a desired subset
21
21
  # @param [String] query a string used to filter for matching resources
22
22
  # @return [Array <Fog::Model>] an Array of Resources, filtered by query
@@ -24,7 +24,9 @@ module FogTracker
24
24
  acct_pattern, svc_pattern, prov_pattern, col_pattern = parse_query(query)
25
25
  filter_by_collection(
26
26
  filter_by_provider(
27
- filter_by_service(get_results_by_account(acct_pattern), svc_pattern),
27
+ filter_by_service(
28
+ attach_query_methods(get_results_by_account(acct_pattern)),
29
+ svc_pattern),
28
30
  prov_pattern),
29
31
  col_pattern)
30
32
  end
@@ -89,6 +91,12 @@ module FogTracker
89
91
  end
90
92
  end
91
93
 
94
+ # adds the _tracker_query_parser attribute to all resources
95
+ def attach_query_methods(resources)
96
+ resources.each {|resource| resource._query_processor = self}
97
+ resources
98
+ end
99
+
92
100
  end
93
101
  end
94
102
  end
@@ -12,7 +12,7 @@ module FogTracker
12
12
  # @param [Hash] options optional additional parameters:
13
13
  # - :delay (Integer) - Default time between polling of accounts
14
14
  # - :callback (Proc) - A Method or Proc to call each time an account is polled.
15
- # (should take the name of the account as its only required parameter)
15
+ # (should take an Array of resources as its only required parameter)
16
16
  # - :error_callback (Proc) - A Method or Proc to call if polling errors occur.
17
17
  # (should take a single Exception as its only required parameter)
18
18
  # - :logger - a Ruby Logger-compatible object
@@ -56,12 +56,12 @@ module FogTracker
56
56
 
57
57
  # Returns an Array of resource types for a given account
58
58
  # @param [String] name the name of the account
59
- # @return [Array<String>] an array of Resource types
59
+ # @return [Array<String>] an array of Resource types
60
60
  def types_for_account(account_name)
61
61
  @trackers[account_name].tracked_types
62
62
  end
63
63
 
64
- # Returns an array of Resources matching the {query_string}.
64
+ # Returns an array of Resources matching the query_string.
65
65
  # Calls any block passed for each resulting resource.
66
66
  # @param [String] query_string a string used to filter for matching resources
67
67
  # it might look like: "Account Name::Compute::AWS::servers"
@@ -1,3 +1,3 @@
1
1
  module FogTracker
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module FogTracker
4
2
 
5
3
  describe AccountTracker do
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module FogTracker
4
2
 
5
3
  describe CollectionTracker do
@@ -0,0 +1,54 @@
1
+ module FogTracker
2
+ module Extensions
3
+ describe FogModel do
4
+
5
+ before(:each) do
6
+ @model = FAKE_AWS.servers.new
7
+ end
8
+
9
+ describe '#tracker_account' do
10
+ context "with no collection tracker assigned" do
11
+ it "returns an empty Hash" do
12
+ @model.tracker_account.should == Hash.new
13
+ end
14
+ end
15
+ context "with a collection tracker assigned" do
16
+ before(:each) do
17
+ @account_data = { # some sample account data
18
+ :name => 'fake account name',
19
+ :provider => 'AWS', :service => 'Compute'
20
+ }
21
+ @fake_tracker = double "mock account tracker"
22
+ @fake_tracker.stub(:clean_account_data).and_return(@account_data)
23
+ @model._fog_collection_tracker = @fake_tracker
24
+ end
25
+ it "returns a Hash of the resource's acccount data" do
26
+ @fake_tracker.should_receive(:clean_account_data)
27
+ @model.tracker_account.should == @account_data
28
+ end
29
+ end
30
+ end
31
+
32
+ describe '#tracker_query' do
33
+ context "with no query processor assigned" do
34
+ it "raises a NoMethodError" do
35
+ q = Proc.new { @model.tracker_query('XXX') }
36
+ q.should raise_error
37
+ end
38
+ end
39
+ context "with a query processor assigned" do
40
+ before(:each) do
41
+ @fake_processor = double "mock query processor"
42
+ @fake_processor.stub(:query).and_return(Array.new)
43
+ @model._query_processor = @fake_processor
44
+ end
45
+ it "forwards the query to its query processor" do
46
+ @fake_processor.should_receive(:execute).with('XXX')
47
+ @model.tracker_query('XXX')
48
+ end
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module FogTracker
4
2
  module Query
5
3
 
@@ -90,9 +88,15 @@ module FogTracker
90
88
  true
91
89
  end
92
90
  end
93
-
91
+ it "assigns itself to @_query_processor on all resoruces" do
92
+ @processor.execute(QUERY['matching all Resources']).each do |resource|
93
+ resource._query_processor.should == @processor
94
+ end
95
+ end
94
96
  end
97
+
95
98
  end
96
99
  end
100
+
97
101
  end
98
102
  end
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  module FogTracker
4
2
  describe Tracker do
5
3
 
@@ -18,6 +18,11 @@ module FogTracker
18
18
  ],
19
19
  }
20
20
  FAKE_ACCOUNTS = {FAKE_ACCOUNT_NAME => FAKE_ACCOUNT}
21
+ FAKE_AWS = Fog::Compute.new(
22
+ :provider => 'AWS',
23
+ :aws_access_key_id => FAKE_ACCOUNT[:credentials][:aws_access_key_id],
24
+ :aws_secret_access_key => FAKE_ACCOUNT[:credentials][:aws_secret_access_key],
25
+ )
21
26
  module Query
22
27
  QUERY = {} # Used in query_processor_spec.rb
23
28
  end
@@ -31,8 +36,9 @@ module Fog
31
36
  (1..NUMBER_OF_FAKE_RESOURCE_TYPES).each do |class_index|
32
37
  eval(%Q{
33
38
  class FakeCollectionType#{class_index} ; end
34
- class FakeResourceType#{class_index}
39
+ class FakeResourceType#{class_index} < Fog::Model
35
40
  def collection ; FakeCollectionType#{class_index}.new end
41
+ def identity; "Fake_ID_for_Type#{class_index} " end
36
42
  end
37
43
  })
38
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fog_tracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-29 00:00:00.000000000Z
12
+ date: 2012-01-31 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fog
16
- requirement: &70093896997020 !ruby/object:Gem::Requirement
16
+ requirement: &70122132638240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70093896997020
24
+ version_requirements: *70122132638240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70093896996120 !ruby/object:Gem::Requirement
27
+ requirement: &70122132637400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70093896996120
35
+ version_requirements: *70122132637400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70093896994520 !ruby/object:Gem::Requirement
38
+ requirement: &70122132636520 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70093896994520
46
+ version_requirements: *70122132636520
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yard
49
- requirement: &70093896993060 !ruby/object:Gem::Requirement
49
+ requirement: &70122132635680 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70093896993060
57
+ version_requirements: *70122132635680
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: guard
60
- requirement: &70093896991540 !ruby/object:Gem::Requirement
60
+ requirement: &70122132634960 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70093896991540
68
+ version_requirements: *70122132634960
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-rspec
71
- requirement: &70093896989420 !ruby/object:Gem::Requirement
71
+ requirement: &70122132634360 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70093896989420
79
+ version_requirements: *70122132634360
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: ruby_gntp
82
- requirement: &70093896987000 !ruby/object:Gem::Requirement
82
+ requirement: &70122132633680 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,13 +87,13 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70093896987000
90
+ version_requirements: *70122132633680
91
91
  description: This gem peridically polls mutiple cloud computing services using the
92
92
  fog gem, asynchronously updating the state of the resulting collections of Fog Resources.
93
93
  email:
94
94
  - benton@bentonroberts.com
95
95
  executables:
96
- - tracker
96
+ - fog_tracker
97
97
  extensions: []
98
98
  extra_rdoc_files: []
99
99
  files:
@@ -103,21 +103,22 @@ files:
103
103
  - Guardfile
104
104
  - README.md
105
105
  - Rakefile
106
- - bin/tracker
106
+ - bin/fog_tracker
107
107
  - config/accounts.yml.example
108
108
  - fog_tracker.gemspec
109
- - lib/fog/core/model.rb
110
109
  - lib/fog_tracker.rb
111
110
  - lib/fog_tracker/account_tracker.rb
112
111
  - lib/fog_tracker/collection_tracker.rb
112
+ - lib/fog_tracker/extensions/fog_model.rb
113
113
  - lib/fog_tracker/query/query_processor.rb
114
114
  - lib/fog_tracker/tracker.rb
115
115
  - lib/fog_tracker/util/string_underscore.rb
116
116
  - lib/fog_tracker/version.rb
117
117
  - lib/tasks/rspec.rake
118
118
  - spec/lib/fog_tracker/account_tracker_spec.rb
119
+ - spec/lib/fog_tracker/collection_tracker_spec.rb
120
+ - spec/lib/fog_tracker/extensions/fog_model_spec.rb
119
121
  - spec/lib/fog_tracker/query/query_processor_spec.rb
120
- - spec/lib/fog_tracker/resource_tracker_spec.rb
121
122
  - spec/lib/fog_tracker/tracker_spec.rb
122
123
  - spec/spec_helper.rb
123
124
  - spec/support/_configure_logging.rb
@@ -137,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
138
  version: '0'
138
139
  segments:
139
140
  - 0
140
- hash: 589883015526840149
141
+ hash: -3623635241096457723
141
142
  required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  none: false
143
144
  requirements:
@@ -146,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
147
  version: '0'
147
148
  segments:
148
149
  - 0
149
- hash: 589883015526840149
150
+ hash: -3623635241096457723
150
151
  requirements: []
151
152
  rubyforge_project: fog_tracker
152
153
  rubygems_version: 1.8.10
@@ -156,8 +157,9 @@ summary: Tracks the state of cloud computing resources across multiple accounts
156
157
  multiple service providers
157
158
  test_files:
158
159
  - spec/lib/fog_tracker/account_tracker_spec.rb
160
+ - spec/lib/fog_tracker/collection_tracker_spec.rb
161
+ - spec/lib/fog_tracker/extensions/fog_model_spec.rb
159
162
  - spec/lib/fog_tracker/query/query_processor_spec.rb
160
- - spec/lib/fog_tracker/resource_tracker_spec.rb
161
163
  - spec/lib/fog_tracker/tracker_spec.rb
162
164
  - spec/spec_helper.rb
163
165
  - spec/support/_configure_logging.rb
@@ -1,21 +0,0 @@
1
- module Fog
2
-
3
- # Adds an accessor and a method to decorate Fog::Model instances
4
- # with tracker account information
5
- class Model
6
-
7
- # a FogTracker::CollectionTracker
8
- attr_accessor :_fog_collection_tracker
9
-
10
- # Returns a cleaned copy of the resource's account information
11
- # from the its collection tracker (credentials are removed).
12
- def tracker_account
13
- if _fog_collection_tracker
14
- _fog_collection_tracker.clean_account_data
15
- else
16
- Hash.new
17
- end
18
- end
19
-
20
- end
21
- end