aliyun-api 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65d3ac4b3cbdf248bfb914946e63c7452e677a1b
4
- data.tar.gz: 0f88ba22119aa5a5a9b8aa265e36804acd4ab32c
3
+ metadata.gz: 1887a58979178cebc960a86c6f55725a37689746
4
+ data.tar.gz: f5a013e26a88226cfa4e1a8c41cb36f9f6038fe9
5
5
  SHA512:
6
- metadata.gz: 4da9e60b500b9c696238cefd53cd8c01c91f007a04dba02a01f5865ac093ac4f1794c4fa90827eddc538b8c7e8ce6008d93a1a7b346ac8d7b05560efabd14a8a
7
- data.tar.gz: b353b8388b9251d040ec0f3b846e925363aea0a3d8c33306ba4c7254108f0ef4eb57ab3cd3dafa88c53163f618d6c65c3b4ea709f17e92ddf278256c8036caa0
6
+ metadata.gz: 40bee8a5ed81ef666e5f7045263e13d425604a21807feeee117b75657b027d06a18afff69f7078660be52c37cdba42053ac947cbcececeec28a98dd0a226bb40
7
+ data.tar.gz: 52a9f5a0ccea503154f4ebcbb587823c9ba802cf7187d7690ae13878e4e499bea5f0c39ac6f769758195612f462232ab63c75a0611273a073a6a9d8c3c27d5c6
data/README.md CHANGED
@@ -26,7 +26,20 @@ Or install it yourself as:
26
26
  require 'aliyun'
27
27
  ```
28
28
 
29
- 然后利用自己阿里云账号下的access_key初始化ECS对象。如果没有access_key,可以通过[阿里云用户中心](https://i.aliyun.com/access_key/)申请access_key。
29
+ ### 配置aliyunaccess_key
30
+
31
+ 1. 全局配置
32
+
33
+ ```
34
+ options = {
35
+ :access_key_id => "xxxxxx",
36
+ :access_key_secret => "yyyyyy"
37
+ }
38
+ Aliyun.config options
39
+ ecs = Aliyun::ECS.new
40
+ ```
41
+
42
+ 2. 直接配置ECS客户端
30
43
 
31
44
  ```
