inspec/docs/resources/azure_resource_group.md.erb

227 lines
6.6 KiB
Text
Raw Normal View History

---
title: About the azure_resource_group_resource_counts Resource
---
2018-02-15 02:23:29 +00:00
# azure\_resource\_group\_resource\_counts
Use the `azure_resource_group_resource_counts` InSpec audit resource to check the number of Azure resources in a resource group
## Syntax
The name of the resource group is specified as a parameter on the resource:
```ruby
describe azure_resource_group(name: 'MyResourceGroup') do
its('property') { should eq 'value' }
end
```
where
* 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
* `value` is the expected output from the matcher
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.
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
```
2018-02-15 02:23:29 +00:00
## Properties
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`
2018-02-15 02:23:29 +00:00
<br>
2018-02-15 02:23:29 +00:00
## Property Examples
### 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.
This is in the format `/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>`.
2018-02-15 02:23:29 +00:00
### provisioning\_state
The provisioning state of the resource group.
2018-02-15 02:23:29 +00:00
### subscription\_id
Returns the subscription ID which contains the resource group.
This is derived from the `id`.
### total
The total number of resources in the resource group
2018-02-15 02:23:29 +00:00
### nic\_count
The number of network interface cards in the resource group
2018-02-15 02:23:29 +00:00
### vm\_count
The number of virtual machines in the resource group
2018-02-15 02:23:29 +00:00
### vnet\_count
The number of virtual networks in the resource group
2018-02-15 02:23:29 +00:00
### sa\_count
The number of storage accounts in the resource group
2018-02-15 02:23:29 +00:00
### public\_ip\_count
The number of Public IP Addresses in the resource group
2018-02-15 02:23:29 +00:00
### managed\_disk\_image\_count
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
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 }
```
## 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.
2018-02-15 02:23:29 +00:00
### have\_tags
This is a simple test to see if the machine has tags assigned to it or not.
2018-02-15 02:23:29 +00:00
```ruby
it { should have_tags }
```
2018-02-15 02:23:29 +00:00
### tag\_count
Returns the number of tags that are assigned to the resource
2018-02-15 02:23:29 +00:00
```ruby
its ('tag_count') { should eq 2 }
```
### tags
It is possible to check if a specific tag has been set on the resource.
2018-02-15 02:23:29 +00:00
```ruby
its('tags') { should include 'Owner' }
```
2018-02-15 02:23:29 +00:00
### xxx\_tag
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`.
2018-02-15 02:23:29 +00:00
```ruby
its('Owner_tag') { should cmp 'Russell Seymour' }
```
Note: The tag name is case sensitive which makes the test case sensitive. E.g. `owner_tag` does not equal `Owner_tag`.
## Examples
The following examples show how to use this InSpec audit resource
Please refer the integration tests for more in depth examples:
- [Resource Group](../../test/integration/verify/controls/resource_group.rb)
### 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}
```
### 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)