Commit b0ed40d9 by 陈雨佳

generate resource

parent d94d1b10
# 阿里云日志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
:access_key_id,
:access_key_secret,
:project_name,
:log_store_name,
:debug,
to: :'AliyunLogRubySdk.config'
......@@ -43,8 +42,9 @@ module AliyunLogRubySdk
def get(path, options = {})
options[:headers] ||= {}
headers = generate_headers(headers: options[:headers],
resource: generate_resource(path, options[:query]))
headers = generate_headers(path: path,
query: options[:query],
headers: options[:headers])
response = self.class.get(path, query: options[:query], headers: headers)
......@@ -74,8 +74,8 @@ module AliyunLogRubySdk
end
headers = generate_headers(verb: 'POST',
path: path,
headers: options[:headers],
resource: generate_resource(path),
body: body)
response = self.class.post(path, headers: headers, body: body)
......@@ -107,8 +107,8 @@ module AliyunLogRubySdk
body = body.to_json if body.present?
headers = generate_headers(verb: 'DELETE',
path: path,
headers: options[:headers],
resource: generate_resource(path),
body: body)
response = self.class.delete(path, headers: headers, body: body)
......@@ -124,14 +124,13 @@ module AliyunLogRubySdk
"http://#{project_name}.#{endpoint}"
end
def generate_resource(path, query = {})
# 这里有一个很奇葩的地方:如果参数的值中存在 =,to_param 方法会将其转变成 %3D,但是阿里云日志签名时是用 = 做签名,因此这里替换回来
query.present? ? "#{path}?#{query.to_param.gsub('%3D', '=')}" : path
end
def debug?
debug
end
def log_store_name(options = {})
options[:log_store_name] || AliyunLogRubySdk.config.log_store_name
end
end
end
......@@ -3,11 +3,11 @@
module AliyunLogRubySdk
module ConsumerGroup
def list_consumer_group(options = {})
get("/logstores/#{options[:log_store_name]}/consumergroups", options)
get("/logstores/#{log_store_name(options)}/consumergroups", options)
end
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
......@@ -29,7 +29,7 @@ module AliyunLogRubySdk
headers['x-log-bodyrawsize'] = '0'
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']
......@@ -38,22 +38,24 @@ module AliyunLogRubySdk
private
def generate_authorization(verb, headers, resource)
"LOG #{access_key_id}:#{generate_signature(verb, headers, resource)}"
def generate_authorization(options)
"LOG #{access_key_id}:#{generate_signature(options)}"
end
def generate_signature(verb, headers, resource)
Base64.encode64(OpenSSL::HMAC.digest('sha1', access_key_secret, generate_sign_string(verb, headers, resource).encode('utf-8'))).rstrip
def generate_signature(options)
Base64.encode64(OpenSSL::HMAC.digest('sha1', access_key_secret, generate_sign_string(options).encode('utf-8'))).rstrip
end
def generate_sign_string(verb, headers, resource)
def generate_sign_string(options)
headers = options[:headers]
%W[
#{verb}
#{options[:verb] || 'GET'}
#{headers['Content-MD5']}
#{headers['Content-Type']}
#{headers['Date']}
#{canonicalized_log_headers(headers)}
#{resource}
#{canonicalized_log_resource(options[:path], options[:query])}
].join("\n")
end
......@@ -66,5 +68,10 @@ module AliyunLogRubySdk
"#{key.downcase}:#{value}"
end.sort.join("\n")
end
def canonicalized_log_resource(path, query = {})
# 这里有一个很奇葩的地方:如果参数的值中存在 =,to_param 方法会将其转变成 %3D,但是阿里云日志签名时是用 = 做签名,因此这里替换回来
query.present? ? "#{path}?#{query.to_param.gsub('%3D', '=')}" : path
end
end
end
......@@ -3,7 +3,7 @@
module AliyunLogRubySdk
module Index
def get_index(options = {})
get("/logstores/#{options[:log_store_name]}/index", options)
get("/logstores/#{log_store_name(options)}/index", options)
end
end
end
......@@ -19,7 +19,7 @@ module AliyunLogRubySdk
end
def get_log_store(options = {})
get("/logstores/#{log_store_name}", options)
get("/logstores/#{log_store_name(options)}", options)
end
def update_log_store(options = {})
......@@ -30,14 +30,14 @@ module AliyunLogRubySdk
options[:query] ||= {}
options[:query][:type] = 'log'
get("/logstores/#{log_store_name}", options)
get("/logstores/#{log_store_name(options)}", options)
end
def get_histograms(options = {})
options[:query] ||= {}
options[:query][:type] = 'histogram'
get("/logstores/#{log_store_name}", options)
get("/logstores/#{log_store_name(options)}", options)
end
end
end
......@@ -3,14 +3,14 @@
module AliyunLogRubySdk
module Shard
def list_shards(options = {})
get("/logstores/#{log_store_name}/shards", options)
get("/logstores/#{log_store_name(options)}/shards", options)
end
def get_cursor(options = {})
options[:query] ||= {}
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
def put_logs(options = {})
......@@ -26,7 +26,7 @@ module AliyunLogRubySdk
options[:body] = Zlib::Deflate.deflate(body)
post("/logstores/#{log_store_name}/shards/lb", options)
post("/logstores/#{log_store_name(options)}/shards/lb", options)
end
def pull_logs(options = {})
......@@ -39,7 +39,7 @@ module AliyunLogRubySdk
options[:query] ||= {}
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
private
......
......@@ -3,7 +3,7 @@
module AliyunLogRubySdk
module Shipper
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
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