Skip to content

Commit

Permalink
Merge pull request inspec#133 from modular-magician/codegen-pr-1549
Browse files Browse the repository at this point in the history
Adding support for backend buckets in InSpec
  • Loading branch information
slevenick authored Mar 20, 2019
2 parents 4d13b4c + a9bf9f8 commit 79c4cd0
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 1 deletion.
42 changes: 42 additions & 0 deletions docs/resources/google_compute_backend_bucket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: About the google_compute_backend_bucket resource
platform: gcp
---

## Syntax
A `google_compute_backend_bucket` is used to test a Google BackendBucket resource

## Examples
```
describe google_compute_backend_bucket(project: 'chef-gcp-inspec', name: 'inspec-gcp-backend-bucket') do
it { should exist }
its('description') { should eq 'Backend bucket example' }
its('enable_cdn') { should be 'true' }
its('bucket_name') { should eq 'gcp-inspec-storage-bucket' }
end
describe google_compute_backend_bucket(project: 'chef-gcp-inspec', name: 'nonexistent') do
it { should_not exist }
end
```

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

* `bucket_name`: Cloud Storage bucket name.

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

* `description`: An optional textual description of the resource; provided by the client when the resource is created.

* `enable_cdn`: If true, enable Cloud CDN for this BackendBucket.

* `id`: Unique identifier for the resource.

* `name`: Name of the resource. Provided by the client when the resource is created. The name must be 1-63 characters long, and comply with RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.



## 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_backend_buckets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: About the google_compute_backend_buckets resource
platform: gcp
---

## Syntax
A `google_compute_backend_buckets` is used to test a Google BackendBucket resource

## Examples
```
describe google_compute_backend_buckets(project: 'chef-gcp-inspec', name: 'inspec-gcp-backend-bucket') do
its('descriptions') { should include 'Backend bucket example' }
its('bucket_names') { should include 'gcp-inspec-storage-bucket' }
end
```

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

See [google_compute_backend_bucket.md](google_compute_backend_bucket.md) for more detailed information
* `bucket_names`: an array of `google_compute_backend_bucket` bucket_name
* `creation_timestamps`: an array of `google_compute_backend_bucket` creation_timestamp
* `descriptions`: an array of `google_compute_backend_bucket` description
* `enable_cdns`: an array of `google_compute_backend_bucket` enable_cdn
* `ids`: an array of `google_compute_backend_bucket` id
* `names`: an array of `google_compute_backend_bucket` 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 [Compute Engine API](https://console.cloud.google.com/apis/library/compute.googleapis.com/) is enabled for the current project.
70 changes: 70 additions & 0 deletions libraries/google_compute_backend_bucket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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 Compute Engine resources.
class BackendBucket < GcpResourceBase
name 'google_compute_backend_bucket'
desc 'BackendBucket'
supports platform: 'gcp'

attr_reader :params
attr_reader :bucket_name
attr_reader :creation_timestamp
attr_reader :description
attr_reader :enable_cdn
attr_reader :id
attr_reader :name

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

def parse
@bucket_name = @fetched['bucketName']
@creation_timestamp = parse_time_string(@fetched['creationTimestamp'])
@description = @fetched['description']
@enable_cdn = @fetched['enableCdn']
@id = @fetched['id']
@name = @fetched['name']
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
"BackendBucket #{@params[:name]}"
end

private

def product_url
'https://www.googleapis.com/compute/v1/'
end

def resource_base_url
'projects/{{project}}/global/backendBuckets/{{name}}'
end
end
94 changes: 94 additions & 0 deletions libraries/google_compute_backend_buckets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# 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 BackendBuckets < GcpResourceBase
name 'google_compute_backend_buckets'
desc 'BackendBucket plural resource'
supports platform: 'gcp'

attr_reader :table

filter_table_config = FilterTable.create

filter_table_config.add(:bucket_names, field: :bucket_name)
filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
filter_table_config.add(:descriptions, field: :description)
filter_table_config.add(:enable_cdns, field: :enable_cdn)
filter_table_config.add(:ids, field: :id)
filter_table_config.add(:names, field: :name)

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)
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
{
'bucketName' => ->(obj) { return :bucket_name, obj['bucketName'] },
'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) },
'description' => ->(obj) { return :description, obj['description'] },
'enableCdn' => ->(obj) { return :enable_cdn, obj['enableCdn'] },
'id' => ->(obj) { return :id, obj['id'] },
'name' => ->(obj) { return :name, obj['name'] },
}
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://www.googleapis.com/compute/v1/'
end

