2017-02-20 13:51:41 +00:00
|
|
|
|
2017-02-23 12:46:15 +00:00
|
|
|
require 'azure_backend'
|
2017-02-20 13:51:41 +00:00
|
|
|
|
2017-02-20 17:15:40 +00:00
|
|
|
# Class to test the resources in Resource Groups
|
|
|
|
#
|
|
|
|
# @author Russell Seymour
|
|
|
|
#
|
|
|
|
# @attr_reader [Hashtable] items List of items in the resource group
|
|
|
|
# @attr_reader [Azure::ARM::Resources::Models::ResourceGroup] rg Resource group under interrogation
|
|
|
|
# @attr_reader [Hashtable] counts Hashtable containing the counts of the different types in the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
class AzureRg < Inspec.resource(1)
|
2017-03-01 17:32:17 +00:00
|
|
|
name 'azure_resource_group'
|
2017-02-20 13:51:41 +00:00
|
|
|
|
|
|
|
desc "
|
|
|
|
This resource returns information about the specified resource group
|
|
|
|
"
|
|
|
|
|
|
|
|
example "
|
|
|
|
describe azure_rg(name: 'ACME') do
|
|
|
|
its('nic_count') { should eq 4 }
|
|
|
|
its('vm_count) { should eq 2 }
|
|
|
|
end
|
|
|
|
"
|
|
|
|
|
|
|
|
attr_reader :items, :rg, :counts
|
|
|
|
|
2017-02-20 17:15:40 +00:00
|
|
|
# Constructor which retrieves the named resource group and parses all of its items
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @author Russell Seymour
|
|
|
|
#
|
|
|
|
# @param [Hash] opts Hashtable of options
|
|
|
|
# opts[:name] The name of the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
def initialize(opts)
|
|
|
|
opts = opts
|
|
|
|
helpers = Helpers.new
|
|
|
|
|
|
|
|
# Get the named resource group
|
2017-02-20 17:15:40 +00:00
|
|
|
@rg = helpers.resource_mgmt.get_resource_group(opts[:name])
|
2017-02-20 13:51:41 +00:00
|
|
|
|
2017-02-23 13:32:59 +00:00
|
|
|
# If the rg is nil raise error
|
|
|
|
raise format("Unable to find resource group '%s' in Azure subscription '%s'", opts[:name], helpers.azure.subscription_id) if rg.nil?
|
|
|
|
|
2017-02-20 13:51:41 +00:00
|
|
|
# Retrieve the items within the resource group
|
2017-02-20 17:15:40 +00:00
|
|
|
rg_items = helpers.resource_mgmt.get_resources(opts[:name])
|
2017-02-20 13:51:41 +00:00
|
|
|
|
|
|
|
# Parse the resources
|
|
|
|
@items = parse_rg_resources(rg_items.value)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a FilterTable so that items can be selected
|
|
|
|
filter = FilterTable.create
|
|
|
|
filter.add_accessor(:where)
|
|
|
|
.add_accessor(:entries)
|
|
|
|
.add_accessor(:count)
|
|
|
|
.add_accessor(:contains)
|
|
|
|
.add(:type, field: 'type')
|
|
|
|
.add(:name, field: 'name')
|
|
|
|
.add(:location, field: 'location')
|
|
|
|
|
|
|
|
filter.connect(self, :items)
|
|
|
|
|
|
|
|
# Determine the location of the resource group
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @return [String Location of the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def location
|
|
|
|
rg.location
|
|
|
|
end
|
|
|
|
|
|
|
|
# Determime how many resources in total there are
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @return [Integer] Total number of items in the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def total
|
|
|
|
counts['total']
|
|
|
|
end
|
|
|
|
|
|
|
|
# Determine how many of a certain type there are
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @return [Integer] Number of specific items in the FilterTable
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def count
|
|
|
|
entries.length
|
|
|
|
end
|
|
|
|
|
|
|
|
# Allows tests to be performed on the resources
|
|
|
|
# For example it is possible to check that a resource of a certain name exists
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @param [Hashtable] settings Hashtable of settings which will be used to perform the filter
|
|
|
|
# settings[:parameter] Name of the parameter being interrogated [name, type, location]
|
|
|
|
# settings[:value] The expected value of the specified paramater
|
|
|
|
#
|
|
|
|
# @return [Boolean] Whether or not the specified item exists in the resources
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def contains(settings)
|
|
|
|
result = false
|
|
|
|
|
|
|
|
entries.each do |entry|
|
|
|
|
if entry[settings[:parameter]] == settings[:value]
|
|
|
|
result = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper method to determine the number of NICs in the resource group
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @return [Integer] Number of NICs in the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def nic_count
|
|
|
|
counts['Microsoft.Network/networkInterfaces']
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper method to determine the number of VMs in the resource group
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @return [Integer] Number of VMs in the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def vm_count
|
|
|
|
counts['Microsoft.Compute/virtualMachines']
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper method to determine the number of NSGs in the resource group
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @return [Integer] Number of NSGs in the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def nsg_count
|
|
|
|
counts['Microsoft.Network/networkSecurityGroups']
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper method to determine the number of Virtual Networks in the resource group
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @return [Integer] Number of VNETs in the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def vnet_count
|
|
|
|
counts['Microsoft.Network/virtualNetworks']
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper method to determine the number of Storage Accounts in the resource group
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @return [Integer] Number of SAs in the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def sa_count
|
|
|
|
counts['Microsoft.Storage/storageAccounts']
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper method to determine the number of Public IP Addresses in the resource group
|
|
|
|
#
|
2017-02-20 17:15:40 +00:00
|
|
|
# @return [Integer] Number of Public IP Addresses in the resource group
|
2017-02-20 13:51:41 +00:00
|
|
|
#
|
|
|
|
def public_ip_count
|
|
|
|
counts['Microsoft.Network/publicIPAddresses']
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-02-20 17:15:40 +00:00
|
|
|
# Parse the Resource Group Resources
|
|
|
|
#
|
|
|
|
# @param [Array] resources Array of resources in the resource group
|
|
|
|
#
|
|
|
|
# @return [Array<Hash>] Array of hashes providing the information about the resources for the FilterTable
|
2017-02-20 13:51:41 +00:00
|
|
|
def parse_rg_resources(resources)
|
|
|
|
# Declare the hashtable of counts
|
|
|
|
@counts = {
|
|
|
|
'total' => 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
resources.each.map do |resource|
|
|
|
|
parse_item(resource)
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2017-02-20 17:15:40 +00:00
|
|
|
# Parses each resource item and extracts the information to be tested
|
|
|
|
#
|
|
|
|
# @return [Hash] Resource information
|
|
|
|
#
|
2017-02-20 13:51:41 +00:00
|
|
|
def parse_item(item)
|
|
|
|
# Increment the count total
|
|
|
|
counts['total'] += 1
|
|
|
|
|
|
|
|
# Update the count for the resource type in the count table
|
|
|
|
counts.key?(item.type) ? counts[item.type] +=1 : counts[item.type] = 1
|
|
|
|
|
|
|
|
{
|
|
|
|
'location' => item.location,
|
|
|
|
'name' => item.name,
|
|
|
|
'type' => item.type,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|