|
| 1 | +# frozen_string_literal: false |
| 2 | + |
| 3 | +# ---------------------------------------------------------------------------- |
| 4 | +# |
| 5 | +# *** AUTO GENERATED CODE *** Type: MMv1 *** |
| 6 | +# |
| 7 | +# ---------------------------------------------------------------------------- |
| 8 | +# |
| 9 | +# This file is automatically generated by Magic Modules and manual |
| 10 | +# changes will be clobbered when the file is regenerated. |
| 11 | +# |
| 12 | +# Please read more about how to change this file in README.md and |
| 13 | +# CONTRIBUTING.md located at the root of this package. |
| 14 | +# |
| 15 | +# ---------------------------------------------------------------------------- |
| 16 | +require 'gcp_backend' |
| 17 | +class MonitoringGroups < GcpResourceBase |
| 18 | + name 'google_monitoring_groups' |
| 19 | + desc 'Group plural resource' |
| 20 | + supports platform: 'gcp' |
| 21 | + |
| 22 | + attr_reader :table |
| 23 | + |
| 24 | + filter_table_config = FilterTable.create |
| 25 | + |
| 26 | + filter_table_config.add(:names, field: :name) |
| 27 | + filter_table_config.add(:display_names, field: :display_name) |
| 28 | + filter_table_config.add(:parent_names, field: :parent_name) |
| 29 | + filter_table_config.add(:filters, field: :filter) |
| 30 | + filter_table_config.add(:is_clusters, field: :is_cluster) |
| 31 | + |
| 32 | + filter_table_config.connect(self, :table) |
| 33 | + |
| 34 | + def initialize(params = {}) |
| 35 | + super(params.merge({ use_http_transport: true })) |
| 36 | + @params = params |
| 37 | + @table = fetch_wrapped_resource('group') |
| 38 | + end |
| 39 | + |
| 40 | + def fetch_wrapped_resource(wrap_path) |
| 41 | + # fetch_resource returns an array of responses (to handle pagination) |
| 42 | + result = @connection.fetch_all(product_url, resource_base_url, @params, 'Get') |
| 43 | + return if result.nil? |
| 44 | + |
| 45 | + # Conversion of string -> object hash to symbol -> object hash that InSpec needs |
| 46 | + converted = [] |
| 47 | + result.each do |response| |
| 48 | + next if response.nil? || !response.key?(wrap_path) |
| 49 | + response[wrap_path].each do |hash| |
| 50 | + hash_with_symbols = {} |
| 51 | + hash.each_key do |key| |
| 52 | + name, value = transform(key, hash) |
| 53 | + hash_with_symbols[name] = value |
| 54 | + end |
| 55 | + converted.push(hash_with_symbols) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + converted |
| 60 | + end |
| 61 | + |
| 62 | + def transform(key, value) |
| 63 | + return transformers[key].call(value) if transformers.key?(key) |
| 64 | + |
| 65 | + [key.to_sym, value] |
| 66 | + end |
| 67 | + |
| 68 | + def transformers |
| 69 | + { |
| 70 | + 'name' => ->(obj) { [:name, obj['name']] }, |
| 71 | + 'displayName' => ->(obj) { [:display_name, obj['displayName']] }, |
| 72 | + 'parentName' => ->(obj) { [:parent_name, obj['parentName']] }, |
| 73 | + 'filter' => ->(obj) { [:filter, obj['filter']] }, |
| 74 | + 'isCluster' => ->(obj) { [:is_cluster, obj['isCluster']] }, |
| 75 | + } |
| 76 | + end |
| 77 | + |
| 78 | + private |
| 79 | + |
| 80 | + def product_url(_ = nil) |
| 81 | + 'https://monitoring.googleapis.com/v3/' |
| 82 | + end |
| 83 | + |
| 84 | + def resource_base_url |
| 85 | + '{{name}}/groups' |
| 86 | + end |
| 87 | +end |
0 commit comments