Method: Compliance::API.profiles

Defined in:
lib/bundles/inspec-compliance/api.rb

.profiles(config) ⇒ Object

return all compliance profiles available for the user the user is either specified in the options hash or by default the username of the account is used that is logged in



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bundles/inspec-compliance/api.rb', line 21

def self.profiles(config)
  owner = config['owner'] || config['user']

  # Chef Compliance

  if is_compliance_server?(config)
    url = "#{config['server']}/user/compliance"
  # Chef Automate

  elsif is_automate_server?(config)
    url = "#{config['server']}/profiles/#{owner}"
  else
    raise ServerConfigurationMissing
  end

  headers = get_headers(config)
  response = Compliance::HTTP.get(url, headers, config['insecure'])
  data = response.body
  response_code = response.code
  case response_code
  when '200'
    msg = 'success'
    profiles = JSON.parse(data)
    # iterate over profiles

    if is_compliance_server?(config)
      mapped_profiles = []
      profiles.values.each { |org|
        mapped_profiles += org.values
      }
    # Chef Automate pre 0.8.0

    elsif is_automate_server_pre_080?(config)
      mapped_profiles = profiles.values.flatten
    else
      mapped_profiles = profiles.map { |e|
        e['owner_id'] = owner
        e
      }
    end
    return msg, mapped_profiles
  when '401'
    msg = '401 Unauthorized. Please check your token.'
    return msg, []
  else
    msg = "An unexpected error occurred (HTTP #{response_code}): #{response.message}"
    return msg, []
  end
end