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.



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

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/api/telemetry_api.rb', line 122

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



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
# File 'lib/api/telemetry_api.rb', line 94

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

  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}", params: original_params, method: original_method, payload: original_payload)
    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, params: original_params, method: original_method, payload: original_payload)
    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



84
85
86
87
88
89
90
91
92
# File 'lib/api/telemetry_api.rb', line 84

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'


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

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



144
145
146
# File 'lib/api/telemetry_api.rb', line 144

def get_branch_id
  throw NotImplementedError
end

#get_hash(machines) ⇒ Object

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



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

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

#get_machinesObject



140
141
142
# File 'lib/api/telemetry_api.rb', line 140

def get_machines
  throw NotImplementedError
end

#post_upload(original_params, original_payload) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/api/telemetry_api.rb', line 74

def post_upload(original_params,
                original_payload)

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