Skip to content

Commit

Permalink
Add HTTP health check for InSpec
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
slevenick authored and Stuart Paterson committed Jan 31, 2019
1 parent a4d9de1 commit 2c892e6
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 1 deletion.
47 changes: 47 additions & 0 deletions docs/resources/google_compute_http_health_check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: About the HttpHealthCheck resource
platform: gcp
---


## Syntax
A `google_compute_http_health_check` is used to test a Google HttpHealthCheck resource

## Examples
```
describe google_compute_http_health_check(project: 'chef-gcp-inspec', name: 'inspec-gcp-http-health-check') do
it { should exist }
its('timeout_sec') { should eq '20' }
its('request_path') { should eq '/health_check' }
its('check_interval_sec') { should eq '20' }
end
describe google_compute_http_health_check(project: 'chef-gcp-inspec', name: 'nonexistent') do
it { should_not exist }
end
```

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

* `check_interval_sec`: How often (in seconds) to send a health check. The default value is 5 seconds.

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

* `description`: An optional description of this resource. Provide this property when you create the resource.

* `healthy_threshold`: A so-far unhealthy instance will be marked healthy after this many consecutive successes. The default value is 2.

* `host`: The value of the host header in the HTTP health check request. If left empty (default value), the public IP on behalf of which this health check is performed will be used.

* `id`: The unique identifier for the resource. This identifier is defined by the server.

