Adding support for data disks using Inspec filter

Signed-off-by: Russell Seymour <russell.seymour@turtlesystems.co.uk>
This commit is contained in:
Russell Seymour 2017-02-09 16:47:46 +00:00
parent 082e5c0041
commit 8e7a600dcb
4 changed files with 180 additions and 87 deletions

71
libraries/azure_vm.rb Normal file
View file

@ -0,0 +1,71 @@
require_relative 'common/helpers'
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
"
# Load the configuration file on initialisation
def initialize(opts)
@opts = opts
@helpers = Helpers.new()
end
def sku
vm = @helpers.get_vm(@opts[:host], @opts[:resource_group])
if vm.instance_of?(String)
vm
else
vm.storage_profile.image_reference.sku
end
end
def publisher
vm = @helpers.get_vm(@opts[:host], @opts[:resource_group])
if vm.instance_of?(String)
vm
else
vm.storage_profile.image_reference.publisher
end
end
def offer
vm = @helpers.get_vm(@opts[:host], @opts[:resource_group])
if vm.instance_of?(String)
vm
else
vm.storage_profile.image_reference.offer
end
end
def has_data_disks?
vm = @helpers.get_vm(@opts[:host], @opts[:resource_group])
if vm.instance_of?(String)
vm
else
vm.storage_profile.data_disks.length > 0
end
end
def data_disk
vm = @helpers.get_vm(@opts[:host], @opts[:resource_group])
dd = DataDisks.new(vm)
end
end

View file

@ -0,0 +1,76 @@
require_relative 'common/helpers'
require 'uri'
class AzureVmDataDisks < Inspec.resource(1)
name 'azure_vm_datadisks'
desc "
This resource gather information about the data disks attached to a virtual machine
"
example "
describe azure_vm_datadisks(host: 'example-01', resource_group: 'MyResourceGroup').where { (disk == 1 and size >= 10) } do
it { should be true }
end
"
attr_reader :params
# Load the configuration on initialisation
def initialize(opts)
@opts = opts
@helpers = Helpers.new()
# Get the VM that needs to be interrogated
vm = @helpers.get_vm(@opts[:host], @opts[:resource_group])
# Parse the data disks
@params = parse_data_disks(vm.storage_profile.data_disks)
end
# Create a filter table for testing
filter = FilterTable.create
filter.add_accessor(:where)
.add_accessor(:entries)
.add(:disk, field: 'disk')
.add(:caching, field: 'caching')
.add(:create_option, field: 'create_option')
.add(:size, field: 'size')
.add(:lun, field: 'lun')
.add(:name, field: 'name')
.add(:uri, field: 'uri')
.add(:storage_account, field: 'storage_account')
filter.connect(self, :params)
private
def parse_data_disks(data_disks)
data_disks.each_with_index.map do |disk, index|
parse_data_disk_item(disk, index)
end.compact
end
def parse_data_disk_item(disk, index)
# Parse the uri of the disk so that the storage account can be retrieved
uri = URI.parse(disk.vhd.uri)
{
'disk' => index,
'caching' => disk.caching,
'create_option' => disk.create_option,
'size' => disk.disk_size_gb,
'lun' => disk.lun,
'name' => disk.name,
'uri' => disk.vhd.uri,
'storage_account' => uri.host.split('.').first
}
end
end

View file

@ -1,87 +0,0 @@
require 'azure_conn'
require 'azure_mgmt_compute'
require_relative 'common/resource_groups'
class AzureVmImage < Inspec.resource(1)
name 'azurevm_image'
desc "
This resource gathers information about which image the vm was created from
"
example "
describe azurevm_image_sku(host: 'acme-test-01', resource_group: 'ACME') do
its('sku') { should eq '16.04.0-LTS'}
end
"
# Load the configuration file on initialisation
def initialize(opts)
@opts = opts
end
def sku
vm = get_vm(@opts[:host], @opts[:resource_group])
if vm.instance_of?(String)
vm
else
vm.storage_profile.image_reference.sku
end
end
def publisher
vm = get_vm(@opts[:host], @opts[:resource_group])
if vm.instance_of?(String)
vm
else
vm.storage_profile.image_reference.publisher
end
end
def offer
vm = get_vm(@opts[:host], @opts[:resource_group])
if vm.instance_of?(String)
vm
else
vm.storage_profile.image_reference.offer
end
end
def has_data_disks?
vm = get_vm(@opts[:host], @opts[:resource_group])
if vm.instance_of?(String)
vm
else
vm.storage_profile.data_disks.length > 0
end
end
# Retrieve the named virtual machine from Azure
def get_vm(name, rg_name)
# Azure connection
azure = AzureConnection.new
client = Azure::ARM::Compute::ComputeManagementClient.new(azure.connection)
client.subscription_id = azure.subscription_id
# Ensure that the resource group exists
rg = ResourceGroups.new(azure)
unless rg.exists(rg_name)
throw "The Resource group cannot be found: #{rg_name}"
end
# get a vm from the named resource group
begin
client.virtual_machines.get(rg_name, name)
rescue => e
e.error_message
end
end
end

View file

@ -0,0 +1,33 @@
require_relative '../azure_conn'
require 'azure_mgmt_compute'
require_relative 'resource_groups'
class Helpers
# Retrieve the named virtual machine from Azure
def get_vm(name, rg_name)
# Azure connection
azure = AzureConnection.new
client = Azure::ARM::Compute::ComputeManagementClient.new(azure.connection)
client.subscription_id = azure.subscription_id
# Ensure that the resource group exists
rg = ResourceGroups.new(azure)
unless rg.exists(rg_name)
throw "The Resource group cannot be found: #{rg_name}"
end
# get a vm from the named resource group
begin
client.virtual_machines.get(rg_name, name)
rescue => e
e.error_message
end
end
end