def resource_base_url
'projects/{{project}}/global/backendBuckets'
end
end
12 changes: 12 additions & 0 deletions test/integration/build/gcp-mm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ variable "cloudfunction" {
type = "map"
}

variable "backend_bucket" {
type = "map"
}

resource "google_compute_ssl_policy" "custom-ssl-policy" {
name = "${var.ssl_policy["name"]}"
min_tls_version = "${var.ssl_policy["min_tls_version"]}"
Expand Down Expand Up @@ -484,4 +488,12 @@ resource "google_cloudfunctions_function" "function" {
environment_variables = {
MY_ENV_VAR = "${var.cloudfunction["env_var_value"]}"
}
}

resource "google_compute_backend_bucket" "image_backend" {
project = "${var.gcp_project_id}"
name = "${var.backend_bucket["name"]}"
description = "${var.backend_bucket["description"]}"
bucket_name = "${google_storage_bucket.generic-storage-bucket.name}"
enable_cdn = "${var.backend_bucket["enable_cdn"]}"
}
7 changes: 6 additions & 1 deletion test/integration/configuration/mm-attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,9 @@ cloudfunction:
trigger_http: true
timeout: 60
entry_point: hello
env_var_value: val1
env_var_value: val1

backend_bucket:
name: inspec-gcp-backend-bucket
description: Backend bucket example
enable_cdn: true
37 changes: 37 additions & 0 deletions test/integration/verify/controls/google_compute_backend_bucket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ----------------------------------------------------------------------------
#
# *** 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_compute_backend_bucket resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
gcp_storage_bucket_name = attribute(:gcp_storage_bucket_name, default: 'gcp_storage_bucket_name', description: 'The GCS bucket name to use for the backend bucket.')
backend_bucket = attribute('backend_bucket', default: {
"name": "inspec-gcp-backend-bucket",
"description": "Backend bucket example",
"enable_cdn": true
}, description: 'Backend bucket definition')
control 'google_compute_backend_bucket-1.0' do
impact 1.0
title 'google_compute_backend_bucket resource test'

describe google_compute_backend_bucket(project: gcp_project_id, name: backend_bucket['name']) do
it { should exist }
its('description') { should eq backend_bucket['description'] }
its('enable_cdn') { should be backend_bucket['enable_cdn'] }
end

describe google_compute_backend_bucket(project: gcp_project_id, name: 'nonexistent') do
it { should_not exist }
end
end
31 changes: 31 additions & 0 deletions test/integration/verify/controls/google_compute_backend_buckets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# ----------------------------------------------------------------------------
#
# *** 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_compute_backend_buckets resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
gcp_storage_bucket_name = attribute(:gcp_storage_bucket_name, default: 'gcp_storage_bucket_name', description: 'The GCS bucket name to use for the backend bucket.')
backend_bucket = attribute('backend_bucket', default: {
"name": "inspec-gcp-backend-bucket",
"description": "Backend bucket example",
"enable_cdn": true
}, description: 'Backend bucket definition')
control 'google_compute_backend_buckets-1.0' do
impact 1.0
title 'google_compute_backend_buckets resource test'

describe google_compute_backend_buckets(project: gcp_project_id, name: backend_bucket['name']) do
its('descriptions') { should include backend_bucket['description'] }
end
end

0 comments on commit 79c4cd0

Please sign in to comment.