Skip to content

Commit

Permalink
Merge pull request #357 from inspec/f/AcceleratorTypes
Browse files Browse the repository at this point in the history
RESOURCE-73 F/accelerator types
  • Loading branch information
sa-progress authored Feb 21, 2022
2 parents b1e19ee + a7a6ae9 commit 9888dcc
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 7 deletions.
50 changes: 50 additions & 0 deletions docs/resources/google_compute_accelerator_type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: About the google_compute_accelerator_type resource
platform: gcp
---

## Syntax
A `google_compute_accelerator_type` is used to test a Google AcceleratorType resource

## Examples
```
describe google_compute_accelerator_type(project: 'chef-gcp-inspec', zone: 'us-east1-b', name: 'accelerator_id') do
it { should exist }
it { should be_up }
end
```

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


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

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

* `state`: 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.
Possible values:
* DEPRECATED
* OBSOLETE
* DELETED

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

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

* `name`: Name of the resource.

* `zone`: The name of the zone where the accelerator type resides.


## GCP Permissions

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

## Syntax
A `google_compute_accelerator_types` is used to test a Google AcceleratorType resource

## Examples
```
describe google_compute_accelerator_types(project: 'chef-gcp-inspec', zone: 'us-east1-b') do
it { should exist }
it { should be_up }
end
```

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

See [google_compute_accelerator_type.md](google_compute_accelerator_type.md) for more detailed information
* `creation_timestamps`: an array of `google_compute_accelerator_type` creation_timestamp
* `deprecateds`: an array of `google_compute_accelerator_type` deprecated
* `descriptions`: an array of `google_compute_accelerator_type` description
* `ids`: an array of `google_compute_accelerator_type` id
* `names`: an array of `google_compute_accelerator_type` name
* `zones`: an array of `google_compute_accelerator_type` zone

## 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 current project.
49 changes: 49 additions & 0 deletions libraries/google/compute/property/acceleratortype_deprecated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** Type: MMv1 ***
#
# ----------------------------------------------------------------------------
#
# 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 AcceleratorTypeDeprecated
attr_reader :state

attr_reader :deprecated

attr_reader :obsolete

attr_reader :replacement

def initialize(args = nil, parent_identifier = nil)
return if args.nil?
@parent_identifier = parent_identifier
@state = parse_time_string(args['state'])
@deprecated = parse_time_string(args['deprecated'])
@obsolete = parse_time_string(args['obsolete'])
@replacement = args['replacement']
@state = args['state']
end

def to_s
"#{@parent_identifier} AcceleratorTypeDeprecated"
end

# Handles parsing RFC3339 time string
def parse_time_string(time_string)
time_string ? Time.parse(time_string) : nil
end
end
end
end
end
71 changes: 71 additions & 0 deletions libraries/google_compute_accelerator_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** Type: MMv1 ***
#
# ----------------------------------------------------------------------------
#
# 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/acceleratortype_deprecated'

# A provider to manage Compute Engine resources.
class ComputeAcceleratorType < GcpResourceBase
name 'google_compute_accelerator_type'
desc 'AcceleratorType'
supports platform: 'gcp'

attr_reader :params
attr_reader :creation_timestamp
attr_reader :deprecated
attr_reader :description
attr_reader :id
attr_reader :name
attr_reader :zone

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 parse
@creation_timestamp = parse_time_string(@fetched['creationTimestamp'])
@deprecated = GoogleInSpec::Compute::Property::AcceleratorTypeDeprecated.new(@fetched['deprecated'], to_s)
@description = @fetched['description']
@id = @fetched['id']
@name = @fetched['name']
@zone = @fetched['zone']
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
"AcceleratorType #{@params[:name]}"
end

private

def product_url(_ = nil)
'https://compute.googleapis.com/compute/v1/'
end

def resource_base_url
'projects/{{project}}/zones/{{zone}}/acceleratorTypes/{{name}}'
end
end
94 changes: 94 additions & 0 deletions libraries/google_compute_accelerator_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** Type: MMv1 ***
#
# ----------------------------------------------------------------------------
#
# 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'
class ComputeAcceleratorTypes < GcpResourceBase
name 'google_compute_accelerator_types'
desc 'AcceleratorType plural resource'
supports platform: 'gcp'

attr_reader :table

filter_table_config = FilterTable.create

filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
filter_table_config.add(:deprecateds, field: :deprecated)
filter_table_config.add(:descriptions, field: :description)
filter_table_config.add(:ids, field: :id)
filter_table_config.add(:names, field: :name)
filter_table_config.add(:zones, field: :zone)

filter_table_config.connect(self, :table)

def initialize(params = {})
super(params.merge({ use_http_transport: true }))
@params = params
@table = fetch_wrapped_resource('items')
end

def fetch_wrapped_resource(wrap_path)
# fetch_resource returns an array of responses (to handle pagination)
result = @connection.fetch_all(product_url, resource_base_url, @params, 'Get')
return if result.nil?

# Conversion of string -> object hash to symbol -> object hash that InSpec needs
converted = []
result.each do |response|
next if response.nil? || !response.key?(wrap_path)
response[wrap_path].each do |hash|
hash_with_symbols = {}
hash.each_key do |key|
name, value = transform(key, hash)
hash_with_symbols[name] = value
end
converted.push(hash_with_symbols)
end
end

converted
end

def transform(key, value)
return transformers[key].call(value) if transformers.key?(key)

[key.to_sym, value]
end

def transformers
{
'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) },
'deprecated' => ->(obj) { return :deprecated, GoogleInSpec::Compute::Property::AcceleratorTypeDeprecated.new(obj['deprecated'], to_s) },
'description' => ->(obj) { return :description, obj['description'] },
'id' => ->(obj) { return :id, obj['id'] },
'name' => ->(obj) { return :name, obj['name'] },
'zone' => ->(obj) { return :zone, obj['zone'] },
}
end

# Handles parsing RFC3339 time string
def parse_time_string(time_string)
time_string ? Time.parse(time_string) : nil
end

private

def product_url(_ = nil)
'https://compute.googleapis.com/compute/v1/'
end

def resource_base_url
'projects/{{project}}/zones/{{zone}}/acceleratorTypes'
end
end
8 changes: 4 additions & 4 deletions test/integration/configuration/gcp_inspec_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def initialize(seed = nil)
end
@config = {
# Generic GCP resource parameters
:gcp_organization_id => '953310715741',
:gcp_project_name => "SPaterson Project",
:gcp_project_id => "spaterson-project",
:gcp_project_number => "1041358276233",
:gcp_organization_id => '827482578277',
:gcp_project_name => "ppradhan",
:gcp_project_id => "ppradhan",
:gcp_project_number => "165434197229",
# Determine the storage account name and the admin password
:gcp_location => "europe-west2",
:gcp_zone => "europe-west2-a",
Expand Down
9 changes: 6 additions & 3 deletions test/integration/configuration/mm-attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ dataproc_cluster:
num_instances: 1
machine_type: n1-standard-1
boot_disk_type: pd-ssd
boot_disk_size_gb: 15
boot_disk_size_gb: 30
worker_config:
num_instances: 2
machine_type: n1-standard-1
boot_disk_size_gb: 20
boot_disk_size_gb: 40
num_local_ssds: 1
preemptible_worker_config:
num_instances: 0
Expand Down Expand Up @@ -448,6 +448,9 @@ security_policy:
memcache_instance:
name: mem-instance

accelerator_type:
name: accelerator_id

container_engine_versions:
valid_master_version: '1.21.5-gke.1802'
valid_node_version: '1.21.5-gke.1802'
valid_node_version: '1.21.5-gke.1802'
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** Type: MMv1 ***
#
# ----------------------------------------------------------------------------
#
# 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.
#
# ----------------------------------------------------------------------------

title 'Test GCP google_compute_accelerator_type resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
accelerator_type = attribute('accelerator_type', default: null, description: 'The accelerator type')
control 'google_compute_accelerator_type-1.0' do
impact 1.0
title 'google_compute_accelerator_type resource test'

describe google_compute_accelerator_type(project: gcp_project_id, zone: 'us-east1-b', name: accelerator_type['name']) do
it { should exist }
it { should be_up }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** Type: MMv1 ***
#
# ----------------------------------------------------------------------------
#
# 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.
#
# ----------------------------------------------------------------------------

title 'Test GCP google_compute_accelerator_types resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
accelerator_type = attribute('accelerator_type', default: null, description: 'The accelerator type')
control 'google_compute_accelerator_types-1.0' do
impact 1.0
title 'google_compute_accelerator_types resource test'

describe google_compute_accelerator_types(project: gcp_project_id, zone: 'us-east1-b') do
it { should exist }
it { should be_up }
end
end

0 comments on commit 9888dcc

Please sign in to comment.