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

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

Instance Attribute Summary collapse

Attributes inherited from Resource

#attrs, #env

Instance Method Summary collapse

Methods inherited from Resource

#delete, #reload, rest_name, short_name, #update

Methods included from SpecifiedAttributes

#get_api_attribute, included

Constructor Details

#initialize(attrs, vm, env) ⇒ Interface

Returns a new instance of Interface.



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

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

Instance Attribute Details

#vmObject (readonly)

Returns the value of attribute vm.



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

def vm
  @vm
end

Instance Method Details

#address_for(vpn) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/vagrant-skytap/api/interface.rb', line 76

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



111
112
113
114
115
116
117
118
# File 'lib/vagrant-skytap/api/interface.rb', line 111

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



72
73
74
# File 'lib/vagrant-skytap/api/interface.rb', line 72

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

#available_ipsObject



64
65
66
67
68
69
70
# File 'lib/vagrant-skytap/api/interface.rb', line 64

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



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/vagrant-skytap/api/interface.rb', line 129

def create_published_service(internal_port)
  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_address_for_network(network) ⇒ Object



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

def nat_address_for_network(network)
  nat_address = network_nat_addresses.find{|addr| addr['network_id'].to_i == network.id.to_i}
  nat_address['ip_address'] if nat_address
end

#nat_addressesObject



107
108
109
# File 'lib/vagrant-skytap/api/interface.rb', line 107

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

#networkObject



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

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

#network_nat_addressesObject



103
104
105
# File 'lib/vagrant-skytap/api/interface.rb', line 103

def network_nat_addresses
  nat_addresses['network_nat_addresses'] || []
end

#public_ipsObject



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

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



120
121
122
123
# File 'lib/vagrant-skytap/api/interface.rb', line 120

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

#published_servicesObject



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

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

#refresh(attrs) ⇒ Object



142
143
144
145
146
# File 'lib/vagrant-skytap/api/interface.rb', line 142

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

#uncreated_published_service_choiceObject



125
126
127
# File 'lib/vagrant-skytap/api/interface.rb', line 125

def uncreated_published_service_choice
  PublishedServiceChoice.uncreated_choice(env, self)
end

#urlObject



60
61
62
# File 'lib/vagrant-skytap/api/interface.rb', line 60

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

#vpn_attachmentsObject



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

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

#vpn_nat_addressesObject



99
100
101
# File 'lib/vagrant-skytap/api/interface.rb', line 99

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