Class: VagrantPlugins::SecuredCloud::Action::HasPublicIps

Inherits:
Object
  • Object
show all
Defined in:
lib/secured-cloud-vagrant/actions/has_public_ips.rb

Overview

This can be used with “Call” built-in to check if the machine is created and branch in the middleware.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ HasPublicIps

Returns a new instance of HasPublicIps.



13
14
15
16
17
# File 'lib/secured-cloud-vagrant/actions/has_public_ips.rb', line 13

def initialize(app, env)
	@app = app
	@machine = env[:machine]
	@logger = Log4r::Logger.new('vagrant::secured_cloud::action::has_public_ips')
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/secured-cloud-vagrant/actions/has_public_ips.rb', line 19

def call(env)

	@logger.debug("Checking whether VM has public IPs ...")
	
	vm_resource_url = @machine.id
	
	if !vm_resource_url.nil? && !vm_resource_url.empty?
	
		begin
		
		# Create a Secured Cloud Connection instance to connect to the SecuredCloud API
        authInfo = @machine.provider_config.auth
        sc_connection = SecuredCloudConnection.new(authInfo.url, authInfo.applicationKey, authInfo.sharedSecret)
      
			# Get the public IPs of the VM
			publicIps = SecuredCloudRestClient.getVMPublicIPs(sc_connection, vm_resource_url)
			env[:has_public_ips] = (!publicIps.nil? && !publicIps.empty?)
			@logger.debug("Has public IPs: #{env[:has_public_ips]}")
		
		rescue Errno::ETIMEDOUT
			env[:ui].error(I18n.t('secured_cloud_vagrant.errors.request_timed_out', :request => "get the public IPs for VM #{env[:vm_name]}"))
        rescue Exception => e
          env[:ui].error(I18n.t("secured_cloud_vagrant.errors.generic_error", :error_message => e.message))
		end
		
	end
	
	@app.call(env)
end