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

Commit

Permalink
Inspec Support for cloud spanner
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
Experience Coder authored and modular-magician committed Oct 16, 2019
1 parent 735f38f commit 16f45e7
Show file tree
Hide file tree
Showing 15 changed files with 617 additions and 1 deletion.
31 changes: 31 additions & 0 deletions docs/resources/google_spanner_database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: About the google_spanner_database resource
platform: gcp
---

## Syntax
A `google_spanner_database` is used to test a Google Database resource

## Examples
```
describe google_spanner_database(project: 'chef-gcp-inspec', name: 'spdatabase') do
it { should exist }
its('name') { should eq 'spdatabase' }
end
```

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


* `name`: A unique identifier for the database, which cannot be changed after the instance is created. Values are of the form [a-z][-a-z0-9]*[a-z0-9].

* `extra_statements`: An optional list of DDL statements to run inside the newly created database. Statements can create tables, indexes, etc. These statements execute atomically with the creation of the database: if there is an error in any statement, the database is not created.

* `instance`: The instance to create the database on.


## GCP Permissions

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

## Syntax
A `google_spanner_databases` is used to test a Google Database resource

## Examples
```
describe google_spanner_databases(project: 'chef-gcp-inspec', name: 'spdatabase') do
it { should exist }
its('name') { should eq 'spdatabase' }
end
```

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

See [google_spanner_database.md](google_spanner_database.md) for more detailed information
* `names`: an array of `google_spanner_database` name
* `extra_statements`: an array of `google_spanner_database` extra_statements
* `instances`: an array of `google_spanner_database` instance

## 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 [Cloud Spanner API](https://console.cloud.google.com/apis/library/spanner.googleapis.com/) is enabled for the current project.
40 changes: 40 additions & 0 deletions docs/resources/google_spanner_instance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: About the google_spanner_instance resource
platform: gcp
---

## Syntax
A `google_spanner_instance` is used to test a Google Instance resource

## Examples
```
describe google_spanner_instance(project: 'chef-gcp-inspec', name: 'spinstance', config: 'regional-us-east1') do
it { should exist }
its('config') { should eq 'projects/'chef-gcp-inspec'/instanceConfigs/regional-us-east1' }
its('name') { should eq 'spinstance' }
its('display_name') { should eq 'inspectest' }
its('node_count') { should eq '2' }
its('labels') { should eq '' }
end
```

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


* `name`: A unique identifier for the instance, which cannot be changed after the instance is created. The name must be between 6 and 30 characters in length.

* `config`: The name of the instance's configuration (similar but not quite the same as a region) which defines defines the geographic placement and replication of your databases in this instance. It determines where your data is stored. Values are typically of the form `regional-europe-west1` , `us-central` etc. In order to obtain a valid list please consult the [Configuration section of the docs](https://cloud.google.com/spanner/docs/instances).

* `display_name`: The descriptive name for this instance as it appears in UIs. Must be unique per project and between 4 and 30 characters in length.

* `node_count`: The number of nodes allocated to this instance.

* `labels`: An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.


## GCP Permissions

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

## Syntax
A `google_spanner_instances` is used to test a Google Instance resource

## Examples
```
describe google_spanner_instances(project: 'chef-gcp-inspec', config: 'regional-us-east1') do
its('config') { should eq 'regional-us-east1' }
end
```

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

See [google_spanner_instance.md](google_spanner_instance.md) for more detailed information
* `names`: an array of `google_spanner_instance` name
* `configs`: an array of `google_spanner_instance` config
* `display_names`: an array of `google_spanner_instance` display_name
* `node_counts`: an array of `google_spanner_instance` node_count
* `labels`: an array of `google_spanner_instance` labels

## 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 [Cloud Spanner API](https://console.cloud.google.com/apis/library/spanner.googleapis.com/) is enabled for the current project.
64 changes: 64 additions & 0 deletions libraries/google_spanner_database.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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'

# A provider to manage Cloud Spanner resources.
class SpannerDatabase < GcpResourceBase
name 'google_spanner_database'
desc 'Database'
supports platform: 'gcp'

attr_reader :params
attr_reader :name
attr_reader :extra_statements
attr_reader :instance

def initialize(params)
super(params.merge({ use_http_transport: true }))
@params = params
@fetched = @connection.fetch(product_url, resource_base_url, params, 'Get')
parse unless @fetched.nil?
end

def parse
@name = @fetched['name']
@extra_statements = @fetched['extraStatements']
@instance = @fetched['instance']
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
"Database #{@params[:name]}"
end

private

def product_url
'https://spanner.googleapis.com/v1/'
end

def resource_base_url
'projects/{{project}}/instances/{{instance}}/databases/{{name}}'
end
end
88 changes: 88 additions & 0 deletions libraries/google_spanner_databases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 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'
class SpannerDatabases < GcpResourceBase
name 'google_spanner_databases'
desc 'Database plural resource'
supports platform: 'gcp'

attr_reader :table

filter_table_config = FilterTable.create

filter_table_config.add(:names, field: :name)
filter_table_config.add(:extra_statements, field: :extra_statements)
filter_table_config.add(:instances, field: :instance)

filter_table_config.connect(self, :table)

def initialize(params = {})
super(params.merge({ use_http_transport: true }))
@params = params
@table = fetch_wrapped_resource('databases')
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
{
'name' => ->(obj) { return :name, obj['name'] },
'extraStatements' => ->(obj) { return :extra_statements, obj['extraStatements'] },
'instance' => ->(obj) { return :instance, obj['instance'] },
}
end

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

private

def product_url
'https://spanner.googleapis.com/v1/'
end

def resource_base_url
'projects/{{project}}/instances/{{instance}}/databases'
end
end
68 changes: 68 additions & 0 deletions libraries/google_spanner_instance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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'

# A provider to manage Cloud Spanner resources.
class SpannerInstance < GcpResourceBase
name 'google_spanner_instance'
desc 'Instance'
supports platform: 'gcp'

attr_reader :params
attr_reader :name
attr_reader :config
attr_reader :display_name
attr_reader :node_count
attr_reader :labels

def initialize(params)
super(params.merge({ use_http_transport: true }))
@params = params
@fetched = @connection.fetch(product_url, resource_base_url, params, 'Get')
parse unless @fetched.nil?
end

def parse
@name = @fetched['name']
@config = @fetched['config']
@display_name = @fetched['displayName']
@node_count = @fetched['nodeCount']
@labels = @fetched['labels']
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
"Instance #{@params[:name]}"
end

private

def product_url
'https://spanner.googleapis.com/v1/'
end

def resource_base_url
'projects/{{project}}/instances/{{name}}'
end
end
Loading

0 comments on commit 16f45e7

Please sign in to comment.