From cfb57150fd383547a38e7b5890c2c66339560b5f Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Tue, 22 Jan 2019 17:49:25 +0000 Subject: [PATCH 1/5] Add cloudbuild trigger InSpec resource Signed-off-by: Modular Magician --- docs/resources/google_cloudbuild_trigger.md | 65 ++++++++++++ docs/resources/google_cloudbuild_triggers.md | 43 ++++++++ docs/resources/google_pubsub_topics.md | 10 +- .../cloudbuild/property/trigger_build.rb | 36 +++++++ .../cloudbuild/property/trigger_steps.rb | 40 +++++++ .../property/trigger_trigger_template.rb | 44 ++++++++ libraries/google_cloudbuild_trigger.rb | 72 +++++++++++++ libraries/google_cloudbuild_triggers.rb | 100 ++++++++++++++++++ test/integration/build/gcp-mm.tf | 14 +++ .../configuration/mm-attributes.yml | 6 ++ .../controls/google_cloudbuild_trigger.rb | 40 +++++++ .../controls/google_cloudbuild_triggers.rb | 40 +++++++ .../verify/controls/google_pubsub_topics.rb | 10 +- 13 files changed, 512 insertions(+), 8 deletions(-) create mode 100644 docs/resources/google_cloudbuild_trigger.md create mode 100644 docs/resources/google_cloudbuild_triggers.md create mode 100644 libraries/google/cloudbuild/property/trigger_build.rb create mode 100644 libraries/google/cloudbuild/property/trigger_steps.rb create mode 100644 libraries/google/cloudbuild/property/trigger_trigger_template.rb create mode 100644 libraries/google_cloudbuild_trigger.rb create mode 100644 libraries/google_cloudbuild_triggers.rb create mode 100644 test/integration/verify/controls/google_cloudbuild_trigger.rb create mode 100644 test/integration/verify/controls/google_cloudbuild_triggers.rb diff --git a/docs/resources/google_cloudbuild_trigger.md b/docs/resources/google_cloudbuild_trigger.md new file mode 100644 index 000000000..5cefb56d9 --- /dev/null +++ b/docs/resources/google_cloudbuild_trigger.md @@ -0,0 +1,65 @@ +--- +title: About the Trigger resource +platform: gcp +--- + + +## Syntax +A `google_cloudbuild_trigger` is used to test a Google Trigger resource + +## Examples +``` +describe google_cloudbuild_triggers(project: 'chef-gcp-inspec') do + its('count') { should eq 1 } +end + +google_cloudbuild_triggers(project: 'chef-gcp-inspec').ids.each do |id| + describe google_cloudbuild_trigger(project: 'chef-gcp-inspec', id: id) do + its('filename') { should eq 'cloudbuild.yaml' } + its('trigger_template.branch_name') { should eq 'trigger-branch' } + its('trigger_template.repo_name') { should eq 'trigger-repo' } + its('trigger_template.project_id') { should eq 'trigger-project' } + end +end +``` + +## Properties +Properties that can be accessed from the `google_cloudbuild_trigger` resource: + + * `id`: The unique identifier for the trigger. + + * `description`: Human-readable description of the trigger. + + * `disabled`: Whether the trigger is disabled or not. If true, the trigger will never result in a build. + + * `create_time`: Time when the trigger was created. + + * `substitutions`: Substitutions data for Build resource. + + * `filename`: Path, from the source root, to a file whose contents is used for the template. + + * `ignored_files`: ignoredFiles and includedFiles are file glob matches using http://godoc/pkg/path/filepath#Match extended with support for `**`. If ignoredFiles and changed files are both empty, then they are not used to determine whether or not to trigger a build. If ignoredFiles is not empty, then we ignore any files that match any of the ignored_file globs. If the change has no files that are outside of the ignoredFiles globs, then we do not trigger a build. + + * `included_files`: ignoredFiles and includedFiles are file glob matches using http://godoc/pkg/path/filepath#Match extended with support for `**`. If any of the files altered in the commit pass the ignoredFiles filter and includedFiles is empty, then as far as this filter is concerned, we should trigger the build. If any of the files altered in the commit pass the ignoredFiles filter and includedFiles is not empty, then we make sure that at least one of those files matches a includedFiles glob. If not, then we do not trigger a build. + + * `trigger_template`: Template describing the types of source changes to trigger a build. Branch and tag names in trigger templates are interpreted as regular expressions. Any branch or tag change that matches that regular expression will trigger a build. + + * `projectId`: ID of the project that owns the Cloud Source Repository. If omitted, the project ID requesting the build is assumed. + + * `repoName`: Name of the Cloud Source Repository. If omitted, the name "default" is assumed. + + * `dir`: Directory, relative to the source root, in which to run the build. This must be a relative path. If a step's dir is specified and is an absolute path, this value is ignored for that step's execution. + + * `branchName`: Name of the branch to build. + + * `tagName`: Name of the tag to build. + + * `commitSha`: Explicit commit SHA to build. + + * `build`: Contents of the build template. + + * `tags`: Tags for annotation of a Build. These are not docker tags. + + * `images`: A list of images to be pushed upon the successful completion of all build steps. The images are pushed using the builder service account's credentials. The digests of the pushed images will be stored in the Build resource's results field. If any of the images fail to be pushed, the build status is marked FAILURE. + + * `steps`: The operations to be performed on the workspace. diff --git a/docs/resources/google_cloudbuild_triggers.md b/docs/resources/google_cloudbuild_triggers.md new file mode 100644 index 000000000..c501ddc93 --- /dev/null +++ b/docs/resources/google_cloudbuild_triggers.md @@ -0,0 +1,43 @@ +--- +title: About the Trigger resource +platform: gcp +--- + + +## Syntax +A `google_cloudbuild_triggers` is used to test a Google Trigger resource + +## Examples +``` +describe google_cloudbuild_triggers(project: 'chef-gcp-inspec') do + its('count') { should eq 1 } +end + +google_cloudbuild_triggers(project: 'chef-gcp-inspec').ids.each do |id| + describe google_cloudbuild_trigger(project: 'chef-gcp-inspec', id: id) do + its('filename') { should eq 'cloudbuild.yaml' } + its('trigger_template.branch_name') { should eq 'trigger-branch' } + its('trigger_template.repo_name') { should eq 'trigger-repo' } + its('trigger_template.project_id') { should eq 'trigger-project' } + end +end +``` + +## Properties +Properties that can be accessed from the `google_cloudbuild_triggers` resource: + +See [google_cloudbuild_trigger.md](google_cloudbuild_trigger.md) for more detailed information + * `ids`: an array of `google_cloudbuild_trigger` id + * `descriptions`: an array of `google_cloudbuild_trigger` description + * `disableds`: an array of `google_cloudbuild_trigger` disabled + * `create_times`: an array of `google_cloudbuild_trigger` create_time + * `substitutions`: an array of `google_cloudbuild_trigger` substitutions + * `filenames`: an array of `google_cloudbuild_trigger` filename + * `ignored_files`: an array of `google_cloudbuild_trigger` ignored_files + * `included_files`: an array of `google_cloudbuild_trigger` included_files + * `trigger_templates`: an array of `google_cloudbuild_trigger` trigger_template + * `builds`: an array of `google_cloudbuild_trigger` build + +## 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/docs/resources/google_pubsub_topics.md b/docs/resources/google_pubsub_topics.md index cb30fdb51..5c4de17cc 100644 --- a/docs/resources/google_pubsub_topics.md +++ b/docs/resources/google_pubsub_topics.md @@ -12,12 +12,14 @@ A `google_pubsub_topics` is used to test a Google Topic resource describe google_pubsub_topics(project: 'chef-gcp-inspec') do it { should exist } its('names') { should include 'inspec-gcp-topic' } - its('count') { should eq 1 } + its('count') { should eq 2 } end -google_pubsub_topics(project: 'chef-gcp-inspec').names.each do |topic_name| - describe google_pubsub_topic(project: 'chef-gcp-inspec', name: topic_name) do - its('name') { should eq 'inspec-gcp-topic' } +describe.one do + google_pubsub_topics(project: 'chef-gcp-inspec').names.each do |topic_name| + describe google_pubsub_topic(project: 'chef-gcp-inspec', name: topic_name) do + its('name') { should eq 'inspec-gcp-topic' } + end end end ``` diff --git a/libraries/google/cloudbuild/property/trigger_build.rb b/libraries/google/cloudbuild/property/trigger_build.rb new file mode 100644 index 000000000..fc6e838af --- /dev/null +++ b/libraries/google/cloudbuild/property/trigger_build.rb @@ -0,0 +1,36 @@ +# 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 'google/cloudbuild/property/trigger_steps' +module GoogleInSpec + module CloudBuild + module Property + class TriggerBuild + attr_reader :tags + + attr_reader :images + + attr_reader :steps + + def initialize(args = nil) + return if args.nil? + @tags = args['tags'] + @images = args['images'] + @steps = GoogleInSpec::CloudBuild::Property::TriggerStepsArray.parse(args['steps']) + end + end + end + end +end diff --git a/libraries/google/cloudbuild/property/trigger_steps.rb b/libraries/google/cloudbuild/property/trigger_steps.rb new file mode 100644 index 000000000..05596caa6 --- /dev/null +++ b/libraries/google/cloudbuild/property/trigger_steps.rb @@ -0,0 +1,40 @@ +# 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. +# +# ---------------------------------------------------------------------------- +module GoogleInSpec + module CloudBuild + module Property + class TriggerSteps + attr_reader :name + + attr_reader :args + + def initialize(args = nil) + return if args.nil? + @name = args['name'] + @args = args['args'] + end + end + + class TriggerStepsArray + def self.parse(value) + return if value.nil? + return TriggerSteps.new(value) unless value.is_a?(::Array) + value.map { |v| TriggerSteps.new(v) } + end + end + end + end +end diff --git a/libraries/google/cloudbuild/property/trigger_trigger_template.rb b/libraries/google/cloudbuild/property/trigger_trigger_template.rb new file mode 100644 index 000000000..af219c92f --- /dev/null +++ b/libraries/google/cloudbuild/property/trigger_trigger_template.rb @@ -0,0 +1,44 @@ +# 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. +# +# ---------------------------------------------------------------------------- +module GoogleInSpec + module CloudBuild + module Property + class TriggerTriggertemplate + attr_reader :project_id + + attr_reader :repo_name + + attr_reader :dir + + attr_reader :branch_name + + attr_reader :tag_name + + attr_reader :commit_sha + + def initialize(args = nil) + return if args.nil? + @project_id = args['projectId'] + @repo_name = args['repoName'] + @dir = args['dir'] + @branch_name = args['branchName'] + @tag_name = args['tagName'] + @commit_sha = args['commitSha'] + end + end + end + end +end diff --git a/libraries/google_cloudbuild_trigger.rb b/libraries/google_cloudbuild_trigger.rb new file mode 100644 index 000000000..eb1ec28cc --- /dev/null +++ b/libraries/google_cloudbuild_trigger.rb @@ -0,0 +1,72 @@ +# 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' +require 'google/cloudbuild/property/trigger_build' +require 'google/cloudbuild/property/trigger_steps' +require 'google/cloudbuild/property/trigger_trigger_template' + +# A provider to manage Cloud build resources. +class Trigger < GcpResourceBase + name 'google_cloudbuild_trigger' + desc 'Trigger' + supports platform: 'gcp' + + attr_reader :id + attr_reader :description + attr_reader :disabled + attr_reader :create_time + attr_reader :substitutions + attr_reader :filename + attr_reader :ignored_files + attr_reader :included_files + attr_reader :trigger_template + attr_reader :build + def base + 'https://cloudbuild.googleapis.com/v1/' + end + + def url + 'projects/{{project}}/triggers/{{id}}' + end + + def initialize(params) + super(params.merge({ use_http_transport: true })) + @fetched = @connection.fetch(base, url, params) + parse unless @fetched.nil? + end + + def parse + @id = @fetched['id'] + @description = @fetched['description'] + @disabled = @fetched['disabled'] + @create_time = parse_time_string(@fetched['createTime']) + @substitutions = @fetched['substitutions'] + @filename = @fetched['filename'] + @ignored_files = @fetched['ignoredFiles'] + @included_files = @fetched['includedFiles'] + @trigger_template = GoogleInSpec::CloudBuild::Property::TriggerTriggertemplate.new(@fetched['triggerTemplate']) + @build = GoogleInSpec::CloudBuild::Property::TriggerBuild.new(@fetched['build']) + 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_cloudbuild_triggers.rb b/libraries/google_cloudbuild_triggers.rb new file mode 100644 index 000000000..a6aa7c99e --- /dev/null +++ b/libraries/google_cloudbuild_triggers.rb @@ -0,0 +1,100 @@ +# 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 Triggers < GcpResourceBase + name 'google_cloudbuild_triggers' + desc 'Trigger plural resource' + supports platform: 'gcp' + + attr_reader :table + + filter_table_config = FilterTable.create + + filter_table_config.add(:ids, field: :id) + filter_table_config.add(:descriptions, field: :description) + filter_table_config.add(:disableds, field: :disabled) + filter_table_config.add(:create_times, field: :create_time) + filter_table_config.add(:substitutions, field: :substitutions) + filter_table_config.add(:filenames, field: :filename) + filter_table_config.add(:ignored_files, field: :ignored_files) + filter_table_config.add(:included_files, field: :included_files) + filter_table_config.add(:trigger_templates, field: :trigger_template) + filter_table_config.add(:builds, field: :build) + + filter_table_config.connect(self, :table) + + def base + 'https://cloudbuild.googleapis.com/v1/' + end + + def url + 'projects/{{project}}/triggers' + end + + def initialize(params = {}) + super(params.merge({ use_http_transport: true })) + @params = params + @table = fetch_wrapped_resource('triggers') + 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 + { + 'id' => ->(obj) { return :id, obj['id'] }, + 'description' => ->(obj) { return :description, obj['description'] }, + 'disabled' => ->(obj) { return :disabled, obj['disabled'] }, + 'createTime' => ->(obj) { return :create_time, parse_time_string(obj['createTime']) }, + 'substitutions' => ->(obj) { return :substitutions, obj['substitutions'] }, + 'filename' => ->(obj) { return :filename, obj['filename'] }, + 'ignoredFiles' => ->(obj) { return :ignored_files, obj['ignoredFiles'] }, + 'includedFiles' => ->(obj) { return :included_files, obj['includedFiles'] }, + 'triggerTemplate' => ->(obj) { return :trigger_template, GoogleInSpec::CloudBuild::Property::TriggerTriggertemplate.new(obj['triggerTemplate']) }, + 'build' => ->(obj) { return :build, GoogleInSpec::CloudBuild::Property::TriggerBuild.new(obj['build']) }, + } + 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 1ef8ce5ef..5a8ab2ad0 100644 --- a/test/integration/build/gcp-mm.tf +++ b/test/integration/build/gcp-mm.tf @@ -30,6 +30,10 @@ variable "target_pool" { type = "map" } +variable "trigger" { + type = "map" +} + resource "google_compute_ssl_policy" "custom-ssl-policy" { name = "${var.ssl_policy["name"]}" min_tls_version = "${var.ssl_policy["min_tls_version"]}" @@ -110,4 +114,14 @@ resource "google_compute_target_pool" "gcp-inspec-target-pool" { instances = [ "${var.gcp_zone}/${var.gcp_ext_vm_name}", ] +} + +resource "google_cloudbuild_trigger" "gcp-inspec-cloudbuild-trigger" { + project = "${var.gcp_project_id}" + trigger_template { + branch_name = "${var.trigger["trigger_template_branch"]}" + project = "${var.trigger["trigger_template_project"]}" + repo_name = "${var.trigger["trigger_template_repo"]}" + } + filename = "${var.trigger["filename"]}" } \ No newline at end of file diff --git a/test/integration/configuration/mm-attributes.yml b/test/integration/configuration/mm-attributes.yml index 0d6609e83..6ae397078 100644 --- a/test/integration/configuration/mm-attributes.yml +++ b/test/integration/configuration/mm-attributes.yml @@ -41,3 +41,9 @@ autoscaler: target_pool: name: 'inspec-gcp-target-pool' session_affinity: CLIENT_IP + +trigger: + trigger_template_project: trigger-project + trigger_template_branch: trigger-branch + trigger_template_repo: trigger-repo + filename: cloudbuild.yaml \ No newline at end of file diff --git a/test/integration/verify/controls/google_cloudbuild_trigger.rb b/test/integration/verify/controls/google_cloudbuild_trigger.rb new file mode 100644 index 000000000..1edc14158 --- /dev/null +++ b/test/integration/verify/controls/google_cloudbuild_trigger.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_cloudbuild_trigger resource.' + +gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.') +trigger = attribute('trigger', default: { + "trigger_template_project": "trigger-project", + "trigger_template_branch": "trigger-branch", + "trigger_template_repo": "trigger-repo", + "filename": "cloudbuild.yaml" +}, description: 'CloudBuild trigger definition') +control 'google_cloudbuild_trigger-1.0' do + impact 1.0 + title 'google_cloudbuild_trigger resource test' + + describe google_cloudbuild_triggers(project: gcp_project_id) do + its('count') { should eq 1 } + end + + google_cloudbuild_triggers(project: gcp_project_id).ids.each do |id| + describe google_cloudbuild_trigger(project: gcp_project_id, id: id) do + its('filename') { should eq trigger['filename'] } + its('trigger_template.branch_name') { should eq trigger['trigger_template_branch'] } + its('trigger_template.repo_name') { should eq trigger['trigger_template_repo'] } + its('trigger_template.project_id') { should eq trigger['trigger_template_project'] } + end + end +end diff --git a/test/integration/verify/controls/google_cloudbuild_triggers.rb b/test/integration/verify/controls/google_cloudbuild_triggers.rb new file mode 100644 index 000000000..13c853257 --- /dev/null +++ b/test/integration/verify/controls/google_cloudbuild_triggers.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_cloudbuild_triggers resource.' + +gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.') +trigger = attribute('trigger', default: { + "trigger_template_project": "trigger-project", + "trigger_template_branch": "trigger-branch", + "trigger_template_repo": "trigger-repo", + "filename": "cloudbuild.yaml" +}, description: 'CloudBuild trigger definition') +control 'google_cloudbuild_triggers-1.0' do + impact 1.0 + title 'google_cloudbuild_triggers resource test' + + describe google_cloudbuild_triggers(project: gcp_project_id) do + its('count') { should eq 1 } + end + + google_cloudbuild_triggers(project: gcp_project_id).ids.each do |id| + describe google_cloudbuild_trigger(project: gcp_project_id, id: id) do + its('filename') { should eq trigger['filename'] } + its('trigger_template.branch_name') { should eq trigger['trigger_template_branch'] } + its('trigger_template.repo_name') { should eq trigger['trigger_template_repo'] } + its('trigger_template.project_id') { should eq trigger['trigger_template_project'] } + end + end +end diff --git a/test/integration/verify/controls/google_pubsub_topics.rb b/test/integration/verify/controls/google_pubsub_topics.rb index ad9b77464..00647e06a 100644 --- a/test/integration/verify/controls/google_pubsub_topics.rb +++ b/test/integration/verify/controls/google_pubsub_topics.rb @@ -24,12 +24,14 @@ describe google_pubsub_topics(project: gcp_project_id) do it { should exist } its('names') { should include topic['name'] } - its('count') { should eq 1 } + its('count') { should eq 2 } end - google_pubsub_topics(project: gcp_project_id).names.each do |topic_name| - describe google_pubsub_topic(project: gcp_project_id, name: topic_name) do - its('name') { should eq topic['name'] } + describe.one do + google_pubsub_topics(project: gcp_project_id).names.each do |topic_name| + describe google_pubsub_topic(project: gcp_project_id, name: topic_name) do + its('name') { should eq topic['name'] } + end end end end From 5a06e30aac4bd4c3f8ba5e1b20fe294b4ec36757 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Tue, 22 Jan 2019 21:41:01 +0000 Subject: [PATCH 2/5] Update test to work in more environments Signed-off-by: Modular Magician --- docs/resources/google_pubsub_subscription.md | 1 - docs/resources/google_pubsub_topics.md | 4 ++-- test/integration/verify/controls/google_pubsub_topics.rb | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/resources/google_pubsub_subscription.md b/docs/resources/google_pubsub_subscription.md index 2b2b81392..9762a829d 100644 --- a/docs/resources/google_pubsub_subscription.md +++ b/docs/resources/google_pubsub_subscription.md @@ -16,7 +16,6 @@ end describe google_pubsub_subscription(project: 'chef-gcp-inspec', name: 'nonexistent') do it { should_not exist } end - ``` ## Properties diff --git a/docs/resources/google_pubsub_topics.md b/docs/resources/google_pubsub_topics.md index 5c4de17cc..85673f142 100644 --- a/docs/resources/google_pubsub_topics.md +++ b/docs/resources/google_pubsub_topics.md @@ -12,13 +12,13 @@ A `google_pubsub_topics` is used to test a Google Topic resource describe google_pubsub_topics(project: 'chef-gcp-inspec') do it { should exist } its('names') { should include 'inspec-gcp-topic' } - its('count') { should eq 2 } + its('count') { should be >=1 } end describe.one do google_pubsub_topics(project: 'chef-gcp-inspec').names.each do |topic_name| describe google_pubsub_topic(project: 'chef-gcp-inspec', name: topic_name) do - its('name') { should eq 'inspec-gcp-topic' } + it { should exist } end end end diff --git a/test/integration/verify/controls/google_pubsub_topics.rb b/test/integration/verify/controls/google_pubsub_topics.rb index 00647e06a..8ca38ac77 100644 --- a/test/integration/verify/controls/google_pubsub_topics.rb +++ b/test/integration/verify/controls/google_pubsub_topics.rb @@ -24,13 +24,13 @@ describe google_pubsub_topics(project: gcp_project_id) do it { should exist } its('names') { should include topic['name'] } - its('count') { should eq 2 } + its('count') { should be >=1 } end describe.one do google_pubsub_topics(project: gcp_project_id).names.each do |topic_name| describe google_pubsub_topic(project: gcp_project_id, name: topic_name) do - its('name') { should eq topic['name'] } + it { should exist } end end end From 2fd7bd1041485c02d086e1b19f8d8381b78adc4f Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Tue, 22 Jan 2019 15:22:34 -0800 Subject: [PATCH 3/5] Change auth scope to cloud-platform to support Cloud Build Signed-off-by: Sam Levenick --- libraries/gcp_backend.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libraries/gcp_backend.rb b/libraries/gcp_backend.rb index 01031738d..8f77b8631 100644 --- a/libraries/gcp_backend.rb +++ b/libraries/gcp_backend.rb @@ -198,9 +198,7 @@ def fetch_auth unless @service_account_file.nil? return Network::Authorization.new.for!( [ - 'https://www.googleapis.com/auth/compute.readonly', - 'https://www.googleapis.com/auth/pubsub', - 'https://www.googleapis.com/auth/ndev.clouddns.readonly', + 'https://www.googleapis.com/auth/cloud-platform', ], ).from_service_account_json!( @service_account_file, From 4223b948ba25b56c481c2822b03f0b4043e11918 Mon Sep 17 00:00:00 2001 From: Chris Stephens Date: Tue, 22 Jan 2019 23:35:28 +0000 Subject: [PATCH 4/5] Change default boot disk size to 15g for dataproc Signed-off-by: Modular Magician --- docs/resources/google_pubsub_subscription.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/resources/google_pubsub_subscription.md b/docs/resources/google_pubsub_subscription.md index 2b2b81392..9762a829d 100644 --- a/docs/resources/google_pubsub_subscription.md +++ b/docs/resources/google_pubsub_subscription.md @@ -16,7 +16,6 @@ end describe google_pubsub_subscription(project: 'chef-gcp-inspec', name: 'nonexistent') do it { should_not exist } end - ``` ## Properties From e82ced259d7fcbb4e6a63a469430f8ee1ee50349 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Wed, 23 Jan 2019 18:23:38 +0000 Subject: [PATCH 5/5] Inspec health check Signed-off-by: Modular Magician --- docs/resources/google_compute_health_check.md | 94 ++++++++++++++++ .../resources/google_compute_health_checks.md | 38 +++++++ .../property/healthcheck_http_health_check.rb | 44 ++++++++ .../healthcheck_https_health_check.rb | 44 ++++++++ .../property/healthcheck_ssl_health_check.rb | 41 +++++++ .../property/healthcheck_tcp_health_check.rb | 41 +++++++ libraries/google_compute_health_check.rb | 79 +++++++++++++ libraries/google_compute_health_checks.rb | 106 ++++++++++++++++++ test/integration/build/gcp-mm.tf | 16 +++ .../configuration/mm-attributes.yml | 8 +- .../controls/google_compute_health_check.rb | 37 ++++++ .../controls/google_compute_health_checks.rb | 32 ++++++ 12 files changed, 579 insertions(+), 1 deletion(-) create mode 100644 docs/resources/google_compute_health_check.md create mode 100644 docs/resources/google_compute_health_checks.md create mode 100644 libraries/google/compute/property/healthcheck_http_health_check.rb create mode 100644 libraries/google/compute/property/healthcheck_https_health_check.rb create mode 100644 libraries/google/compute/property/healthcheck_ssl_health_check.rb create mode 100644 libraries/google/compute/property/healthcheck_tcp_health_check.rb create mode 100644 libraries/google_compute_health_check.rb create mode 100644 libraries/google_compute_health_checks.rb create mode 100644 test/integration/verify/controls/google_compute_health_check.rb create mode 100644 test/integration/verify/controls/google_compute_health_checks.rb diff --git a/docs/resources/google_compute_health_check.md b/docs/resources/google_compute_health_check.md new file mode 100644 index 000000000..8f0e68b56 --- /dev/null +++ b/docs/resources/google_compute_health_check.md @@ -0,0 +1,94 @@ +--- +title: About the HealthCheck resource +platform: gcp +--- + + +## Syntax +A `google_compute_health_check` is used to test a Google HealthCheck resource + +## Examples +``` +describe google_compute_health_check(project: 'chef-gcp-inspec', name: 'inspec-gcp-health-check') do + it { should exist } + its('timeout_sec') { should eq '10' } + its('tcp_health_check.port') { should eq '80' } +end + +describe google_compute_health_check(project: 'chef-gcp-inspec', name: 'nonexistent') do + it { should_not exist } +end +``` + +## Properties +Properties that can be accessed from the `google_compute_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. + + * `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. + + * `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. + + * `type`: Specifies the type of the healthCheck, either TCP, SSL, HTTP or HTTPS. If not specified, the default is TCP. Exactly one of the protocol-specific health check field must be specified, which must match type field. + + * `http_health_check`: A nested object resource + + * `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. + + * `requestPath`: The request path of the HTTP health check request. The default value is /. + + * `response`: The bytes to match against the beginning of the response data. If left empty (the default value), any response will indicate health. The response data can only be ASCII. + + * `port`: The TCP port number for the HTTP health check request. The default value is 80. + + * `portName`: Port name as defined in InstanceGroup#NamedPort#name. If both port and port_name are defined, port takes precedence. + + * `proxyHeader`: Specifies the type of proxy header to append before sending data to the backend, either NONE or PROXY_V1. The default is NONE. + + * `https_health_check`: A nested object resource + + * `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. + + * `requestPath`: The request path of the HTTPS health check request. The default value is /. + + * `response`: The bytes to match against the beginning of the response data. If left empty (the default value), any response will indicate health. The response data can only be ASCII. + + * `port`: The TCP port number for the HTTPS health check request. The default value is 443. + + * `portName`: Port name as defined in InstanceGroup#NamedPort#name. If both port and port_name are defined, port takes precedence. + + * `proxyHeader`: Specifies the type of proxy header to append before sending data to the backend, either NONE or PROXY_V1. The default is NONE. + + * `tcp_health_check`: A nested object resource + + * `request`: The application data to send once the TCP connection has been established (default value is empty). If both request and response are empty, the connection establishment alone will indicate health. The request data can only be ASCII. + + * `response`: The bytes to match against the beginning of the response data. If left empty (the default value), any response will indicate health. The response data can only be ASCII. + + * `port`: The TCP port number for the TCP health check request. The default value is 443. + + * `portName`: Port name as defined in InstanceGroup#NamedPort#name. If both port and port_name are defined, port takes precedence. + + * `proxyHeader`: Specifies the type of proxy header to append before sending data to the backend, either NONE or PROXY_V1. The default is NONE. + + * `ssl_health_check`: A nested object resource + + * `request`: The application data to send once the SSL connection has been established (default value is empty). If both request and response are empty, the connection establishment alone will indicate health. The request data can only be ASCII. + + * `response`: The bytes to match against the beginning of the response data. If left empty (the default value), any response will indicate health. The response data can only be ASCII. + + * `port`: The TCP port number for the SSL health check request. The default value is 443. + + * `portName`: Port name as defined in InstanceGroup#NamedPort#name. If both port and port_name are defined, port takes precedence. + + * `proxyHeader`: Specifies the type of proxy header to append before sending data to the backend, either NONE or PROXY_V1. The default is NONE. diff --git a/docs/resources/google_compute_health_checks.md b/docs/resources/google_compute_health_checks.md new file mode 100644 index 000000000..58eb9651c --- /dev/null +++ b/docs/resources/google_compute_health_checks.md @@ -0,0 +1,38 @@ +--- +title: About the HealthCheck resource +platform: gcp +--- + + +## Syntax +A `google_compute_health_checks` is used to test a Google HealthCheck resource + +## Examples +``` +describe google_compute_health_checks(project: 'chef-gcp-inspec') do + its('names') { should include 'inspec-gcp-health-check' } + its('timeout_secs') { should include '10' } +end +``` + +## Properties +Properties that can be accessed from the `google_compute_health_checks` resource: + +See [google_compute_health_check.md](google_compute_health_check.md) for more detailed information + * `check_interval_secs`: an array of `google_compute_health_check` check_interval_sec + * `creation_timestamps`: an array of `google_compute_health_check` creation_timestamp + * `descriptions`: an array of `google_compute_health_check` description + * `healthy_thresholds`: an array of `google_compute_health_check` healthy_threshold + * `ids`: an array of `google_compute_health_check` id + * `names`: an array of `google_compute_health_check` name + * `timeout_secs`: an array of `google_compute_health_check` timeout_sec + * `unhealthy_thresholds`: an array of `google_compute_health_check` unhealthy_threshold + * `types`: an array of `google_compute_health_check` type + * `http_health_checks`: an array of `google_compute_health_check` http_health_check + * `https_health_checks`: an array of `google_compute_health_check` https_health_check + * `tcp_health_checks`: an array of `google_compute_health_check` tcp_health_check + * `ssl_health_checks`: an array of `google_compute_health_check` ssl_health_check + +## 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/property/healthcheck_http_health_check.rb b/libraries/google/compute/property/healthcheck_http_health_check.rb new file mode 100644 index 000000000..c90d392ea --- /dev/null +++ b/libraries/google/compute/property/healthcheck_http_health_check.rb @@ -0,0 +1,44 @@ +# 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. +# +# ---------------------------------------------------------------------------- +module GoogleInSpec + module Compute + module Property + class HealthCheckHttphealthcheck + attr_reader :host + + attr_reader :request_path + + attr_reader :response + + attr_reader :port + + attr_reader :port_name + + attr_reader :proxy_header + + def initialize(args = nil) + return if args.nil? + @host = args['host'] + @request_path = args['requestPath'] + @response = args['response'] + @port = args['port'] + @port_name = args['portName'] + @proxy_header = args['proxyHeader'] + end + end + end + end +end diff --git a/libraries/google/compute/property/healthcheck_https_health_check.rb b/libraries/google/compute/property/healthcheck_https_health_check.rb new file mode 100644 index 000000000..3b88901ba --- /dev/null +++ b/libraries/google/compute/property/healthcheck_https_health_check.rb @@ -0,0 +1,44 @@ +# 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. +# +# ---------------------------------------------------------------------------- +module GoogleInSpec + module Compute + module Property + class HealthCheckHttpshealthcheck + attr_reader :host + + attr_reader :request_path + + attr_reader :response + + attr_reader :port + + attr_reader :port_name + + attr_reader :proxy_header + + def initialize(args = nil) + return if args.nil? + @host = args['host'] + @request_path = args['requestPath'] + @response = args['response'] + @port = args['port'] + @port_name = args['portName'] + @proxy_header = args['proxyHeader'] + end + end + end + end +end diff --git a/libraries/google/compute/property/healthcheck_ssl_health_check.rb b/libraries/google/compute/property/healthcheck_ssl_health_check.rb new file mode 100644 index 000000000..f4c11d938 --- /dev/null +++ b/libraries/google/compute/property/healthcheck_ssl_health_check.rb @@ -0,0 +1,41 @@ +# 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. +# +# ---------------------------------------------------------------------------- +module GoogleInSpec + module Compute + module Property + class HealthCheckSslhealthcheck + attr_reader :request + + attr_reader :response + + attr_reader :port + + attr_reader :port_name + + attr_reader :proxy_header + + def initialize(args = nil) + return if args.nil? + @request = args['request'] + @response = args['response'] + @port = args['port'] + @port_name = args['portName'] + @proxy_header = args['proxyHeader'] + end + end + end + end +end diff --git a/libraries/google/compute/property/healthcheck_tcp_health_check.rb b/libraries/google/compute/property/healthcheck_tcp_health_check.rb new file mode 100644 index 000000000..85e6e8165 --- /dev/null +++ b/libraries/google/compute/property/healthcheck_tcp_health_check.rb @@ -0,0 +1,41 @@ +# 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. +# +# ---------------------------------------------------------------------------- +module GoogleInSpec + module Compute + module Property + class HealthCheckTcphealthcheck + attr_reader :request + + attr_reader :response + + attr_reader :port + + attr_reader :port_name + + attr_reader :proxy_header + + def initialize(args = nil) + return if args.nil? + @request = args['request'] + @response = args['response'] + @port = args['port'] + @port_name = args['portName'] + @proxy_header = args['proxyHeader'] + end + end + end + end +end diff --git a/libraries/google_compute_health_check.rb b/libraries/google_compute_health_check.rb new file mode 100644 index 000000000..894751361 --- /dev/null +++ b/libraries/google_compute_health_check.rb @@ -0,0 +1,79 @@ +# 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' +require 'google/compute/property/healthcheck_http_health_check' +require 'google/compute/property/healthcheck_https_health_check' +require 'google/compute/property/healthcheck_ssl_health_check' +require 'google/compute/property/healthcheck_tcp_health_check' + +# A provider to manage Google Compute Engine resources. +class HealthCheck < GcpResourceBase + name 'google_compute_health_check' + desc 'HealthCheck' + supports platform: 'gcp' + + attr_reader :check_interval_sec + attr_reader :creation_timestamp + attr_reader :description + attr_reader :healthy_threshold + attr_reader :id + attr_reader :name + attr_reader :timeout_sec + attr_reader :unhealthy_threshold + attr_reader :type + attr_reader :http_health_check + attr_reader :https_health_check + attr_reader :tcp_health_check + attr_reader :ssl_health_check + def base + 'https://www.googleapis.com/compute/v1/' + end + + def url + 'projects/{{project}}/global/healthChecks/{{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'] + @id = @fetched['id'] + @name = @fetched['name'] + @timeout_sec = @fetched['timeoutSec'] + @unhealthy_threshold = @fetched['unhealthyThreshold'] + @type = @fetched['type'] + @http_health_check = GoogleInSpec::Compute::Property::HealthCheckHttphealthcheck.new(@fetched['httpHealthCheck']) + @https_health_check = GoogleInSpec::Compute::Property::HealthCheckHttpshealthcheck.new(@fetched['httpsHealthCheck']) + @tcp_health_check = GoogleInSpec::Compute::Property::HealthCheckTcphealthcheck.new(@fetched['tcpHealthCheck']) + @ssl_health_check = GoogleInSpec::Compute::Property::HealthCheckSslhealthcheck.new(@fetched['sslHealthCheck']) + 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_health_checks.rb b/libraries/google_compute_health_checks.rb new file mode 100644 index 000000000..ae436b26b --- /dev/null +++ b/libraries/google_compute_health_checks.rb @@ -0,0 +1,106 @@ +# 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 HealthChecks < GcpResourceBase + name 'google_compute_health_checks' + desc 'HealthCheck 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(:ids, field: :id) + filter_table_config.add(:names, field: :name) + filter_table_config.add(:timeout_secs, field: :timeout_sec) + filter_table_config.add(:unhealthy_thresholds, field: :unhealthy_threshold) + filter_table_config.add(:types, field: :type) + filter_table_config.add(:http_health_checks, field: :http_health_check) + filter_table_config.add(:https_health_checks, field: :https_health_check) + filter_table_config.add(:tcp_health_checks, field: :tcp_health_check) + filter_table_config.add(:ssl_health_checks, field: :ssl_health_check) + + filter_table_config.connect(self, :table) + + def base + 'https://www.googleapis.com/compute/v1/' + end + + def url + 'projects/{{project}}/global/healthChecks' + 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'] }, + 'id' => ->(obj) { return :id, obj['id'] }, + 'name' => ->(obj) { return :name, obj['name'] }, + 'timeoutSec' => ->(obj) { return :timeout_sec, obj['timeoutSec'] }, + 'unhealthyThreshold' => ->(obj) { return :unhealthy_threshold, obj['unhealthyThreshold'] }, + 'type' => ->(obj) { return :type, obj['type'] }, + 'httpHealthCheck' => ->(obj) { return :http_health_check, GoogleInSpec::Compute::Property::HealthCheckHttphealthcheck.new(obj['httpHealthCheck']) }, + 'httpsHealthCheck' => ->(obj) { return :https_health_check, GoogleInSpec::Compute::Property::HealthCheckHttpshealthcheck.new(obj['httpsHealthCheck']) }, + 'tcpHealthCheck' => ->(obj) { return :tcp_health_check, GoogleInSpec::Compute::Property::HealthCheckTcphealthcheck.new(obj['tcpHealthCheck']) }, + 'sslHealthCheck' => ->(obj) { return :ssl_health_check, GoogleInSpec::Compute::Property::HealthCheckSslhealthcheck.new(obj['sslHealthCheck']) }, + } + 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 5a8ab2ad0..74402c25f 100644 --- a/test/integration/build/gcp-mm.tf +++ b/test/integration/build/gcp-mm.tf @@ -34,6 +34,10 @@ variable "trigger" { type = "map" } +variable "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"]}" @@ -124,4 +128,16 @@ resource "google_cloudbuild_trigger" "gcp-inspec-cloudbuild-trigger" { repo_name = "${var.trigger["trigger_template_repo"]}" } filename = "${var.trigger["filename"]}" +} + +resource "google_compute_health_check" "gcp-inspec-health-check" { + project = "${var.gcp_project_id}" + name = "${var.health_check["name"]}" + + timeout_sec = "${var.health_check["timeout_sec"]}" + check_interval_sec = "${var.health_check["check_interval_sec"]}" + + tcp_health_check { + port = "${var.health_check["tcp_health_check_port"]}" + } } \ No newline at end of file diff --git a/test/integration/configuration/mm-attributes.yml b/test/integration/configuration/mm-attributes.yml index 6ae397078..e59e95e9a 100644 --- a/test/integration/configuration/mm-attributes.yml +++ b/test/integration/configuration/mm-attributes.yml @@ -46,4 +46,10 @@ trigger: trigger_template_project: trigger-project trigger_template_branch: trigger-branch trigger_template_repo: trigger-repo - filename: cloudbuild.yaml \ No newline at end of file + filename: cloudbuild.yaml + +health_check: + name: inspec-gcp-health-check + timeout_sec: 10 + check_interval_sec: 10 + tcp_health_check_port: 80 \ No newline at end of file diff --git a/test/integration/verify/controls/google_compute_health_check.rb b/test/integration/verify/controls/google_compute_health_check.rb new file mode 100644 index 000000000..2b64c09de --- /dev/null +++ b/test/integration/verify/controls/google_compute_health_check.rb @@ -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_health_check resource.' + +gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.') +health_check = attribute('health_check', default: { + "name": "inspec-gcp-health-check", + "timeout_sec": 10, + "check_interval_sec": 10, + "tcp_health_check_port": 80 +}, description: 'Health check definition') +control 'google_compute_health_check-1.0' do + impact 1.0 + title 'google_compute_health_check resource test' + + describe google_compute_health_check(project: gcp_project_id, name: health_check['name']) do + it { should exist } + its('timeout_sec') { should eq health_check['timeout_sec'] } + its('tcp_health_check.port') { should eq health_check['tcp_health_check_port'] } + end + + describe google_compute_health_check(project: gcp_project_id, name: 'nonexistent') do + it { should_not exist } + end +end diff --git a/test/integration/verify/controls/google_compute_health_checks.rb b/test/integration/verify/controls/google_compute_health_checks.rb new file mode 100644 index 000000000..90da22336 --- /dev/null +++ b/test/integration/verify/controls/google_compute_health_checks.rb @@ -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_compute_health_checks resource.' + +gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.') +health_check = attribute('health_check', default: { + "name": "inspec-gcp-health-check", + "timeout_sec": 10, + "check_interval_sec": 10, + "tcp_health_check_port": 80 +}, description: 'Health check definition') +control 'google_compute_health_checks-1.0' do + impact 1.0 + title 'google_compute_health_checks resource test' + + describe google_compute_health_checks(project: gcp_project_id) do + its('names') { should include health_check['name'] } + its('timeout_secs') { should include health_check['timeout_sec'] } + end +end