misfo-aws-sdk-cloudwatch 0.0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
data/licence.rb ADDED
@@ -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,35 @@
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
+ # -*- encoding: utf-8 -*-
16
+ $:.push File.expand_path("../lib", __FILE__)
17
+
18
+ Gem::Specification.new do |s|
19
+ s.name = "misfo-aws-sdk-cloudwatch"
20
+ s.version = '0.0.4.1'
21
+ s.authors = ["Rafał Rzepecki"]
22
+ s.email = ["divided.mind@gmail.com"]
23
+ s.homepage = "https://github.com/inscitiv/aws-sdk-cloudwatch"
24
+ s.summary = 'AWS CloudWatch API support'
25
+ s.description = 'Extends the original aws-sdk gem from Amazon with support for the CloudWatch API'
26
+
27
+ s.files = `git ls-files`.split("\n") - ['aws-sdk-cloudwatch.gemspec'] + ['misfo-aws-sdk-cloudwatch.gemspec']
28
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
29
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
30
+ s.require_paths = ["lib"]
31
+
32
+ s.add_runtime_dependency 'aws-sdk', '~> 1.3', '>= 1.3.9'
33
+ s.add_development_dependency 'bundler', '~> 1.0', '>= 1.0.15'
34
+ s.add_development_dependency 'rspec', '~> 2.10'
35
+ end
@@ -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,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: misfo-aws-sdk-cloudwatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rafał Rzepecki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-16 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: aws-sdk
16
+ requirement: &70365066289780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: 1.3.9
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: *70365066289780
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: &70365066288560 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: '1.0'
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: 1.0.15
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: *70365066288560
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: &70365066286560 !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ version: '2.10'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: *70365066286560
53
+ description: Extends the original aws-sdk gem from Amazon with support for the CloudWatch
54
+ API
55
+ email:
56
+ - divided.mind@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - .gitignore
62
+ - CHANGELOG
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - NOTICE.txt
66
+ - README.rdoc
67
+ - Rakefile
68
+ - lib/aws/api_config/CloudWatch-2010-08-01.yml
69
+ - lib/aws/cloud_watch.rb
70
+ - lib/aws/cloud_watch/client.rb
71
+ - lib/aws/cloud_watch/config.rb
72
+ - lib/aws/cloud_watch/ec2.rb
73
+ - lib/aws/cloud_watch/errors.rb
74
+ - lib/aws/cloud_watch/metric.rb
75
+ - lib/aws/cloud_watch/metric_base.rb
76
+ - lib/aws/cloud_watch/metric_collection.rb
77
+ - lib/aws/cloud_watch/request.rb
78
+ - lib/aws/core/option_grammar/double.rb
79
+ - licence.rb
80
+ - spec/cloud_watch_client_spec.rb
81
+ - spec/cloud_watch_ec2_spec.rb
82
+ - spec/metric_base_spec.rb
83
+ - spec/metric_collection_spec.rb
84
+ - spec/metric_spec.rb
85
+ - misfo-aws-sdk-cloudwatch.gemspec
86
+ homepage: https://github.com/inscitiv/aws-sdk-cloudwatch
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.10
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: AWS CloudWatch API support
110
+ test_files:
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
+ has_rdoc: