Skip to content

Commit

Permalink
Merge pull request #396 from inspec/RESOURCE-36-sql-flags
Browse files Browse the repository at this point in the history
RESOURCE-36 sql flags
  • Loading branch information
sa-progress authored Feb 23, 2022
2 parents 50b3b88 + 964db5c commit 2962a8c
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 1 deletion.
39 changes: 39 additions & 0 deletions docs/resources/google_sql_flags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: About the google_sql_flags resource
platform: gcp
---

## Syntax
A `google_sql_flags` is used to test a Google Flag resource

## Examples
```
describe google_sql_flags do
its('names') { should include , 'audit_log' }
its('types') { should include , 'STRING' }
its('applies_tos.first') { should include , 'MYSQL_5_6' }
its('allowed_string_values.first') { should include , 'true' }
its('requires_restarts') { should include , 'true' }
end
```

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

See [google_sql_flag.md](google_sql_flag.md) for more detailed information
* `allowed_string_values`: an array of `google_sql_flag` allowed_string_values
* `applies_tos`: an array of `google_sql_flag` applies_to
* `max_values`: an array of `google_sql_flag` max_value
* `min_values`: an array of `google_sql_flag` min_value
* `names`: an array of `google_sql_flag` name
* `requires_restarts`: an array of `google_sql_flag` requires_restart
* `types`: an array of `google_sql_flag` type

## 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 SQL Admin API](https://console.cloud.google.com/apis/library/sqladmin.googleapis.com/) is enabled for the current project.
91 changes: 91 additions & 0 deletions libraries/google_sql_flags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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 SQLFlags < GcpResourceBase
name 'google_sql_flags'
desc 'Flag plural resource'
supports platform: 'gcp'

attr_reader :table

filter_table_config = FilterTable.create

filter_table_config.add(:allowed_string_values, field: :allowed_string_values)
filter_table_config.add(:applies_tos, field: :applies_to)
filter_table_config.add(:max_values, field: :max_value)
filter_table_config.add(:min_values, field: :min_value)
filter_table_config.add(:names, field: :name)
filter_table_config.add(:requires_restarts, field: :requires_restart)
filter_table_config.add(:types, field: :type)

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
{
'allowedStringValues' => ->(obj) { return :allowed_string_values, obj['allowedStringValues'] },
'appliesTo' => ->(obj) { return :applies_to, obj['appliesTo'] },
'maxValue' => ->(obj) { return :max_value, obj['maxValue'] },
'minValue' => ->(obj) { return :min_value, obj['minValue'] },
'name' => ->(obj) { return :name, obj['name'] },
'requiresRestart' => ->(obj) { return :requires_restart, obj['requiresRestart'] },
'type' => ->(obj) { return :type, obj['type'] },
}
end

private

def product_url(_ = nil)
'https://sqladmin.googleapis.com/sql/v1beta4/'
end

def resource_base_url
'flags'
end
end
13 changes: 12 additions & 1 deletion test/integration/configuration/mm-attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,17 @@ security_policy:
memcache_instance:
name: mem-instance

container_engine_versions:
valid_master_version: '1.21.5-gke.1802'
valid_node_version: '1.21.5-gke.1802'

sql_database_flag:
name : audit_log
type : STRING
applies_to: MYSQL_5_6
allowed_string_values: ON
requires_restart: true

accelerator_type:
name: accelerator_id

Expand All @@ -473,4 +484,4 @@ license_code:
name: akl-zone1-1353

public_delegated_prefix:
name: test
name: test
37 changes: 37 additions & 0 deletions test/integration/verify/controls/google_sql_flags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ----------------------------------------------------------------------------
#
# *** 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_sql_flags resource.'

sql_database_flag = attribute('sql_database_flag', default: {
"name": "audit_log",
"type": "STRING",
"applies_to": "MYSQL_5_6",
"allowed_string_values": true,
"requires_restart": true
}, description: 'Database flags for Cloud SQL instances')

control 'google_sql_flags-1.0' do
impact 1.0
title 'google_sql_flags resource test'


describe google_sql_flags do
its('names') { should include , sql_database_flag['name'] }
its('types') { should include , sql_database_flag['type'] }
its('applies_tos.first') { should include , sql_database_flag['applies_to'] }
its('allowed_string_values.first') { should include , sql_database_flag['allowed_string_values'] }
its('requires_restarts') { should include , sql_database_flag['requires_restart'] }
end
end

0 comments on commit 2962a8c

Please sign in to comment.