mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
monitor / unmonitor for AWS
This commit is contained in:
parent
2e2c6dd183
commit
87b5a30e19
8 changed files with 235 additions and 24 deletions
|
@ -69,6 +69,8 @@ module Fog
|
||||||
request :terminate_instances
|
request :terminate_instances
|
||||||
request :start_instances
|
request :start_instances
|
||||||
request :stop_instances
|
request :stop_instances
|
||||||
|
request :monitor_instances
|
||||||
|
request :unmonitor_instances
|
||||||
|
|
||||||
class Mock
|
class Mock
|
||||||
|
|
||||||
|
@ -134,8 +136,9 @@ module Fog
|
||||||
|
|
||||||
@aws_access_key_id = options[:aws_access_key_id]
|
@aws_access_key_id = options[:aws_access_key_id]
|
||||||
@region = options[:region] || 'us-east-1'
|
@region = options[:region] || 'us-east-1'
|
||||||
@owner_id = @data[:owner_id]
|
|
||||||
reset_data
|
reset_data
|
||||||
|
@owner_id = @data[:owner_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_data
|
def reset_data
|
||||||
|
|
|
@ -215,6 +215,29 @@ module Fog
|
||||||
connection.volumes(:server => self)
|
connection.volumes(:server => self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#I tried to call it monitoring= and be smart with attributes[]
|
||||||
|
#but in #save a merge_attribute is called after run_instance
|
||||||
|
#thus making an un-necessary request. Use this until finding a clever solution
|
||||||
|
def monitor=(boolean)
|
||||||
|
|
||||||
|
#we don't have a server yet. the status silently goes in the attributes for run_instances
|
||||||
|
if !identity
|
||||||
|
self.monitoring=boolean
|
||||||
|
end
|
||||||
|
|
||||||
|
case boolean
|
||||||
|
when true
|
||||||
|
response = connection.monitor_instances(identity)
|
||||||
|
when false
|
||||||
|
response = connection.unmonitor_instances(identity)
|
||||||
|
else
|
||||||
|
raise ArgumentError.new("only Boolean allowed here")
|
||||||
|
end
|
||||||
|
|
||||||
|
#set the attribute
|
||||||
|
response[identity] == 'enabled' ? self.monitoring=true : self.monitoring=false
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
29
lib/fog/compute/parsers/aws/monitor_unmonitor_instances.rb
Normal file
29
lib/fog/compute/parsers/aws/monitor_unmonitor_instances.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
module Fog
|
||||||
|
module Parsers
|
||||||
|
module AWS
|
||||||
|
module Compute
|
||||||
|
|
||||||
|
class MonitorUnmonitorInstances < Fog::Parsers::Base
|
||||||
|
|
||||||
|
def reset
|
||||||
|
@response = {}
|
||||||
|
@current_id = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def end_element(name)
|
||||||
|
case name
|
||||||
|
when 'instanceId'
|
||||||
|
@current_id = @value
|
||||||
|
when 'item'
|
||||||
|
@current_id = nil
|
||||||
|
when 'state'
|
||||||
|
@response[@current_id] = @value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
43
lib/fog/compute/requests/aws/monitor_instances.rb
Normal file
43
lib/fog/compute/requests/aws/monitor_instances.rb
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
module Fog
|
||||||
|
module AWS
|
||||||
|
class Compute
|
||||||
|
|
||||||
|
class Real
|
||||||
|
|
||||||
|
require 'fog/compute/parsers/aws/monitor_unmonitor_instances'
|
||||||
|
|
||||||
|
# Monitor specified instance
|
||||||
|
# http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-MonitorInstances.html
|
||||||
|
#
|
||||||
|
# ==== Parameters
|
||||||
|
# * instance_ids<~Array> - Arrays of instances Ids to monitor
|
||||||
|
#
|
||||||
|
# ==== Returns
|
||||||
|
# * response<~Excon::Response>:
|
||||||
|
# * body<~Hash>:
|
||||||
|
# * 'requestId'<~String> - Id of request
|
||||||
|
# * 'instancesSet': http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-MonitorInstancesResponseSetItemType.html
|
||||||
|
def monitor_instances(instance_ids)
|
||||||
|
params = AWS.indexed_param('InstanceId', instance_ids)
|
||||||
|
request({
|
||||||
|
'Action' => 'MonitorInstances',
|
||||||
|
:idempotent => true,
|
||||||
|
:parser => Fog::Parsers::AWS::Compute::MonitorUnmonitorInstances.new
|
||||||
|
}.merge!(params))
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
class Mock
|
||||||
|
|
||||||
|
def monitor_instances(instance_ids)
|
||||||
|
response = Excon::Response.new
|
||||||
|
response.status = 200
|
||||||
|
response.body = instance_ids.inject({}) { |memo, id| {memo[id] => 'enabled'} }
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
44
lib/fog/compute/requests/aws/unmonitor_instances.rb
Normal file
44
lib/fog/compute/requests/aws/unmonitor_instances.rb
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
module Fog
|
||||||
|
module AWS
|
||||||
|
class Compute
|
||||||
|
|
||||||
|
class Real
|
||||||
|
|
||||||
|
require 'fog/compute/parsers/aws/monitor_unmonitor_instances'
|
||||||
|
|
||||||
|
# UnMonitor specified instance
|
||||||
|
# http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-UnmonitorInstances.html
|
||||||
|
#
|
||||||
|
# ==== Parameters
|
||||||
|
# * instance_ids<~Array> - Arrays of instances Ids to monitor
|
||||||
|
#
|
||||||
|
# ==== Returns
|
||||||
|
# * response<~Excon::Response>:
|
||||||
|
# * body<~Hash>:
|
||||||
|
# * 'requestId'<~String> - Id of request
|
||||||
|
# * 'instancesSet': http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-MonitorInstancesResponseSetItemType.html
|
||||||
|
def unmonitor_instances(instance_ids)
|
||||||
|
params = AWS.indexed_param('InstanceId', instance_ids)
|
||||||
|
request({
|
||||||
|
'Action' => 'UnmonitorInstances',
|
||||||
|
:idempotent => true,
|
||||||
|
:parser => Fog::Parsers::AWS::Compute::MonitorUnmonitorInstances.new
|
||||||
|
}.merge!(params))
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
class Mock
|
||||||
|
|
||||||
|
def unmonitor_instances(instance_ids)
|
||||||
|
response = Excon::Response.new
|
||||||
|
response.status = 200
|
||||||
|
response.body = instance_ids.inject({}) { |memo, id| {memo[id] => 'disabled'} }
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
45
tests/compute/models/aws/server_monitor_tests.rb
Normal file
45
tests/compute/models/aws/server_monitor_tests.rb
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
Shindo.tests("AWS::Compute::Server | monitor", ['aws']) do
|
||||||
|
|
||||||
|
config = compute_providers[AWS]
|
||||||
|
|
||||||
|
if !Fog.mocking? || config[:mocked]
|
||||||
|
@instance = AWS[:compute].servers.new(config[:server_attributes])
|
||||||
|
end
|
||||||
|
|
||||||
|
tests('new instance') do
|
||||||
|
|
||||||
|
test('monitor') do
|
||||||
|
@instance.monitoring = true
|
||||||
|
@instance.attributes[:monitoring] == true
|
||||||
|
end
|
||||||
|
|
||||||
|
test('unmonitor') do
|
||||||
|
@instance.monitoring = false
|
||||||
|
@instance.attributes[:monitoring] == false
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
tests('existing instance') do
|
||||||
|
|
||||||
|
@instance.save
|
||||||
|
|
||||||
|
test('monitor') do
|
||||||
|
#what I'd really like to test is if connection.monitor_instances is being called
|
||||||
|
#this is a safeguard
|
||||||
|
@instance.identity != nil
|
||||||
|
@instance.monitoring = true
|
||||||
|
@instance.attributes[:monitoring] == true
|
||||||
|
end
|
||||||
|
|
||||||
|
test('unmonitor') do
|
||||||
|
#what I'd really like to test is if connection.monitor_instances is being called
|
||||||
|
#this is a safeguard
|
||||||
|
@instance.identity != nil
|
||||||
|
@instance.monitoring = false
|
||||||
|
@instance.attributes[:monitoring] == false
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
|
@ -4,29 +4,13 @@ for provider, config in compute_providers
|
||||||
|
|
||||||
server_tests(provider[:compute], (config[:server_attributes] || {}), config[:mocked]) do
|
server_tests(provider[:compute], (config[:server_attributes] || {}), config[:mocked]) do
|
||||||
|
|
||||||
tests('responds_to(:bootstrap)') do
|
if Fog.mocking? && !config[:mocked]
|
||||||
pending if Fog.mocking? && !config[:mocked]
|
pending
|
||||||
@instance.responds_to(:bootstrap)
|
else
|
||||||
end
|
responds_to(:boostrap)
|
||||||
|
responds_to(:public_ip_address)
|
||||||
tests('responds_to(:private_ip_address)') do
|
responds_to(:scp)
|
||||||
pending if Fog.mocking? && !config[:mocked]
|
responds_to(:ssh)
|
||||||
@instance.responds_to(:public_ip_address)
|
|
||||||
end
|
|
||||||
|
|
||||||
tests('responds_to(:public_ip_address)') do
|
|
||||||
pending if Fog.mocking? && !config[:mocked]
|
|
||||||
@instance.responds_to(:public_ip_address)
|
|
||||||
end
|
|
||||||
|
|
||||||
tests('responds_to(:scp)') do
|
|
||||||
pending if Fog.mocking? && !config[:mocked]
|
|
||||||
@instance.responds_to(:ssh)
|
|
||||||
end
|
|
||||||
|
|
||||||
tests('responds_to(:ssh)') do
|
|
||||||
pending if Fog.mocking? && !config[:mocked]
|
|
||||||
@instance.responds_to(:ssh)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
require 'fog/compute/parsers/aws/monitor_unmonitor_instances'
|
||||||
|
|
||||||
|
Shindo.tests('AWS::Compute::MonitorUnmonitorInstances | monitor/unmonitor parser') do
|
||||||
|
|
||||||
|
tests('success') do
|
||||||
|
|
||||||
|
tests('#parse') do
|
||||||
|
|
||||||
|
aws_result = <<-AWS
|
||||||
|
<UnmonitorInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2010-11-15/">
|
||||||
|
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
|
||||||
|
<instancesSet>
|
||||||
|
<item>
|
||||||
|
<instanceId>i-43a4412a</instanceId>
|
||||||
|
<monitoring>
|
||||||
|
<state>enabled</state>
|
||||||
|
</monitoring>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<instanceId>i-23a3397d</instanceId>
|
||||||
|
<monitoring>
|
||||||
|
<state>disabled</state>
|
||||||
|
</monitoring>
|
||||||
|
</item>
|
||||||
|
</instancesSet>
|
||||||
|
</UnmonitorInstancesResponse>
|
||||||
|
AWS
|
||||||
|
|
||||||
|
parser = Fog::Parsers::AWS::Compute::MonitorUnmonitorInstances.new
|
||||||
|
body = Nokogiri::XML::SAX::PushParser.new(parser)
|
||||||
|
body << aws_result
|
||||||
|
|
||||||
|
test('enabled') { parser.response['i-43a4412a'] == 'enabled' }
|
||||||
|
test('disabled') { parser.response['i-23a3397d'] == 'disabled' }
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue