Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESOURCE-109 Support sql vm groups #595

Merged
merged 6 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ The following is a list of static resources.
- [azure_sql_servers](https://docs.chef.io/inspec/resources/azure_sql_servers/)
- [azure_sql_virtual_machine](https://docs.chef.io/inspec/resources/azure_sql_virtual_machine.md)
- [azure_sql_virtual_machines](https://docs.chef.io/inspec/resources/azure_sql_virtual_machines.md)
- [azure_sql_virtual_machine_group](https://docs.chef.io/inspec/resources/azure_sql_virtual_machine_group.md)
- [azure_sql_virtual_machine_groups](https://docs.chef.io/inspec/resources/azure_sql_virtual_machine_groups.md)
- [azure_sql_virtual_machine_group_availability_listener](https://docs.chef.io/inspec/resources/azure_sql_virtual_machine_group_availability_listener.md)
- [azure_sql_virtual_machine_group_availability_listeners](https://docs.chef.io/inspec/resources/azure_sql_virtual_machine_group_availability_listeners.md)
- [azure_storage_account](https://docs.chef.io/inspec/resources/azure_storage_account/)
Expand Down
100 changes: 100 additions & 0 deletions docs/resources/azure_sql_virtual_machine_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
title: About the azure_sql_virtual_machine_group Resource
platform: azure
---

# azure_sql_virtual_machine_group

Use the `azure_sql_virtual_machine_group` InSpec audit resource to test properties related to an Azure SQL virtual machine group.

## Azure REST API version, endpoint and http client parameters

This resource interacts with api versions supported by the resource provider.
The `api_version` can be defined as a resource parameter.
If not provided, the latest version will be used.
For more information, refer to [`azure_generic_resource`](azure_generic_resource.md).

Unless defined, `azure_cloud` global endpoint, and default values for the http client will be used.
For more information, refer to the resource pack [README](../../README.md).

## Availability

### Installation

This resource is available in the [InSpec Azure resource pack](https://github.com/inspec/inspec-azure).
For an example `inspec.yml` file and how to set up your Azure credentials, refer to resource pack [README](../../README.md#Service-Principal).

## Syntax

`name`, `resource_group` are required parameters.

```ruby
describe azure_sql_virtual_machine_group(resource_group: 'RESOURCE_GROUP', name: 'SQL_VIRTUAL_MACHINE_GROUP') do
it { should exist }
its('type') { should eq 'Microsoft.SqlVirtualMachine/sqlVirtualMachineGroups' }
its('location') { should eq 'eastus' }
end
```

```ruby
describe azure_sql_virtual_machine_group(resource_group: 'RESOURCE_GROUP', name: 'SQL_VIRTUAL_MACHINE_GROUP') do
it { should exist }
end
```

## Parameters

`name` _(required)_

Name of the Azure SQL virtual machine group to test.

`resource_group` _(required)_

Azure resource group that the targeted resource resides in.

## Properties

| Property | Description |
|--------------------------|------------------------------------------------------------------|
| id | Resource Id. |
| name | Resource name. |
| type | Resource type. `Microsoft.SqlVirtualMachine/sqlVirtualMachineGroups`|
| location | The Geo-location where the resource lives. |
| properties | The properties of the SQL virtual machine group. |
| properties.provisioningState | State of the resource. |


For properties applicable to all resources, such as `type`, `name`, `id`, `properties`, refer to [`azure_generic_resource`](azure_generic_resource.md#properties).

Also, refer to [Azure documentation](https://docs.microsoft.com/en-us/rest/api/sqlvm/sql-virtual-machine-groups/get) for other properties available.

## Examples

### Test that the SQL virtual machine group is provisioned successfully.

```ruby
describe azure_sql_virtual_machine_group(resource_group: 'RESOURCE_GROUP', name: 'SQL_VIRTUAL_MACHINE_GROUP') do
its('properties.provisioningState') { should eq 'Succeeded' }
end
```

## Matchers

This InSpec audit resource has the following special matchers. For a full list of available matchers, please visit our [Universal Matchers page](/inspec/matchers/).

### exists

```ruby
# If a SQL virtual machine group is found it will exist
describe azure_sql_virtual_machine_group(resource_group: 'RESOURCE_GROUP', name: 'SQL_VIRTUAL_MACHINE_GROUP') do
it { should exist }
end
# if SQL virtual machine group is not found it will not exist
describe azure_sql_virtual_machine_group(resource_group: 'RESOURCE_GROUP', name: 'SQL_VIRTUAL_MACHINE_GROUP') do
it { should_not exist }
end
```

## Azure Permissions

Your [Service Principal](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal) must be setup with a `reader` role on the subscription you wish to test.
94 changes: 94 additions & 0 deletions docs/resources/azure_sql_virtual_machine_groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: About the azure_sql_virtual_machine_groups Resource
platform: azure
---

# azure_sql_virtual_machine_groups

Use the `azure_sql_virtual_machine_groups` InSpec audit resource to test properties related to all Azure SQL virtual machine groups.

## Azure REST API version, endpoint and http client parameters

This resource interacts with api versions supported by the resource provider.
The `api_version` can be defined as a resource parameter.
If not provided, the latest version will be used.
For more information, refer to [`azure_generic_resource`](azure_generic_resource.md).

Unless defined, `azure_cloud` global endpoint, and default values for the http client will be used.
For more information, refer to the resource pack [README](../../README.md).

## Availability

### Installation

This resource is available in the [InSpec Azure resource pack](https://github.com/inspec/inspec-azure).
For an example `inspec.yml` file and how to set up your Azure credentials, refer to resource pack [README](../../README.md#Service-Principal).

## Syntax

An `azure_sql_virtual_machine_groups` resource block returns all Azure SQL Virtual Machine Groups.

```ruby
describe azure_sql_virtual_machine_groups do
#...
end
```

## Parameters

`resource_group` _(optional)_

Azure resource group that the targeted resource resides in.

## Properties

|Property | Description | Filter Criteria<superscript>*</superscript> |
|--------------------------------|------------------------------------------------------------------------|------------------|
| ids | A list of resource IDs. | `id` |
| names | A list of resource names. | `name` |
| types | A list of the resource types. | `type` |
| properties | A list of properties for all the SQL virtual machine groups. | `properties` |
| locations | A list of the resource locations. | `location` |
| provisioningStates | A list of provisioning states of the SQL virtual machine groups. | `provisioningState`|

<superscript>*</superscript> For information on how to use filter criteria on plural resources refer to [FilterTable usage](https://github.com/inspec/inspec/blob/master/dev-docs/filtertable-usage.md).

## Examples

### Loop through SQL virtual machine groups by their names.

```ruby
azure_sql_virtual_machine_groups(resource_group: 'RESOURCE_GROUP').names.each do |name|
describe azure_sql_virtual_machine_group(resource_group: 'RESOURCE_GROUP', name: name) do
it { should exist }
end
end
```

### Test that there are SQL virtual machine groups that are successfully provisioned.

```ruby
describe azure_sql_virtual_machine_groups(resource_group: 'RESOURCE_GROUP').where(provisioningState: 'Succeeded') do
it { should exist }
end
```

## Matchers

This InSpec audit resource has the following special matchers. For a full list of available matchers, please visit our [Universal Matchers page](https://www.inspec.io/docs/reference/matchers/).

### exists

```ruby
# Should not exist if no SQL Virtual Machine Groups are present
describe azure_sql_virtual_machine_groups(resource_group: 'RESOURCE_GROUP') do
it { should_not exist }
end
# Should exist if the filter returns at least one SQL Virtual Machine Groups
describe azure_sql_virtual_machine_groups(resource_group: 'RESOURCE_GROUP') do
it { should exist }
end
```
## Azure Permissions

Your [Service Principal](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal) must be setup with a `reader` role on the subscription you wish to test.
22 changes: 22 additions & 0 deletions libraries/azure_sql_virtual_machine_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'azure_generic_resource'

class AzureSQLVirtualMachineGroup < AzureGenericResource
name 'azure_sql_virtual_machine_group'
desc 'Retrieves and verifies the settings of an Azure SQL Virtual Machine Group.'
example <<-EXAMPLE
describe azure_sql_virtual_machine_group(resource_group: 'inspec-def-rg', name: 'fabric-app') do
it { should exist }
end
EXAMPLE

def initialize(opts = {})
raise ArgumentError, 'Parameters must be provided in an Hash object.' unless opts.is_a?(Hash)

opts[:resource_provider] = specific_resource_constraint('Microsoft.SqlVirtualMachine/sqlVirtualMachineGroups', opts)
super(opts, true)
end

def to_s
super(AzureSQLVirtualMachineGroup)
end
end
33 changes: 33 additions & 0 deletions libraries/azure_sql_virtual_machine_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'azure_generic_resources'

class AzureSQLVirtualMachineGroups < AzureGenericResources
name 'azure_sql_virtual_machine_groups'
desc 'Verifies settings for a collection of Azure SQL Virtual Machine Groups'
example <<-EXAMPLE
describe azure_sql_virtual_machine_groups do
it { should exist }
end
EXAMPLE

def initialize(opts = {})
raise ArgumentError, 'Parameters must be provided in an Hash object.' unless opts.is_a?(Hash)

opts[:resource_provider] = specific_resource_constraint('Microsoft.SqlVirtualMachine/sqlVirtualMachineGroups', opts)
super(opts, true)
return if failed_resource?

populate_filter_table_from_response
end

def to_s
super(AzureSQLVirtualMachineGroups)
end

private

def populate_table
@resources.each do |resource|
@table << resource.merge(resource[:properties]).merge(resource.dig(:properties, :wsfcDomainProfile))
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
location = input(:location, value: '')
rg = input(:resource_group, value: '')

control 'test the properties of an Azure SQL Virtual Machine Group' do
describe azure_sql_virtual_machine_group(resource_group: rg, name: 'inspec-sql-vm-group') do
it { should exist }
its('name') { should eq 'inspec-sql-vm-group' }
its('properties.sqlImageSku') { should eq 'Enterprise' }
its('location') { should eq location.downcase.gsub("\s", '') }
its('properties.wsfcDomainProfile.domainFqdn') { should eq 'testdomain.com' }
its('properties.provisioningState') { should eq 'Succeeded' }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
location = input(:location, value: '')

control 'test the properties of all Azure SQL Virtual Machine Groups' do
describe azure_sql_virtual_machine_groups do
it { should exist }
its('names') { should include 'inspec-sql-vm-group' }
its('sqlImageSkus') { should include 'Enterprise' }
its('locations') { should include location.downcase.gsub("\s", '') }
its('domainFqdns') { should include 'testdomain.com' }
its('provisioningStates') { should include 'Succeeded' }
end
end
17 changes: 17 additions & 0 deletions test/unit/resources/azure_sql_virtual_machine_group_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'helper'
require 'azure_sql_virtual_machine_group'

class AzureSQLVirtualMachineGroupConstructorTest < Minitest::Test
def test_empty_param_not_ok
assert_raises(ArgumentError) { AzureSQLVirtualMachineGroup.new }
end

# resource_provider should not be allowed.
def test_resource_provider_not_ok
assert_raises(ArgumentError) { AzureSQLVirtualMachineGroup.new(resource_provider: 'some_type') }
end

def test_resource_group_name_alone_not_ok
assert_raises(ArgumentError) { AzureSQLVirtualMachineGroup.new(resource_group: 'test') }
end
end
21 changes: 21 additions & 0 deletions test/unit/resources/azure_sql_virtual_machine_groups_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative 'helper'
require 'azure_sql_virtual_machine_groups'

class AzureSQLVirtualMachineGroupsConstructorTest < Minitest::Test
# resource_type should not be allowed.
def test_resource_type_not_ok
assert_raises(ArgumentError) { AzureSQLVirtualMachineGroups.new(resource_provider: 'some_type') }
end

def tag_value_not_ok
assert_raises(ArgumentError) { AzureSQLVirtualMachineGroups.new(tag_value: 'some_tag_value') }
end

def tag_name_not_ok
assert_raises(ArgumentError) { AzureSQLVirtualMachineGroups.new(tag_name: 'some_tag_name') }
end

def test_name_not_ok
assert_raises(ArgumentError) { AzureSQLVirtualMachineGroups.new(name: 'some_name') }
end
end