Class: VagrantPlugins::Skytap::API::Interface

Inherits:
Resource show all
Includes:
Busyable
Defined in:
lib/vagrant-skytap/api/interface.rb

Constant Summary

Constants included from Busyable

Busyable::WAIT_ITERATION_PERIOD, Busyable::WAIT_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Resource

#attrs, #env

Instance Method Summary collapse

Methods included from Busyable

#retry_while_resource_busy, #update_with_retry

Methods inherited from Resource

#reload, resource_name

Methods included from SpecifiedAttributes

#get_api_attribute, included

Constructor Details

#initialize(attrs, vm, env) ⇒ Interface

Returns a new instance of Interface.



16
17
18
19
# File 'lib/vagrant-skytap/api/interface.rb', line 16

def initialize(attrs, vm, env)
  super
  @vm = vm
end

Instance Attribute Details

#vmObject (readonly)

Returns the value of attribute vm.



12
13
14
# File 'lib/vagrant-skytap/api/interface.rb', line 12

def vm
  @vm
end

Instance Method Details

#address_for(vpn) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/vagrant-skytap/api/interface.rb', line 57

def address_for(vpn)
  if vpn.nat_enabled?
    if info = vpn_nat_addresses.detect {|aa| aa['vpn_id'] == vpn.id}
      info['ip_address']
    end
  elsif vpn_attachments.any? {|aa| aa.vpn['id'] == vpn.id}
    ip
  end
end

#attach_public_ip(ip) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/vagrant-skytap/api/interface.rb', line 83

def attach_public_ip(ip)
  address = ip.is_a?(PublicIp) ? ip.address : ip.to_s
  begin
    resp = api_client.post("#{url}/ips", JSON.dump(ip: address))
  rescue Errors::OperationFailed => ex
    raise Errors::OperationFailed, err: 'Failed to attach public IP'
  end
end

#attachment_for(vpn) ⇒ Object



53
54
55
# File 'lib/vagrant-skytap/api/interface.rb', line 53

def attachment_for(vpn)
  network.try(:attachment_for, vpn)
end

#available_ipsObject



45
46
47
48
49
50
51
# File 'lib/vagrant-skytap/api/interface.rb', line 45

def available_ips
  resp = api_client.get("#{url}/ips/available")
  ip_attrs = JSON.load(resp.body)
  ip_attrs.collect do |attrs|
    PublicIp.new(attrs, nil, env)
  end
end

#create_published_service(internal_port) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vagrant-skytap/api/interface.rb', line 101

def create_published_service(internal_port)
  #TODO:NLA Move this into PublishedService::create! Same with other places.
  begin
    resp = api_client.post("#{url}/services", JSON.dump(internal_port: internal_port))
  rescue Errors::OperationFailed => ex
    raise Errors::OperationFailed, err: 'Failed to create published service'
  end

  service_attrs = JSON.load(resp.body)
  PublishedService.new(service_attrs, self, env).tap do |service|
    published_services << service
  end
end

#nat_addressesObject



79
80
81
# File 'lib/vagrant-skytap/api/interface.rb', line 79

def nat_addresses
  get_api_attribute('nat_addresses') || {}
end

#networkObject



21
22
23
24
25
26
27
# File 'lib/vagrant-skytap/api/interface.rb', line 21

def network
  if network_id
    vm.environment.networks.detect do |network|
      network.id == network_id
    end
  end
end

#public_ipsObject



29
30
31
32
33
# File 'lib/vagrant-skytap/api/interface.rb', line 29

def public_ips
  @public_ips ||= (get_api_attribute('public_ips') || []).collect do |ip_attrs|
    PublicIp.new(ip_attrs, self, env)
  end
end

#published_service_choicesObject



92
93
94
95
# File 'lib/vagrant-skytap/api/interface.rb', line 92

def published_service_choices
  [uncreated_published_service_choice] +
    published_services.collect(&:choice_for_setup)
end

#published_servicesObject



35
36
37
38
39
# File 'lib/vagrant-skytap/api/interface.rb', line 35

def published_services
  @published_services ||= (get_api_attribute('services') || []).collect do |service_attrs|
    PublishedService.new(service_attrs, self, env)
  end
end

#refresh(attrs) ⇒ Object



115
116
117
118
119
# File 'lib/vagrant-skytap/api/interface.rb', line 115

def refresh(attrs)
  @public_ips = nil
  @published_services = nil
  super
end

#uncreated_published_service_choiceObject



97
98
99
# File 'lib/vagrant-skytap/api/interface.rb', line 97

def uncreated_published_service_choice
  PublishedServiceChoice.uncreated_choice(env, self)
end

#urlObject



41
42
43
# File 'lib/vagrant-skytap/api/interface.rb', line 41

def url
  "/configurations/#{vm.environment.id}/vms/#{vm.id}/interfaces/#{id}"
end

#vpn_attachmentsObject



67
68
69
70
71
72
73
# File 'lib/vagrant-skytap/api/interface.rb', line 67

def vpn_attachments
  if network
    network.vpn_attachments
  else
    []
  end
end

#vpn_nat_addressesObject



75
76
77
# File 'lib/vagrant-skytap/api/interface.rb', line 75

def vpn_nat_addresses
  nat_addresses['vpn_nat_addresses'] || []
end