Class: RedHatSupportLib::TelemetryApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/api/telemetry_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(upload_url, api_url, creds, optional) ⇒ Client

Returns a new instance of Client.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/api/telemetry_api.rb', line 56

def initialize(upload_url,
               api_url,
               creds,
               optional)

  @creds = creds
  @upload_url = upload_url
  @api_url = api_url
  @subset_url = "#{@api_url}/subsets"
  @subset_list_type = SUBSET_LIST_TYPE_LEAF_ID

  if optional
    @logger = optional[:logger]
    @http_proxy = optional[:http_proxy] if optional[:http_proxy]
    @user_agent = optional[:user_agent] if optional[:user_agent]
    @http_headers = optional[:headers] if optional[:headers]
    @subset_list_type = optional[SUBSET_LIST_TYPE_KEY] if optional[SUBSET_LIST_TYPE_KEY]
  end
  ldebug ("HTTP proxy is set to #{@http_proxy}")
end

Instance Method Details

#call_strata(original_method, resource, original_params, original_payload, _extra) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/api/telemetry_api.rb', line 131

def call_strata(original_method,
                resource,
                original_params,
                original_payload,
                _extra)

  url = "#{@api_url}/#{resource}"
  client = default_rest_client(url, params: original_params, method: original_method, payload: original_payload)
  response = client.execute
  return {data: response, code: response.code}
rescue RestClient::ExceptionWithResponse => e
  lerror nil, "Caught HTTP error when proxying call to tapi: #{e}"
  return {data: e.response, error: e, code: e.response.code}
rescue Exception => e
  lerror e, "Caught unexpected error when proxying call to tapi: #{e}"
  return {data: e, error: e, code: 500}
end

#call_tapi(original_method, resource, original_params, original_payload, extra, use_subsets = true) ⇒ Object



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
125
126
127
128
129
# File 'lib/api/telemetry_api.rb', line 99

def call_tapi(original_method,
              resource,
              original_params,
              original_payload,
              extra, use_subsets = true)

  # merge all the params into one hash - this lets us add additional options
  args = {params: original_params, method: original_method, payload: original_payload}
  args.merge!(extra) if extra

  if (use_subsets && subset_resource = create_subset_route(resource))
    ldebug "Doing subset call to #{subset_resource} (was : #{resource})"
    response = do_subset_call("#{@api_url}/#{subset_resource}", args)
    return {data: response, code: response.code}
  else
    if extra && extra[:do_upload]
      url = @upload_url
    else
      url = "#{@api_url}/#{resource}"
    end
    client = default_rest_client(url, args)
    response = client.execute
    return {data: response, code: response.code}
  end
rescue RestClient::ExceptionWithResponse => e
  lerror nil, "Caught HTTP error when proxying call to tapi: #{e}"
  return {data: e.response, error: e, code: e.response.code}
rescue Exception => e
  lerror e, "Caught unexpected error when proxying call to tapi: #{e}"
  return {data: e, error: e, code: 500}
end

#call_tapi_no_subset(original_method, resource, original_params, original_payload, extra) ⇒ Object

not currently used by foreman-plugin…



89
90
91
92
93
94
95
96
97
# File 'lib/api/telemetry_api.rb', line 89

def call_tapi_no_subset(original_method,
                        resource,
                        original_params,
                        original_payload,
                        extra)

  ldebug ('Called no subset proxy')
  call_tapi(original_method, resource, original_params, original_payload, extra, false)
end

#create_subset_route(path) ⇒ Object

Creates and returns the subset route corresponding to ‘path’ if ‘path’ is a subset resource, otherwise

returns 'nil'


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/api/telemetry_api.rb', line 37

def create_subset_route(path)
  SUBSET_ROUTE_REGEX.each do |regex|
    if regex.match(path)
      subset_id = get_hash(get_machines)
      path.gsub(regex) do |s|
        api_version = Regexp.last_match[1]
        if api_version
          path = s.sub(api_version, "#{api_version}/subsets/#{subset_id}")
        else
          path = "subsets/#{subset_id}/#{s}"
        end
      end
      return path
    end
  end
  nil
end

#get_branch_idObject



153
154
155
# File 'lib/api/telemetry_api.rb', line 153

def get_branch_id
  throw NotImplementedError
end

#get_hash(machines) ⇒ Object

Returns the machines hash used for /subset/$hash/



158
159
160
161
162
# File 'lib/api/telemetry_api.rb', line 158

def get_hash(machines)
  branch = get_branch_id
  hash = Digest::SHA1.hexdigest(machines.sort.join)
  "#{branch}__#{hash}"
end

#get_machinesObject



149
150
151
# File 'lib/api/telemetry_api.rb', line 149

def get_machines
  throw NotImplementedError
end

#post_upload(original_params, original_payload) ⇒ Object

not currently used by foreman-plugin…



78
79
80
81
82
83
84
85
86
# File 'lib/api/telemetry_api.rb', line 78

def post_upload(original_params,
                original_payload)

  call_tapi('POST',
            '/',
            original_params,
            original_payload,
            do_upload: true)
end