From 5a57da53b56369fcef104a057d0c7b295aa367a1 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Thu, 17 Jan 2019 21:55:40 +0000 Subject: [PATCH] Inspec autoscalers Signed-off-by: Modular Magician --- docs/resources/google_compute_autoscaler.md | 54 +++++++++++ docs/resources/google_compute_autoscalers.md | 39 ++++++++ .../google_compute_instance_group_managers.md | 1 - docs/resources/google_pubsub_subscriptions.md | 1 - docs/resources/google_pubsub_topics.md | 1 - .../property/autoscaler_autoscaling_policy.rb | 47 ++++++++++ .../property/autoscaler_cpu_utilization.rb | 29 ++++++ .../autoscaler_custom_metric_utilizations.rb | 43 +++++++++ .../autoscaler_load_balancing_utilization.rb | 29 ++++++ libraries/google_compute_autoscaler.rb | 67 +++++++++++++ libraries/google_compute_autoscalers.rb | 94 +++++++++++++++++++ libraries/google_compute_disks.rb | 65 ++++++++++--- .../google_compute_instance_group_managers.rb | 50 ++++++++-- libraries/google_compute_ssl_policies.rb | 39 +++++++- libraries/google_dns_resource_record_sets.rb | 32 +++++-- libraries/google_pubsub_subscriptions.rb | 30 +++++- libraries/google_pubsub_topics.rb | 23 ++++- test/integration/build/gcp-mm.tf | 21 +++++ .../configuration/mm-attributes.yml | 6 ++ .../controls/google_compute_autoscaler.rb | 49 ++++++++++ .../controls/google_compute_autoscalers.rb | 47 ++++++++++ 21 files changed, 724 insertions(+), 43 deletions(-) create mode 100644 docs/resources/google_compute_autoscaler.md create mode 100644 docs/resources/google_compute_autoscalers.md create mode 100644 libraries/google/compute/property/autoscaler_autoscaling_policy.rb create mode 100644 libraries/google/compute/property/autoscaler_cpu_utilization.rb create mode 100644 libraries/google/compute/property/autoscaler_custom_metric_utilizations.rb create mode 100644 libraries/google/compute/property/autoscaler_load_balancing_utilization.rb create mode 100644 libraries/google_compute_autoscaler.rb create mode 100644 libraries/google_compute_autoscalers.rb create mode 100644 test/integration/verify/controls/google_compute_autoscaler.rb create mode 100644 test/integration/verify/controls/google_compute_autoscalers.rb diff --git a/docs/resources/google_compute_autoscaler.md b/docs/resources/google_compute_autoscaler.md new file mode 100644 index 000000000..8ce7f1302 --- /dev/null +++ b/docs/resources/google_compute_autoscaler.md @@ -0,0 +1,54 @@ +--- +title: About the Autoscaler resource +platform: gcp +--- + + +## Syntax +A `google_compute_autoscaler` is used to test a Google Autoscaler resource + +## Examples +``` +describe google_compute_autoscaler(project: 'chef-gcp-inspec', zone: 'zone', name: 'inspec-gcp-autoscaler') do + it { should exist } + + its('target') { should match /\/inspec-gcp-igm$/ } + its('autoscaling_policy.max_num_replicas') { should eq '5' } + its('autoscaling_policy.min_num_replicas') { should eq '1' } + its('autoscaling_policy.cool_down_period_sec') { should eq '60' } + its('autoscaling_policy.cpu_utilization.utilization_target') { should eq '0.5' } +end + +describe google_compute_autoscaler(project: 'chef-gcp-inspec', zone: 'zone', name: 'nonexistent') do + it { should_not exist } +end +``` + +## Properties +Properties that can be accessed from the `google_compute_autoscaler` resource: + + * `id`: Unique identifier for the resource. + + * `creation_timestamp`: Creation timestamp in RFC3339 text format. + + * `name`: Name of the resource. 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. + + * `description`: An optional description of this resource. + + * `autoscaling_policy`: The configuration parameters for the autoscaling algorithm. You can define one or more of the policies for an autoscaler: cpuUtilization, customMetricUtilizations, and loadBalancingUtilization. If none of these are specified, the default will be to autoscale based on cpuUtilization to 0.6 or 60%. + + * `minNumReplicas`: The minimum number of replicas that the autoscaler can scale down to. This cannot be less than 0. If not provided, autoscaler will choose a default value depending on maximum number of instances allowed. + + * `maxNumReplicas`: The maximum number of instances that the autoscaler can scale up to. This is required when creating or updating an autoscaler. The maximum number of replicas should not be lower than minimal number of replicas. + + * `coolDownPeriodSec`: The number of seconds that the autoscaler should wait before it starts collecting information from a new instance. This prevents the autoscaler from collecting information when the instance is initializing, during which the collected usage would not be reliable. The default time autoscaler waits is 60 seconds. Virtual machine initialization times might vary because of numerous factors. We recommend that you test how long an instance may take to initialize. To do this, create an instance and time the startup process. + + * `cpuUtilization`: Defines the CPU utilization policy that allows the autoscaler to scale based on the average CPU utilization of a managed instance group. + + * `customMetricUtilizations`: Defines the CPU utilization policy that allows the autoscaler to scale based on the average CPU utilization of a managed instance group. + + * `loadBalancingUtilization`: Configuration parameters of autoscaling based on a load balancer. + + * `target`: URL of the managed instance group that this autoscaler will scale. + + * `zone`: URL of the zone where the instance group resides. diff --git a/docs/resources/google_compute_autoscalers.md b/docs/resources/google_compute_autoscalers.md new file mode 100644 index 000000000..0f076076e --- /dev/null +++ b/docs/resources/google_compute_autoscalers.md @@ -0,0 +1,39 @@ +--- +title: About the Autoscaler resource +platform: gcp +--- + + +## Syntax +A `google_compute_autoscalers` is used to test a Google Autoscaler resource + +## Examples +``` +autoscalers = google_compute_autoscalers(project: 'chef-gcp-inspec', zone: 'zone') +describe.one do + autoscalers.autoscaling_policies.each do |autoscaling_policy| + describe autoscaling_policy do + its('max_num_replicas') { should eq '5' } + its('min_num_replicas') { should eq '1' } + its('cool_down_period_sec') { should eq '60' } + its('cpu_utilization.utilization_target') { should eq '0.5' } + end + end +end +``` + +## Properties +Properties that can be accessed from the `google_compute_autoscalers` resource: + +See [google_compute_autoscaler.md](google_compute_autoscaler.md) for more detailed information + * `ids`: an array of `google_compute_autoscaler` id + * `creation_timestamps`: an array of `google_compute_autoscaler` creation_timestamp + * `names`: an array of `google_compute_autoscaler` name + * `descriptions`: an array of `google_compute_autoscaler` description + * `autoscaling_policies`: an array of `google_compute_autoscaler` autoscaling_policy + * `targets`: an array of `google_compute_autoscaler` target + * `zones`: an array of `google_compute_autoscaler` zone + +## 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_compute_instance_group_managers.md b/docs/resources/google_compute_instance_group_managers.md index 03fc20747..a5cf98836 100644 --- a/docs/resources/google_compute_instance_group_managers.md +++ b/docs/resources/google_compute_instance_group_managers.md @@ -12,7 +12,6 @@ A `google_compute_instance_group_managers` is used to test a Google InstanceGrou describe google_compute_instance_group_managers(project: 'chef-gcp-inspec', zone: 'zone') do its('base_instance_names') { should include 'igm' } end - ``` ## Properties diff --git a/docs/resources/google_pubsub_subscriptions.md b/docs/resources/google_pubsub_subscriptions.md index 3213eb84c..ae01487c3 100644 --- a/docs/resources/google_pubsub_subscriptions.md +++ b/docs/resources/google_pubsub_subscriptions.md @@ -19,7 +19,6 @@ google_pubsub_subscriptions(project: 'chef-gcp-inspec').names.each do |subscript it { should exist } end end - ``` ## Properties diff --git a/docs/resources/google_pubsub_topics.md b/docs/resources/google_pubsub_topics.md index 888a844ae..cb30fdb51 100644 --- a/docs/resources/google_pubsub_topics.md +++ b/docs/resources/google_pubsub_topics.md @@ -20,7 +20,6 @@ google_pubsub_topics(project: 'chef-gcp-inspec').names.each do |topic_name| its('name') { should eq 'inspec-gcp-topic' } end end - ``` ## Properties diff --git a/libraries/google/compute/property/autoscaler_autoscaling_policy.rb b/libraries/google/compute/property/autoscaler_autoscaling_policy.rb new file mode 100644 index 000000000..a7e0f9bb2 --- /dev/null +++ b/libraries/google/compute/property/autoscaler_autoscaling_policy.rb @@ -0,0 +1,47 @@ +# 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/compute/property/autoscaler_cpu_utilization' +require 'google/compute/property/autoscaler_custom_metric_utilizations' +require 'google/compute/property/autoscaler_load_balancing_utilization' +module GoogleInSpec + module Compute + module Property + class AutoscalerAutoscalingpolicy + attr_reader :min_num_replicas + + attr_reader :max_num_replicas + + attr_reader :cool_down_period_sec + + attr_reader :cpu_utilization + + attr_reader :custom_metric_utilizations + + attr_reader :load_balancing_utilization + + def initialize(args = nil) + return if args.nil? + @min_num_replicas = args['minNumReplicas'] + @max_num_replicas = args['maxNumReplicas'] + @cool_down_period_sec = args['coolDownPeriodSec'] + @cpu_utilization = GoogleInSpec::Compute::Property::AutoscalerCpuutilization.new(args['cpuUtilization']) + @custom_metric_utilizations = GoogleInSpec::Compute::Property::AutoscalerCustommetricutilizationsArray.parse(args['customMetricUtilizations']) + @load_balancing_utilization = GoogleInSpec::Compute::Property::AutoscalerLoadbalancingutilization.new(args['loadBalancingUtilization']) + end + end + end + end +end diff --git a/libraries/google/compute/property/autoscaler_cpu_utilization.rb b/libraries/google/compute/property/autoscaler_cpu_utilization.rb new file mode 100644 index 000000000..12d179582 --- /dev/null +++ b/libraries/google/compute/property/autoscaler_cpu_utilization.rb @@ -0,0 +1,29 @@ +# 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 AutoscalerCpuutilization + attr_reader :utilization_target + + def initialize(args = nil) + return if args.nil? + @utilization_target = args['utilizationTarget'] + end + end + end + end +end diff --git a/libraries/google/compute/property/autoscaler_custom_metric_utilizations.rb b/libraries/google/compute/property/autoscaler_custom_metric_utilizations.rb new file mode 100644 index 000000000..9307323c2 --- /dev/null +++ b/libraries/google/compute/property/autoscaler_custom_metric_utilizations.rb @@ -0,0 +1,43 @@ +# 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 AutoscalerCustommetricutilizations + attr_reader :metric + + attr_reader :utilization_target + + attr_reader :utilization_target_type + + def initialize(args = nil) + return if args.nil? + @metric = args['metric'] + @utilization_target = args['utilizationTarget'] + @utilization_target_type = args['utilizationTargetType'] + end + end + + class AutoscalerCustommetricutilizationsArray + def self.parse(value) + return if value.nil? + return AutoscalerCustommetricutilizations.new(value) unless value.is_a?(::Array) + value.map { |v| AutoscalerCustommetricutilizations.new(v) } + end + end + end + end +end diff --git a/libraries/google/compute/property/autoscaler_load_balancing_utilization.rb b/libraries/google/compute/property/autoscaler_load_balancing_utilization.rb new file mode 100644 index 000000000..b6510b902 --- /dev/null +++ b/libraries/google/compute/property/autoscaler_load_balancing_utilization.rb @@ -0,0 +1,29 @@ +# 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 AutoscalerLoadbalancingutilization + attr_reader :utilization_target + + def initialize(args = nil) + return if args.nil? + @utilization_target = args['utilizationTarget'] + end + end + end + end +end diff --git a/libraries/google_compute_autoscaler.rb b/libraries/google_compute_autoscaler.rb new file mode 100644 index 000000000..b5c590dca --- /dev/null +++ b/libraries/google_compute_autoscaler.rb @@ -0,0 +1,67 @@ +# 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/autoscaler_autoscaling_policy' +require 'google/compute/property/autoscaler_cpu_utilization' +require 'google/compute/property/autoscaler_custom_metric_utilizations' +require 'google/compute/property/autoscaler_load_balancing_utilization' + +# A provider to manage Google Compute Engine resources. +class Autoscaler < GcpResourceBase + name 'google_compute_autoscaler' + desc 'Autoscaler' + supports platform: 'gcp' + + attr_reader :id + attr_reader :creation_timestamp + attr_reader :name + attr_reader :description + attr_reader :autoscaling_policy + attr_reader :target + attr_reader :zone + def base + 'https://www.googleapis.com/compute/v1/' + end + + def url + 'projects/{{project}}/zones/{{zone}}/autoscalers/{{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 + @id = @fetched['id'] + @creation_timestamp = parse_time_string(@fetched['creationTimestamp']) + @name = @fetched['name'] + @description = @fetched['description'] + @autoscaling_policy = GoogleInSpec::Compute::Property::AutoscalerAutoscalingpolicy.new(@fetched['autoscalingPolicy']) + @target = @fetched['target'] + @zone = @fetched['zone'] + 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_autoscalers.rb b/libraries/google_compute_autoscalers.rb new file mode 100644 index 000000000..e98ad8fce --- /dev/null +++ b/libraries/google_compute_autoscalers.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: false + +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in README.md and +# CONTRIBUTING.md located at the root of this package. +# +# ---------------------------------------------------------------------------- +require 'gcp_backend' +class Autoscalers < GcpResourceBase + name 'google_compute_autoscalers' + desc 'Autoscaler plural resource' + supports platform: 'gcp' + + attr_reader :table + + filter_table_config = FilterTable.create + + filter_table_config.add(:ids, field: :id) + filter_table_config.add(:creation_timestamps, field: :creation_timestamp) + filter_table_config.add(:names, field: :name) + filter_table_config.add(:descriptions, field: :description) + filter_table_config.add(:autoscaling_policies, field: :autoscaling_policy) + filter_table_config.add(:targets, field: :target) + filter_table_config.add(:zones, field: :zone) + + filter_table_config.connect(self, :table) + + def base + 'https://www.googleapis.com/compute/v1/' + end + + def url + 'projects/{{project}}/zones/{{zone}}/autoscalers' + 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 + { + 'id' => ->(obj) { return :id, obj['id'] }, + 'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) }, + 'name' => ->(obj) { return :name, obj['name'] }, + 'description' => ->(obj) { return :description, obj['description'] }, + 'autoscalingPolicy' => ->(obj) { return :autoscaling_policy, GoogleInSpec::Compute::Property::AutoscalerAutoscalingpolicy.new(obj['autoscalingPolicy']) }, + 'target' => ->(obj) { return :target, obj['target'] }, + 'zone' => ->(obj) { return :zone, obj['zone'] }, + } + end + + # Handles parsing RFC3339 time string + def parse_time_string(time_string) + time_string ? Time.parse(time_string) : nil + end +end diff --git a/libraries/google_compute_disks.rb b/libraries/google_compute_disks.rb index aed4c3363..32e875a1d 100644 --- a/libraries/google_compute_disks.rb +++ b/libraries/google_compute_disks.rb @@ -23,26 +23,26 @@ class Disks < GcpResourceBase filter_table_config = FilterTable.create - filter_table_config.add(:label_fingerprints, field: :labelFingerprint) - filter_table_config.add(:creation_timestamps, field: :creationTimestamp) + filter_table_config.add(:label_fingerprints, field: :label_fingerprint) + 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(:last_attach_timestamps, field: :lastAttachTimestamp) - filter_table_config.add(:last_detach_timestamps, field: :lastDetachTimestamp) + filter_table_config.add(:last_attach_timestamps, field: :last_attach_timestamp) + filter_table_config.add(:last_detach_timestamps, field: :last_detach_timestamp) filter_table_config.add(:labels, field: :labels) filter_table_config.add(:licenses, field: :licenses) filter_table_config.add(:names, field: :name) - filter_table_config.add(:size_gbs, field: :sizeGb) + filter_table_config.add(:size_gbs, field: :size_gb) filter_table_config.add(:users, field: :users) filter_table_config.add(:types, field: :type) - filter_table_config.add(:source_images, field: :sourceImage) + filter_table_config.add(:source_images, field: :source_image) filter_table_config.add(:zones, field: :zone) - filter_table_config.add(:source_image_encryption_keys, field: :sourceImageEncryptionKey) - filter_table_config.add(:source_image_ids, field: :sourceImageId) - filter_table_config.add(:disk_encryption_keys, field: :diskEncryptionKey) - filter_table_config.add(:source_snapshots, field: :sourceSnapshot) - filter_table_config.add(:source_snapshot_encryption_keys, field: :sourceSnapshotEncryptionKey) - filter_table_config.add(:source_snapshot_ids, field: :sourceSnapshotId) + filter_table_config.add(:source_image_encryption_keys, field: :source_image_encryption_key) + filter_table_config.add(:source_image_ids, field: :source_image_id) + filter_table_config.add(:disk_encryption_keys, field: :disk_encryption_key) + filter_table_config.add(:source_snapshots, field: :source_snapshot) + filter_table_config.add(:source_snapshot_encryption_keys, field: :source_snapshot_encryption_key) + filter_table_config.add(:source_snapshot_ids, field: :source_snapshot_id) filter_table_config.connect(self, :table) @@ -71,11 +71,50 @@ def fetch_wrapped_resource(wrap_path) next if response.nil? || !response.key?(wrap_path) response[wrap_path].each do |hash| hash_with_symbols = {} - hash.each_pair { |k, v| hash_with_symbols[k.to_sym] = v } + 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 + { + 'labelFingerprint' => ->(obj) { return :label_fingerprint, obj['labelFingerprint'] }, + 'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) }, + 'description' => ->(obj) { return :description, obj['description'] }, + 'id' => ->(obj) { return :id, obj['id'] }, + 'lastAttachTimestamp' => ->(obj) { return :last_attach_timestamp, parse_time_string(obj['lastAttachTimestamp']) }, + 'lastDetachTimestamp' => ->(obj) { return :last_detach_timestamp, parse_time_string(obj['lastDetachTimestamp']) }, + 'labels' => ->(obj) { return :labels, obj['labels'] }, + 'licenses' => ->(obj) { return :licenses, obj['licenses'] }, + 'name' => ->(obj) { return :name, obj['name'] }, + 'sizeGb' => ->(obj) { return :size_gb, obj['sizeGb'] }, + 'users' => ->(obj) { return :users, obj['users'] }, + 'type' => ->(obj) { return :type, obj['type'] }, + 'sourceImage' => ->(obj) { return :source_image, obj['sourceImage'] }, + 'zone' => ->(obj) { return :zone, obj['zone'] }, + 'sourceImageEncryptionKey' => ->(obj) { return :source_image_encryption_key, GoogleInSpec::Compute::Property::DiskSourceimageencryptionkey.new(obj['sourceImageEncryptionKey']) }, + 'sourceImageId' => ->(obj) { return :source_image_id, obj['sourceImageId'] }, + 'diskEncryptionKey' => ->(obj) { return :disk_encryption_key, GoogleInSpec::Compute::Property::DiskDiskencryptionkey.new(obj['diskEncryptionKey']) }, + 'sourceSnapshot' => ->(obj) { return :source_snapshot, obj['sourceSnapshot'] }, + 'sourceSnapshotEncryptionKey' => ->(obj) { return :source_snapshot_encryption_key, GoogleInSpec::Compute::Property::DiskSourcesnapshotencryptionkey.new(obj['sourceSnapshotEncryptionKey']) }, + 'sourceSnapshotId' => ->(obj) { return :source_snapshot_id, obj['sourceSnapshotId'] }, + } + end + + # Handles parsing RFC3339 time string + def parse_time_string(time_string) + time_string ? Time.parse(time_string) : nil + end end diff --git a/libraries/google_compute_instance_group_managers.rb b/libraries/google_compute_instance_group_managers.rb index c2b2b7545..45aa62e61 100644 --- a/libraries/google_compute_instance_group_managers.rb +++ b/libraries/google_compute_instance_group_managers.rb @@ -23,18 +23,18 @@ class InstanceGroupManagers < GcpResourceBase filter_table_config = FilterTable.create - filter_table_config.add(:base_instance_names, field: :baseInstanceName) - filter_table_config.add(:creation_timestamps, field: :creationTimestamp) - filter_table_config.add(:current_actions, field: :currentActions) + filter_table_config.add(:base_instance_names, field: :base_instance_name) + filter_table_config.add(:creation_timestamps, field: :creation_timestamp) + filter_table_config.add(:current_actions, field: :current_actions) filter_table_config.add(:descriptions, field: :description) filter_table_config.add(:ids, field: :id) - filter_table_config.add(:instance_groups, field: :instanceGroup) - filter_table_config.add(:instance_templates, field: :instanceTemplate) + filter_table_config.add(:instance_groups, field: :instance_group) + filter_table_config.add(:instance_templates, field: :instance_template) filter_table_config.add(:names, field: :name) - filter_table_config.add(:named_ports, field: :namedPorts) + filter_table_config.add(:named_ports, field: :named_ports) filter_table_config.add(:regions, field: :region) - filter_table_config.add(:target_pools, field: :targetPools) - filter_table_config.add(:target_sizes, field: :targetSize) + filter_table_config.add(:target_pools, field: :target_pools) + filter_table_config.add(:target_sizes, field: :target_size) filter_table_config.add(:zones, field: :zone) filter_table_config.connect(self, :table) @@ -64,11 +64,43 @@ def fetch_wrapped_resource(wrap_path) next if response.nil? || !response.key?(wrap_path) response[wrap_path].each do |hash| hash_with_symbols = {} - hash.each_pair { |k, v| hash_with_symbols[k.to_sym] = v } + 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 + { + 'baseInstanceName' => ->(obj) { return :base_instance_name, obj['baseInstanceName'] }, + 'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) }, + 'currentActions' => ->(obj) { return :current_actions, GoogleInSpec::Compute::Property::InstanceGroupManagerCurrentactions.new(obj['currentActions']) }, + 'description' => ->(obj) { return :description, obj['description'] }, + 'id' => ->(obj) { return :id, obj['id'] }, + 'instanceGroup' => ->(obj) { return :instance_group, obj['instanceGroup'] }, + 'instanceTemplate' => ->(obj) { return :instance_template, obj['instanceTemplate'] }, + 'name' => ->(obj) { return :name, obj['name'] }, + 'namedPorts' => ->(obj) { return :named_ports, GoogleInSpec::Compute::Property::InstanceGroupManagerNamedportsArray.parse(obj['namedPorts']) }, + 'region' => ->(obj) { return :region, obj['region'] }, + 'targetPools' => ->(obj) { return :target_pools, obj['targetPools'] }, + 'targetSize' => ->(obj) { return :target_size, obj['targetSize'] }, + 'zone' => ->(obj) { return :zone, obj['zone'] }, + } + end + + # Handles parsing RFC3339 time string + def parse_time_string(time_string) + time_string ? Time.parse(time_string) : nil + end end diff --git a/libraries/google_compute_ssl_policies.rb b/libraries/google_compute_ssl_policies.rb index 567f84dfe..c1175286b 100644 --- a/libraries/google_compute_ssl_policies.rb +++ b/libraries/google_compute_ssl_policies.rb @@ -23,14 +23,14 @@ class SslPolicys < GcpResourceBase filter_table_config = FilterTable.create - filter_table_config.add(:creation_timestamps, field: :creationTimestamp) + 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(:profiles, field: :profile) - filter_table_config.add(:min_tls_versions, field: :minTlsVersion) - filter_table_config.add(:enabled_features, field: :enabledFeatures) - filter_table_config.add(:custom_features, field: :customFeatures) + filter_table_config.add(:min_tls_versions, field: :min_tls_version) + filter_table_config.add(:enabled_features, field: :enabled_features) + filter_table_config.add(:custom_features, field: :custom_features) filter_table_config.add(:fingerprints, field: :fingerprint) filter_table_config.add(:warnings, field: :warnings) @@ -61,11 +61,40 @@ def fetch_wrapped_resource(wrap_path) next if response.nil? || !response.key?(wrap_path) response[wrap_path].each do |hash| hash_with_symbols = {} - hash.each_pair { |k, v| hash_with_symbols[k.to_sym] = v } + 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'] }, + 'profile' => ->(obj) { return :profile, obj['profile'] }, + 'minTlsVersion' => ->(obj) { return :min_tls_version, obj['minTlsVersion'] }, + 'enabledFeatures' => ->(obj) { return :enabled_features, obj['enabledFeatures'] }, + 'customFeatures' => ->(obj) { return :custom_features, obj['customFeatures'] }, + 'fingerprint' => ->(obj) { return :fingerprint, obj['fingerprint'] }, + 'warnings' => ->(obj) { return :warnings, GoogleInSpec::Compute::Property::SslPolicyWarningsArray.parse(obj['warnings']) }, + } + end + + # Handles parsing RFC3339 time string + def parse_time_string(time_string) + time_string ? Time.parse(time_string) : nil + end end diff --git a/libraries/google_dns_resource_record_sets.rb b/libraries/google_dns_resource_record_sets.rb index 3ab457ab8..6c54d9495 100644 --- a/libraries/google_dns_resource_record_sets.rb +++ b/libraries/google_dns_resource_record_sets.rb @@ -39,12 +39,6 @@ def url 'projects/{{project}}/managedZones/{{managed_zone}}/rrsets' end - def api_names - { - 'rrdatas' => 'target', - } - end - def initialize(params = {}) super(params.merge({ use_http_transport: true })) @params = params @@ -62,11 +56,35 @@ def fetch_wrapped_resource(wrap_path) next if response.nil? || !response.key?(wrap_path) response[wrap_path].each do |hash| hash_with_symbols = {} - hash.each_pair { |k, v| api_names.key?(k) ? hash_with_symbols[api_names[k].to_sym] = v : hash_with_symbols[k.to_sym] = v } + 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 + { + 'name' => ->(obj) { return :name, obj['name'] }, + 'type' => ->(obj) { return :type, obj['type'] }, + 'ttl' => ->(obj) { return :ttl, obj['ttl'] }, + 'rrdatas' => ->(obj) { return :target, obj['rrdatas'] }, + 'managed_zone' => ->(obj) { return :managed_zone, obj['managed_zone'] }, + } + end + + # Handles parsing RFC3339 time string + def parse_time_string(time_string) + time_string ? Time.parse(time_string) : nil + end end diff --git a/libraries/google_pubsub_subscriptions.rb b/libraries/google_pubsub_subscriptions.rb index 902c6ff61..099da6cc7 100644 --- a/libraries/google_pubsub_subscriptions.rb +++ b/libraries/google_pubsub_subscriptions.rb @@ -25,8 +25,8 @@ class Subscriptions < GcpResourceBase filter_table_config.add(:names, field: :name) filter_table_config.add(:topics, field: :topic) - filter_table_config.add(:push_configs, field: :pushConfig) - filter_table_config.add(:ack_deadline_seconds, field: :ackDeadlineSeconds) + filter_table_config.add(:push_configs, field: :push_config) + filter_table_config.add(:ack_deadline_seconds, field: :ack_deadline_seconds) filter_table_config.connect(self, :table) @@ -55,12 +55,34 @@ def fetch_wrapped_resource(wrap_path) next if response.nil? || !response.key?(wrap_path) response[wrap_path].each do |hash| hash_with_symbols = {} - hash.each_pair { |k, v| hash_with_symbols[k.to_sym] = v } - hash_with_symbols[:name] = name_from_self_link(hash_with_symbols[:name]) + 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 + { + 'name' => ->(obj) { return :name, name_from_self_link(obj['name']) }, + 'topic' => ->(obj) { return :topic, obj['topic'] }, + 'pushConfig' => ->(obj) { return :push_config, GoogleInSpec::Pubsub::Property::SubscriptionPushconfig.new(obj['pushConfig']) }, + 'ackDeadlineSeconds' => ->(obj) { return :ack_deadline_seconds, obj['ackDeadlineSeconds'] }, + } + end + + # Handles parsing RFC3339 time string + def parse_time_string(time_string) + time_string ? Time.parse(time_string) : nil + end end diff --git a/libraries/google_pubsub_topics.rb b/libraries/google_pubsub_topics.rb index 236f933e5..8399223b5 100644 --- a/libraries/google_pubsub_topics.rb +++ b/libraries/google_pubsub_topics.rb @@ -52,12 +52,31 @@ def fetch_wrapped_resource(wrap_path) next if response.nil? || !response.key?(wrap_path) response[wrap_path].each do |hash| hash_with_symbols = {} - hash.each_pair { |k, v| hash_with_symbols[k.to_sym] = v } - hash_with_symbols[:name] = name_from_self_link(hash_with_symbols[:name]) + 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 + { + 'name' => ->(obj) { return :name, name_from_self_link(obj['name']) }, + } + 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 fc0ba35d6..c81cf7f4f 100644 --- a/test/integration/build/gcp-mm.tf +++ b/test/integration/build/gcp-mm.tf @@ -22,6 +22,10 @@ variable "instance_group_manager" { type = "map" } +variable "autoscaler" { + type = "map" +} + resource "google_compute_ssl_policy" "custom-ssl-policy" { name = "${var.ssl_policy["name"]}" min_tls_version = "${var.ssl_policy["min_tls_version"]}" @@ -76,3 +80,20 @@ resource "google_compute_instance_group_manager" "gcp-inspec-igm" { port = "${var.instance_group_manager["named_port_port"]}" } } + +resource "google_compute_autoscaler" "gcp-inspec-autoscaler" { + project = "${var.gcp_project_id}" + name = "${var.autoscaler["name"]}" + zone = "${var.gcp_zone}" + target = "${google_compute_instance_group_manager.gcp-inspec-igm.self_link}" + + autoscaling_policy = { + max_replicas = "${var.autoscaler["max_replicas"]}" + min_replicas = "${var.autoscaler["min_replicas"]}" + cooldown_period = "${var.autoscaler["cooldown_period"]}" + + cpu_utilization { + target = "${var.autoscaler["cpu_utilization_target"]}" + } + } +} \ No newline at end of file diff --git a/test/integration/configuration/mm-attributes.yml b/test/integration/configuration/mm-attributes.yml index ec110f497..2ef77126e 100644 --- a/test/integration/configuration/mm-attributes.yml +++ b/test/integration/configuration/mm-attributes.yml @@ -31,3 +31,9 @@ instance_group_manager: base_instance_name: 'igm' named_port_name: 'port' named_port_port: 80 +autoscaler: + name: 'inspec-gcp-autoscaler' + max_replicas: 5 + min_replicas: 1 + cooldown_period: 60 + cpu_utilization_target: 0.5 \ No newline at end of file diff --git a/test/integration/verify/controls/google_compute_autoscaler.rb b/test/integration/verify/controls/google_compute_autoscaler.rb new file mode 100644 index 000000000..0c3ac281c --- /dev/null +++ b/test/integration/verify/controls/google_compute_autoscaler.rb @@ -0,0 +1,49 @@ +# ---------------------------------------------------------------------------- +# +# *** 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_autoscaler resource.' + +gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.') +gcp_zone = attribute(:gcp_zone, default: 'gcp_zone', description: 'The GCP project zone.') +instance_group_manager = attribute('instance_group_manager', default: { + "name": "inspec-gcp-igm", + "base_instance_name": "igm", + "named_port_name": "port", + "named_port_port": 80 +}, description: 'Instance group manager definition') +autoscaler = attribute('autoscaler', default: { + "name": "inspec-gcp-autoscaler", + "max_replicas": 5, + "min_replicas": 1, + "cooldown_period": 60, + "cpu_utilization_target": 0.5 +}, description: 'Autoscaler definition') +control 'google_compute_autoscaler-1.0' do + impact 1.0 + title 'google_compute_autoscaler resource test' + + describe google_compute_autoscaler(project: gcp_project_id, zone: gcp_zone, name: autoscaler['name']) do + it { should exist } + + its('target') { should match /\/inspec-gcp-igm$/ } + its('autoscaling_policy.max_num_replicas') { should eq autoscaler['max_replicas'] } + its('autoscaling_policy.min_num_replicas') { should eq autoscaler['min_replicas'] } + its('autoscaling_policy.cool_down_period_sec') { should eq autoscaler['cooldown_period'] } + its('autoscaling_policy.cpu_utilization.utilization_target') { should eq autoscaler['cpu_utilization_target'] } + end + + describe google_compute_autoscaler(project: gcp_project_id, zone: gcp_zone, name: 'nonexistent') do + it { should_not exist } + end +end diff --git a/test/integration/verify/controls/google_compute_autoscalers.rb b/test/integration/verify/controls/google_compute_autoscalers.rb new file mode 100644 index 000000000..f3e78e2f7 --- /dev/null +++ b/test/integration/verify/controls/google_compute_autoscalers.rb @@ -0,0 +1,47 @@ +# ---------------------------------------------------------------------------- +# +# *** 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_autoscalers resource.' + +gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.') +gcp_zone = attribute(:gcp_zone, default: 'gcp_zone', description: 'The GCP project zone.') +instance_group_manager = attribute('instance_group_manager', default: { + "name": "inspec-gcp-igm", + "base_instance_name": "igm", + "named_port_name": "port", + "named_port_port": 80 +}, description: 'Instance group manager definition') +autoscaler = attribute('autoscaler', default: { + "name": "inspec-gcp-autoscaler", + "max_replicas": 5, + "min_replicas": 1, + "cooldown_period": 60, + "cpu_utilization_target": 0.5 +}, description: 'Autoscaler definition') +control 'google_compute_autoscalers-1.0' do + impact 1.0 + title 'google_compute_autoscalers resource test' + + autoscalers = google_compute_autoscalers(project: gcp_project_id, zone: gcp_zone) + describe.one do + autoscalers.autoscaling_policies.each do |autoscaling_policy| + describe autoscaling_policy do + its('max_num_replicas') { should eq autoscaler['max_replicas'] } + its('min_num_replicas') { should eq autoscaler['min_replicas'] } + its('cool_down_period_sec') { should eq autoscaler['cooldown_period'] } + its('cpu_utilization.utilization_target') { should eq autoscaler['cpu_utilization_target'] } + end + end + end +end