Skip to content

Commit

Permalink
Inspec nested refactor (#1368)
Browse files Browse the repository at this point in the history
Merged PR #1368.
  • Loading branch information
slevenick authored and modular-magician committed Feb 13, 2019
1 parent 2c10204 commit 5d75070
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion build/inspec
Submodule inspec updated 76 files
+0 −9 docs/resources/google_bigquery_dataset.md
+2 −2 libraries/google/bigquery/property/dataset_access.rb
+1 −1 libraries/google/bigquery/property/dataset_access_view.rb
+1 −1 libraries/google/bigquery/property/dataset_dataset_reference.rb
+2 −2 libraries/google/cloudbuild/property/trigger_build.rb
+4 −4 libraries/google/cloudbuild/property/trigger_build_steps.rb
+1 −1 libraries/google/cloudbuild/property/trigger_trigger_template.rb
+7 −7 libraries/google/compute/property/autoscaler_autoscaling_policy.rb
+1 −1 libraries/google/compute/property/autoscaler_autoscaling_policy_cpu_utilization.rb
+4 −4 libraries/google/compute/property/autoscaler_autoscaling_policy_custom_metric_utilizations.rb
+1 −1 libraries/google/compute/property/autoscaler_autoscaling_policy_load_balancing_utilization.rb
+3 −3 libraries/google/compute/property/backendservice_cdn_policy.rb
+1 −1 libraries/google/compute/property/backendservice_cdn_policy_cache_key_policy.rb
+1 −1 libraries/google/compute/property/backendservice_connection_draining.rb
+1 −1 libraries/google/compute/property/disk_disk_encryption_key.rb
+1 −1 libraries/google/compute/property/disk_source_image_encryption_key.rb
+1 −1 libraries/google/compute/property/disk_source_snapshot_encryption_key.rb
+1 −1 libraries/google/compute/property/healthcheck_http_health_check.rb
+1 −1 libraries/google/compute/property/healthcheck_https_health_check.rb
+1 −1 libraries/google/compute/property/healthcheck_ssl_health_check.rb
+1 −1 libraries/google/compute/property/healthcheck_tcp_health_check.rb
+1 −1 libraries/google/compute/property/instancegroupmanager_current_actions.rb
+4 −4 libraries/google/compute/property/instancegroupmanager_named_ports.rb
+12 −12 libraries/google/compute/property/instancetemplate_properties.rb
+9 −9 libraries/google/compute/property/instancetemplate_properties_disks.rb
+1 −1 libraries/google/compute/property/instancetemplate_properties_disks_disk_encryption_key.rb
+3 −3 libraries/google/compute/property/instancetemplate_properties_disks_initialize_params.rb
+1 −1 .../google/compute/property/instancetemplate_properties_disks_initialize_params_source_image_encryption_key.rb
+4 −4 libraries/google/compute/property/instancetemplate_properties_guest_accelerators.rb
+8 −8 libraries/google/compute/property/instancetemplate_properties_network_interfaces.rb
+4 −4 libraries/google/compute/property/instancetemplate_properties_network_interfaces_access_configs.rb
+4 −4 libraries/google/compute/property/instancetemplate_properties_network_interfaces_alias_ip_ranges.rb
+1 −1 libraries/google/compute/property/instancetemplate_properties_scheduling.rb
+4 −4 libraries/google/compute/property/instancetemplate_properties_service_accounts.rb
+1 −1 libraries/google/compute/property/instancetemplate_properties_tags.rb
+2 −2 libraries/google/compute/property/router_bgp.rb
+4 −4 libraries/google/compute/property/router_bgp_advertised_ip_ranges.rb
+1 −1 libraries/google/compute/property/snapshot_snapshot_encryption_key.rb
+1 −1 libraries/google/compute/property/snapshot_source_disk_encryption_key.rb
+4 −4 libraries/google/compute/property/urlmap_host_rules.rb
+6 −6 libraries/google/compute/property/urlmap_path_matchers.rb
+4 −4 libraries/google/compute/property/urlmap_path_matchers_path_rules.rb
+5 −5 libraries/google/container/property/regionalcluster_addons_config.rb
+1 −1 libraries/google/container/property/regionalcluster_addons_config_horizontal_pod_autoscaling.rb
+1 −1 libraries/google/container/property/regionalcluster_addons_config_http_load_balancing.rb
+1 −1 libraries/google/container/property/regionalcluster_master_auth.rb
+1 −1 libraries/google/container/property/regionalcluster_node_config.rb
+1 −1 libraries/google/pubsub/property/subscription_push_config.rb
+1 −1 libraries/google_bigquery_dataset.rb
+1 −1 libraries/google_bigquery_datasets.rb
+2 −2 libraries/google_cloudbuild_trigger.rb
+1 −1 libraries/google_cloudbuild_triggers.rb
+4 −4 libraries/google_compute_autoscaler.rb
+1 −1 libraries/google_compute_autoscalers.rb
+3 −3 libraries/google_compute_backend_service.rb
+2 −2 libraries/google_compute_backend_services.rb
+3 −3 libraries/google_compute_disk.rb
+3 −3 libraries/google_compute_disks.rb
+4 −4 libraries/google_compute_health_check.rb
+4 −4 libraries/google_compute_health_checks.rb
+2 −2 libraries/google_compute_instance_group_manager.rb
+2 −2 libraries/google_compute_instance_group_managers.rb
+6 −6 libraries/google_compute_instance_template.rb
+1 −1 libraries/google_compute_router.rb
+2 −2 libraries/google_compute_snapshot.rb
+2 −2 libraries/google_compute_snapshots.rb
+2 −2 libraries/google_compute_url_map.rb
+2 −2 libraries/google_compute_url_maps.rb
+5 −5 libraries/google_container_regional_cluster.rb
+3 −3 libraries/google_container_regional_clusters.rb
+1 −1 libraries/google_pubsub_subscription.rb
+1 −1 libraries/google_pubsub_subscriptions.rb
+21 −4 test/integration/build/gcp-mm.tf
+7 −3 test/integration/configuration/mm-attributes.yml
+0 −11 test/integration/verify/controls/google_bigquery_dataset.rb
+0 −2 test/integration/verify/controls/google_bigquery_datasets.rb
2 changes: 1 addition & 1 deletion build/terraform
5 changes: 5 additions & 0 deletions products/bigquery/inspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides

Table: !ruby/object:Overrides::Inspec::ResourceOverride
exclude: true
properties:
name: !ruby/object:Overrides::Inspec::PropertyOverride
exclude_plural: true
description: !ruby/object:Overrides::Inspec::PropertyOverride
exclude_plural: true
32 changes: 23 additions & 9 deletions provider/inspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,9 @@ def generate_inspec_test(data, name, target_folder, attribute_file_name)
end

def emit_nested_object(property)
target = if property.is_a?(Api::Type::Array)
property.item_type.property_file
else
property.property_file
end
{
source: File.join('templates', 'inspec', 'nested_object.erb'),
target: "libraries/#{target}.rb",
target: "libraries/#{nested_object_requires(property)}.rb",
property: property
}
end
Expand Down Expand Up @@ -193,7 +188,7 @@ def nested_object_requires(nested_object_type)
'google',
nested_object_type.__resource.__product.api_name,
'property',
[nested_object_type.__resource.name, nested_object_type.name.underscore].join('_')
qualified_property_class(nested_object_type)
).downcase
end

Expand Down Expand Up @@ -234,6 +229,25 @@ def inspec_property_type(property)
property.property_type.sub('Google::', 'GoogleInSpec::')
end

def qualified_property_class(property)
name = property.name.underscore
other = property.__resource.name
until property.parent.nil?
property = property.parent
next if typed_array?(property)

name = property.name.underscore + '_' + name
end

other + '_' + name
end

def modularized_property_class(property)
class_name = qualified_property_class(property).camelize(:upper)
product_name = property.__resource.__product.name.camelize(:upper)
"GoogleInSpec::#{product_name}::Property::#{class_name}"
end

# Returns Ruby code that will parse the given property from a hash
# This is used in several places that need to parse an arbitrary property
# from a JSON representation
Expand All @@ -247,9 +261,9 @@ def parse_code(property, hash_name)

return item_from_hash.to_s
elsif typed_array?(property)
return "#{inspec_property_type(property)}.parse(#{item_from_hash})"
return "#{modularized_property_class(property.item_type)}Array.parse(#{item_from_hash})"
end
"#{inspec_property_type(property)}.new(#{item_from_hash})"
"#{modularized_property_class(property)}.new(#{item_from_hash})"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ describe google_bigquery_dataset(project: <%= doc_generation ? "#{gcp_project_id
its('default_table_expiration_ms') { should cmp <%= doc_generation ? "'#{dataset['default_table_expiration_ms']}'" : "dataset['default_table_expiration_ms']" -%> }
end

describe.one do
google_bigquery_dataset(project: <%= doc_generation ? "#{gcp_project_id}" : "gcp_project_id" -%>, name: <%= doc_generation ? "'#{dataset['dataset_id']}'" : "dataset['dataset_id']" -%>).access.each do |dataset_access|
describe dataset_access do
its('role') { should eq <%= doc_generation ? "'#{dataset['access_reader_role']}'" : "dataset['access_reader_role']" -%> }
its('domain') { should eq <%= doc_generation ? "'#{dataset['access_reader_domain']}'" : "dataset['access_reader_domain']" -%> }
end
end
end

describe.one do
google_bigquery_dataset(project: <%= doc_generation ? "#{gcp_project_id}" : "gcp_project_id" -%>, name: <%= doc_generation ? "'#{dataset['dataset_id']}'" : "dataset['dataset_id']" -%>).access.each do |dataset_access|
describe dataset_access do
Expand Down
2 changes: 1 addition & 1 deletion templates/inspec/nested_object.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
else
property
end
class_name = nested_property.property_class.last
class_name = qualified_property_class(nested_property).camelize(:upper)
product_ns = product.name.camelize(:upper)
-%>
# frozen_string_literal: false
Expand Down
25 changes: 21 additions & 4 deletions templates/inspec/tests/integration/build/gcp-mm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ variable "dataset" {
type = "map"
}

variable "bigquery_table" {
type = "map"
}

resource "google_compute_ssl_policy" "custom-ssl-policy" {
name = "${var.ssl_policy["name"]}"
min_tls_version = "${var.ssl_policy["min_tls_version"]}"
Expand Down Expand Up @@ -408,12 +412,25 @@ resource "google_bigquery_dataset" "gcp-inspec-dataset" {
default_table_expiration_ms = "${var.dataset["default_table_expiration_ms"]}"

access {
role = "${var.dataset["access_reader_role"]}"
domain = "${var.dataset["access_reader_domain"]}"
role = "${var.dataset["access_writer_role"]}"
special_group = "${var.dataset["access_writer_special_group"]}"
}

access {
role = "${var.dataset["access_writer_role"]}"
special_group = "${var.dataset["access_writer_special_group"]}"
role = "OWNER"
special_group = "projectOwners"
}
}

resource "google_bigquery_table" "gcp-inspec-bigquery-table" {
project = "${var.gcp_project_id}"
dataset_id = "${google_bigquery_dataset.gcp-inspec-dataset.dataset_id}"
table_id = "${var.bigquery_table["table_id"]}"

time_partitioning {
type = "${var.bigquery_table["time_partitioning_type"]}"
}

description = "${var.bigquery_table["description"]}"
expiration_time = "${var.bigquery_table["expiration_time"]}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ dataset:
description: Test BigQuery dataset description
location: EU
default_table_expiration_ms: 3600000
access_reader_role: READER
access_reader_domain: example.com
access_writer_role: WRITER
access_writer_special_group: projectWriters
access_writer_special_group: projectWriters

bigquery_table:
table_id: inspec_gcp_bigquery_table
description: A BigQuery table
expiration_time: 1738882264000
time_partitioning_type: DAY

0 comments on commit 5d75070

Please sign in to comment.