* `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.

* `port`: The TCP port number for the HTTP health check request. The default value is 80.

* `request_path`: The request path of the HTTP health check request. The default value is /.

* `timeout_sec`: How long (in seconds) to wait before claiming failure. The default value is 5 seconds. It is invalid for timeoutSec to have greater value than checkIntervalSec.

* `unhealthy_threshold`: A so-far healthy instance will be marked unhealthy after this many consecutive failures. The default value is 2.
37 changes: 37 additions & 0 deletions docs/resources/google_compute_http_health_checks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: About the HttpHealthCheck resource
platform: gcp
---


## Syntax
A `google_compute_http_health_checks` is used to test a Google HttpHealthCheck resource

## Examples
```
describe google_compute_http_health_checks(project: 'chef-gcp-inspec') do
its('names') { should include 'inspec-gcp-http-health-check' }
its('timeout_secs') { should include '20' }
its('check_interval_secs') { should include '20' }
end
```

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

See [google_compute_http_health_check.md](google_compute_http_health_check.md) for more detailed information
* `check_interval_secs`: an array of `google_compute_http_health_check` check_interval_sec
* `creation_timestamps`: an array of `google_compute_http_health_check` creation_timestamp
* `descriptions`: an array of `google_compute_http_health_check` description
* `healthy_thresholds`: an array of `google_compute_http_health_check` healthy_threshold
* `hosts`: an array of `google_compute_http_health_check` host
* `ids`: an array of `google_compute_http_health_check` id
* `names`: an array of `google_compute_http_health_check` name
* `ports`: an array of `google_compute_http_health_check` port
* `request_paths`: an array of `google_compute_http_health_check` request_path
* `timeout_secs`: an array of `google_compute_http_health_check` timeout_sec
* `unhealthy_thresholds`: an array of `google_compute_http_health_check` unhealthy_threshold

## 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.
71 changes: 71 additions & 0 deletions libraries/google_compute_http_health_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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 HttpHealthCheck < GcpResourceBase
name 'google_compute_http_health_check'
desc 'HttpHealthCheck'
supports platform: 'gcp'

attr_reader :check_interval_sec
attr_reader :creation_timestamp
attr_reader :description
attr_reader :healthy_threshold
attr_reader :host
attr_reader :id
attr_reader :name
attr_reader :port
attr_reader :request_path
attr_reader :timeout_sec
attr_reader :unhealthy_threshold
def base
'https://www.googleapis.com/compute/v1/'
end

def url
'projects/{{project}}/global/httpHealthChecks/{{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
@check_interval_sec = @fetched['checkIntervalSec']
@creation_timestamp = parse_time_string(@fetched['creationTimestamp'])
@description = @fetched['description']
@healthy_threshold = @fetched['healthyThreshold']
@host = @fetched['host']
@id = @fetched['id']
@name = @fetched['name']
@port = @fetched['port']
@request_path = @fetched['requestPath']
@timeout_sec = @fetched['timeoutSec']
@unhealthy_threshold = @fetched['unhealthyThreshold']
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
end
102 changes: 102 additions & 0 deletions libraries/google_compute_http_health_checks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# 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 HttpHealthChecks < GcpResourceBase
name 'google_compute_http_health_checks'
desc 'HttpHealthCheck plural resource'
supports platform: 'gcp'

attr_reader :table

filter_table_config = FilterTable.create

filter_table_config.add(:check_interval_secs, field: :check_interval_sec)
filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
filter_table_config.add(:descriptions, field: :description)
filter_table_config.add(:healthy_thresholds, field: :healthy_threshold)
filter_table_config.add(:hosts, field: :host)
filter_table_config.add(:ids, field: :id)
filter_table_config.add(:names, field: :name)
filter_table_config.add(:ports, field: :port)
filter_table_config.add(:request_paths, field: :request_path)
filter_table_config.add(:timeout_secs, field: :timeout_sec)
filter_table_config.add(:unhealthy_thresholds, field: :unhealthy_threshold)

filter_table_config.connect(self, :table)

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

def url
'projects/{{project}}/global/httpHealthChecks'
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
{
'checkIntervalSec' => ->(obj) { return :check_interval_sec, obj['checkIntervalSec'] },
'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) },
'description' => ->(obj) { return :description, obj['description'] },
'healthyThreshold' => ->(obj) { return :healthy_threshold, obj['healthyThreshold'] },
'host' => ->(obj) { return :host, obj['host'] },
'id' => ->(obj) { return :id, obj['id'] },
'name' => ->(obj) { return :name, obj['name'] },
'port' => ->(obj) { return :port, obj['port'] },
'requestPath' => ->(obj) { return :request_path, obj['requestPath'] },
'timeoutSec' => ->(obj) { return :timeout_sec, obj['timeoutSec'] },
'unhealthyThreshold' => ->(obj) { return :unhealthy_threshold, obj['unhealthyThreshold'] },
}
end

# Handles parsing RFC3339 time string
def parse_time_string(time_string)
time_string ? Time.parse(time_string) : nil
end
end
13 changes: 13 additions & 0 deletions test/integration/build/gcp-mm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ variable "backend_service" {
type = "map"
}

variable "http_health_check" {
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 @@ -160,4 +164,13 @@ resource "google_compute_backend_service" "gcp-inspec-backend-service" {
}

health_checks = ["${google_compute_health_check.gcp-inspec-health-check.self_link}"]
}

resource "google_compute_http_health_check" "gcp-inspec-http-health-check" {
project = "${var.gcp_project_id}"
name = "${var.http_health_check["name"]}"
request_path = "${var.http_health_check["request_path"]}"

timeout_sec = "${var.http_health_check["timeout_sec"]}"
check_interval_sec = "${var.http_health_check["check_interval_sec"]}"
}
8 changes: 7 additions & 1 deletion test/integration/configuration/mm-attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ backend_service:
port_name: http
protocol: HTTP
timeout_sec: 10
enable_cdn: true
enable_cdn: true

http_health_check:
name: inspec-gcp-http-health-check
request_path: /health_check
timeout_sec: 20
check_interval_sec: 20
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# ----------------------------------------------------------------------------
#
# *** 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_http_health_check resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
http_health_check = attribute('http_health_check', default: {
"name": "inspec-gcp-http-health-check",
"request_path": "/health_check",
"timeout_sec": 20,
"check_interval_sec": 20
}, description: 'HTTP health check definition')
control 'google_compute_http_health_check-1.0' do
impact 1.0
title 'google_compute_http_health_check resource test'

describe google_compute_http_health_check(project: gcp_project_id, name: http_health_check['name']) do
it { should exist }
its('timeout_sec') { should eq http_health_check['timeout_sec'] }
its('request_path') { should eq http_health_check['request_path'] }
its('check_interval_sec') { should eq http_health_check['check_interval_sec'] }
end

describe google_compute_http_health_check(project: gcp_project_id, name: 'nonexistent') do
it { should_not exist }
end
end
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_compute_http_health_checks resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
http_health_check = attribute('http_health_check', default: {
"name": "inspec-gcp-http-health-check",
"request_path": "/health_check",
"timeout_sec": 20,
"check_interval_sec": 20
}, description: 'HTTP health check definition')
control 'google_compute_http_health_checks-1.0' do
impact 1.0
title 'google_compute_http_health_checks resource test'

describe google_compute_http_health_checks(project: gcp_project_id) do
its('names') { should include http_health_check['name'] }
its('timeout_secs') { should include http_health_check['timeout_sec'] }
its('check_interval_secs') { should include http_health_check['check_interval_sec'] }
end
end

0 comments on commit 2c892e6

Please sign in to comment.