-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #396 from inspec/RESOURCE-36-sql-flags
RESOURCE-36 sql flags
- Loading branch information
Showing
4 changed files
with
179 additions
and
1 deletion.
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
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. |
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,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 |
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 |
---|---|---|
@@ -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 |