Class: Inspec::Resources::AzureVirtualMachine

Inherits:
AzureResourceBase show all
Defined in:
lib/resources/azure/azure_virtual_machine.rb

Instance Attribute Summary

Attributes inherited from AzureResourceBase

#azure, #client, #opts

Instance Method Summary collapse

Methods inherited from AzureResourceBase

#catch_azure_errors, #create_tag_methods, #failed_resource?, #has_tags?, #resource_group, #resources, #tag_count

Constructor Details

#initialize(opts = {}) ⇒ AzureVirtualMachine

Constructor for the resource. This calls the parent constructor to get the generic resource for the specified machine. This will provide static methods that are documented

Author:

  • Russell Seymour



18
19
20
21
22
23
24
25
26
27
# File 'lib/resources/azure/azure_virtual_machine.rb', line 18

def initialize(opts = {})
  # The generic resource needs to pass back a Microsoft.Compute/virtualMachines object so force it
  opts[:type] = "Microsoft.Compute/virtualMachines"
  super(opts)

  # Find the virtual machines
  resources

  create_tag_methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id) ⇒ Object

Method to catch calls that are not explicitly defined. This allows the simple attributes of the virtual machine to be read without having to define each one in turn.

rubocop:disable Metrics/AbcSize

Parameters:

  • symobl

    method_id The symbol of the method that has been called

Returns:

  • Value of attribute that has been called



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/resources/azure/azure_virtual_machine.rb', line 38

def method_missing(method_id)
  # Depending on the method that has been called, determine what value should be returned
  # These are set as camel case methods to comply with rubocop
  image_reference_attrs = %w{sku publisher offer}
  osdisk_attrs = %w{os_type caching create_option disk_size_gb}
  hardware_profile_attrs = %w{vm_size}
  os_profile_attrs = %w{computer_name admin_username}
  osdisk_managed_disk_attrs = %w{storage_account_type}

  # determine the method name to call by converting the snake_case to camelCase
  # method_name = self.camel_case(method_id.to_s)
  method_name = method_id.to_s.split("_").inject([]) { |buffer, e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
  method_name.end_with?("Gb") ? method_name.gsub!(/Gb/, &:upcase) : false

  if image_reference_attrs.include?(method_id.to_s)
    properties.storageProfile.imageReference.send(method_name)
  elsif osdisk_attrs.include?(method_id.to_s)
    properties.storageProfile.osDisk.send(method_name)
  elsif hardware_profile_attrs.include?(method_id.to_s)
    properties.hardwareProfile.send(method_name)
  elsif os_profile_attrs.include?(method_id.to_s)
    properties.osProfile.send(method_name)
  elsif osdisk_managed_disk_attrs.include?(method_id.to_s)
    properties.storageProfile.osDisk.managedDisk.send(method_name)
  end
end

Instance Method Details

#boot_diagnostics_storage_uriObject

Return the URI that has been set for the boot diagnostics storage

Returns:

  • string



224
225
226
# File 'lib/resources/azure/azure_virtual_machine.rb', line 224

def boot_diagnostics_storage_uri
  properties.diagnosticsProfile.bootDiagnostics.storageUri
end

#connected_nicsObject

Return an array of the connected NICs so that it can be tested to ensure the machine is connected properly

Returns:

  • array Array of NIC names connected to the machine



97
98
99
100
101
102
103
# File 'lib/resources/azure/azure_virtual_machine.rb', line 97

def connected_nics
  nic_names = []
  properties.networkProfile.networkInterfaces.each do |nic|
    nic_names << nic.id.split(%r{/}).last
  end
  nic_names
end

#custom_data?Boolean

Determine if custom data has been set

Returns:

  • (Boolean)

    boolean



157
158
159
160
161
162
163
# File 'lib/resources/azure/azure_virtual_machine.rb', line 157

def custom_data?
  if defined?(properties.osProfile.CustomData)
    true
  else
    false
  end
end

#data_disk_countObject

How many data disks are connected

Returns:

  • integer



115
116
117
# File 'lib/resources/azure/azure_virtual_machine.rb', line 115

def data_disk_count
  properties.storageProfile.dataDisks.count
end

#has_automatic_agent_update?Boolean

If a windows machine see if automatic updates for the agent are enabled

Returns:

  • (Boolean)

    boolean



242
243
244
245
246
247
248
# File 'lib/resources/azure/azure_virtual_machine.rb', line 242

def has_automatic_agent_update?
  if defined?(properties.osProfile.windowsConfiguration)
    properties.osProfile.windowsConfiguration.enableAutomaticUpdates
  else
    false
  end
end

#has_boot_diagnostics?Boolean

Does the machine have boot diagnostics enabled

Returns:

  • (Boolean)

    boolean



213
214
215
216
217
218
219
# File 'lib/resources/azure/azure_virtual_machine.rb', line 213

def has_boot_diagnostics?
  if defined?(properties.diagnosticsProfile)
    properties.diagnosticsProfile.bootDiagnostics.enabled
  else
    false
  end
end

#has_custom_data?Boolean

Has the machine been given Custom Data at creation

This allows the use of

it { should have_custom_data }

within the InSpec Profile

Returns:

  • (Boolean)

    boolean



150
151
152
# File 'lib/resources/azure/azure_virtual_machine.rb', line 150

def has_custom_data?
  custom_data?
end

#has_data_disks?Boolean

Whether the machine has data disks or not

Returns:

  • (Boolean)

    boolean



108
109
110
# File 'lib/resources/azure/azure_virtual_machine.rb', line 108

def has_data_disks?
  properties.storageProfile.dataDisks.count != 0
end

#has_managed_osdisk?Boolean

Determine if the OS disk is a managed disk

Returns:

  • (Boolean)

    boolean



75
76
77
# File 'lib/resources/azure/azure_virtual_machine.rb', line 75

def has_managed_osdisk?
  defined?(properties.storageProfile.osDisk.managedDisk)
end

#has_nics?Boolean

Does the machine have any NICs connected

Returns:

  • (Boolean)

    boolean



82
83
84
# File 'lib/resources/azure/azure_virtual_machine.rb', line 82

def has_nics?
  properties.networkProfile.networkInterfaces.count != 0
end

#has_password_authentication?Boolean

Does the machine allow password authentication

This allows the use of

it { should have_password_authentication }

within the InSpec profile

Returns:

  • (Boolean)

    boolean



126
127
128
# File 'lib/resources/azure/azure_virtual_machine.rb', line 126

def has_password_authentication?
  password_authentication?
end

#has_provision_vmagent?Boolean

If this is a windows machine, returns whether the agent was provisioned or not

Returns:

  • (Boolean)

    boolean



231
232
233
234
235
236
237
# File 'lib/resources/azure/azure_virtual_machine.rb', line 231

def has_provision_vmagent?
  if defined?(properties.osProfile.windowsConfiguration)
    properties.osProfile.windowsConfiguration.provisionVMAgent
  else
    false
  end
end

#has_ssh_keys?Boolean

Are any SSH Keys assigned to the machine

This allows the use of

it { should have_ssh_keys }

within the InSpec Profile

Returns:

  • (Boolean)

    boolean



172
173
174
# File 'lib/resources/azure/azure_virtual_machine.rb', line 172

def has_ssh_keys?
  ssh_keys?
end

#has_winrm_options?Boolean

If this is a windows machine return a boolean to state of the WinRM options have been set

Returns:

  • (Boolean)

    boolean



254
255
256
257
258
259
260
# File 'lib/resources/azure/azure_virtual_machine.rb', line 254

def has_winrm_options?
  if defined?(properties.osProfile.windowsConfiguration) && defined?(properties.osProfile.windowsConfiguration.winrm)
    properties.osProfile.windowsConfiguration.winrm.protocol
  else
    false
  end
end

#nic_countObject

How many NICs are connected to the machine

Returns:

  • integer



89
90
91
# File 'lib/resources/azure/azure_virtual_machine.rb', line 89

def nic_count
  properties.networkProfile.networkInterfaces.count
end

#os_disk_nameObject

Return the name of the os disk

Returns:

  • string Name of the OS disk



68
69
70
# File 'lib/resources/azure/azure_virtual_machine.rb', line 68

def os_disk_name
  properties.storageProfile.osDisk.name
end

#password_authentication?Boolean

Deteremine if the machine allows password authentication

Returns:

  • (Boolean)

    boolean



133
134
135
136
137
138
139
140
141
# File 'lib/resources/azure/azure_virtual_machine.rb', line 133

def password_authentication?
  # if the osProfile property has a linuxConfiguration section then interrogate that
  # otherwise it is a Windows machine and that always has password auth
  if defined?(properties.osProfile.linuxConfiguration)
    !properties.osProfile.linuxConfiguration.disablePasswordAuthentication
  else
    true
  end
end

#ssh_key_countObject

Return the number of ssh keys that have been assigned to the machine

Returns:

  • integer



190
191
192
193
194
195
196
# File 'lib/resources/azure/azure_virtual_machine.rb', line 190

def ssh_key_count
  if defined?(properties.osProfile.linuxConfiguration.ssh)
    properties.osProfile.linuxConfiguration.ssh.publicKeys.count
  else
    0
  end
end

#ssh_keysObject

Determine is the specified key is in the ssh_keys list

Returns:

  • array Array of the public keys that are assigned to allow for testing of that key



201
202
203
204
205
206
207
208
# File 'lib/resources/azure/azure_virtual_machine.rb', line 201

def ssh_keys
  # iterate around the keys
  keys = []
  properties.osProfile.linuxConfiguration.ssh.publicKeys.each do |key|
    keys << key.keyData
  end
  keys
end

#ssh_keys?Boolean

Determine if any ssh keys have been asigned to the machine

Returns:

  • (Boolean)

    boolean



179
180
181
182
183
184
185
# File 'lib/resources/azure/azure_virtual_machine.rb', line 179

def ssh_keys?
  if defined?(properties.osProfile.linuxConfiguration.ssh)
    properties.osProfile.linuxConfiguration.ssh.publicKeys != 0
  else
    false
  end
end