Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Add InSpec support for google_compute_region(s). #128

Merged
merged 1 commit into from
Mar 19, 2019
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
125 changes: 77 additions & 48 deletions docs/resources/google_compute_region.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,106 @@
---
title: About the google_compute_region Resource
title: About the google_compute_region resource
platform: gcp
---

# google\_compute\_region

Use the `google_compute_region` InSpec audit resource to test properties of a single GCP compute region.

<br>

## Syntax
A `google_compute_region` is used to test a Google Region resource

A `google_compute_region` resource block declares the tests for a single GCP region by project and name.

describe google_compute_region(project: 'chef-inspec-gcp', region: 'us-east1') do
its('name') { should match 'us-east1' }
## Examples
```
describe google_compute_region(project: 'chef-gcp-inspec', name: 'europe-west2') do
it { should exist }
it { should be_up }
its('zone_names') { should include "#{gcp_location}-a" }
end

describe google_compute_region(project: 'chef-gcp-inspec', name: 'notthere') do
it { should_not exist }
end

```
### Test that a GCP compute region exists
```
describe google_compute_region(project: 'chef-inspec-gcp', region: 'europe-west2') do
it { should exist }
end
```
### Test that a GCP compute region is in the expected state
```
describe google_compute_region(project: 'chef-inspec-gcp', region: 'europe-west2') do
its('status') { should eq 'UP' }
# or equivalently
it { should be_up }
end
```
### Test a GCP compute region identifier
```
describe google_compute_region(project: 'chef-inspec-gcp', region: "asia-east1") do
its('id') { should eq "1220" }
end
```
### Check that a region is associated with the expected zone fully qualified name
```
describe google_compute_region(project: 'chef-inspec-gcp', region: "asia-east1") do
its('zones') { should include "https://www.googleapis.com/compute/v1/projects/spaterson-project/zones/asia-east1-a" }
end
```
### Check that a region is associated with the expected zone short name
```
describe google_compute_region(project: 'chef-inspec-gcp', region: "asia-east1") do
its('zone_names') { should include "asia-east1-a" }
end
```

The `zone_names` property is also useful for subsequently looping over associated `google_compute_zone` resources. For example:
```
google_compute_region(project: 'chef-inspec-gcp', region: "asia-east1").zone_names.each do |zone_name|
describe google_compute_zone(project: 'chef-inspec-gcp', name: zone_name) do
it { should be_up }
end
end

<br>
```

## Examples
## Properties
Properties that can be accessed from the `google_compute_region` resource:

The following examples show how to use this InSpec audit resource.
* `creation_timestamp`: Creation timestamp in RFC3339 text format.

### Test that a GCP compute region exists
* `deprecated`: The deprecation state of this resource.

describe google_compute_region(project: 'chef-inspec-gcp', region: 'europe-west2') do
it { should exist }
end
* `deleted`: An optional RFC3339 timestamp on or after which the deprecation state of this resource will be changed to DELETED.

### Test that a GCP compute region is in the expected state
* `deprecated`: An optional RFC3339 timestamp on or after which the deprecation state of this resource will be changed to DEPRECATED.

describe google_compute_region(project: 'chef-inspec-gcp', region: 'europe-west2') do
its('status') { should eq 'UP' }
# or equivalently
it { should be_up }
end
* `obsolete`: An optional RFC3339 timestamp on or after which the deprecation state of this resource will be changed to OBSOLETE.

### Test a GCP compute region identifier
* `replacement`: The URL of the suggested replacement for a deprecated resource. The suggested replacement resource must be the same kind of resource as the deprecated resource.

describe google_compute_region(project: 'chef-inspec-gcp', region: "asia-east1") do
its('id') { should eq 1220 }
end
* `state`: The deprecation state of this resource. This can be DEPRECATED, OBSOLETE, or DELETED. Operations which create a new resource using a DEPRECATED resource will return successfully, but with a warning indicating the deprecated resource and recommending its replacement. Operations which use OBSOLETE or DELETED resources will be rejected and result in an error.

