2017-02-09 16:47:46 +00:00
2017-02-23 12:46:15 +00:00
require 'azure_backend'
2017-02-09 16:47:46 +00:00
2017-02-20 17:15:40 +00:00
# Class to retrieve information about the specified virtual machine
#
# @author Russell Seymour
#
# @attr_reader [Azure::ARM::Compute::Models::VirtualMachine] vm VM object as retrieved from Azure
2017-02-09 16:47:46 +00:00
class AzureVm < Inspec . resource ( 1 )
name 'azure_vm'
desc "
This resource gathers information about which image the vm was created from
"
example "
describe azure_vm ( host : 'acme-test-01' , resource_group : 'ACME' ) do
its ( 'sku' ) { should eq '16.04.0-LTS' }
end
"
2017-02-20 09:52:28 +00:00
attr_accessor :vm
2017-02-20 17:15:40 +00:00
# Constructor to retrieve the VM from Azure
#
# @author Russell Seymour
#
# @param [Hash] opts Hashtable of options
# opts[:host] The name of the host in the resource group. NOTE, this is the name as seen in Azure and not the name of the machine in the Operating System
# opts[:resource_group] Name of the resource group in which the host will be found
2017-02-09 16:47:46 +00:00
def initialize ( opts )
2017-02-20 09:52:28 +00:00
opts = opts
helpers = Helpers . new
@vm = helpers . get_vm ( opts [ :host ] , opts [ :resource_group ] )
# Ensure that the vm is an object
raise format ( 'An error has occured: %s' , vm ) if vm . instance_of? ( String )
2017-02-09 16:47:46 +00:00
end
2017-02-20 09:52:28 +00:00
# Determine the SKU used to create the machine
#
2017-02-20 17:15:40 +00:00
# @return [String] Showing the sku, e.g. 16.04.0-LTS
2017-02-20 09:52:28 +00:00
#
2017-02-09 16:47:46 +00:00
def sku
2017-02-20 09:52:28 +00:00
vm . storage_profile . image_reference . sku
2017-02-09 16:47:46 +00:00
end
2017-02-20 09:52:28 +00:00
# Determine the publisher of the SKU
#
2017-02-20 17:15:40 +00:00
# @return [String] Publisher, e.g. Canonical
2017-02-20 09:52:28 +00:00
#
2017-02-09 16:47:46 +00:00
def publisher
2017-02-20 09:52:28 +00:00
vm . storage_profile . image_reference . publisher
end
# Determine the offer from the publisher
#
2017-02-20 17:15:40 +00:00
# @return [String] offer, e.g. UbuntuServer
2017-02-20 09:52:28 +00:00
#
def offer
vm . storage_profile . image_reference . offer
end
# Determine the size of the machine
#
2017-02-20 17:15:40 +00:00
# @return [String] Size of the machine, e.g. Standard_DS1_v2
2017-02-20 09:52:28 +00:00
#
def size
vm . hardware_profile . vm_size
end
# Determine the location of the vm
#
2017-02-20 17:15:40 +00:00
# @return [String] location of the machinem, e.g. westeurope
2017-02-20 09:52:28 +00:00
#
def location
vm . location
end
# State if boot diagnostics is enabled
#
2017-02-20 17:15:40 +00:00
# @return [Boolean]
2017-02-20 09:52:28 +00:00
#
def boot_diagnostics?
vm . diagnostics_profile . boot_diagnostics . enabled
end
2017-02-09 16:47:46 +00:00
2017-02-20 09:52:28 +00:00
# Determine how many network cards are connected to the machine
#
2017-02-20 17:15:40 +00:00
# @return [Integer]
2017-02-20 09:52:28 +00:00
#
def nic_count
vm . network_profile . network_interfaces . length
end
# The admin username for the machine
#
2017-02-20 17:15:40 +00:00
# @return [String] Admin username when the machine was created, e.g. azure
2017-02-20 09:52:28 +00:00
#
def username
vm . os_profile . admin_username
end
# The computername as seen by the operating system
# This might be different to the VM name as seen in Azure
#
2017-02-20 17:15:40 +00:00
# @return [String]
2017-02-20 09:52:28 +00:00
#
def computername
vm . os_profile . computer_name
end
# Alias for computername
#
2017-02-20 17:15:40 +00:00
# @return [String]
2017-02-20 09:52:28 +00:00
#
def hostname
computername
end
# Determine if password authentication is enabled
# For Windows this is always True. On Linux this will be determined
#
2017-02-20 17:15:40 +00:00
# @return [Boolean]
2017-02-20 09:52:28 +00:00
#
def password_authentication?
# if the vm has a linux configuration then interrogate that, otherwise return true
if ! vm . os_profile . linux_configuration . nil?
! vm . os_profile . linux_configuration . disable_password_authentication
2017-02-09 16:47:46 +00:00
else
2017-02-20 09:52:28 +00:00
true
2017-02-09 16:47:46 +00:00
end
end
2017-02-20 09:52:28 +00:00
# How many SSH keys have been added to the machine
# For Windows this will be 0, for Linux this will be determined
#
2017-02-20 17:15:40 +00:00
# @return [Integer]
2017-02-20 09:52:28 +00:00
#
def ssh_key_count
if ! vm . os_profile . linux_configuration . nil?
vm . os_profile . linux_configuration . ssh . public_keys . length
2017-02-09 16:47:46 +00:00
else
2017-02-20 09:52:28 +00:00
0
2017-02-09 16:47:46 +00:00
end
end
2017-02-20 09:52:28 +00:00
# Determine the Operating system type using the os_disk object
#
2017-02-20 17:15:40 +00:00
# @return [String] OS type, e.g. Windows or Linux
2017-02-20 09:52:28 +00:00
#
def os_type
vm . storage_profile . os_disk . os_type
end
2017-02-09 16:47:46 +00:00
end