diff --git a/docs/resources/google_compute_https_health_check.md b/docs/resources/google_compute_https_health_check.md new file mode 100644 index 000000000..300688155 --- /dev/null +++ b/docs/resources/google_compute_https_health_check.md @@ -0,0 +1,48 @@ +--- +title: About the HttpsHealthCheck resource +platform: gcp +--- + + +## Syntax +A `google_compute_https_health_check` is used to test a Google HttpsHealthCheck resource + +## Examples +``` +describe google_compute_https_health_check(project: 'chef-gcp-inspec', name: 'inspec-gcp-https-health-check') do + it { should exist } + its('timeout_sec') { should eq '15' } + its('request_path') { should eq '/https_health_check' } + its('check_interval_sec') { should eq '15' } + its('unhealthy_threshold') { should eq '3' } +end + +describe google_compute_https_health_check(project: 'chef-gcp-inspec', name: 'nonexistent') do + it { should_not exist } +end +``` + +## Properties +Properties that can be accessed from the `google_compute_https_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 HTTPS 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 HTTPS health check request. The default value is 80. + + * `request_path`: The request path of the HTTPS 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. diff --git a/docs/resources/google_compute_https_health_checks.md b/docs/resources/google_compute_https_health_checks.md new file mode 100644 index 000000000..9ef01d5f8 --- /dev/null +++ b/docs/resources/google_compute_https_health_checks.md @@ -0,0 +1,37 @@ +--- +title: About the HttpsHealthCheck resource +platform: gcp +--- + + +## Syntax +A `google_compute_https_health_checks` is used to test a Google HttpsHealthCheck resource + +## Examples +``` +describe google_compute_https_health_checks(project: 'chef-gcp-inspec') do + its('names') { should include 'inspec-gcp-https-health-check' } + its('timeout_secs') { should include '15' } + its('check_interval_secs') { should include '15' } +end +``` + +## Properties +Properties that can be accessed from the `google_compute_https_health_checks` resource: + +See [google_compute_https_health_check.md](google_compute_https_health_check.md) for more detailed information + * `check_interval_secs`: an array of `google_compute_https_health_check` check_interval_sec + * `creation_timestamps`: an array of `google_compute_https_health_check` creation_timestamp + * `descriptions`: an array of `google_compute_https_health_check` description + * `healthy_thresholds`: an array of `google_compute_https_health_check` healthy_threshold + * `hosts`: an array of `google_compute_https_health_check` host + * `ids`: an array of `google_compute_https_health_check` id + * `names`: an array of `google_compute_https_health_check` name + * `ports`: an array of `google_compute_https_health_check` port + * `request_paths`: an array of `google_compute_https_health_check` request_path + * `timeout_secs`: an array of `google_compute_https_health_check` timeout_sec + * `unhealthy_thresholds`: an array of `google_compute_https_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. diff --git a/libraries/google_compute_https_health_check.rb b/libraries/google_compute_https_health_check.rb new file mode 100644 index 000000000..104278916 --- /dev/null +++ b/libraries/google_compute_https_health_check.rb @@ -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 HttpsHealthCheck < GcpResourceBase + name 'google_compute_https_health_check' + desc 'HttpsHealthCheck' + 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/httpsHealthChecks/{{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 diff --git a/libraries/google_compute_https_health_checks.rb b/libraries/google_compute_https_health_checks.rb new file mode 100644 index 000000000..dc4fa7907 --- /dev/null +++ b/libraries/google_compute_https_health_checks.rb @@ -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 HttpsHealthChecks < GcpResourceBase + name 'google_compute_https_health_checks' + desc 'HttpsHealthCheck 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/httpsHealthChecks' + 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 diff --git a/test/integration/build/gcp-mm.tf b/test/integration/build/gcp-mm.tf index c41cda6ad..9901bcb9e 100644 --- a/test/integration/build/gcp-mm.tf +++ b/test/integration/build/gcp-mm.tf @@ -46,6 +46,10 @@ variable "http_health_check" { type = "map" } +variable "https_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"]}" @@ -173,4 +177,14 @@ resource "google_compute_http_health_check" "gcp-inspec-http-health-check" { timeout_sec = "${var.http_health_check["timeout_sec"]}" check_interval_sec = "${var.http_health_check["check_interval_sec"]}" +} + +resource "google_compute_https_health_check" "gcp-inspec-https-health-check" { + project = "${var.gcp_project_id}" + name = "${var.https_health_check["name"]}" + request_path = "${var.https_health_check["request_path"]}" + + timeout_sec = "${var.https_health_check["timeout_sec"]}" + check_interval_sec = "${var.https_health_check["check_interval_sec"]}" + unhealthy_threshold = "${var.https_health_check["unhealthy_threshold"]}" } \ No newline at end of file diff --git a/test/integration/configuration/mm-attributes.yml b/test/integration/configuration/mm-attributes.yml index 38742e372..c81af3529 100644 --- a/test/integration/configuration/mm-attributes.yml +++ b/test/integration/configuration/mm-attributes.yml @@ -66,4 +66,11 @@ http_health_check: name: inspec-gcp-http-health-check request_path: /health_check timeout_sec: 20 - check_interval_sec: 20 \ No newline at end of file + check_interval_sec: 20 + +https_health_check: + name: inspec-gcp-https-health-check + request_path: /https_health_check + timeout_sec: 15 + check_interval_sec: 15 + unhealthy_threshold: 3 diff --git a/test/integration/verify/controls/google_compute_https_health_check.rb b/test/integration/verify/controls/google_compute_https_health_check.rb new file mode 100644 index 000000000..395f0583f --- /dev/null +++ b/test/integration/verify/controls/google_compute_https_health_check.rb @@ -0,0 +1,40 @@ +# ---------------------------------------------------------------------------- +# +# *** 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_https_health_check resource.' + +gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.') +https_health_check = attribute('https_health_check', default: { + "name": "inspec-gcp-https-health-check", + "request_path": "/https_health_check", + "timeout_sec": 15, + "check_interval_sec": 15, + "unhealthy_threshold": 3 +}, description: 'HTTPS health check definition') +control 'google_compute_https_health_check-1.0' do + impact 1.0 + title 'google_compute_https_health_check resource test' + + describe google_compute_https_health_check(project: gcp_project_id, name: https_health_check['name']) do + it { should exist } + its('timeout_sec') { should eq https_health_check['timeout_sec'] } + its('request_path') { should eq https_health_check['request_path'] } + its('check_interval_sec') { should eq https_health_check['check_interval_sec'] } + its('unhealthy_threshold') { should eq https_health_check['unhealthy_threshold'] } + end + + describe google_compute_https_health_check(project: gcp_project_id, name: 'nonexistent') do + it { should_not exist } + end +end diff --git a/test/integration/verify/controls/google_compute_https_health_checks.rb b/test/integration/verify/controls/google_compute_https_health_checks.rb new file mode 100644 index 000000000..2e191ce50 --- /dev/null +++ b/test/integration/verify/controls/google_compute_https_health_checks.rb @@ -0,0 +1,34 @@ +# ---------------------------------------------------------------------------- +# +# *** 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_https_health_checks resource.' + +gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.') +https_health_check = attribute('https_health_check', default: { + "name": "inspec-gcp-https-health-check", + "request_path": "/https_health_check", + "timeout_sec": 15, + "check_interval_sec": 15, + "unhealthy_threshold": 3 +}, description: 'HTTPS health check definition') +control 'google_compute_https_health_checks-1.0' do + impact 1.0 + title 'google_compute_https_health_checks resource test' + + describe google_compute_https_health_checks(project: gcp_project_id) do + its('names') { should include https_health_check['name'] } + its('timeout_secs') { should include https_health_check['timeout_sec'] } + its('check_interval_secs') { should include https_health_check['check_interval_sec'] } + end +end