### Check that a region is associated with the expected zone fully qualified name
* `description`: An optional description of this resource.

describe google_compute_region(project: 'chef-inspec-gcp', region: "asia-east1") do
its('zones') { should include "https://www.googleapis.com/compute/v1/projects/spaterson-project/zones/asia-east1-a" }
end

### Check that a region is associated with the expected zone short name
* `id`: The unique identifier for the resource.

describe google_compute_region(project: 'chef-inspec-gcp', region: "asia-east1") do
its('zone_names') { should include "asia-east1-a" }
end
* `name`: Name of the resource.

The `zone_names` property is also useful for subsequently looping over associated `google_compute_zone` resources. For example:
* `quotas`: Quotas assigned to this region.

google_compute_region(project: 'chef-inspec-gcp', region: "asia-east1").zone_names.each do |zone_name|
describe google_compute_zone(project: 'chef-inspec-gcp', name: zone_name) do
it { should be_up }
end
end
* `metric`: Name of the quota metric.

<br>
* `limit`: Quota limit for this metric.

## Properties
* `usage`: Current usage of this metric.

* `owner`: Owning resource. This is the resource on which this quota is applied.

* `status`: Status of the region, either UP or DOWN.

* `creation_timestamp`, `description`, `id`, `kind`, `name`, `quotas`, `status`, `zones`, `zone_names`
* `zones`: List of zones within the region

<br>


## GCP Permissions

