aliyun 0.1.1 → 0.3.0

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: cb5b5695a2a5302a32783da37632ab0d9fdd437a
4
- data.tar.gz: ce4ab67b3f779536c950852607bdbf05af36da7c
3
+ metadata.gz: 805a759b4575f23be67e18372e06c5c66d487ace
4
+ data.tar.gz: 9c14c17f044ad1f047c8bc2fa202ab3298ab5b58
5
5
  SHA512:
6
- metadata.gz: d175ce40436f6e7117960dff67a801640b304f38f412a3fc4b8fd95069d363cdab1e2f572d505208448f21d666cf3b1ecd2173728f705e75ff9de3518bb89b47
7
- data.tar.gz: 5eed2b104e6ee53cd2377a49c610ab881450709b484613f9ca3eb0a3a36765f0a984f1992aa8e254efa07ea15704905e7eb005109a18bfe3df622e17698eb18d
6
+ metadata.gz: 06eae905ebdf489de03547cef5d7636f99ab94c546822328f18c42ce43c3dfb767fde74ec43583a01185bac0abdb2b3851c6df7c704b1fc4e7ba845aa4b68124
7
+ data.tar.gz: 2bd6e052e61ccc33911e03a5603c62f687de84d6e9b98230d40c7ff9022d72c4fad3c8567f98c935ae1d7ebdad51d4bc9949c85e1e7cf376db989e5dfc05f7d3
@@ -0,0 +1,2 @@
1
+ :access_key_id:
2
+ :access_key_secret:
data/.gitignore CHANGED
@@ -20,3 +20,4 @@ tmp
20
20
  *.o
21
21
  *.a
22
22
  mkmf.log
23
+ .aliyun.yml
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Ruby wrapper of Aliyun API for system adminstrator.
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/aliyun.png)](http://badge.fury.io/rb/aliyun)
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -26,10 +28,10 @@ Example:
26
28
  options = {
27
29
  :access_key_id => "_YOUR_API_KEY_",
28
30
  :access_key_secret => "_YOUR_API_SECRET_",
29
- :endpoint_url => "https://ecs.aliyuncs.com/"
31
+ :service => :ecs
30
32
  }
31
33
 
32
- service = Aliyun::ECS::Service.new options
34
+ service = Aliyun::Service.new options
33
35
  parameters = {}
34
36
 
35
37
  puts service.DescribeRegions parameters
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
+ # RSpec and Test
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new('spec')
7
+ task :default => :spec
8
+ task :test => :spec
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.6"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
25
26
  spec.add_dependency "ruby-hmac"
26
27
  end
@@ -5,9 +5,9 @@ $DEBUG=false
5
5
 
6
6
  options = {:access_key_id => "xxxxxx",
7
7
  :access_key_secret => "yyyyy",
8
- :endpoint_url => "https://ecs.aliyuncs.com/"}
8
+ :service => :ecs}
9
9
 
10
- service = Aliyun::ECS::Service.new options
10
+ service = Aliyun::Service.new options
11
11
 
12
12
  parameters = {}
13
13
 
@@ -1,3 +1,4 @@
1
1
  require "aliyun/version"
2
2
  require "aliyun/base"
3
- require "aliyun/ecs"
3
+ require "aliyun/services"
4
+ require "aliyun/common"
@@ -1,18 +1,21 @@
1
1
  module Aliyun
2
- ALIYUN_API_ENDPOINT='https://ecs.aliyuncs.com/'
3
- SEPARATOR = "&"
4
- HTTP_METHOD = "GET"
5
-
6
- $ENDPOINT_URL = nil
7
- $ACCESS_KEY_ID = nil
8
- $ACCESS_KEY_SECRET = nil
9
-
10
- DEFAULT_PARAMETERS = {
11
- :Format=>"JSON",
12
- :Version=>"2014-05-26",
13
- :SignatureMethod=>"HMAC-SHA1",
14
- :SignatureVersion=>"1.0"
15
- }
2
+ class APIConfig
3
+ def self.info
4
+ raise "Service Name Missing!"
5
+ end
6
+ def self.endpoint
7
+ raise "Service Endpoint Missing!"
8
+ end
9
+ def self.default_parameters
10
+ raise "Service Default Parameters Missing!"
11
+ end
12
+ def self.separator
13
+ "&"
14
+ end
15
+ def self.http_method
16
+ "GET"
17
+ end
18
+ end
16
19
 
17
20
  class AliyunAPIException < RuntimeError
18
21
  end
