2017-03-02 10:11:18 +00:00
---
2017-12-12 18:20:22 +00:00
title: About the azure_resource_group_resource_counts Resource
2017-03-02 10:11:18 +00:00
---
2018-02-15 02:23:29 +00:00
# azure\_resource\_group\_resource\_counts
2017-03-02 10:11:18 +00:00
2017-12-12 18:20:22 +00:00
Use the `azure_resource_group_resource_counts` InSpec audit resource to check the number of Azure resources in a resource group
2017-03-02 10:11:18 +00:00
## Syntax
2017-12-12 18:20:22 +00:00
The name of the resource group is specified as a parameter on the resource:
2017-03-02 10:11:18 +00:00
```ruby
describe azure_resource_group(name: 'MyResourceGroup') do
2017-12-12 18:20:22 +00:00
its('property') { should eq 'value' }
2017-03-02 10:11:18 +00:00
end
```
where
2017-12-12 18:20:22 +00:00
* Resource Parameters
* `MyResourceGroup` is the name of the resource group being interrogated
2018-02-15 02:23:29 +00:00
* `property` is one a resource property
2017-03-02 10:11:18 +00:00
* `value` is the expected output from the matcher
2017-12-12 18:20:22 +00:00
The options that can be passed to the resource are as follows.
| Name | Description | Required | Example |
|-------------|---------------------------------------------------------------------------------------------------------------------|----------|-----------------------------------|
| group_name: | Azure Resource Group to be tested | yes | MyResourceGroup |
| name: | Name of the Azure resource to test | no | MyVM |
If both `group_name` and `name` is set then `name` will take priority
These options can also be set using the environment variables:
- `AZURE_RESOURCE_GROUP_NAME`
- `AZURE_RESOURCE_NAME`
When the options have been set as well as the environment variables, the environment variables take priority.
2017-03-02 10:11:18 +00:00
For example:
2018-02-15 02:23:29 +00:00
```ruby
describe azure_resource_group_resource_counts(name: 'ChefAutomate') do
its('total') { should eq 7}
its('nic_count') { should eq 1 }
its('vm_count') { should eq 1 }
end
```
2017-12-12 18:20:22 +00:00
2018-02-15 02:23:29 +00:00
## Properties
2017-12-12 18:20:22 +00:00
2018-02-15 02:23:29 +00:00
* `name`, `location` ,`id`, `provisioning_state`, `subscription_id`, `total`, `nic_count`, `vm_count`, `extension_count`, `vnet_count`, `sa_count`, `public_ip_count`,`managed_disk_image_count`, `managed_disk_count`, `tag_count`
2017-12-12 18:20:22 +00:00
2018-02-15 02:23:29 +00:00
<br>
2017-03-02 10:11:18 +00:00
2018-02-15 02:23:29 +00:00
## Property Examples
2017-03-02 10:11:18 +00:00
2017-12-12 18:20:22 +00:00
### name
Returns the name of the resource group.
### location
Returns where in Azure the resource group is located.
### id
Returns the full qualified ID of the resource group.
2017-03-02 10:11:18 +00:00
2017-12-12 18:20:22 +00:00
This is in the format `/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>`.
2017-03-02 10:41:05 +00:00
2018-02-15 02:23:29 +00:00
### provisioning\_state
2017-12-12 18:20:22 +00:00
The provisioning state of the resource group.
2018-02-15 02:23:29 +00:00
### subscription\_id
2017-12-12 18:20:22 +00:00
Returns the subscription ID which contains the resource group.
This is derived from the `id`.
2017-03-02 10:11:18 +00:00
### total
The total number of resources in the resource group
2018-02-15 02:23:29 +00:00
### nic\_count
2017-03-02 10:11:18 +00:00
The number of network interface cards in the resource group
2018-02-15 02:23:29 +00:00
### vm\_count
2017-03-02 10:11:18 +00:00
The number of virtual machines in the resource group
2018-02-15 02:23:29 +00:00
### vnet\_count
2017-03-02 10:11:18 +00:00
The number of virtual networks in the resource group
2018-02-15 02:23:29 +00:00
### sa\_count
2017-03-02 10:11:18 +00:00
The number of storage accounts in the resource group
2018-02-15 02:23:29 +00:00
### public\_ip\_count
2017-03-02 10:11:18 +00:00
The number of Public IP Addresses in the resource group
2018-02-15 02:23:29 +00:00
### managed\_disk\_image\_count
2017-03-02 11:10:41 +00:00
The number of managed disk images that are in the resource group.
These are the items from which managed disks are created which are attached to machines. Generally the images are created from a base image or a custom image (e.g. Packer)
2018-02-15 02:23:29 +00:00
### managed\_disk\_count
2017-03-02 11:10:41 +00:00
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.
2018-02-15 02:23:29 +00:00
<br>
## Matchers
This resource has a number of `have_xxxx` matchers that provide a simple way to test of a specific Azure Resoure Type exists in the resource group.
The following table shows the methods that are currently supported and what their associated Azure Resource Type is.
| Matcher Name | Azure Resource Type |
|-------------|---------------------|
| have_nics | Microsoft.Network/networkInterfaces |
| have_vms | Microsoft.Compute/virtualMachines |
| have_extensions | Microsoft.Compute/virtualMachines/extensions |
| have_nsgs | Microsoft.Network/networkSecurityGroups |
| have_vnets | Microsoft.Network/virtualNetworks |
| have_managed_disks | Microsoft.Compute/disks |
| have_managed_disk_images | Microsoft.Compute/images |
| have_sas | Microsoft.Storage/storageAccounts |
| have_public_ips | Microsoft.Network/publicIPAddresses |
With these methods the following tests are possible
```ruby
it { should have_nics }
it { should_not have_extensions }
```
2017-12-12 18:20:22 +00:00
## Tags
It is possible to test the tags that have been assigned to the resource. There are a number of properties that can be called to check that it has tags, that it has the correct number and that the correct ones are assigned.
2017-03-02 10:11:18 +00:00
2018-02-15 02:23:29 +00:00
### have\_tags
2017-12-12 18:20:22 +00:00
This is a simple test to see if the machine has tags assigned to it or not.
2017-03-02 10:11:18 +00:00
2018-02-15 02:23:29 +00:00
```ruby
it { should have_tags }
```
2017-03-02 10:11:18 +00:00
2018-02-15 02:23:29 +00:00
### tag\_count
2017-12-12 18:20:22 +00:00
Returns the number of tags that are assigned to the resource
2017-03-02 10:11:18 +00:00
2018-02-15 02:23:29 +00:00
```ruby
its ('tag_count') { should eq 2 }
```
2017-03-02 10:11:18 +00:00
2017-12-12 18:20:22 +00:00
### tags
2017-03-02 10:11:18 +00:00
2017-12-12 18:20:22 +00:00
It is possible to check if a specific tag has been set on the resource.
2017-03-02 10:11:18 +00:00
2018-02-15 02:23:29 +00:00
```ruby
its('tags') { should include 'Owner' }
```
2017-12-12 18:20:22 +00:00
2018-02-15 02:23:29 +00:00
### xxx\_tag
2017-12-12 18:20:22 +00:00
To get the value of the tag, a number of preoprties have been created from the tags that are set.
For example, if the following tag is set on a resource:
| Tag Name | Value |
|----------|-------|
| Owner | Russell Seymour |
Then a property is available called `Owner_tag`.
2017-03-02 10:11:18 +00:00
2018-02-15 02:23:29 +00:00
```ruby
its('Owner_tag') { should cmp 'Russell Seymour' }
2017-03-02 10:11:18 +00:00
```
2017-12-12 18:20:22 +00:00
Note: The tag name is case sensitive which makes the test case sensitive. E.g. `owner_tag` does not equal `Owner_tag`.
2017-03-02 10:11:18 +00:00
## Examples
The following examples show how to use this InSpec audit resource
2017-12-12 18:20:22 +00:00
Please refer the integration tests for more in depth examples:
- [Resource Group](../../test/integration/verify/controls/resource_group.rb)
2017-03-02 10:11:18 +00:00
### Test Resource Group has the correct number of resources
2018-02-15 02:23:29 +00:00
```ruby
describe azure_resource_group_resource_counts(name: 'Inspec-Azure') do
its('total') { should eq 7}
```
2017-03-02 10:11:18 +00:00
### Ensure that the Resource Group contains the correct resources
2018-02-15 02:23:29 +00:00
```ruby
describe azure_resource_group_resource_counts(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
```
## References
- [Azure Ruby SDK - Resources](https://github.com/Azure/azure-sdk-for-ruby/tree/master/management/azure_mgmt_resources)