Ensure the [Compute Engine API](https://console.cloud.google.com/apis/library/compute.googleapis.com/) is enabled for the project where the resource is located.
Ensure the [Compute Engine API](https://console.cloud.google.com/apis/library/compute.googleapis.com/) is enabled for the current project.
121 changes: 60 additions & 61 deletions docs/resources/google_compute_regions.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,76 @@
---
title: About the google_compute_regions Resource
title: About the google_compute_regions resource
platform: gcp
---

# google\_compute\_regions

Use the `google_compute_regions` InSpec audit resource to test properties of all, or a filtered group of, GCP compute regions for a project.

<br>

## Syntax

A `google_compute_regions` resource block collects GCP regions by project then tests that group.

describe google_compute_regions(project: 'chef-inspec-gcp') do
it { should exist }
end

Use this InSpec resource to enumerate IDs then test in-depth using `google_compute_region`.

google_compute_regions(project: 'chef-inspec-gcp').region_names.each do |region_name|
describe google_compute_region(project: 'chef-inspec-gcp', region: region_name) do
it { should be_up }
end
end

<br>
A `google_compute_regions` is used to test a Google Region resource

## Examples

The following examples show how to use this InSpec audit resource.

### Test that there are more than a specified number of regions available for the project

describe google_compute_regions(project: 'chef-inspec-gcp') do
its('count') { should be >= 10}
```
describe google_compute_regions(project: 'chef-gcp-inspec') do
its('count') { should be >= 1 }
its('region_names') { should include "#{gcp_location}" }
its('region_statuses') { should_not include "DOWN" }
its('region_ids') { should include gcp_region_id }
end

```
### Use this InSpec resource to enumerate IDs then test in-depth using `google_compute_region`
```
google_compute_regions(project: 'chef-inspec-gcp').region_names.each do |region_name|
describe google_compute_region(project: 'chef-inspec-gcp', region: region_name) do
it { should be_up }
end

### Test that an expected region is available for the project

describe google_compute_regions(project: 'chef-inspec-gcp') do
its('region_names') { should include 'europe-west2' }
end
```

### Test that there are more than a specified number of regions available for the project
```
describe google_compute_regions(project: 'chef-inspec-gcp') do
its('count') { should be >= 10}
end
```
### Test that an expected region is available for the project
```
describe google_compute_regions(project: 'chef-inspec-gcp') do
its('region_names') { should include 'europe-west2' }
end
```
### Test whether any regions are in status "DOWN"
```
describe google_compute_regions(project: 'chef-inspec-gcp') do
its('region_statuses') { should_not include "DOWN" }
end
```

### Test that a subset of all regions matching "europe*" are "UP"
```
google_compute_regions(project: gcp_project_id).where(region_name: /^europe/).region_names.each do |region_name|
describe google_compute_region(project: 'chef-inspec-gcp', region: region_name) do
it { should be_up }
end
end

### Test whether any regions are in status "DOWN"

describe google_compute_regions(project: 'chef-inspec-gcp') do
its('region_statuses') { should_not include "DOWN" }
end

### Test that a subset of all regions matching "europe*" are "UP"

describe google_compute_regions(project: gcp_project_id).where(region_name: /^europe/).region_names.each do |region_name|
describe google_compute_region(project: 'chef-inspec-gcp', region: region_name) do
it { should be_up }
end
end

<br>

## Filter Criteria

This resource supports the following filter criteria: `region_id`; `region_name` and `region_status`. Any of these may be used with `where`, as a block or as a method.
```

## Properties
Properties that can be accessed from the `google_compute_regions` resource:

See [google_compute_region.md](google_compute_region.md) for more detailed information
* `creation_timestamps`: an array of `google_compute_region` creation_timestamp
* `deprecateds`: an array of `google_compute_region` deprecated
* `descriptions`: an array of `google_compute_region` description
* `region_ids`: an array of `google_compute_region` id
* `region_names`: an array of `google_compute_region` name
* `quotas`: an array of `google_compute_region` quotas
* `region_statuses`: an array of `google_compute_region` status
* `zones`: an array of `google_compute_region` zones

* `region_ids` - an array of google_compute_region identifier integers
* `region_names` - an array of google_compute_region name strings
* `region_statuses`- an array of google_compute_region statuses

<br>

## Filter Criteria
This resource supports all of the above properties as filter criteria, which can be used
with `where` as a block or a method.

## GCP Permissions

Ensure the [Compute Engine API](https://console.cloud.google.com/apis/library/compute.googleapis.com/) is enabled for the project.
Ensure the [Compute Engine API](https://console.cloud.google.com/apis/library/compute.googleapis.com/) is enabled for the current project.
41 changes: 41 additions & 0 deletions libraries/google/compute/property/region_deprecated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
#
# ----------------------------------------------------------------------------
#
# This file is automatically generated by Magic Modules and manual
# changes will be clobbered when the file is regenerated.
#
# Please read more about how to change this file in README.md and
# CONTRIBUTING.md located at the root of this package.
#
# ----------------------------------------------------------------------------
module GoogleInSpec
module Compute
module Property
class RegionDeprecated
attr_reader :deleted

attr_reader :deprecated

attr_reader :obsolete

attr_reader :replacement

attr_reader :state

def initialize(args = nil)
return if args.nil?
@deleted = parse_time_string(args['deleted'])
@deprecated = parse_time_string(args['deprecated'])
@obsolete = parse_time_string(args['obsolete'])
@replacement = args['replacement']
@state = args['state']
end
end
end
end
end
46 changes: 46 additions & 0 deletions libraries/google/compute/property/region_quotas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
#
# ----------------------------------------------------------------------------
#
# This file is automatically generated by Magic Modules and manual
# changes will be clobbered when the file is regenerated.
#
# Please read more about how to change this file in README.md and
# CONTRIBUTING.md located at the root of this package.
#
# ----------------------------------------------------------------------------
module GoogleInSpec
module Compute
module Property
class RegionQuotas
attr_reader :metric

attr_reader :limit

attr_reader :usage

attr_reader :owner

def initialize(args = nil)
return if args.nil?
@metric = args['metric']
@limit = args['limit']
@usage = args['usage']
@owner = args['owner']
end
end

class RegionQuotasArray
def self.parse(value)
return if value.nil?
return RegionQuotas.new(value) unless value.is_a?(::Array)
value.map { |v| RegionQuotas.new(v) }
end
end
end
end
end
Loading