@@ -0,0 +1,124 @@
1
+ require 'net/http'
2
+ require 'time'
3
+ require 'securerandom'
4
+ require 'uri'
5
+ require 'base64'
6
+ require 'hmac-sha1'
7
+ require 'json'
8
+
9
+ module Aliyun
10
+ class Service
11
+ attr_accessor :access_key_id, :access_key_secret
12
+ attr_accessor :options
13
+ attr_accessor :endpoint_url
14
+ attr_accessor :service
15
+
16
+ def initialize(options={})
17
+ options[:service] ||= :ecs
18
+ self.service = SERVICES[options[:service].to_sym]
19
+ self.access_key_id = options[:access_key_id]
20
+ self.access_key_secret = options[:access_key_secret]
21
+ self.endpoint_url = options[:endpoint_url] || self.service.endpoint
22
+ self.options = {:AccessKeyId => self.access_key_id}
23
+ end
24
+
25
+ def method_missing(method, *args)
26
+ if $DEBUG
27
+ puts "Not Found Method: #{method}"
28
+ end
29
+
30
+ if args[0].nil?
31
+ raise AliyunAPIException.new "Method missing: #{method}!"
32
+ end
33
+
34
+ call_aliyun_with_parameter(method, args[0])
35
+ end
36
+
37
+ #Dispatch the request with parameter
38
+ private
39
+ def call_aliyun_with_parameter(method, params)
40
+ params = gen_request_parameters method, params
41
+ uri = URI(endpoint_url)
42
+
43
+ uri.query = URI.encode_www_form(params)
44
+
45
+ http = Net::HTTP.new(uri.host, uri.port)
46
+ http.use_ssl = (uri.scheme == "https")
47
+
48
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
49
+ if $DEBUG
50
+ puts "Request URI: #{uri.request_uri}"
51
+ end
52
+
53
+ request = Net::HTTP::Get.new(uri.request_uri)
54
+ response = http.request(request)
55
+
56
+ case response
57
+ when Net::HTTPSuccess
58
+ return JSON.parse(response.body)
59
+ else
60
+ raise AliyunAPIException.new "Response code: #{response.code}, message: #{response.body}"
61
+ end
62
+ end
63
+
64
+ #generate the parameters
65
+ def gen_request_parameters method, params
66
+ #add common parameters
67
+ params.merge! self.service.default_parameters
68
+
69
+ params.merge! self.options
70
+
71
+ params[:Action] = method.to_s
72
+ params[:TimeStamp] = Time.now.utc.iso8601
73
+ params[:SignatureNonce] = SecureRandom.uuid
74
+ params[:Signature] = compute_signature params
75
+
76
+ params
77
+ end
78
+
79
+ #compute the signature of the parameters String
80
+ def compute_signature params
81
+ if $DEBUG
82
+ puts "keys before sorted: #{params.keys}"
83
+ end
84
+
85
+ sorted_keys = params.keys.sort
86
+
87
+ if $DEBUG
88
+ puts "keys after sorted: #{sorted_keys}"
89
+ end
90
+
91
+ canonicalized_query_string = ""
92
+
93
+ canonicalized_query_string = sorted_keys.map {|key|
94
+ "%s=%s" % [safe_encode(key.to_s), safe_encode(params[key])]
95
+ }.join(self.service.separator)
96
+
97
+ length = canonicalized_query_string.length
98
+
99
+ string_to_sign = self.service.http_method + self.service.separator + safe_encode('/') + self.service.separator + safe_encode(canonicalized_query_string)
100
+
101
+ if $DEBUG
102
+ puts "string_to_sign is #{string_to_sign}"
103
+ end
104
+
105
+ signature = calculate_signature access_key_secret+"&", string_to_sign
106
+ end
107
+
108
+ #calculate the signature
109
+ def calculate_signature key, string_to_sign
110
+ hmac = HMAC::SHA1.new(key)
111
+ hmac.update(string_to_sign)
112
+ signature = Base64.encode64(hmac.digest).gsub("\n", '')
113
+ if $DEBUG
114
+ puts "Signature #{signature}"
115
+ end
116
+ signature
117
+ end
118
+
119
+ #encode the value to aliyun's requirement
120
+ def safe_encode value
121
+ value = URI.encode_www_form_component(value).gsub(/\+/,'%20').gsub(/\*/,'%2A').gsub(/%7E/,'~')
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,52 @@
1
+ module Aliyun
2
+ class ECSConfig < APIConfig
3
+ def self.info
4
+ "Aliyu ECS Service"
5
+ end
6
+ def self.endpoint
7
+ 'https://ecs.aliyuncs.com/'
8
+ end
9
+ def self.default_parameters
10
+ {
11
+ :Format=>"JSON",
12
+ :Version=>"2014-05-26",
13
+ :SignatureMethod=>"HMAC-SHA1",
14
+ :SignatureVersion=>"1.0"
15
+ }
16
+ end
17
+ def self.separator
18
+ super
19
+ end
20
+ def self.http_method
21
+ super
22
+ end
23
+ end
24
+
25
+ class SLBConfig < APIConfig
26
+ def self.info
27
+ "Aliyu SLB Service"
28
+ end
29
+ def self.endpoint
30
+ 'https://slb.aliyuncs.com/'
31
+ end
32
+ def self.default_parameters
33
+ {
34
+ :Format=>"JSON",
35
+ :Version=>"2014-05-15",
36
+ :SignatureMethod=>"HMAC-SHA1",
37
+ :SignatureVersion=>"1.0"
38
+ }
39
+ end
40
+ def self.separator
41
+ super
42
+ end
43
+ def self.http_method
44
+ super
45
+ end
46
+ end
47
+
48
+ SERVICES = {
49
+ :ecs => ECSConfig,
50
+ :slb => SLBConfig
51
+ }
52
+ end
@@ -1,3 +1,3 @@
1
1
  module Aliyun
