mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #472 from panthomakos/master
Added a fast way to add your current machine to an RDS security group.
This commit is contained in:
commit
58abbf62ba
3 changed files with 71 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
require 'fog/core/model'
|
require 'fog/core/model'
|
||||||
|
require 'fog/core/current_machine'
|
||||||
|
|
||||||
module Fog
|
module Fog
|
||||||
module AWS
|
module AWS
|
||||||
|
@ -43,6 +44,16 @@ module Fog
|
||||||
authorize_ingress({'CIDRIP' => cidrip})
|
authorize_ingress({'CIDRIP' => cidrip})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Add the current machine to the RDS security group.
|
||||||
|
def authorize_me
|
||||||
|
authorize_ip_address(Fog::CurrentMachine.ip_address)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Add the ip address to the RDS security group.
|
||||||
|
def authorize_ip_address(ip)
|
||||||
|
authorize_cidrip("#{ip}/32")
|
||||||
|
end
|
||||||
|
|
||||||
def authorize_ingress(opts)
|
def authorize_ingress(opts)
|
||||||
data = connection.authorize_db_security_group_ingress(id, opts).body['AuthorizeDBSecurityGroupIngressResult']['DBSecurityGroup']
|
data = connection.authorize_db_security_group_ingress(id, opts).body['AuthorizeDBSecurityGroupIngressResult']['DBSecurityGroup']
|
||||||
merge_attributes(data)
|
merge_attributes(data)
|
||||||
|
|
38
lib/fog/core/current_machine.rb
Normal file
38
lib/fog/core/current_machine.rb
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
require 'net/http'
|
||||||
|
require 'uri'
|
||||||
|
|
||||||
|
module Fog
|
||||||
|
class CurrentMachine
|
||||||
|
@@lock = Mutex.new
|
||||||
|
AMAZON_AWS_CHECK_IP = 'http://checkip.amazonaws.com'
|
||||||
|
|
||||||
|
def self.ip_address= ip_address
|
||||||
|
@@lock.synchronize do
|
||||||
|
@@ip_address = ip_address
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get the ip address of the machine from which this command is run. It is
|
||||||
|
# recommended that you surround calls to this function with a timeout block
|
||||||
|
# to ensure optimum performance in the case where the amazonaws checkip
|
||||||
|
# service is unavailable.
|
||||||
|
#
|
||||||
|
# @example Get the current ip address
|
||||||
|
# begin
|
||||||
|
# Timeout::timeout(5) do
|
||||||
|
# puts "Your ip address is #{Fog::CurrentMachine.ip_address}"
|
||||||
|
# end
|
||||||
|
# rescue Timeout::Error
|
||||||
|
# puts "Service timeout"
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# @raise [Net::HTTPExceptions] if the net/http request fails.
|
||||||
|
def self.ip_address
|
||||||
|
@@lock.synchronize do
|
||||||
|
@@ip_address ||= Net::HTTP \
|
||||||
|
.get_response(URI.parse(AMAZON_AWS_CHECK_IP)) \
|
||||||
|
.body.chomp
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
22
spec/core/current_machine_spec.rb
Normal file
22
spec/core/current_machine_spec.rb
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
require 'fog/core/current_machine'
|
||||||
|
|
||||||
|
describe Fog::CurrentMachine do
|
||||||
|
context '#ip_address' do
|
||||||
|
before(:each){ described_class.ip_address = nil }
|
||||||
|
|
||||||
|
it 'should be threadsafe' do
|
||||||
|
Net::HTTP.should_receive(:get_response).once{ Struct.new(:body).new('') }
|
||||||
|
|
||||||
|
(1..10).map {
|
||||||
|
Thread.new { described_class.ip_address }
|
||||||
|
}.each{ |t| t.join }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should remove trailing endline characters' do
|
||||||
|
Net::HTTP.stub(:get_response){ Struct.new(:body).new("192.168.0.1\n") }
|
||||||
|
|
||||||
|
described_class.ip_address.should == '192.168.0.1'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue