aliyun-api 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6ab3ca967777d01eb3761e25b07cffa95b9e2f2
4
- data.tar.gz: 43bd5e346eaf5e8d1406cf575fdd214d314dbd09
3
+ metadata.gz: 65d3ac4b3cbdf248bfb914946e63c7452e677a1b
4
+ data.tar.gz: 0f88ba22119aa5a5a9b8aa265e36804acd4ab32c
5
5
  SHA512:
6
- metadata.gz: fbc55d2940637b01152b890f547898d1c93d29a254bb8927d80bbbe1b681edcfcc3b6b7d7130f6a4a202c3a31500f88192488a7d63c3452007c7a95910d2adce
7
- data.tar.gz: 7d73a98d2be106b22d72f64a80094ac2988a1c689cf68e3388f60084362da71aa23a334c3dd94fb778d4c21208143c782c1049867a0c36b9ff70784cbcf1271f
6
+ metadata.gz: 4da9e60b500b9c696238cefd53cd8c01c91f007a04dba02a01f5865ac093ac4f1794c4fa90827eddc538b8c7e8ce6008d93a1a7b346ac8d7b05560efabd14a8a
7
+ data.tar.gz: b353b8388b9251d040ec0f3b846e925363aea0a3d8c33306ba4c7254108f0ef4eb57ab3cd3dafa88c53163f618d6c65c3b4ea709f17e92ddf278256c8036caa0
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ *.gem
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Aliyun::Api
1
+ # Aliyun
2
2
 
3
- TODO: Write a gem description
3
+ The Aliyun ECS API Client for Ruby 是调用 [阿里云 ECS服务](http://help.aliyun.com/view/11108189_13730407.html) 的 Ruby客户端类库.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,7 +20,42 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ 首先,需要在代码中引入类库:
24
+
25
+ ```
26
+ require 'aliyun'
27
+ ```
28
+
29
+ 然后利用自己阿里云账号下的access_key初始化ECS对象。如果没有access_key,可以通过[阿里云用户中心](https://i.aliyun.com/access_key/)申请access_key。
30
+
31
+ ```
32
+ options = {
33
+ :access_key_id => "xxxxxx",
34
+ :access_key_secret => "yyyyyy"
35
+ }
36
+ ecs = Aliyun::ECS.new options
37
+ ```
38
+
39
+ 这样, 你就可以根据 [阿里云弹性计算服务API参考手册](http://help.aliyun.com/view/11108189_13730407.html)初始化业务参数(除Action参数之外)为一个hash对象,并且将其作为参数传给Action方法(Action参数)。
40
+
41
+ ```
42
+ parameters = {:parameter_name => parameter_value}
43
+ ecs.action parameters
44
+ ```
45
+
46
+ (1) 例如查询可用地域列表,其Action参数为DescribeRegions,而没有其他参数,代码如下
47
+
48
+ ```
49
+ ecs.describe_regions {}
50
+ ```
51
+
52
+ (2) 再比如查询可用镜像,代码如下
53
+
54
+ ```
55
+ parameters = {:RegionId => "cn-beijing", :PageNumber => 2, :RageSize => 20}
56
+ service.describe_images parameters
57
+ ```
58
+
24
59
 
25
60
  ## Contributing
26
61
 
@@ -1,16 +1,16 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'aliyun/api/version'
4
+ require 'aliyun/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "aliyun-api"
8
- spec.version = Aliyun::Api::VERSION
8
+ spec.version = Aliyun::VERSION
9
9
  spec.authors = ["qjpcpu"]
10
10
  spec.email = ["qjpcpu@gmail.com"]
11
11
  spec.summary = %q{Aliyun ECS API}
12
12
  spec.description = %q{Used for aliyun ecs api}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/qjpcpu/aliyun-api"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_runtime_dependency 'rest-client'
23
24
  end
@@ -0,0 +1,4 @@
1
+ module Aliyun
2
+ class AliyunError < RuntimeError
3
+ end
4
+ end
data/lib/aliyun/ecs.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'time'
2
+ require 'securerandom'
3
+ require 'uri'
4
+ require 'rest_client'
5
+ require 'base64'
6
+ require 'hmac-sha1'
7
+ require 'json'
8
+
9
+
10
+ module Aliyun
11
+ class ECS
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]
16
+ end
17
+
18
+ def method_missing(method_name, *args)
19
+ super if /[A-Z]/ =~ method_name.to_s
20
+ method_name = method_name.to_s.split('_').map{|w| w.capitalize }.join('').gsub '_',''
21
+ proxy_to_aliyun(method_name, args[0])
22
+ end
23
+
24
+ private
25
+ def proxy_to_aliyun(method_name, params)
26
+ params = build_request_parameters method_name, params
27
+ begin
28
+ res = RestClient.get Aliyun.config.endpoint_url, {:params => params,:verify_ssl => OpenSSL::SSL::VERIFY_PEER }
29
+ return JSON.parse res.body if res.code == 200
30
+ rescue RestClient::Exception => rcex
31
+ raise AliyunError.new "response error code: #{rcex.response.code}\ndetails: #{rcex.response.body}\nrequest parameters: #{params}"
32
+ rescue =>e
33
+ raise AliyunError.new e.to_s
34
+ end
35
+ end
36
+
37
+ def build_request_parameters(method_name, params={})
38
+ params = Aliyun.config.request_parameters.merge(params||{})
39
+ params.merge!({
40
+ :AccessKeyId => Aliyun.config.access_key_id,
41
+ :Action => method_name.to_s,
42
+ :SignatureNonce => SecureRandom.uuid,
43
+ :TimeStamp => Time.now.utc.iso8601
44
+ })
45
+ params[:Signature] = compute_signature params
46
+ params
47
+ end
48
+
49
+ def compute_signature(params)
50
+ params = params.inject({}){|memo,(k,v)| memo[k.to_sym]=v;memo}
51
+ sorted_keys = params.keys.sort
52
+ canonicalized_query_string = ""
53
+ sorted_keys.each do |key|
54
+ canonicalized_query_string << '&'
55
+ canonicalized_query_string << percent_encode(key.to_s)
56
+ canonicalized_query_string << '='
57
+ canonicalized_query_string << percent_encode(params[key])
58
+ end
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
62
+ end
63
+
64
+ def calculate_signature key, string_to_sign
65
+ hmac = HMAC::SHA1.new(key)
66
+ hmac.update(string_to_sign)
67
+ signature = Base64.encode64(hmac.digest).gsub("\n", '')
68
+ signature
69
+ end
70
+
71
+ def percent_encode value
72
+ value = URI.encode_www_form_component(value).gsub(/\+/,'%20').gsub(/\*/,'%2A').gsub(/%7E/,'~')
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module Aliyun
2
+ VERSION = "0.0.2"
3
+ end
data/lib/aliyun.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "aliyun/version"
2
+ require "aliyun/aliyun_error"
3
+ require "aliyun/ecs"
4
+ require 'singleton'
5
+
6
+ module Aliyun
7
+ class Config
8
+ include Singleton
9
+ attr_accessor :request_parameters, :access_key_id, :access_key_secret, :endpoint_url, :request_method
10
+ def initialize
11
+ self.request_parameters={
12
+ :Format=>"JSON",
13
+ :Version=>"2014-05-26",
14
+ :SignatureMethod=>"HMAC-SHA1",
15
+ :SignatureVersion=>"1.0"
16
+ }
17
+ self.request_method = 'GET'
18
+ self.endpoint_url= 'https://ecs.aliyuncs.com/'
19
+ end
20
+ end
21
+ def self.config
22
+ Config.instance
23
+ end
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - qjpcpu
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Used for aliyun ecs api
42
56
  email:
43
57
  - qjpcpu@gmail.com
@@ -50,10 +64,12 @@ files:
50
64
  - LICENSE.txt
51
65
  - README.md
52
66
  - Rakefile
53
- - aliyun-api.gemspec
54
- - lib/aliyun/api.rb
55
- - lib/aliyun/api/version.rb
56
- homepage: ''
67
+ - aliyun_api.gemspec
68
+ - lib/aliyun.rb
69
+ - lib/aliyun/aliyun_error.rb
70
+ - lib/aliyun/ecs.rb
71
+ - lib/aliyun/version.rb
72
+ homepage: https://github.com/qjpcpu/aliyun-api
57
73
  licenses:
58
74
  - MIT
59
75
  metadata: {}
@@ -1,5 +0,0 @@
1
- module Aliyun
2
- module Api
3
- VERSION = "0.0.1"
4
- end
5
- end
data/lib/aliyun/api.rb DELETED
@@ -1,7 +0,0 @@
1
- require "aliyun/api/version"
2
-
3
- module Aliyun
4
- module Api
5
- # Your code goes here...
6
- end
7
- end