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

Add InSpec support for HTTP proxy #94

Merged
merged 1 commit into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/resources/google_compute_target_http_proxies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: About the TargetHttpProxy resource
platform: gcp
---


## Syntax
A `google_compute_target_http_proxies` is used to test a Google TargetHttpProxy resource

## Examples
```
describe google_compute_target_http_proxies(project: 'chef-gcp-inspec') do
its('names') { should include 'inspec-gcp-http-proxy' }
its('descriptions') { should include 'A HTTP proxy' }
end
```

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

See [google_compute_target_http_proxy.md](google_compute_target_http_proxy.md) for more detailed information
* `creation_timestamps`: an array of `google_compute_target_http_proxy` creation_timestamp
* `descriptions`: an array of `google_compute_target_http_proxy` description
* `ids`: an array of `google_compute_target_http_proxy` id
* `names`: an array of `google_compute_target_http_proxy` name
* `url_maps`: an array of `google_compute_target_http_proxy` url_map

## 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.
34 changes: 34 additions & 0 deletions docs/resources/google_compute_target_http_proxy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: About the TargetHttpProxy resource
platform: gcp
---


## Syntax
A `google_compute_target_http_proxy` is used to test a Google TargetHttpProxy resource

## Examples
```
describe google_compute_target_http_proxy(project: 'chef-gcp-inspec', name: 'inspec-gcp-http-proxy') do
it { should exist }
its('description') { should eq 'A HTTP proxy' }
its('url_map') { should match /\/inspec-gcp-url-map$/ }
end

describe google_compute_target_http_proxy(project: 'chef-gcp-inspec', name: 'nonexistent') do
it { should_not exist }
end
```

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

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

* `description`: An optional description of this resource.

* `id`: The 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.

* `url_map`: A reference to the UrlMap resource that defines the mapping from URL to the BackendService.
90 changes: 90 additions & 0 deletions libraries/google_compute_target_http_proxies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# 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 TargetHttpProxys < GcpResourceBase
name 'google_compute_target_http_proxies'
desc 'TargetHttpProxy plural resource'
supports platform: 'gcp'

attr_reader :table

filter_table_config = FilterTable.create

filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
filter_table_config.add(:descriptions, field: :description)
filter_table_config.add(:ids, field: :id)
filter_table_config.add(:names, field: :name)
filter_table_config.add(:url_maps, field: :url_map)

filter_table_config.connect(self, :table)

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

def url
'projects/{{project}}/global/targetHttpProxies'
end

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(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
{
'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) },
'description' => ->(obj) { return :description, obj['description'] },
'id' => ->(obj) { return :id, obj['id'] },
'name' => ->(obj) { return :name, obj['name'] },
'urlMap' => ->(obj) { return :url_map, obj['urlMap'] },
}
end

# Handles parsing RFC3339 time string
def parse_time_string(time_string)
time_string ? Time.parse(time_string) : nil
end
end
59 changes: 59 additions & 0 deletions libraries/google_compute_target_http_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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 Google Compute Engine resources.
class TargetHttpProxy < GcpResourceBase
name 'google_compute_target_http_proxy'
desc 'TargetHttpProxy'
supports platform: 'gcp'

attr_reader :creation_timestamp
attr_reader :description
attr_reader :id
attr_reader :name
attr_reader :url_map
def base
'https://www.googleapis.com/compute/v1/'
end

def url
'projects/{{project}}/global/targetHttpProxies/{{name}}'
end

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

def parse
@creation_timestamp = parse_time_string(@fetched['creationTimestamp'])
@description = @fetched['description']
@id = @fetched['id']
@name = @fetched['name']
@url_map = @fetched['urlMap']
end

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

def exists?
[email protected]?
end
end
11 changes: 11 additions & 0 deletions test/integration/build/gcp-mm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ variable "url_map" {
type = "map"
}

variable "http_proxy" {
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 @@ -266,4 +270,11 @@ resource "google_compute_url_map" "gcp-inspec-url-map" {
host = "${var.url_map["test_host"]}"
path = "${var.url_map["test_path"]}"
}
}

resource "google_compute_target_http_proxy" "gcp-inspec-http-proxy" {
project = "${var.gcp_project_id}"
name = "${var.http_proxy["name"]}"
url_map = "${google_compute_url_map.gcp-inspec-url-map.self_link}"
description = "${var.http_proxy["description"]}"
}
4 changes: 4 additions & 0 deletions test/integration/configuration/mm-attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ url_map:
path_rule_path: /home
test_host: test.com
test_path: /home

http_proxy:
name: inspec-gcp-http-proxy
description: A HTTP proxy
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ----------------------------------------------------------------------------
#
# *** 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_target_http_proxies resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
http_proxy = attribute('http_proxy', default: {
"name": "inspec-gcp-http-proxy",
"description": "A HTTP proxy"
}, description: 'Compute HTTP proxy definition')
control 'google_compute_target_http_proxies-1.0' do
impact 1.0
title 'google_compute_target_http_proxies resource test'

describe google_compute_target_http_proxies(project: gcp_project_id) do
its('names') { should include http_proxy['name'] }
its('descriptions') { should include http_proxy['description'] }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ----------------------------------------------------------------------------
#
# *** 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_target_http_proxy resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
http_proxy = attribute('http_proxy', default: {
"name": "inspec-gcp-http-proxy",
"description": "A HTTP proxy"
}, description: 'Compute HTTP proxy definition')
control 'google_compute_target_http_proxy-1.0' do
impact 1.0
title 'google_compute_target_http_proxy resource test'

describe google_compute_target_http_proxy(project: gcp_project_id, name: http_proxy['name']) do
it { should exist }
its('description') { should eq http_proxy['description'] }
its('url_map') { should match /\/inspec-gcp-url-map$/ }
end

describe google_compute_target_http_proxy(project: gcp_project_id, name: 'nonexistent') do
it { should_not exist }
end
end