Skip to content

Commit

Permalink
Merge pull request #439 from inspec/support-azure-migrate-project-dat…
Browse files Browse the repository at this point in the history
…abases

Support Azure Migrate Project Databases
  • Loading branch information
sathish-progress authored Sep 16, 2021
2 parents e39cc61 + 29bc47a commit 7717481
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ The following is a list of static resources.
- [azure_mariadb_servers](docs/resources/azure_mariadb_servers.md)
- [azure_migrate_assessment_project](docs/resources/azure_migrate_assessment_project.md)
- [azure_migrate_assessment_projects](docs/resources/azure_migrate_assessment_projects.md)
- [azure_migrate_project_database](docs/resources/azure_migrate_project_database.md)
- [azure_migrate_project_databases](docs/resources/azure_migrate_project_databases.md)
- [azure_migrate_project_solution](docs/resources/azure_migrate_project_solution.md)
- [azure_migrate_project_solutions](docs/resources/azure_migrate_project_solutions.md)
- [azure_monitor_activity_log_alert](docs/resources/azure_monitor_activity_log_alert.md)
Expand Down
105 changes: 105 additions & 0 deletions docs/resources/azure_migrate_project_database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
title: About the azure_migrate_project_database Resource
platform: azure
---

# azure_migrate_project_database

Use the `azure_migrate_project_database` InSpec audit resource to test the properties related to an Azure Migrate project database.

## Azure REST API version, Endpoint, and HTTP Client Parameters

This resource interacts with API versions supported by the resource provider. The `api_version` is defined as a resource parameter.
If not provided, the latest version is 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 is 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` is a required parameter and `resource_group` is an optional parameter.

```ruby
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'PROJECT_DB_NAME') do
it { should exist }
its('name') { should eq 'PROJECT_DB_NAME' }
its('type') { should eq 'Microsoft.Migrate/MigrateProjects/Databases' }
its('solutionNames') { should include 'MIGRATEDBSOLUTION' }
end
```

```ruby
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'PROJECT_DB_NAME') do
it { should exist }
end
```

## Parameters

| Name | Description |
|----------------|----------------------------------------------------------------------------------|
| name | Name of the Azure Migrate project database to test. |
| resource_group | Azure resource group that the targeted resource resides in. |
| project_name | Azure Migrate assessment project. |

The parameter set should be provided for a valid query:

- `resource_group`, `project_name`, and `name`.

## Properties

| Property | Description |
|-------------------------------|------------------------------------------------------------------|
| id | Path reference to the migrate project database. |
| name | Unique name of a migrate project database. |
| type | Type of the object. `Microsoft.Migrate/MigrateProjects/Databases`|
| properties | Properties of the assessment. |
| properties.assessmentData | Assessment details of the database published by various sources. |
| assessmentIds | The database assessment scope/Ids. |
| migrationBlockersCounts | The number of blocking changes found. |
| breakingChangesCounts | The number of breaking changes found. |
| assessmentTargetTypes | The assessed target database types. |
| solutionNames | The names of the solutions that sent the data. |
| instanceIds | The database servers' instance Ids. |
| databaseNames | The name of the databases. |

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

Also, refer to [Azure documentation](https://docs.microsoft.com/en-us/rest/api/migrate/projects/databases/get-database) for other properties available. Any attribute in the response nested within properties is accessed with the key names separated by dots (`.`) and attributes nested in the assessmentData are pluralized and listed as a collection.

## Examples

### Test that migrate project database has a SQL assessmentTargetType

```ruby
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'PROJECT_DB_NAME') do
its('assessmentTargetTypes') { should include 'SQL' }
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 Migrate Project Database is found, it will exist
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'PROJECT_DB_NAME') do
it { should exist }
end

# if Migrate Project Database is not found, it will not exist
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'PROJECT_DB_NAME') 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 set up with a `contributor` role on the subscription you wish to test.
110 changes: 110 additions & 0 deletions docs/resources/azure_migrate_project_databases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: About the azure_migrate_project_database Resource
platform: azure
---

# azure_migrate_project_database

Use the `azure_migrate_project_database` InSpec audit resource to test the properties related to all Azure Migrate Project Databases within a project.

## Azure REST API Version, Endpoint, and HTTP Client Parameters

