Method: Inspec::Resources::AzureResourceBase#resources

Defined in:
lib/resources/azure/azure_backend.rb

#resourcesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/resources/azure/azure_backend.rb', line 68

def resources
  resources = nil
  catch_azure_errors do
    resources = client.resources.list_by_resource_group(opts[:group_name])
  end
  return if failed_resource?

  # filter the resources based on the type, and the name if they been specified
  resources = filter_resources(resources, opts)

  # if there is one resource then define methods on this class
  if resources.count == 1
    @total = 1

    resource = nil
    catch_azure_errors do
      # get the apiversion for the resource, if one has not been specified
      apiversion = azure.get_api_version(resources[0].type, opts)

      # get the resource by id so it can be interrogated
      resource = client.resources.get_by_id(resources[0].id, apiversion)
    end
    return if failed_resource?

    dm = AzureResourceDynamicMethods.new

    dm.create_methods(self, resource)
  else

    # As there are many resources, parse each one so that it can be
    # interrogated by the FilterTable
    # @probes = parse_resources(resources, azure)
    @probes = resources.each.map do |item|
      # update the total
      @total += 1

      # determine the counts for each type
      namespace, type_name = item.type.split(/\./)
      counts.key?(namespace) ? false : counts[namespace] = {}
      counts[namespace].key?(type_name) ? counts[namespace][type_name] += 1 : counts[namespace][type_name] = 1

      # get the detail about the resource
      apiversion = azure.get_api_version(item.type, opts)
      resource = client.resources.get_by_id(item.id, apiversion)

      # parse the resource
      parse_resource(resource)
    end.compact

    # Iterate around the counts and create the necessary classes
    counts.each do |namespace, ns_counts|
      define_singleton_method namespace do
        AzureResourceTypeCounts.new(ns_counts)
      end
    end
  end
end