2
- VERSION = "0.1.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aliyun do
4
+ it 'has a version number' do
5
+ expect(Aliyun::VERSION).not_to be nil
6
+ end
7
+
8
+ it 'can load options' do
9
+ option = load_options
10
+
11
+ expect(option).to have_key(:access_key_id)
12
+ expect(option).to have_key(:access_key_secret)
13
+ end
14
+
15
+ it 'can create aliyun ecs service' do
16
+ options = load_options
17
+ options[:service] = :ecs
18
+ service = Aliyun::Service.new options
19
+
20
+ expect(service).to be_instance_of(Aliyun::Service)
21
+ expect(service.service).to be(Aliyun::ECSConfig)
22
+ end
23
+
24
+ it 'can create aliyun slb service' do
25
+ options = load_options
26
+ options[:service] = :slb
27
+ service = Aliyun::Service.new options
28
+
29
+ expect(service).to be_instance_of(Aliyun::Service)
30
+ expect(service.service).to be(Aliyun::SLBConfig)
31
+ end
32
+
33
+ it 'can query aliyun ecs regions' do
34
+ options = load_options
35
+ options[:service] = :ecs
36
+ service = Aliyun::Service.new options
37
+ parameters = {}
38
+ regions = service.DescribeRegions parameters
39
+
40
+ expect(regions).to have_key("Regions")
41
+ expect(regions["Regions"]).to have_key("Region")
42
+ end
43
+
44
+ it 'can query aliyun slb regions' do
45
+ options = load_options
46
+ options[:service] = :slb
47
+ service = Aliyun::Service.new options
48
+ parameters = {}
49
+ regions = service.DescribeRegions parameters
50
+
51
+ expect(regions).to have_key("Regions")
52
+ expect(regions["Regions"]).to have_key("Region")
53
+ end
54
+
55
+ it "can describe aliyun slb regions" do
56
+ options = load_options
57
+ options[:service] = :slb
58
+ service = Aliyun::Service.new options
59
+ parameters = {}
60
+ slbs = service.DescribeLoadBalancers :RegionId => "cn-beijing"
61
+
62
+ expect(slbs).to have_key("LoadBalancers")
63
+ #expect(slbs["LoadBalancers"]).to have_key("LoadBalancer")
64
+ end
65
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'aliyun'
3
+ require 'yaml'
4
+
5
+ def load_options(src='.aliyun.yml')
6
+ YAML.load_file(src)
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Lantao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-13 00:00:00.000000000 Z
11
+ date: 2015-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: ruby-hmac
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +74,7 @@ executables: []
60
74
  extensions: []
61
75
  extra_rdoc_files: []
62
76
  files:
77
+ - ".aliyun.yml.example"
63
78
  - ".gitignore"
64
79
  - Gemfile
65
80
  - LICENSE.txt
@@ -69,8 +84,11 @@ files:
69
84
  - examples/describe_regions.rb
70
85
  - lib/aliyun.rb
71
86
  - lib/aliyun/base.rb
72
- - lib/aliyun/ecs.rb
87
+ - lib/aliyun/common.rb
88
+ - lib/aliyun/services.rb
73
89
  - lib/aliyun/version.rb
90
+ - spec/aliyun_spec.rb
91
+ - spec/spec_helper.rb
74
92
  homepage: https://github.com/Lax/aliyun
75
93
  licenses:
76
94
  - MIT
