aws-sdk-cloudwatch 0.0.4

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.
@@ -0,0 +1,29 @@
1
+ require 'aws/core/inflection'
2
+ require 'aws/core/option_grammar'
3
+ require 'aws/core/meta_utils'
4
+
5
+ module AWS
6
+ module Core
7
+ class OptionGrammar
8
+ module Descriptors
9
+
10
+ # @private
11
+ module Double
12
+
13
+ extend NoArgs
14
+
15
+ def validate(value, context = nil)
16
+ raise format_error("float value", context) unless
17
+ value.respond_to? :to_f
18
+ end
19
+
20
+ def encode_value(value)
21
+ value.to_s
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
@@ -0,0 +1,27 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/cloud_watch/client'
16
+
17
+ describe AWS::CloudWatch::Client, '.api_config' do
18
+ it 'loads api config from proper directory' do
19
+ expected_dir = Pathname.new(File.dirname(__FILE__)).parent
20
+ expected_file = expected_dir.to_s + '/lib/aws/api_config/CloudWatch-2010-08-01.yml'
21
+
22
+ File.should_receive(:read).with(expected_file).and_return(file = double)
23
+ YAML.should_receive(:load).with(file).and_return(api = double)
24
+
25
+ AWS::CloudWatch::Client.api_config.should == api
26
+ end
27
+ end
@@ -0,0 +1,70 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/cloud_watch/ec2'
16
+
17
+ describe AWS::EC2::Instance, '#metrics' do
18
+ it 'returns the collection of metrics filtered by the instance' do
19
+ inst = AWS::EC2::Instance.new :test_id
20
+ ms = inst.metrics
21
+ ms.kind_of? AWS::CloudWatch::MetricCollection.should be_true
22
+ ms.namespace.should == 'AWS/EC2'
23
+ ms.dimensions[:instance_id].should == :test_id
24
+ end
25
+
26
+ it 'always returns the same instance of the collection' do
27
+ inst = AWS::EC2::Instance.new :test_id
28
+ ms = inst.metrics
29
+ inst.metrics.should === ms
30
+ end
31
+ end
32
+
33
+ describe AWS::EC2::Instance, '#cpu_utilization' do
34
+ before :each do
35
+ @inst = AWS::EC2::Instance.new :some_id
36
+ end
37
+
38
+ it 'uses the proper metrics' do
39
+ @inst.cpu_utilization.name.should == 'CPUUtilization'
40
+ end
41
+
42
+ it 'aggregates by average by default' do
43
+ @inst.should_receive(:metrics).and_return(double :[] => (metric = double))
44
+ metric.should_receive(:average).with(:some, :args).and_return(result = double)
45
+
46
+ @inst.cpu_utilization(:some, :args).should == result
47
+ end
48
+ end
49
+
50
+ describe AWS::EC2::Instance, ' synthetic metric methods' do
51
+ before :each do
52
+ @inst = AWS::EC2::Instance.new :some_id
53
+ @ms = @inst.metrics
54
+ end
55
+
56
+ it 'retrieves the proper metric and aggregates by sum' do
57
+ [:disk_read_ops, :disk_write_ops, :network_out, :disk_read_bytes, :network_in, :disk_write_bytes].each do |name|
58
+ @inst.send(name).name == name
59
+ end
60
+ end
61
+
62
+ it 'aggregates the metric with sum' do
63
+ [:disk_read_ops, :disk_write_ops, :network_out, :disk_read_bytes, :network_in, :disk_write_bytes].each do |name|
64
+ @ms.should_receive(:[]).with(name).and_return(metric = double)
65
+ metric.should_receive(:sum).with(:some, :args).and_return(result = double)
66
+
67
+ @inst.send(name, :some, :args).should == result
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,50 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/cloud_watch/metric_base'
16
+
17
+ describe AWS::CloudWatch::MetricBase do
18
+ it 'puts unknown keys in dimensions when initializing' do
19
+ mb = AWS::CloudWatch::MetricBase.new :name => :foo,
20
+ 'Something' => :bar, :other => :baz
21
+ mb.name.should == :foo
22
+ mb.dimensions[:other].should == :baz
23
+ mb.dimensions['Something'].should == :bar
24
+ end
25
+ end
26
+
27
+ describe AWS::CloudWatch::MetricBase, '#dimensions=' do
28
+ it 'transforms argument into direct hash form' do
29
+ mb = AWS::CloudWatch::MetricBase.new({})
30
+ mb.dimensions = [{:name => 'Test', :value => 'dimension'},
31
+ {:name => :another, :value => :dimension}]
32
+ mb.dimensions.should == {'Test' => 'dimension',
33
+ :another => :dimension}
34
+ end
35
+ end
36
+
37
+ describe AWS::CloudWatch::MetricBase, '#options' do
38
+ it 'folds all parameters into a single hash ready for sending to AWS' do
39
+ mb = AWS::CloudWatch::MetricBase.new :name => :foo,
40
+ 'Something' => :bar, :other => :baz
41
+ options = mb.options
42
+ options.keys.length.should == 2
43
+ options[:metric_name].should == :foo
44
+ dims = options[:dimensions]
45
+ dims.length.should == 2
46
+ [ { :name => 'Something', :value => :bar }, { :name => 'Other', :value => :baz } ].each do |dim|
47
+ dims.include?(dim).should be_true
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,46 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/cloud_watch/metric_collection'
16
+
17
+ describe AWS::CloudWatch::MetricCollection, '#each' do
18
+ it 'paginates through a token' do
19
+ mc = AWS::CloudWatch::MetricCollection.new({})
20
+ client = double("client")
21
+ mc.should_receive(:client).any_number_of_times.and_return(client)
22
+
23
+ names = [:one, :two, :three, :four]
24
+ first_page = names[0..1].map {|n| {:name => n}}
25
+ second_page = names[2..3].map {|n| {:name => n}}
26
+
27
+ client.should_receive(:list_metrics).and_return(stub :metrics => first_page, :data => {:next_token => :a_token})
28
+ client.should_receive(:list_metrics) do |options|
29
+ options[:next_token].should == :a_token
30
+ stub :metrics => second_page, :data => {}
31
+ end
32
+
33
+ mc.each do |metric|
34
+ metric.name.should == names.shift
35
+ end
36
+ end
37
+ end
38
+
39
+ describe AWS::CloudWatch::MetricCollection, '#[]' do
40
+ it 'makes a metric with the given name' do
41
+ mc = AWS::CloudWatch::MetricCollection.new :namespace => :foo
42
+ metric = mc[:some_name]
43
+ metric.name.should == 'SomeName'
44
+ metric.namespace.should == :foo
45
+ end
46
+ end
@@ -0,0 +1,159 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/cloud_watch/metric'
16
+
17
+ describe AWS::CloudWatch::Metric, '#options' do
18
+ it 'also takes the unit' do
19
+ m = AWS::CloudWatch::Metric.new :unit => 'barn'
20
+ m.options[:unit].should == 'barn'
21
+ end
22
+ end
23
+
24
+ describe AWS::CloudWatch::Metric, '#get' do
25
+ before(:each) do
26
+ @metric = AWS::CloudWatch::Metric.new({})
27
+ @metric.should_receive(:client).any_number_of_times.and_return(@client = double)
28
+ @begin = Time.utc(2000, 1, 2, 2, 2, 2)
29
+ @begin_iso = "2000-01-02T02:02:02Z"
30
+ @end = Time.utc(2000, 2, 2, 2, 2, 3)
31
+ @end_iso = "2000-02-02T02:02:03Z"
32
+ end
33
+
34
+ it 'can collect a single statistic' do
35
+ @client.should_receive(:get_metric_statistics).with(\
36
+ :start_time => @begin_iso,
37
+ :end_time => @end_iso,
38
+ :period => 4600,
39
+ :statistics => ["Sum"]
40
+ ).and_return(double :datapoints => [])
41
+ @metric.get(:sum, 4600, @begin..@end)
42
+ end
43
+
44
+ it 'collects one period worth of data if no range is given' do
45
+ @client.should_receive(:get_metric_statistics).with(\
46
+ :start_time => "2000-02-02T01:02:03Z",
47
+ :end_time => @end_iso,
48
+ :period => 60*60, # one hour
49
+ :statistics => ["Sum"]
50
+ ).and_return(double :datapoints => [{:sum => :result}])
51
+ @metric.get(:sum, 60 * 60, @end).should == :result
52
+ end
53
+
54
+ it 'returns nil when no datapoinst are returned' do
55
+ @client.should_receive(:get_metric_statistics).with(\
56
+ :start_time => "2000-02-02T01:02:03Z",
57
+ :end_time => @end_iso,
58
+ :period => 60*60, # one hour
59
+ :statistics => ["Sum"]
60
+ ).and_return(double :datapoints => [])
61
+ @metric.get(:sum, 60 * 60, @end).should be_nil
62
+ end
63
+
64
+ it 'returns stats sorted by timestamp' do
65
+ datapoints = [
66
+ {:timestamp => Time.utc(2001, 4, 2, 5, 2, 0)},
67
+ {:timestamp => Time.utc(2011, 3, 1, 12, 4, 32)},
68
+ {:timestamp => Time.utc(2000, 1, 5, 2, 3, 0)}
69
+ ]
70
+ sorted = [
71
+ {:timestamp => Time.utc(2000, 1, 5, 2, 3, 0)},
72
+ {:timestamp => Time.utc(2001, 4, 2, 5, 2, 0)},
73
+ {:timestamp => Time.utc(2011, 3, 1, 12, 4, 32)}
74
+ ]
75
+
76
+ @client.should_receive(:get_metric_statistics).with(\
77
+ :start_time => @begin_iso,
78
+ :end_time => @end_iso,
79
+ :period => 4600,
80
+ :statistics => ["Sum"]
81
+ ).and_return(double :datapoints => datapoints)
82
+ @metric.get(:sum, 4600, @begin..@end).should == sorted
83
+ end
84
+
85
+ it 'fetches many batches if number of datapoints is too large' do
86
+ _begin = Time.utc(2012, 01, 01, 0, 0, 0)
87
+ _end = Time.utc(2012, 01, 03, 0, 0, 0)
88
+
89
+ @client.should_receive(:get_metric_statistics).with(\
90
+ :start_time => "2012-01-01T00:00:00Z",
91
+ :end_time => "2012-01-02T00:00:00Z",
92
+ :period => 60,
93
+ :statistics => ["Sum"]
94
+ ).and_return(double :datapoints => [1, 2, 3])
95
+ @client.should_receive(:get_metric_statistics).with(\
96
+ :start_time => "2012-01-02T00:00:00Z",
97
+ :end_time => "2012-01-03T00:00:00Z",
98
+ :period => 60,
99
+ :statistics => ["Sum"]
100
+ ).and_return(double :datapoints => [4, 5, 6])
101
+ @metric.get(:sum, 60, _begin.._end).should == [1, 2, 3, 4, 5, 6]
102
+ end
103
+
104
+ it 'handles minute differences in begin and end times gracefully' do
105
+ _begin = Time.utc(2012, 6, 8, 17, 36, 58, 901063)
106
+ _end = Time.utc(2012, 6, 13, 17, 36, 58, 901588)
107
+
108
+ @client.should_receive(:get_metric_statistics).with(\
109
+ :start_time => "2012-06-08T17:36:58Z",
110
+ :end_time => "2012-06-13T17:36:58Z",
111
+ :period => 300,
112
+ :statistics => ["Sum"]
113
+ ).and_return(double :datapoints => [1, 2, 3])
114
+ @client.should_not_receive(:get_metric_statistics).with(\
115
+ :start_time => "2012-06-13T17:36:58Z",
116
+ :end_time => "2012-06-13T17:36:58Z",
117
+ :period => 300,
118
+ :statistics => ["Sum"]
119
+ )
120
+ @metric.get(:sum, 300, _begin.._end).should == [1, 2, 3]
121
+ end
122
+
123
+ it 'returns [] trivially if range is shorter than period' do
124
+ _begin = Time.utc(2012, 6, 8, 17, 36, 58)
125
+ _end = Time.utc(2012, 6, 8, 17, 41, 57)
126
+ @client.should_not_receive:get_metric_statistics
127
+ @metric.get(:sum, 300, _begin.._end).should == []
128
+ end
129
+ end
130
+
131
+ describe AWS::CloudWatch::Metric, " synthetic methods" do
132
+ before(:each) do
133
+ @metric = AWS::CloudWatch::Metric.new({})
134
+ @metric.stub(:client => (@client = double))
135
+ @begin = Time.utc(2000, 2, 2, 2, 2, 2)
136
+ @begin_iso = "2000-02-02T02:02:02Z"
137
+ @end = Time.utc(2000, 2, 2, 4, 2, 3)
138
+ @end_iso = "2000-02-02T04:02:03Z"
139
+ end
140
+
141
+ it "fetches proper statistic and substitutes :value field" do
142
+ stats = {:average => 'Average',
143
+ :sum => 'Sum',
144
+ :sample_count => 'SampleCount',
145
+ :maximum => 'Maximum',
146
+ :minimum => 'Minimum'
147
+ }
148
+
149
+ stats.keys.each do |name|
150
+ @client.should_receive(:get_metric_statistics).with(\
151
+ :start_time => @begin_iso,
152
+ :end_time => @end_iso,
153
+ :period => 60*60, # one hour
154
+ :statistics => [stats[name]]
155
+ ).and_return(double :datapoints => [{name => :result}])
156
+ @metric.send(name, 60 * 60, (@begin..@end))[0][:value].should == :result
157
+ end
158
+ end
159
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-sdk-cloudwatch
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
11
+ platform: ruby
12
+ authors:
13
+ - "Rafa\xC5\x82 Rzepecki"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-15 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: aws-sdk
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 9
29
+ segments:
30
+ - 1
31
+ - 3
32
+ version: "1.3"
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ hash: 9
36
+ segments:
37
+ - 1
38
+ - 3
39
+ - 9
40
+ version: 1.3.9
41
+ type: :runtime
42
+ version_requirements: *id001
43
+ - !ruby/object:Gem::Dependency
44
+ name: bundler
45
+ prerelease: false
46
+ requirement: &id002 !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ~>
50
+ - !ruby/object:Gem::Version
51
+ hash: 15
52
+ segments:
53
+ - 1
54
+ - 0
55
+ version: "1.0"
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 9
59
+ segments:
60
+ - 1
61
+ - 0
62
+ - 15
63
+ version: 1.0.15
64
+ type: :development
65
+ version_requirements: *id002
66
+ - !ruby/object:Gem::Dependency
67
+ name: rspec
68
+ prerelease: false
69
+ requirement: &id003 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 23
75
+ segments:
76
+ - 2
77
+ - 10
78
+ version: "2.10"
79
+ type: :development
80
+ version_requirements: *id003
81
+ description: Extends the original aws-sdk gem from Amazon with support for the CloudWatch API
82
+ email:
83
+ - divided.mind@gmail.com
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files: []
89
+
90
+ files:
91
+ - .gitignore
92
+ - CHANGELOG
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - NOTICE.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - aws-sdk-cloudwatch.gemspec
99
+ - lib/aws/api_config/CloudWatch-2010-08-01.yml
100
+ - lib/aws/cloud_watch.rb
101
+ - lib/aws/cloud_watch/client.rb
102
+ - lib/aws/cloud_watch/config.rb
103
+ - lib/aws/cloud_watch/ec2.rb
104
+ - lib/aws/cloud_watch/errors.rb
105
+ - lib/aws/cloud_watch/metric.rb
106
+ - lib/aws/cloud_watch/metric_base.rb
107
+ - lib/aws/cloud_watch/metric_collection.rb
108
+ - lib/aws/cloud_watch/request.rb
109
+ - lib/aws/core/option_grammar/double.rb
110
+ - licence.rb
111
+ - spec/cloud_watch_client_spec.rb
112
+ - spec/cloud_watch_ec2_spec.rb
113
+ - spec/metric_base_spec.rb
114
+ - spec/metric_collection_spec.rb
115
+ - spec/metric_spec.rb
116
+ homepage: https://github.com/inscitiv/aws-sdk-cloudwatch
117
+ licenses: []
118
+
119
+ post_install_message:
120
+ rdoc_options: []
121
+
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ hash: 3
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ requirements: []
143
+
144
+ rubyforge_project:
145
+ rubygems_version: 1.8.24
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: AWS CloudWatch API support
149
+ test_files:
150
+ - spec/cloud_watch_client_spec.rb
151
+ - spec/cloud_watch_ec2_spec.rb
152
+ - spec/metric_base_spec.rb
153
+ - spec/metric_collection_spec.rb
154
+ - spec/metric_spec.rb