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

Commit

Permalink
Migrate compute_zone to MM
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
slevenick authored and Stuart Paterson committed Apr 6, 2020
1 parent 0394b37 commit 9d11cd5
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 183 deletions.
57 changes: 37 additions & 20 deletions docs/resources/google_compute_zone.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
---
title: About the google_compute_zone Resource
title: About the google_compute_zone resource
platform: gcp
---

# google\_compute\_zone

Use the `google_compute_zone` InSpec audit resource to test properties of a single GCP compute zone.

<br>

## Syntax
A `google_compute_zone` is used to test a Google Zone resource

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

describe google_compute_zone(project: 'chef-inspec-gcp', zone: 'us-east1-b') do
its('name') { should match 'us-east1-b' }
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.
```
describe google_compute_zone(project: 'chef-gcp-inspec', "us-central1-a") do
it { should exist }
it { should be_up }
end
```

### Test that a GCP compute zone exists

Expand All @@ -43,15 +38,37 @@ The following examples show how to use this InSpec audit resource.
its('available_cpu_platforms') { should include "Intel Skylake" }
end

<br>

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


* `creation_timestamp`: Creation timestamp in RFC3339 text format.

* `deprecated`: The deprecation status associated with this machine type.

* `deleted`: An optional RFC3339 timestamp on or after which the state of this resource is intended to change to DELETED. This is only informational and the status will not change unless the client explicitly changes it.

* `deprecated`: An optional RFC3339 timestamp on or after which the state of this resource is intended to change to DEPRECATED. This is only informational and the status will not change unless the client explicitly changes it.

* `obsolete`: An optional RFC3339 timestamp on or after which the state of this resource is intended to change to OBSOLETE. This is only informational and the status will not change unless the client explicitly changes it.

* `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.

* `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.

* `description`: An optional textual description of the resource.

* `id`: The unique identifier for the resource.

* `name`: Name of the resource.

* `region`: The region where the zone is located.

* `available_cpu_platforms`, `creation_timestamp`, `description`, `id`, `kind`, `name`, `region`, `status`, `region_name`
* `status`: The status of the zone.

<br>
* `available_cpu_platforms`: The available CPU platforms in this zone


## 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.
99 changes: 27 additions & 72 deletions docs/resources/google_compute_zones.md
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.
51 changes: 51 additions & 0 deletions libraries/google/compute/property/zone_deprecated.rb
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
114 changes: 78 additions & 36 deletions libraries/google_compute_zone.rb
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
Loading

0 comments on commit 9d11cd5

Please sign in to comment.