This resource interacts with API versions supported by the resource provider. The `api_version` is defined as a resource parameter.
If not provided, the latest version is 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 is 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_migrate_project_database` resource block returns all Azure Migrate Project Databases within a project.

```ruby
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME') do
#...
end
```

## Parameters

| Name | Description |
|----------------|----------------------------------------------------------------------------------|
| resource_group | Azure resource group that the targeted resource resides in. |
| project_name | Azure Migrate Project. |

The parameter set should be provided for a valid query:

- `resource_group` and `project_name`.

## Properties

|Property | Description | Filter Criteria<superscript>*</superscript> |
|--------------------------------|------------------------------------------------------------------------|------------------|
| ids | Path reference to the Project Databases. | `id` |
| names | Unique names for all Project Databases. | `name` |
| types | Type of the objects. | `type` |
| properties | A list of Properties for all the Project Databases. | `properties` |
| assessmentDatas | The assessment details of the database published by various sources. | `assessmentData` |
| assessmentIds | The database assessment scopes/Ids. | `assessmentId` |
| assessmentTargetTypes | The assessed target database types. | `assessmentTargetType` |
| breakingChangesCounts | The number of breaking changes found. | `breakingChangesCount` |
| compatibilityLevels | The compatibility levels of the database. | `compatibilityLevel` |
| databaseNames | The database names. | `databaseName` |
| databaseSizeInMBs | The sizes of the databases. | `databaseSizeInMB`|
| enqueueTimes | The list of times the message is enqueued. | `enqueueTime` |
| extendedInfos | The extended properties of all the database. | `extendedInfo` |
| instanceIds | The database server instance Ids. | `instanceId` |
| isReadyForMigrations | The values indicating whether the database is ready for migration. | `isReadyForMigration` |
| lastAssessedTimes | The time when the databases were last assessed. | `lastAssessedTime`|
| lastUpdatedTimes | The time of the last modifications of the database details. | `lastUpdatedTime`|
| migrationBlockersCounts | The number of blocking changes found. | `migrationBlockersCount` |
| solutionNames | The names of the solution that sent the data. | `solutionName` |

<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 migrate project databases by their names

```ruby
azure_migrate_project_databases(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME').names.each do |name|
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'NAME') do
it { should exist }
end
end
```

### Test there are migrate project databases are ready for migration

```ruby
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME').where{ isReadyForMigration.include?(true) } 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 Migrate Project Databases are present in the project and in the resource group
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME') do
it { should_not exist }
end

# Should exist if the filter returns at least one Migrate Project Databases in the project and in the resource group
describe azure_migrate_project_database(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME') 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 set up with a `contributor` role on the subscription you wish to test.
28 changes: 28 additions & 0 deletions libraries/azure_migrate_project_database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'azure_generic_resource'

class AzureMigrateProjectDatabase < AzureGenericResource
name 'azure_migrate_project_database'
desc 'Retrieves and verifies the settings of an Azure Migrate Project Database.'
example <<-EXAMPLE
describe azure_migrate_project_database(resource_group: 'migrated_vms', project_name: 'zoneA_migrate_assessment_project', name: 'sql_db') 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.Migrate/assessmentProjects', opts)
opts[:required_parameters] = %i(project_name)
opts[:resource_path] = [opts[:project_name], 'groups'].join('/')
super(opts, true)
assessment_data_hash = properties.assessmentData.each_with_object(Hash.new { |h, k| h[k] = [] }) do |assessment_data, hash|
assessment_data.each_pair { |key, value| hash[key.to_s.pluralize.to_sym] << value }
end
create_resource_methods(assessment_data_hash)
end

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

class AzureMigrateProjectDatabases < AzureGenericResources
name 'azure_migrate_project_databases'
desc 'Verifies settings for a collection of Azure Migrate Project Databases for a Azure Migrate Project in a Resource Group'
example <<-EXAMPLE
describe azure_migrate_project_databases(resource_group: 'migrated_vms', project_name: 'zoneA_migrate_project') 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.Migrate/migrateProjects', opts)
opts[:required_parameters] = %i(project_name)
opts[:resource_path] = [opts[:project_name], 'databases'].join('/')
super(opts, true)
return if failed_resource?

populate_filter_table_from_response
end

def to_s
super(AzureMigrateProjectDatabases)
end

private

def populate_table
@resources.each do |resource|
resource = resource.merge(resource[:properties])
assessment_data_hash = resource[:assessmentData].each_with_object(Hash.new { |h, k| h[k] = [] }) do |assessment_data, hash|
assessment_data.each_pair { |key, value| hash[key] << value }
end
@table << resource.merge(assessment_data_hash)
end
end
end
12 changes: 12 additions & 0 deletions test/integration/verify/controls/azure_migrate_project_database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource_group = input(:resource_group, value: '')
project_name = input(:project_name, value: 'inspec-migrate-integ')

control 'test the properties of an azure migrate project database' do
describe azure_migrate_project_database(resource_group: resource_group, project_name: project_name, name: 'myDB') do
it { should exist }
its('name') { should eq 'mydb' }
its('type') { should eq 'Microsoft.Migrate/MigrateProjects/Databases' }
its('assessmentIds') { should include 'myassessment' }
its('assessmentTargetTypes') { should include 'SQL' }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource_group = input(:resource_group, value: '')
project_name = input(:project_name, value: 'inspec-migrate-integ')

control 'test the properties of all azure migrate project database' do
describe azure_migrate_project_databases(resource_group: resource_group, project_name: project_name) do
it { should exist }
its('names') { should include 'mydb' }
its('types') { should include 'Microsoft.Migrate/MigrateProjects/Databases' }
its('assessmentIds') { should include 'myassessment' }
its('assessmentTargetTypes') { should include 'SQL' }
end
end
17 changes: 17 additions & 0 deletions test/unit/resources/azure_migrate_project_database_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'helper'
require 'azure_migrate_project_database'

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

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

def test_resource_group_name_alone_ok
assert_raises(ArgumentError) { AzureMigrateProjectDatabase.new(name: 'my-name', resource_group: 'test') }
end
end
21 changes: 21 additions & 0 deletions test/unit/resources/azure_migrate_project_databases_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative 'helper'
require 'azure_migrate_project_databases'

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

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

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

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

0 comments on commit 7717481

Please sign in to comment.