32
45
  options = {
@@ -36,7 +49,17 @@ options = {
36
49
  ecs = Aliyun::ECS.new options
37
50
  ```
38
51
 
39
- 这样, 你就可以根据 [阿里云弹性计算服务API参考手册](http://help.aliyun.com/view/11108189_13730407.html)初始化业务参数(除Action参数之外)为一个hash对象,并且将其作为参数传给Action方法(Action参数)。
52
+ 3. 环境变量
53
+
54
+ 如果环境变量里`ACCESS_KEY_ID`和`ACCESS_KEY_SECRET`初始化了密钥,那么可以直接初始化ecs:
55
+
56
+ ```
57
+ ecs = Aliyun::ECS.new
58
+ ```
59
+
60
+ ### 调用ECS
61
+
62
+ 这样, 你就可以根据 [阿里云弹性计算服务API参考手册](http://help.aliyun.com/view/11108189_13730407.html)初始化业务参数(除Action参数之外)为一个hash对象,并且将其作为参数传给Action方法(Action参数), action方法需要将阿里云手册中的Action名按ruby方式命名, 如:阿里云手册中的Action名`StartInstance`对应到这里的方法名为`start_instance`。
40
63
 
41
64
  ```
42
65
  parameters = {:parameter_name => parameter_value}
@@ -59,7 +82,7 @@ service.describe_images parameters
59
82
 
60
83
  ## Contributing
61
84
 
62
- 1. Fork it ( https://github.com/[my-github-username]/aliyun-api/fork )
85
+ 1. Fork it ( https://github.com/qjpcpu/aliyun-api/fork )
63
86
  2. Create your feature branch (`git checkout -b my-new-feature`)
64
87
  3. Commit your changes (`git commit -am 'Add some feature'`)
65
88
  4. Push to the branch (`git push origin my-new-feature`)
data/lib/aliyun/ecs.rb CHANGED
@@ -10,9 +10,9 @@ require 'json'
10
10
  module Aliyun
11
11
  class ECS
12
12
  def initialize(options={})
13
- Aliyun.config.access_key_id ||= options[:access_key_id]
14
- Aliyun.config.access_key_secret ||= options[:access_key_secret]
15
- Aliyun.config.endpoint_url ||= options[:endpoint_url]
13
+ Aliyun[:access_key_id] = options[:access_key_id] || Aliyun[:access_key_id] || ENV['ACCESS_KEY_ID']
14
+ Aliyun[:access_key_secret] = options[:access_key_secret] || Aliyun[:access_key_secret] || ENV['ACCESS_KEY_SECRET']
15
+ Aliyun[:endpoint_url] ||= options[:endpoint_url]
16
16
  end
17
17
 
18
18
  def method_missing(method_name, *args)
@@ -25,19 +25,19 @@ module Aliyun
25
25
  def proxy_to_aliyun(method_name, params)
26
26
  params = build_request_parameters method_name, params
27
27
  begin
28
- res = RestClient.get Aliyun.config.endpoint_url, {:params => params,:verify_ssl => OpenSSL::SSL::VERIFY_PEER }
28
+ res = RestClient.send Aliyun[:request_method].downcase, Aliyun[:endpoint_url], {:params => params,:verify_ssl => OpenSSL::SSL::VERIFY_PEER }
29
29
  return JSON.parse res.body if res.code == 200
30
30
  rescue RestClient::Exception => rcex
31
- raise AliyunError.new "response error code: #{rcex.response.code}\ndetails: #{rcex.response.body}\nrequest parameters: #{params}"
31
+ raise AliyunError.new "response error code: #{rcex.response.code}\ndetails: #{rcex.response.body}\nrequest parameters: #{params.reject{|k,v| k==:AccessKeyId}}"
32
32
  rescue =>e
33
33
  raise AliyunError.new e.to_s
34
34
  end
35
35
  end
36
36
 
37
37
  def build_request_parameters(method_name, params={})
38
- params = Aliyun.config.request_parameters.merge(params||{})
38
+ params = Aliyun[:request_parameters].merge(params||{})
39
39
  params.merge!({
40
- :AccessKeyId => Aliyun.config.access_key_id,
40
+ :AccessKeyId => Aliyun[:access_key_id],
41
41
  :Action => method_name.to_s,
42
42
  :SignatureNonce => SecureRandom.uuid,
43
43
  :TimeStamp => Time.now.utc.iso8601
@@ -57,8 +57,8 @@ module Aliyun
57
57
  canonicalized_query_string << percent_encode(params[key])
58
58
  end
59
59
  length = canonicalized_query_string.length
60
- string_to_sign = Aliyun.config.request_method + '&' + percent_encode('/') + '&' + percent_encode(canonicalized_query_string[1,length])
61
- calculate_signature Aliyun.config.access_key_secret+"&", string_to_sign
60
+ string_to_sign = Aliyun[:request_method] + '&' + percent_encode('/') + '&' + percent_encode(canonicalized_query_string[1,length])
61
+ calculate_signature Aliyun[:access_key_secret]+"&", string_to_sign
62
62
  end
63
63
 
64
64
  def calculate_signature key, string_to_sign
@@ -1,3 +1,3 @@
1
1
  module Aliyun
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/aliyun.rb CHANGED
@@ -7,7 +7,7 @@ module Aliyun
7
7
  class Config
8
8
  include Singleton
9
9
  attr_accessor :request_parameters, :access_key_id, :access_key_secret, :endpoint_url, :request_method
10
- def initialize
10
+ def initialize()
11
11
  self.request_parameters={
12
12
  :Format=>"JSON",
13
13
  :Version=>"2014-05-26",
@@ -15,10 +15,21 @@ module Aliyun
15
15
  :SignatureVersion=>"1.0"
16
16
  }
17
17
  self.request_method = 'GET'
18
- self.endpoint_url= 'https://ecs.aliyuncs.com/'
18
+ self.endpoint_url = 'https://ecs.aliyuncs.com/'
19
+ self.access_key_id = ENV['ACCESS_KEY_ID']
20
+ self.access_key_secret = ENV['ACCESS_KEY_SECRET']
19
21
  end
20
22
  end
21
- def self.config
22
- Config.instance
23
+ def self.[](key)
24
+ Config.instance.send key if Config.instance.respond_to? key
25
+ end
26
+ def self.[]=(key,value)
27
+ key="#{key}="
28
+ Config.instance.send key,value if Config.instance.respond_to? key
29
+ end
30
+ def self.config(cfg)
31
+ cfg.each do |k,v|
32
+ self[k]=v
33
+ end
23
34
  end
24
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - qjpcpu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-07 00:00:00.000000000 Z
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler