/libraries/google_compute_subnetworks.rb
https://github.com/inspec/inspec-gcp · Ruby · 114 lines · 81 code · 16 blank · 17 comment · 5 complexity · a228c2ada172a9bf75d2f6317a5676b8 MD5 · raw file
- # 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 ComputeSubnetworks < GcpResourceBase
- name 'google_compute_subnetworks'
- desc 'Subnetwork plural resource'
- supports platform: 'gcp'
- attr_reader :table
- filter_table_config = FilterTable.create
- filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
- filter_table_config.add(:descriptions, field: :description)
- filter_table_config.add(:gateway_addresses, field: :gateway_address)
- filter_table_config.add(:subnetwork_ids, field: :subnetwork_id)
- filter_table_config.add(:ip_cidr_ranges, field: :ip_cidr_range)
- filter_table_config.add(:subnetwork_names, field: :subnetwork_name)
- filter_table_config.add(:networks, field: :network)
- filter_table_config.add(:purposes, field: :purpose)
- filter_table_config.add(:roles, field: :role)
- filter_table_config.add(:secondary_ip_ranges, field: :secondary_ip_ranges)
- filter_table_config.add(:private_ip_google_accesses, field: :private_ip_google_access)
- filter_table_config.add(:private_ipv6_google_accesses, field: :private_ipv6_google_access)
- filter_table_config.add(:regions, field: :region)
- filter_table_config.add(:log_configs, field: :log_config)
- filter_table_config.connect(self, :table)
- 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(product_url, resource_base_url, @params, 'Get')
- 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
- {
- 'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) },
- 'description' => ->(obj) { return :description, obj['description'] },
- 'gatewayAddress' => ->(obj) { return :gateway_address, obj['gatewayAddress'] },
- 'id' => ->(obj) { return :subnetwork_id, obj['id'] },
- 'ipCidrRange' => ->(obj) { return :ip_cidr_range, obj['ipCidrRange'] },
- 'name' => ->(obj) { return :subnetwork_name, obj['name'] },
- 'network' => ->(obj) { return :network, obj['network'] },
- 'purpose' => ->(obj) { return :purpose, obj['purpose'] },
- 'role' => ->(obj) { return :role, obj['role'] },
- 'secondaryIpRanges' => ->(obj) { return :secondary_ip_ranges, GoogleInSpec::Compute::Property::SubnetworkSecondaryIpRangesArray.parse(obj['secondaryIpRanges'], to_s) },
- 'privateIpGoogleAccess' => ->(obj) { return :private_ip_google_access, obj['privateIpGoogleAccess'] },
- 'privateIpv6GoogleAccess' => ->(obj) { return :private_ipv6_google_access, obj['privateIpv6GoogleAccess'] },
- 'region' => ->(obj) { return :region, obj['region'] },
- 'logConfig' => ->(obj) { return :log_config, GoogleInSpec::Compute::Property::SubnetworkLogConfig.new(obj['logConfig'], to_s) },
- }
- end
- # Handles parsing RFC3339 time string
- def parse_time_string(time_string)
- time_string ? Time.parse(time_string) : nil
- end
- private
- def product_url(beta = false)
- if beta
- 'https://compute.googleapis.com/compute/beta/'
- else
- 'https://compute.googleapis.com/compute/v1/'
- end
- end
- def resource_base_url
- 'projects/{{project}}/regions/{{region}}/subnetworks'
- end
- end