Class: Fog::Hyperv::Compute::NetworkAdapter

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/hyperv/compute/models/network_adapter.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

NIC_STATUS_ENUM_VALUES =
Note:

Defined by Microsoft.HyperV.PowerShell.VMNetworkAdapterOperationalStatus

Network adapter statuses

{
  Unknown:             0,
  Other:               1,
  Ok:                  2,
  Degraded:            3,
  Stressed:            4,
  PredictiveFailure:   5,
  Error:               6,
  NonRecoverableError: 7,
  Starting:            8,
  Stopping:            9,
  Stopped:             10,
  InService:           11,
  NoContact:           12,
  LostCommunication:   13,
  Aborted:             14,
  Dormant:             15,
  SupportingEntity:    16,
  Completed:           17,
  PowerMode:           18,
  ProtocolVersion:     32_775
}.freeze
NIC_FALLBACK_MAC =

rubocop:enable Layout/HashAlignment

'000000000000'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#initialize

Methods included from ModelExtends

#collection

Methods included from ModelIncludes

#cluster, #computer, #dirty, #dirty?, #vm

Constructor Details

This class inherits a constructor from Fog::Hyperv::Model

Instance Attribute Details

#allow_teaming:On, :Off

Returns if the NIC should be allowed to be teamed with other NICs on the same switch.

Returns:

  • (:On, :Off)

    if the NIC should be allowed to be teamed with other NICs on the same switch



89
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 89

attribute :allow_teaming, type: :hypervenum, values: Fog::Hyperv::ON_OFF_STATE_ENUM_VALUES

#computer_nameString (readonly)

Returns the name of the computer running the VM that this network adapter is attached to.

Returns:

  • (String)

    the name of the computer running the VM that this network adapter is attached to



43
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 43

attribute :computer_name

#connectedBoolean (readonly)

Returns if the network adapter is connected to the network.

Returns:

  • (Boolean)

    if the network adapter is connected to the network

See Also:



56
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 56

attribute :connected, type: :boolean

#dhcp_guard:On, :Off

Returns if the NIC should drop DHCP messages from unauthorized VMs.

Returns:

  • (:On, :Off)

    if the NIC should drop DHCP messages from unauthorized VMs



83
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 83

attribute :dhcp_guard, type: :hypervenum, values: Fog::Hyperv::ON_OFF_STATE_ENUM_VALUES

#dynamic_mac_address_enabledBoolean

Returns if the network adapter is assigned a dynamic MAC address.

Returns:

  • (Boolean)

    if the network adapter is assigned a dynamic MAC address



60
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 60

attribute :dynamic_mac_address_enabled, type: :boolean, default: true

#idString (readonly)

Returns the GUID of this network adapter.

Returns:

  • (String)

    the GUID of this network adapter



39
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 39

identity :id

#ip_addressesArray<String> (readonly)

Returns the IP addresses currently assigned to the network adapter.

Returns:

  • (Array<String>)

    the IP addresses currently assigned to the network adapter



63
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 63

attribute :ip_addresses

#is_external_adapterBoolean (readonly)

Returns if the network adapter is external to the VM.

Returns:

  • (Boolean)

    if the network adapter is external to the VM



67
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 67

attribute :is_external_adapter, type: :boolean

#is_legacyBoolean (readonly)

Returns if the network adapter is using legacy option ROM.

Returns:

  • (Boolean)

    if the network adapter is using legacy option ROM



70
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 70

attribute :is_legacy, type: :boolean

#is_management_osBoolean (readonly)

Returns if the network adapter is attached to the management OS.

Returns:

  • (Boolean)

    if the network adapter is attached to the management OS



49
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 49

attribute :is_management_os, type: :boolean

#mac_addressString

Note:

Can only be changed if dynamic_mac_address_enabled is false

Returns the MAC address of the network adapter.

Returns:

  • (String)

    the MAC address of the network adapter



74
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 74

attribute :mac_address

#mac_address_spoofing:On, :Off

Returns if the NIC should be allowed to send packets with different MAC address.

Returns:

  • (:On, :Off)

    if the NIC should be allowed to send packets with different MAC address



80
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 80

attribute :mac_address_spoofing, type: :hypervenum, values: Fog::Hyperv::ON_OFF_STATE_ENUM_VALUES

#nameString

Returns the name of the network adapter.

Returns:

  • (String)

    the name of the network adapter



77
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 77

attribute :name

#router_guard:On, :Off

Returns if the NIC should drop RA/Redirection messages from unauthorized VMs.

Returns:

  • (:On, :Off)

    if the NIC should drop RA/Redirection messages from unauthorized VMs



86
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 86

attribute :router_guard, type: :hypervenum, values: Fog::Hyperv::ON_OFF_STATE_ENUM_VALUES

#statusSymbol (readonly)

Returns the status of the network adapter.

Returns:

  • (Symbol)

    the status of the network adapter

See Also:



93
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 93

attribute :status, type: :hypervenumarray, values: NIC_STATUS_ENUM_VALUES

#switchSwitch?

Returns the switch the network adapter is connected to.

Returns:

  • (Switch, nil)

    the switch the network adapter is connected to

See Also:



187
188
189
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 187

def switch
  service.switches.get(switch_id:, switch_name:, computer_name:) if switch_name.any? || switch_id.any?
end

#switch_idString

