Commit b0ed40d9 by 陈雨佳

generate resource

parent d94d1b10
# 阿里云日志SDK # 阿里云日志SDK
### https://help.aliyun.com/product/28958.html?spm=a2c4g.750001.9.1.1f087b13e7djrJ ### https://help.aliyun.com/product/28958.html
### 依赖 ### 依赖
......
...@@ -32,7 +32,6 @@ module AliyunLogRubySdk ...@@ -32,7 +32,6 @@ module AliyunLogRubySdk
:access_key_id, :access_key_id,
:access_key_secret, :access_key_secret,
:project_name, :project_name,
:log_store_name,
:debug, :debug,
to: :'AliyunLogRubySdk.config' to: :'AliyunLogRubySdk.config'
...@@ -43,8 +42,9 @@ module AliyunLogRubySdk ...@@ -43,8 +42,9 @@ module AliyunLogRubySdk
def get(path, options = {}) def get(path, options = {})
options[:headers] ||= {} options[:headers] ||= {}
headers = generate_headers(headers: options[:headers], headers = generate_headers(path: path,
resource: generate_resource(path, options[:query])) query: options[:query],
headers: options[:headers])
response = self.class.get(path, query: options[:query], headers: headers) response = self.class.get(path, query: options[:query], headers: headers)
...@@ -74,8 +74,8 @@ module AliyunLogRubySdk ...@@ -74,8 +74,8 @@ module AliyunLogRubySdk
end end
headers = generate_headers(verb: 'POST', headers = generate_headers(verb: 'POST',
path: path,
headers: options[:headers], headers: options[:headers],
resource: generate_resource(path),
body: body) body: body)
response = self.class.post(path, headers: headers, body: body) response = self.class.post(path, headers: headers, body: body)
...@@ -107,8 +107,8 @@ module AliyunLogRubySdk ...@@ -107,8 +107,8 @@ module AliyunLogRubySdk
body = body.to_json if body.present? body = body.to_json if body.present?
headers = generate_headers(verb: 'DELETE', headers = generate_headers(verb: 'DELETE',
path: path,
headers: options[:headers], headers: options[:headers],
resource: generate_resource(path),
body: body) body: body)
response = self.class.delete(path, headers: headers, body: body) response = self.class.delete(path, headers: headers, body: body)
...@@ -124,14 +124,13 @@ module AliyunLogRubySdk ...@@ -124,14 +124,13 @@ module AliyunLogRubySdk
"http://#{project_name}.#{endpoint}" "http://#{project_name}.#{endpoint}"
end end
def generate_resource(path, query = {})
# 这里有一个很奇葩的地方:如果参数的值中存在 =,to_param 方法会将其转变成 %3D,但是阿里云日志签名时是用 = 做签名,因此这里替换回来
query.present? ? "#{path}?#{query.to_param.gsub('%3D', '=')}" : path
end
def debug? def debug?
debug debug
end end
def log_store_name(options = {})
options[:log_store_name] || AliyunLogRubySdk.config.log_store_name
end
end end
end end
\ No newline at end of file
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
module AliyunLogRubySdk module AliyunLogRubySdk
module ConsumerGroup module ConsumerGroup
def list_consumer_group(options = {}) def list_consumer_group(options = {})
get("/logstores/#{options[:log_store_name]}/consumergroups", options) get("/logstores/#{log_store_name(options)}/consumergroups", options)
end end
def get_check_point(options = {}) def get_check_point(options = {})
get("/logstores/#{options[:log_store_name]}/consumergroups/#{options[:consumer_group]}", options) get("/logstores/#{log_store_name(options)}/consumergroups/#{options[:consumer_group]}", options)
end end
end end
end end
\ No newline at end of file
...@@ -29,7 +29,7 @@ module AliyunLogRubySdk ...@@ -29,7 +29,7 @@ module AliyunLogRubySdk
headers['x-log-bodyrawsize'] = '0' headers['x-log-bodyrawsize'] = '0'
end end
headers['Authorization'] = generate_authorization(options[:verb] || 'GET', headers, options[:resource]) headers['Authorization'] = generate_authorization(options.merge(headers: headers))
headers['x-log-date'] = headers['Date'] headers['x-log-date'] = headers['Date']
...@@ -38,22 +38,24 @@ module AliyunLogRubySdk ...@@ -38,22 +38,24 @@ module AliyunLogRubySdk
private private
def generate_authorization(verb, headers, resource) def generate_authorization(options)
"LOG #{access_key_id}:#{generate_signature(verb, headers, resource)}" "LOG #{access_key_id}:#{generate_signature(options)}"
end end
def generate_signature(verb, headers, resource) def generate_signature(options)
Base64.encode64(OpenSSL::HMAC.digest('sha1', access_key_secret, generate_sign_string(verb, headers, resource).encode('utf-8'))).rstrip Base64.encode64(OpenSSL::HMAC.digest('sha1', access_key_secret, generate_sign_string(options).encode('utf-8'))).rstrip
end end
def generate_sign_string(verb, headers, resource) def generate_sign_string(options)
headers = options[:headers]
%W[ %W[
#{verb} #{options[:verb] || 'GET'}
#{headers['Content-MD5']} #{headers['Content-MD5']}
#{headers['Content-Type']} #{headers['Content-Type']}
#{headers['Date']} #{headers['Date']}
#{canonicalized_log_headers(headers)} #{canonicalized_log_headers(headers)}
#{resource} #{canonicalized_log_resource(options[:path], options[:query])}
].join("\n") ].join("\n")
end end
...@@ -66,5 +68,10 @@ module AliyunLogRubySdk ...@@ -66,5 +68,10 @@ module AliyunLogRubySdk
"#{key.downcase}:#{value}" "#{key.downcase}:#{value}"
end.sort.join("\n") end.sort.join("\n")
end end
def canonicalized_log_resource(path, query = {})
# 这里有一个很奇葩的地方:如果参数的值中存在 =,to_param 方法会将其转变成 %3D,但是阿里云日志签名时是用 = 做签名,因此这里替换回来
query.present? ? "#{path}?#{query.to_param.gsub('%3D', '=')}" : path
end
end end
end end
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module AliyunLogRubySdk module AliyunLogRubySdk
module Index module Index
def get_index(options = {}) def get_index(options = {})
get("/logstores/#{options[:log_store_name]}/index", options) get("/logstores/#{log_store_name(options)}/index", options)
end end
end end
end end
\ No newline at end of file
...@@ -19,7 +19,7 @@ module AliyunLogRubySdk ...@@ -19,7 +19,7 @@ module AliyunLogRubySdk
end end
def get_log_store(options = {}) def get_log_store(options = {})
get("/logstores/#{log_store_name}", options) get("/logstores/#{log_store_name(options)}", options)
end end
def update_log_store(options = {}) def update_log_store(options = {})
...@@ -30,14 +30,14 @@ module AliyunLogRubySdk ...@@ -30,14 +30,14 @@ module AliyunLogRubySdk
options[:query] ||= {} options[:query] ||= {}
options[:query][:type] = 'log' options[:query][:type] = 'log'
get("/logstores/#{log_store_name}", options) get("/logstores/#{log_store_name(options)}", options)
end end
def get_histograms(options = {}) def get_histograms(options = {})
options[:query] ||= {} options[:query] ||= {}
options[:query][:type] = 'histogram' options[:query][:type] = 'histogram'
get("/logstores/#{log_store_name}", options) get("/logstores/#{log_store_name(options)}", options)
end end
end end
end end
...@@ -3,14 +3,14 @@ ...@@ -3,14 +3,14 @@
module AliyunLogRubySdk module AliyunLogRubySdk
module Shard module Shard
def list_shards(options = {}) def list_shards(options = {})
get("/logstores/#{log_store_name}/shards", options) get("/logstores/#{log_store_name(options)}/shards", options)
end end
def get_cursor(options = {}) def get_cursor(options = {})
options[:query] ||= {} options[:query] ||= {}
options[:query][:type] = 'cursor' options[:query][:type] = 'cursor'
get("/logstores/#{log_store_name}/shards/#{options[:shard_id]}", options) get("/logstores/#{log_store_name(options)}/shards/#{options[:shard_id]}", options)
end end
def put_logs(options = {}) def put_logs(options = {})
...@@ -26,7 +26,7 @@ module AliyunLogRubySdk ...@@ -26,7 +26,7 @@ module AliyunLogRubySdk
options[:body] = Zlib::Deflate.deflate(body) options[:body] = Zlib::Deflate.deflate(body)
post("/logstores/#{log_store_name}/shards/lb", options) post("/logstores/#{log_store_name(options)}/shards/lb", options)
end end
def pull_logs(options = {}) def pull_logs(options = {})
...@@ -39,7 +39,7 @@ module AliyunLogRubySdk ...@@ -39,7 +39,7 @@ module AliyunLogRubySdk
options[:query] ||= {} options[:query] ||= {}
options[:query][:type] = 'log' options[:query][:type] = 'log'
parse(get("/logstores/#{log_store_name}/shards/#{options[:shard_id]}", options)) parse(get("/logstores/#{log_store_name(options)}/shards/#{options[:shard_id]}", options))
end end
private private
...@@ -88,4 +88,4 @@ module AliyunLogRubySdk ...@@ -88,4 +88,4 @@ module AliyunLogRubySdk
puts "response: #{group_list.inspect}" if debug? puts "response: #{group_list.inspect}" if debug?
end end
end end
end end
\ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module AliyunLogRubySdk module AliyunLogRubySdk
module Shipper module Shipper
def get_shipper_status(options = {}) def get_shipper_status(options = {})
get("/logstores/#{options[:log_store_name]}/shipper/#{options[:shipper_name]}/tasks", options) get("/logstores/#{log_store_name(options)}/shipper/#{options[:shipper_name]}/tasks", options)
end end
end end
end end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment