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

Support azure migrate project events #450

Merged
merged 8 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -164,6 +164,8 @@ The following is a list of static resources.
- [azure_management_groups](docs/resources/azure_management_groups.md)
- [azure_mariadb_server](docs/resources/azure_mariadb_server.md)
- [azure_mariadb_servers](docs/resources/azure_mariadb_servers.md)
- [azure_migrate_project_event](docs/resources/azure_migrate_project_event.md)
- [azure_migrate_project_events](docs/resources/azure_migrate_project_events.md)
- [azure_monitor_activity_log_alert](docs/resources/azure_monitor_activity_log_alert.md)
- [azure_monitor_activity_log_alerts](docs/resources/azure_monitor_activity_log_alerts.md)
- [azure_monitor_log_profile](docs/resources/azure_monitor_log_profile.md)
Expand Down
96 changes: 96 additions & 0 deletions docs/resources/azure_migrate_project_event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
title: About the azure_migrate_project_event Resource
platform: azure
---

# azure_migrate_project_event

Use the `azure_migrate_project_event` InSpec audit resource to test the properties related to an Azure Migrate project event.

## 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 are 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

`resource_group`, `project_name` and `name` are required parameters.

```ruby
describe azure_migrate_project_event(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'PROJECT_EVENT_NAME') do
it { should exist }
its('properties.instanceType') { should eq 'SERVERS' }
end
```

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

## Parameters

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

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 event. |
| name | Unique name of a Migrate project event. |
| type | Type of the object. `Microsoft.Migrate/MigrateProjects/Databases`|
| properties | Properties of the assessment. |

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/events/get-event) 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 the migrate project event is of servers instanceType

```ruby
describe azure_migrate_project_event(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'PROJECT_EVENT_NAME') do
its('properties.instanceType') { should eq 'SERVERS' }
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 event is found, it will exist
describe azure_migrate_project_event(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'PROJECT_EVENT_NAME') do
it { should exist }
end
# if migrate project event is not found, it will not exist
describe azure_migrate_project_event(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME', name: 'PROJECT_EVENT_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.
103 changes: 103 additions & 0 deletions docs/resources/azure_migrate_project_events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
title: About the azure_migrate_project_events Resource
platform: azure
---

# azure_migrate_project_events

Use the `azure_migrate_project_events` InSpec audit resource to test the properties related to all Azure Migrate project events 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_events` resource block returns all Azure Migrate project events within a project.

```ruby
describe azure_migrate_project_events(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 events. | `id` |
| names | Unique names for all project events. | `name` |
| types | Type of the objects. | `type` |
| properties | A list of properties for all the project events. | `properties` |
| instanceTypes | The instance types. | `instanceType` |
| machines | The machines for which the error is reported. | `machine` |
| errorCodes | The error codes. | `errorCode` |
| errorMessages | The error messages. | `errorMessage` |
| recommendations | The recommendations for the error. | `recommendation` |
| possibleCauses | The possible causes for the error. | `possibleCause` |
| solutions | The solutions for which the error is reported. | `solution` |
| clientRequestIds | The client request Ids of the payload for which the event is reported. | `clientRequestId` |

<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).

For more details on the available properties, refer to [Azure documentation](https://docs.microsoft.com/en-us/rest/api/migrate/projects/events/enumerate-events).

## Examples

### Loop through migrate project events by their names

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

### Test that there are migrate project events for databases

```ruby
describe azure_migrate_project_events(resource_group: 'RESOURCE_GROUP', project_name: 'PROJECT_NAME').where(instanceType: 'Databases') 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 events are present in the project and in the resource group
describe azure_migrate_project_events(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 events in the project and in the resource group
describe azure_migrate_project_events(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.
24 changes: 24 additions & 0 deletions libraries/azure_migrate_project_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'azure_generic_resource'

class AzureMigrateProjectEvent < AzureGenericResource
name 'azure_migrate_project_event'
desc 'Retrieves and verifies the settings of an Azure Migrate Project Machine.'
example <<-EXAMPLE
describe azure_migrate_project_event(resource_group: 'migrate_vms', project_name: 'zoneA_migrate_project', name: 'MigrateEvent01') 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], 'migrateEvents'].join('/')
super(opts, true)
end

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

class AzureMigrateProjectEvents < AzureGenericResources
name 'azure_migrate_project_events'
desc 'Verifies settings for a collection of Azure Migrate Project Events for a Azure Migrate Project in a Resource Group'
example <<-EXAMPLE
describe azure_migrate_project_events(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], 'migrateEvents'].join('/')
super(opts, true)
return if failed_resource?

populate_filter_table_from_response
end

def to_s
super(AzureMigrateProjectEvents)
end

private

def populate_table
@resources.each do |resource|
@table << resource.merge(resource[:properties])
end
end
end
11 changes: 11 additions & 0 deletions test/integration/verify/controls/azure_migrate_project_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource_group = input(:resource_group, value: '')
project_name = input(:project_name, value: 'inspec-migrate-integ')
sa-progress marked this conversation as resolved.
Show resolved Hide resolved
event_name = 'c042be9e-3d93-42cf-917f-b92c68318ded'

control 'test the properties of an azure migrate project event' do
describe azure_migrate_project_machine(resource_group: resource_group, project_name: project_name, name: event_name) do
it { should exist }
its('type') { should eq 'Microsoft.Migrate/MigrateProjects/MigrateEvents' }
its('properties.instanceType') { should eq 'Servers' }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource_group = input(:resource_group, value: '')
project_name = input(:project_name, value: 'inspec-migrate-integ')
sa-progress marked this conversation as resolved.
Show resolved Hide resolved

control 'test the properties of all azure migrate project events' do
describe azure_migrate_project_events(resource_group: resource_group, project_name: project_name) do
it { should exist }
its('types') { should include 'Microsoft.Migrate/MigrateProjects/MigrateEvents' }
its('instanceTypes') { should include 'Servers' }
end
end
17 changes: 17 additions & 0 deletions test/unit/resources/azure_migrate_project_event_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'helper'
require 'azure_migrate_project_event'

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

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

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

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

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

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

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