This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
forked from inspec/inspec-gcp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Modular Magician <magic-modules@google.com>
- Loading branch information
Showing
7 changed files
with
327 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,42 @@ | ||
--- | ||
title: About the google_compute_zones Resource | ||
title: About the google_compute_zones resource | ||
platform: gcp | ||
--- | ||
|
||
# google\_compute\_zones | ||
|
||
Use the `google_compute_zones` InSpec audit resource to test properties of all, or a filtered group of, GCP compute zones for a project in a particular zone. | ||
|
||
<br> | ||
|
||
## Syntax | ||
A `google_compute_zones` is used to test a Google Zone resource | ||
|
||
A `google_compute_zones` resource block collects GCP zones by project then tests that group. | ||
|
||
describe google_compute_zones(project: 'chef-inspec-gcp') do | ||
it { should exist } | ||
end | ||
|
||
Use this InSpec resource to enumerate IDs then test in-depth using `google_compute_zone`. | ||
|
||
google_compute_zones(project: 'chef-inspec-gcp').zone_names.each do |zone_name| | ||
describe google_compute_zone(project: 'chef-inspec-gcp', zone: zone_name) do | ||
it { should exist } | ||
its('kind') { should eq "compute#zone" } | ||
its('status') { should eq 'UP' } | ||
end | ||
end | ||
|
||
<br> | ||
## Beta Resource | ||
This resource has beta fields available. To retrieve these fields, include `beta: true` in the constructor for the resource | ||
|
||
## Examples | ||
|
||
The following examples show how to use this InSpec audit resource. | ||
|
||
### Test that there are no more than a specified number of zones available for the project | ||
|
||
describe google_compute_zones(project: 'chef-inspec-gcp') do | ||
its('count') { should be <= 100} | ||
end | ||
|
||
### Test the exact number of zones in the project | ||
|
||
describe google_compute_zones(project: 'chef-inspec-gcp') do | ||
its('zone_ids.count') { should cmp 9 } | ||
end | ||
|
||
### Test that an expected zone is available for the project | ||
|
||
describe google_compute_zones(project: 'chef-inspec-gcp') do | ||
its('zone_names') { should include "us-east1-b" } | ||
end | ||
|
||
### Test whether any zones are in status "DOWN" | ||
|
||
describe google_compute_zones(project: 'chef-inspec-gcp') do | ||
its('zone_statuses') { should_not include "DOWN" } | ||
end | ||
|
||
### Test that a subset of all zones matching "us*" are "UP" | ||
|
||
google_compute_zones(project: 'chef-inspec-gcp').where(zone_name: /^us/).zone_names.each do |zone_name| | ||
describe google_compute_zone(project: 'chef-inspec-gcp', zone: zone_name) do | ||
it { should exist } | ||
its('kind') { should eq "compute#zone" } | ||
its('status') { should eq 'UP' } | ||
end | ||
end | ||
|
||
<br> | ||
|
||
## Filter Criteria | ||
|
||
This resource supports the following filter criteria: `zone_id`; `zone_name` and `zone_status`. Any of these may be used with `where`, as a block or as a method. | ||
``` | ||
google_compute_zones(project: 'chef-gcp-inspec').zone_names.each do |zone_name| | ||
describe google_compute_zones(project: 'chef-gcp-inspec', name: zone_name) do | ||
it { should exist } | ||
it { should be_up } | ||
end | ||
end | ||
``` | ||
|
||
## Properties | ||
Properties that can be accessed from the `google_compute_zones` resource: | ||
|
||
See [google_compute_zone.md](google_compute_zone.md) for more detailed information | ||
* `creation_timestamps`: an array of `google_compute_zone` creation_timestamp | ||
* `deprecateds`: an array of `google_compute_zone` deprecated | ||
* `descriptions`: an array of `google_compute_zone` description | ||
* `zone_ids`: an array of `google_compute_zone` id | ||
* `zone_names`: an array of `google_compute_zone` name | ||
* `regions`: an array of `google_compute_zone` region | ||
* `zone_statuses`: an array of `google_compute_zone` status | ||
* `available_cpu_platforms`: an array of `google_compute_zone` available_cpu_platforms | ||
|
||
* `zone_ids` - an array of google_compute_zone identifier integers | ||
* `zone_names` - an array of google_compute_zone name strings | ||
* `zone_statuses`- an array of google_compute_zone 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 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 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 ZoneDeprecated | ||
attr_reader :deleted | ||
|
||
attr_reader :deprecated | ||
|
||
attr_reader :obsolete | ||
|
||
attr_reader :replacement | ||
|
||
attr_reader :state | ||
|
||
def initialize(args = nil, parent_identifier = nil) | ||
return if args.nil? | ||
@parent_identifier = parent_identifier | ||
@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 | ||
|
||
def to_s | ||
"#{@parent_identifier} ZoneDeprecated" | ||
end | ||
|
||
# Handles parsing RFC3339 time string | ||
def parse_time_string(time_string) | ||
time_string ? Time.parse(time_string) : nil | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,88 @@ | ||
# frozen_string_literal: true | ||
# 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. | ||
# | ||
# ---------------------------------------------------------------------------- | ||
require 'gcp_backend' | ||
require 'google/compute/property/zone_deprecated' | ||
|
||
module Inspec::Resources | ||
class GoogleComputeZone < GcpResourceBase | ||
name 'google_compute_zone' | ||
desc 'Verifies settings for a zone' | ||
|
||
example " | ||
describe google_compute_zone(project: 'chef-inspec-gcp', zone: 'us-east1-b') do | ||
it { should exist } | ||
its('name') { should match 'us-east1-b' } | ||
end | ||
" | ||
|
||
def initialize(opts = {}) | ||
# Call the parent class constructor | ||
super(opts) | ||
@display_name = opts[:name] | ||
catch_gcp_errors do | ||
@zone = @gcp.gcp_compute_client.get_zone(opts[:project], opts[:name]) | ||
create_resource_methods(@zone) | ||
end | ||
end | ||
# A provider to manage Compute Engine resources. | ||
class ComputeZone < GcpResourceBase | ||
name 'google_compute_zone' | ||
desc 'Zone' | ||
supports platform: 'gcp' | ||
|
||
# helper method for retrieving a region name | ||
def region_name | ||
return '' if !defined?(region) || region.nil? | ||
region.split('/').last | ||
end | ||
attr_reader :params | ||
attr_reader :creation_timestamp | ||
attr_reader :deprecated | ||
attr_reader :description | ||
attr_reader :id | ||
attr_reader :name | ||
attr_reader :region | ||
attr_reader :status | ||
attr_reader :available_cpu_platforms | ||
|
||
def exists? | ||
!@zone.nil? | ||
end | ||
def initialize(params) | ||
super(params.merge({ use_http_transport: true })) | ||
@params = params | ||
@fetched = @connection.fetch(product_url(params[:beta]), resource_base_url, params, 'Get') | ||
parse unless @fetched.nil? | ||
end | ||
|
||
def up? | ||
return false if !defined?(status) || status.nil? | ||
status == 'UP' | ||
end | ||
def parse | ||
@creation_timestamp = parse_time_string(@fetched['creationTimestamp']) | ||
@deprecated = GoogleInSpec::Compute::Property::ZoneDeprecated.new(@fetched['deprecated'], to_s) | ||
@description = @fetched['description'] | ||
@id = @fetched['id'] | ||
@name = @fetched['name'] | ||
@region = @fetched['region'] | ||
@status = @fetched['status'] | ||
@available_cpu_platforms = @fetched['availableCpuPlatforms'] | ||
end | ||
|
||
# Handles parsing RFC3339 time string | ||
def parse_time_string(time_string) | ||
time_string ? Time.parse(time_string) : nil | ||
end | ||
|
||
def exists? | ||
!@fetched.nil? | ||
end | ||
|
||
def to_s | ||
"Zone #{@display_name}" | ||
def to_s | ||
"Zone #{@params[:name]}" | ||
end | ||
|
||
# helper method for retrieving a region name | ||
def region_name | ||
@region&.split('/').last | ||
end | ||
|
||
def up? | ||
@status == 'UP' | ||
end | ||
|
||
private | ||
|
||
def product_url(beta = false) | ||
if beta | ||
'https://www.googleapis.com/compute/beta/' | ||
else | ||
'https://www.googleapis.com/compute/v1/' | ||
end | ||
end | ||
|
||
def resource_base_url | ||
'projects/{{project}}/zones/{{name}}' | ||
end | ||
end |
Oops, something went wrong.