@@ -95,4 +113,6 @@ rubygems_version: 2.2.2
95
113
  signing_key:
96
114
  specification_version: 4
97
115
  summary: Ruby wrapper of Aliyun API for system administrator
98
- test_files: []
116
+ test_files:
117
+ - spec/aliyun_spec.rb
118
+ - spec/spec_helper.rb
@@ -1,123 +0,0 @@
1
- require 'net/http'
2
- require 'time'
3
- require 'securerandom'
4
- require 'uri'
5
- require 'base64'
6
- require 'hmac-sha1'
7
- require 'json'
8
-
9
- module Aliyun
10
- module ECS
11
- class Service
12
- attr_accessor :access_key_id, :access_key_secret
13
- attr_accessor :options
14
- attr_accessor :endpoint_url
15
-
16
- def initialize(options={})
17
- self.access_key_id = options[:access_key_id] || $ACCESS_KEY_ID || ""
18
- self.access_key_secret = options[:access_key_secret] || $ACCESS_KEY_SECRET || ""
19
- self.endpoint_url = options[:endpoint_url] || $ENDPOINT_URL || ALIYUN_API_ENDPOINT
20
- self.options = {:AccessKeyId => self.access_key_id}
21
- end
22
-
23
- def method_missing(method, *args)
24
- if $DEBUG
25
- puts "Not Found Method: #{method}"
26
- end
27
-
28
- if args[0].nil?
29
- raise AliyunAPIException.new "Method missing: #{method}!"
30
- end
31
-
32
- call_aliyun_with_parameter(method, args[0])
33
- end
34
-
35
- #Dispatch the request with parameter
36
- private
37
- def call_aliyun_with_parameter(method, params)
38
- params = gen_request_parameters method, params
39
- uri = URI(endpoint_url)
40
-
41
- uri.query = URI.encode_www_form(params)
42
-
43
- http = Net::HTTP.new(uri.host, uri.port)
44
- http.use_ssl = (uri.scheme == "https")
45
-
46
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
47
- if $DEBUG
48
- puts "Request URI: #{uri.request_uri}"
49
- end
50
-
51
- request = Net::HTTP::Get.new(uri.request_uri)
52
- response = http.request(request)
53
-
54
- case response
55
- when Net::HTTPSuccess
56
- return JSON.parse(response.body)
57
- else
58
- raise AliyunAPIException.new "Response code: #{response.code}, message: #{response.body}"
59
- end
60
- end
61
-
62
- #generate the parameters
63
- def gen_request_parameters method, params
64
- #add common parameters
65
- params.merge! DEFAULT_PARAMETERS
66
-
67
- params.merge! self.options
68
-
69
- params[:Action] = method.to_s
70
- params[:TimeStamp] = Time.now.utc.iso8601
71
- params[:SignatureNonce] = SecureRandom.uuid
72
- params[:Signature] = compute_signature params
73
-
74
- params
75
- end
76
-
77
- #compute the signature of the parameters String
78
- def compute_signature params
79
- if $DEBUG
80
- puts "keys before sorted: #{params.keys}"
81
- end
82
-
83
- sorted_keys = params.keys.sort
84
-
85
- if $DEBUG
86
- puts "keys after sorted: #{sorted_keys}"
87
- end
88
-
89
- canonicalized_query_string = ""
90
-
91
- canonicalized_query_string = sorted_keys.map {|key|
92
- "%s=%s" % [safe_encode(key.to_s), safe_encode(params[key])]
93
- }.join(SEPARATOR)
94
-
95
- length = canonicalized_query_string.length
96
-
97
- string_to_sign = HTTP_METHOD + SEPARATOR + safe_encode('/') + SEPARATOR + safe_encode(canonicalized_query_string)
98
-
99
- if $DEBUG
100
- puts "string_to_sign is #{string_to_sign}"
101
- end
102
-
103
- signature = calculate_signature access_key_secret+"&", string_to_sign
104
- end
105
-
106
- #calculate the signature
107
- def calculate_signature key, string_to_sign
108
- hmac = HMAC::SHA1.new(key)
109
- hmac.update(string_to_sign)
110
- signature = Base64.encode64(hmac.digest).gsub("\n", '')
111
- if $DEBUG
112
- puts "Signature #{signature}"
113
- end
114
- signature
115
- end
116
-
117
- #encode the value to aliyun's requirement
118
- def safe_encode value
119
- value = URI.encode_www_form_component(value).gsub(/\+/,'%20').gsub(/\*/,'%2A').gsub(/%7E/,'~')
120
- end
121
- end
122
- end
123
- end