Returns the ID of the switch the adapter is connected to.

Returns:

  • (String)

    the ID of the switch the adapter is connected to

See Also:



98
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 98

attribute :switch_id

#switch_nameString

Returns the name of the switch the adapter is connected to.

Returns:

  • (String)

    the name of the switch the adapter is connected to

See Also:



103
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 103

attribute :switch_name

#vlan_settingNetworkAdapterVlan (readonly)

Returns the VLAN that the network adapter is connected to.

Returns:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 109

def vlan_setting
  return associations[:vlan_setting] if associations[:vlan_setting]

  require_relative 'network_adapter_vlan'
  attrs = { parent_adapter: self, service: @service, vm: @vm }

  if persisted?
    requires :id
    requires :vm_id unless is_management_os

    associations[:vlan_setting] = Fog::Hyperv::Compute::NetworkAdapterVlan.new(
      **service.get_vm_network_adapter_vlan(
        computer_name:,
        management_os: is_management_os,
        vm_id:,
        id:,

        _return_fields: Fog::Hyperv::Compute::NetworkAdapterVlan.attributes
      ),
      **attrs
    )
  else
    associations[:vlan_setting] = Fog::Hyperv::Compute::NetworkAdapterVlan.new(attrs)
  end
end

#vm_idString? (readonly)

Returns the GUID of the VM this network adapter is attached to.

Returns:

  • (String, nil)

    the GUID of the VM this network adapter is attached to



46
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 46

attribute :vm_id

Instance Method Details

#connect(switch, **options) ⇒ Object

Connect the network adapter to a given switch

Parameters:

  • switch (Switch, String)

    a switch - or the ID/name of one - to connect to



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 137

def connect(switch, **options)
  requires :id

  if switch.is_a? Fog::Hyperv::Compute::Switch
    new_switch_id = switch.id
    new_switch_name = switch.name
  else
    new_switch_id = switch if switch.is_a?(String) && switch =~ Fog::Hyperv::GUID
    new_switch_name = switch unless new_switch_id
  end
  options[:management_os] = true if is_management_os

  service.connect_vm_network_adapter(
    computer_name:,
    vm_id:,
    id:,

    switch_id: new_switch_id,
    switch_name: new_switch_name,

    **options
  )

  old.switch_id = attributes[:switch_id] = new_switch_id
  old.switch_name = attributes[:switch_name] = new_switch_name
  true
end

#createObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 205

def create
  selector = {}
  if is_management_os
    selector[:management_os] = true
  else
    requires :vm_id

    selector[:vm_id] = vm_id
  end

  args = { name:, switch_name: }
  args[:is_legacy] = true if is_legacy
  if !dynamic_mac_address_enabled && mac_address != NIC_FALLBACK_MAC
    args[:static_mac_address] = mac_address
  else
    args[:dynamic_mac_address] = true
  end
  data = service.add_vm_network_adapter(
    **selector,
    computer_name:,

    **args,

    _return_fields: self.class.attributes
  )
  post_save_changes = {
    mac_address_spoofing: mac_address_spoofing,
    dhcp_guard: dhcp_guard,
    router_guard: router_guard,
    allow_teaming: allow_teaming
  }.compact

  merge_attributes(data)
  vlan_setting.save if associations[:vlan_setting]
  return self unless post_save_changes.any?

  attributes.merge!(post_save_changes)
  update if dirty?

  self
end

#destroyObject



291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 291

def destroy
  requires :id
  requires :vm_id unless is_management_os

  service.remove_vm_network_adapter(
    computer_name:,
    vm_id:,
    id:,
    management_os: is_management_os
  )
  true
end

#disconnect(**options) ⇒ Object

Disconnect the network adapter from any connected switch



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 166

def disconnect(**options)
  requires :id

  options[:management_os] = true if is_management_os
  service.disconnect_vm_network_adapter(
    computer_name:,
    vm_id:,
    id:,

    **options
  )

  old.switch_id = attributes[:switch_id] = nil
  old.switch_name = attributes[:switch_name] = nil
  true
end

#reloadObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 304

def reload
  requires :id
  requires :vm_id unless is_management_os

  data = service.get_vm_network_adapter(
    computer_name:,
    vm_id:,
    id:,
    management_os: is_management_os,

    _return_fields: self.class.attributes
  )
  return unless data

  merge_attributes(data)
end

#updateObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/fog/hyperv/compute/models/network_adapter.rb', line 247

def update
  requires :id
  requires :vm_id unless is_management_os

  data = {}
  if changed?(:name)
    service.rename_vm_network_adapter(
      computer_name: old.computer_name,
      id: old.id,
      vm_id: old.vm_id,
      management_os: old.is_management_os,

      new_name: name
    )
    data[:name] = name
  end

  changes = build_changelist
  if changes.any?
    data.merge!(
      service.set_vm_network_adapter(
        computer_name: old.computer_name,
        id: old.id,
        vm_id: old.vm_id,
        management_os: old.is_management_os,

        **changes,

        _always_include: changes.keys,
        _return_fields: self.class.attributes
      )
    )
  end

  if changed?(:switch_name) || changed?(:switch_id)
    save_switch
    data[:switch_name] = switch_name
    data[:switch_id] = switch_id
  end
  vlan_setting.save if associations[:vlan_setting] && vlan_setting.dirty?

  merge_attributes(data)
end