This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
forked from inspec/inspec-gcp
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #120 from modular-magician/codegen-pr-1460
Inspec resource folder
- Loading branch information
Showing
8 changed files
with
312 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,38 @@ | ||
--- | ||
title: About the google_resourcemanager_folder resource | ||
platform: gcp | ||
--- | ||
|
||
## Syntax | ||
A `google_resourcemanager_folder` is used to test a Google Folder resource | ||
|
||
## Examples | ||
``` | ||
describe.one do | ||
google_resourcemanager_folders(parent: 'organizations/12345').names.each do |name| | ||
describe google_resourcemanager_folder(name: name) do | ||
it { should exist } | ||
its('display_name') { should eq 'inspec-gcp-folder' } | ||
end | ||
end | ||
end | ||
``` | ||
|
||
## Properties | ||
Properties that can be accessed from the `google_resourcemanager_folder` resource: | ||
|
||
* `name`: The resource name of the Folder. Its format is folders/{folder_id}, for example: "folders/1234". | ||
|
||
* `lifecycle_state`: The lifecycle state of the folder. Updates to the lifecycleState must be performed via folders.delete and folders.undelete. | ||
|
||
* `create_time`: Time of creation | ||
|
||
* `parent`: The Folder’s parent's resource name. Updates to the folder's parent must be performed via folders.move. | ||
|
||
* `display_name`: The folder’s display name. A folder’s display name must be unique amongst its siblings, e.g. no two folders with the same parent can share the same display name. The display name must start and end with a letter or digit, may contain letters, digits, spaces, hyphens and underscores and can be no longer than 30 characters. This is captured by the regular expression: `[\p{L}\p{N}]([\p{L}\p{N}_- ]{0,28}[\p{L}\p{N}])?`. | ||
|
||
|
||
|
||
## GCP Permissions | ||
|
||
Ensure the [Cloud Resource Manager API](https://console.cloud.google.com/apis/library/cloudresourcemanager.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,36 @@ | ||
--- | ||
title: About the google_resourcemanager_folders resource | ||
platform: gcp | ||
--- | ||
|
||
## Syntax | ||
A `google_resourcemanager_folders` is used to test a Google Folder resource | ||
|
||
## Examples | ||
``` | ||
describe.one do | ||
google_resourcemanager_folders(parent: 'organizations/12345').display_names.each do |display_name| | ||
describe display_name do | ||
it { should eq 'inspec-gcp-folder' } | ||
end | ||
end | ||
end | ||
``` | ||
|
||
## Properties | ||
Properties that can be accessed from the `google_resourcemanager_folders` resource: | ||
|
||
See [google_resourcemanager_folder.md](google_resourcemanager_folder.md) for more detailed information | ||
* `names`: an array of `google_resourcemanager_folder` name | ||
* `lifecycle_states`: an array of `google_resourcemanager_folder` lifecycle_state | ||
* `create_times`: an array of `google_resourcemanager_folder` create_time | ||
* `parents`: an array of `google_resourcemanager_folder` parent | ||
* `display_names`: an array of `google_resourcemanager_folder` display_name | ||
|
||
## 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 Resource Manager API](https://console.cloud.google.com/apis/library/cloudresourcemanager.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,62 @@ | ||
# 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 Resource Manager resources. | ||
class Folder < GcpResourceBase | ||
name 'google_resourcemanager_folder' | ||
desc 'Folder' | ||
supports platform: 'gcp' | ||
|
||
attr_reader :name | ||
attr_reader :lifecycle_state | ||
attr_reader :create_time | ||
attr_reader :parent | ||
attr_reader :display_name | ||
|
||
def initialize(params) | ||
super(params.merge({ use_http_transport: true })) | ||
@fetched = @connection.fetch(product_url, resource_base_url, params) | ||
parse unless @fetched.nil? | ||
end | ||
|
||
def parse | ||
@name = @fetched['name'] | ||
@lifecycle_state = @fetched['lifecycleState'] | ||
@create_time = parse_time_string(@fetched['createTime']) | ||
@parent = @fetched['parent'] | ||
@display_name = @fetched['displayName'] | ||
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 | ||
|
||
private | ||
|
||
def product_url | ||
'https://cloudresourcemanager.googleapis.com/v2/' | ||
end | ||
|
||
def resource_base_url | ||
'{{name}}' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# 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 Folders < GcpResourceBase | ||
name 'google_resourcemanager_folders' | ||
desc 'Folder plural resource' | ||
supports platform: 'gcp' | ||
|
||
attr_reader :table | ||
|
||
filter_table_config = FilterTable.create | ||
|
||
filter_table_config.add(:names, field: :name) | ||
filter_table_config.add(:lifecycle_states, field: :lifecycle_state) | ||
filter_table_config.add(:create_times, field: :create_time) | ||
filter_table_config.add(:parents, field: :parent) | ||
filter_table_config.add(:display_names, field: :display_name) | ||
|
||
filter_table_config.connect(self, :table) | ||
|
||
def initialize(params = {}) | ||
super(params.merge({ use_http_transport: true })) | ||
@params = params | ||
@table = fetch_wrapped_resource('folders') | ||
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) | ||
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'] }, | ||
'lifecycleState' => ->(obj) { return :lifecycle_state, obj['lifecycleState'] }, | ||
'createTime' => ->(obj) { return :create_time, parse_time_string(obj['createTime']) }, | ||
'parent' => ->(obj) { return :parent, obj['parent'] }, | ||
'displayName' => ->(obj) { return :display_name, obj['displayName'] }, | ||
} | ||
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://cloudresourcemanager.googleapis.com/v2/' | ||
end | ||
|
||
def resource_base_url | ||
'folders?parent={{parent}}' | ||
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
33 changes: 33 additions & 0 deletions
33
test/integration/verify/controls/google_resourcemanager_folder.rb
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,33 @@ | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# *** 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. | ||
# | ||
# ---------------------------------------------------------------------------- | ||
|
||
title 'Test GCP google_resourcemanager_folder resource.' | ||
|
||
folder = attribute('folder', default: {"display_name"=>"inspec-gcp-folder"}) | ||
gcp_organization_id = attribute(:gcp_organization_id, default: gcp_organization_id, description: 'The identifier of the organization that is the parent of this folder') | ||
gcp_enable_privileged_resources = attribute(:gcp_enable_privileged_resources, default:0, description:'Flag to enable privileged resources requiring elevated privileges in GCP.') | ||
control 'google_resourcemanager_folder-1.0' do | ||
impact 1.0 | ||
title 'google_resourcemanager_folder resource test' | ||
|
||
only_if { gcp_enable_privileged_resources.to_i == 1 && gcp_organization_id != ''} | ||
describe.one do | ||
google_resourcemanager_folders(parent: "organizations/#{gcp_organization_id}").names.each do |name| | ||
describe google_resourcemanager_folder(name: name) do | ||
it { should exist } | ||
its('display_name') { should eq folder['display_name'] } | ||
end | ||
end | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
test/integration/verify/controls/google_resourcemanager_folders.rb
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,32 @@ | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# *** 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. | ||
# | ||
# ---------------------------------------------------------------------------- | ||
|
||
title 'Test GCP google_resourcemanager_folders resource.' | ||
|
||
folder = attribute('folder', default: {"display_name"=>"inspec-gcp-folder"}) | ||
gcp_organization_id = attribute(:gcp_organization_id, default: gcp_organization_id, description: 'The identifier of the organization that is the parent of this folder') | ||
gcp_enable_privileged_resources = attribute(:gcp_enable_privileged_resources, default:0, description:'Flag to enable privileged resources requiring elevated privileges in GCP.') | ||
control 'google_resourcemanager_folders-1.0' do | ||
impact 1.0 | ||
title 'google_resourcemanager_folders resource test' | ||
|
||
only_if { gcp_enable_privileged_resources.to_i == 1 && gcp_organization_id != ''} | ||
describe.one do | ||
google_resourcemanager_folders(parent: "organizations/#{gcp_organization_id}").display_names.each do |display_name| | ||
describe display_name do | ||
it { should eq folder['display_name'] } | ||
end | ||
end | ||
end | ||
end |