Use the azure_resource_group_resource_count Chef InSpec audit resource to check the number of Azure resources in a resource group.
This resource is distributed along with Chef InSpec itself. You can use it automatically.
This resource first became available in v2.0.16 of InSpec.
The name of the resource group is specified as a parameter on the resource:
describe azure_resource_group(name: 'MyResourceGroup') do
its('property') { should eq 'value' }
end
where
MyResourceGroup is the name of the resource group being interrogatedproperty is one a resource propertyvalue is the expected output from the matcherThe following examples show how to use this Chef InSpec audit resource:
describe azure_resource_group_resource_count(name: 'InSpec-Azure') do
its('total') { should eq 7}
end
describe azure_resource_group_resource_count(name: 'InSpec-Azure') do
its('total') { should eq 7 }
its('vm_count') { should eq 2 }
its('nic_count') { should eq 2 }
its('public_ip_count') { should eq 1 }
its('sa_count') { should eq 1 }
its('vnet_count') { should eq 1 }
end
group_namenamegroup_name (required)Use this parameter to define the Azure Resource Group to be tested.
describe azure_virtual_machine_data_disk(group_name: 'InSpec-Azure') do
...
end
nameUse this parameter to define the name of the Azure resource to test.
describe azure_virtual_machine_data_disk(group_name: 'InSpec-Azure', name: 'Windows-Internal-VM') do
...
end
If both group_name and name is set then name takes priority
These options can also be set using the environment variables:
AZURE_RESOURCE_GROUP_NAMEAZURE_RESOURCE_NAMEWhen the options have been set as well as the environment variables, the environment variables take priority.
describe azure_generic_resource(group_name: 'InSpec-Azure', name: 'Linux-Internal-VM') do
its('location') { should eq 'westeurope' }
end
namelocationidprovisioning_statesubscription_idtotalnic_countvm_countextension_countvnet_countsa_countpublic_ip_countmanaged_disk_image_countmanaged_disk_counttag_countTests the name of the resource group.
its('name') { should cmp 'InSpec-Azure' }
Tests where in Azure the resource group is located.
its('location') { should cmp 'westeurope' }
Tests the full qualified ID of the resource group.
This takes the format: /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>.
its('id') { should cmp 'FQDN' }
Tests the provisioning state of the resource group.
its('provisioning_state') { should cmp 'Succeeded' }
Tests the total number of resources in the resource group.
its('total') { should eq 13 }
Tests the number of network interface cards in the resource group.
it { should have_nics }
its('nic_count') { should eq 3 }
Tests the number of virtual machines in the resource group.
its('vm_count') { should eq 5 }
Tests the number of virtual networks in the resource group.
its('vnet_count') { should eq 5 }
Tests the number of storage accounts in the resource group.
its('sa_count') { should eq 5 }
Tests the number of Public IP Addresses in the resource group.
its('public_ip_count') { should eq 5 }
Tests the number of managed disk images that are in the resource group.
Managed disks are created from disk images and then attached to the machines. Generally, the images are created from a base image or a custom image (e.g., Packer)
its('managed_disk_image_count') { should eq 5 }
Tests the number of managed disks in the resource group.
If a resource group contains one virtual machine with an OS disk and 2 data disks that are all Managed Disks, then the count would be 3.
its('managed_disk_count') { should eq 3 }
This resource has a number of special matchers that provide a simple way to test if a specific Azure Resource type exists in the resource group.
For a full list of available matchers, please visit our matchers page.
Use this matcher to test if network interfaces exist.
it { should have_nics }
Use this matcher to test that virtual machines exist.
it { should have_vms }
Use this matcher to test for virtual machine extensions.
it { should have_extensions }
Use this matcher to test that network security groups exist.
it { should have_nsgs }
Use this matcher to test that virtual networks exist.
it { should have_vnets }
Use this matcher to test that managed disks exist.
it { should have_managed_disks }
Use this matcher to test that managed disk images exist.
it { should have_managed_disk_images }
Use this matcher to test that storage accounts exist.
it { should have_sas }
Use this matcher to test that public ips exist.
it { should have_public_ips }
It is possible to test the tags that have been assigned to the resource. There are some properties for checking that a resource has tags, that it has the correct number of tags, and that the correct tags are assigned.
This is a simple test to see if the machine has tags assigned to it or not.
it { should have_tags }
Returns the number of tags that are assigned to the resource
its ('tag_count') { should eq 2 }
It is possible to check if a specific tag has been set on the resource.
its('tags') { should include 'owner' }
To get the value of the tag, some properties are created from the tags themselves.
For example, if the following tag is set on a resource:
Then a property is available called Owner_tag.
its('owner_tag') { should cmp 'JG Jinglehimerschmidt' }
Note: The tag name is case sensitive which makes the test case sensitive. E.g. owner_tag does not equal Owner_tag.
For more information on Azure Ruby SDK resources, see:
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs.chef.io/inspec/resources/azure_resource_group/