/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

  1. # frozen_string_literal: false
  2. # ----------------------------------------------------------------------------
  3. #
  4. # *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
  5. #
  6. # ----------------------------------------------------------------------------
  7. #
  8. # This file is automatically generated by Magic Modules and manual
  9. # changes will be clobbered when the file is regenerated.
  10. #
  11. # Please read more about how to change this file in README.md and
  12. # CONTRIBUTING.md located at the root of this package.
  13. #
  14. # ----------------------------------------------------------------------------
  15. require 'gcp_backend'
  16. class ComputeSubnetworks < GcpResourceBase
  17. name 'google_compute_subnetworks'
  18. desc 'Subnetwork plural resource'
  19. supports platform: 'gcp'
  20. attr_reader :table
  21. filter_table_config = FilterTable.create
  22. filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
  23. filter_table_config.add(:descriptions, field: :description)
  24. filter_table_config.add(:gateway_addresses, field: :gateway_address)
  25. filter_table_config.add(:subnetwork_ids, field: :subnetwork_id)
  26. filter_table_config.add(:ip_cidr_ranges, field: :ip_cidr_range)
  27. filter_table_config.add(:subnetwork_names, field: :subnetwork_name)
  28. filter_table_config.add(:networks, field: :network)
  29. filter_table_config.add(:purposes, field: :purpose)
  30. filter_table_config.add(:roles, field: :role)
  31. filter_table_config.add(:secondary_ip_ranges, field: :secondary_ip_ranges)
  32. filter_table_config.add(:private_ip_google_accesses, field: :private_ip_google_access)
  33. filter_table_config.add(:private_ipv6_google_accesses, field: :private_ipv6_google_access)
  34. filter_table_config.add(:regions, field: :region)
  35. filter_table_config.add(:log_configs, field: :log_config)
  36. filter_table_config.connect(self, :table)
  37. def initialize(params = {})
  38. super(params.merge({ use_http_transport: true }))
  39. @params = params
  40. @table = fetch_wrapped_resource('items')
  41. end
  42. def fetch_wrapped_resource(wrap_path)
  43. # fetch_resource returns an array of responses (to handle pagination)
  44. result = @connection.fetch_all(product_url, resource_base_url, @params, 'Get')
  45. return if result.nil?
  46. # Conversion of string -> object hash to symbol -> object hash that InSpec needs
  47. converted = []
  48. result.each do |response|
  49. next if response.nil? || !response.key?(wrap_path)
  50. response[wrap_path].each do |hash|
  51. hash_with_symbols = {}
  52. hash.each_key do |key|
  53. name, value = transform(key, hash)
  54. hash_with_symbols[name] = value
  55. end
  56. converted.push(hash_with_symbols)
  57. end
  58. end
  59. converted
  60. end
  61. def transform(key, value)
  62. return transformers[key].call(value) if transformers.key?(key)
  63. [key.to_sym, value]
  64. end
  65. def transformers
  66. {
  67. 'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) },
  68. 'description' => ->(obj) { return :description, obj['description'] },
  69. 'gatewayAddress' => ->(obj) { return :gateway_address, obj['gatewayAddress'] },
  70. 'id' => ->(obj) { return :subnetwork_id, obj['id'] },
  71. 'ipCidrRange' => ->(obj) { return :ip_cidr_range, obj['ipCidrRange'] },
  72. 'name' => ->(obj) { return :subnetwork_name, obj['name'] },
  73. 'network' => ->(obj) { return :network, obj['network'] },
  74. 'purpose' => ->(obj) { return :purpose, obj['purpose'] },
  75. 'role' => ->(obj) { return :role, obj['role'] },
  76. 'secondaryIpRanges' => ->(obj) { return :secondary_ip_ranges, GoogleInSpec::Compute::Property::SubnetworkSecondaryIpRangesArray.parse(obj['secondaryIpRanges'], to_s) },
  77. 'privateIpGoogleAccess' => ->(obj) { return :private_ip_google_access, obj['privateIpGoogleAccess'] },
  78. 'privateIpv6GoogleAccess' => ->(obj) { return :private_ipv6_google_access, obj['privateIpv6GoogleAccess'] },
  79. 'region' => ->(obj) { return :region, obj['region'] },
  80. 'logConfig' => ->(obj) { return :log_config, GoogleInSpec::Compute::Property::SubnetworkLogConfig.new(obj['logConfig'], to_s) },
  81. }
  82. end
  83. # Handles parsing RFC3339 time string
  84. def parse_time_string(time_string)
  85. time_string ? Time.parse(time_string) : nil
  86. end
  87. private
  88. def product_url(beta = false)
  89. if beta
  90. 'https://compute.googleapis.com/compute/beta/'
  91. else
  92. 'https://compute.googleapis.com/compute/v1/'
  93. end
  94. end
  95. def resource_base_url
  96. 'projects/{{project}}/regions/{{region}}/subnetworks'
  97. end
  98. end