qingyun-api-demo 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 018e043f8ef1da203396e067db9cf40f68ba2e96
4
+ data.tar.gz: 01ab2f2c3527a19862fefc4d58409f266ec1cb63
5
+ SHA512:
6
+ metadata.gz: 5d9e80cbafffc388fb96d738bd1f042689845cf11927c82d4f39f834497147c38800c1f6fb8902776fc6677525b3a0a6f2e2625b82d88ef6e794a3cf1e61bb2c
7
+ data.tar.gz: a2bcb3c6f231bbd107be89fa1cde0a3f00e8b0254bfddf8a75c62b39dcb73624c17c6bd9b1e3399cc0fda2ce226bc93003b97535b7088aec2876cd63bd577d14
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source "https://ruby.taobao.org"
3
+
4
+ gem 'faraday'
5
+ gem 'larrow-qingcloud'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require './qingyun-api-demo'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'qingyun-api-demo'
8
+ spec.version = QingyunApiDemo.VERSION
9
+ spec.authors = ['chen188']
10
+ spec.email = ['fengzhishuiwo@163.com']
11
+ spec.summary = "qingcloud's api demo."
12
+ spec.description = 'Some use cases such as create/destroy instance, create/destroy Eip, etc.'
13
+ spec.homepage = 'https://github.com/Chen188/qingyun-api-demo.git'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'faraday', '~> 0.10.0'
22
+ spec.add_dependency 'larrow-qingcloud', '~> 0.0.2'
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ spec.add_development_dependency 'rake', '~> 0'
25
+ end
@@ -0,0 +1,88 @@
1
+ require 'larrow/qingcloud'
2
+
3
+ class QingyunApiDemo
4
+ def self.VERSION
5
+ '0.0.1'
6
+ end
7
+ def initialize(access_key, secret_key, zone_id)
8
+ Larrow::Qingcloud.establish_connection access_key, secret_key, zone_id
9
+ @connection = Larrow::Qingcloud.connection
10
+ end
11
+
12
+ # describe instance
13
+ def desc_instance(instance_id)
14
+ @connection.get('DescribeInstances', {'instances.1': instance_id})['instance_set']
15
+ end
16
+
17
+ # create instance
18
+ def c_instance(param)
19
+ @connection.get('RunInstances', param)['instances']
20
+ end
21
+
22
+ # create public ip
23
+ def c_ip(param)
24
+ @connection.get('AllocateEips', param)['eips']
25
+ end
26
+
27
+ def desc_ip(id)
28
+ @connection.get('DescribeEips', {
29
+ 'eips.1': id
30
+ })['eip_set']
31
+ end
32
+
33
+ # associate ip to instance
34
+ def ass_ip(instance_id, ip)
35
+ @connection.get 'AssociateEip', {
36
+ eip: ip,
37
+ instance: instance_id,
38
+ zone: 'sh1a'
39
+ }
40
+ end
41
+
42
+ # disassociate ip
43
+ def dis_ass_ip(ip)
44
+ @connection.get 'DissociateEips', {
45
+ 'eips.1': ip,
46
+ zone: 'sh1a',
47
+ }
48
+ end
49
+
50
+ # release ip
51
+ def release_ip(ip)
52
+ @connection.get 'ReleaseEips', {
53
+ 'eips.1': ip,
54
+ zone: 'sh1a',
55
+ }
56
+ end
57
+
58
+ # destroy intance
59
+ def des_instance(instance_id)
60
+ @connection.get 'TerminateInstances', {
61
+ 'instances.1': instance_id,
62
+ zone: 'sh1a',
63
+ }
64
+ end
65
+
66
+ # shutdown
67
+ def shut_instance(instance_id)
68
+ @connection.get 'StopInstances', {
69
+ 'instances.1': instance_id,
70
+ force: 0,
71
+ zone: 'sh1a',
72
+ }
73
+ end
74
+
75
+ def wait_ip_for_status(id, status, time_span=1)
76
+ while status != desc_ip(id)[0]['status']
77
+ p "waiting IP for #{status}. current status: #{desc_ip(id)[0]['status']}"
78
+ sleep time_span
79
+ end
80
+ end
81
+
82
+ def wait_instance_for_status(id, status, time_span=1)
83
+ while status != desc_instance(id)[0]['status']
84
+ p "waiting instance for #{status}. current status: #{desc_instance(id)[0]['status']}"
85
+ sleep time_span
86
+ end
87
+ end
88
+ end
data/test.rb ADDED
@@ -0,0 +1,55 @@
1
+ require './qingyun-api-demo'
2
+
3
+ q = QingyunApiDemo.new('', '', '')# example zone_id: sh1a, (上海1区-A)
4
+ puts q.desc_instance 'your_instance_id'
5
+
6
+ i = q.c_instance({
7
+ image_id: 'xenialx64b',
8
+ cpu: 1,
9
+ memory: 1024,
10
+ count: 1, #default 1
11
+ instance_name: 'for-test',
12
+ 'vxnets.n': 'vxnet-0', # 基础网络
13
+ zone: 'sh1a',
14
+ login_mode: 'passwd',
15
+ login_passwd: 'Chenbin123'
16
+ })[0]
17
+
18
+ ip = q.c_ip(
19
+ {
20
+ bandwidth: 1,
21
+ billing_mode: 'traffic', #bandwidth
22
+ eip_name: 'for-test-ip',
23
+ count: 1, # default 1
24
+ zone: 'sh1a',
25
+ }
26
+ )[0]
27
+
28
+ # wait instance
29
+ q.wait_instance_for_status(i, 'running')
30
+
31
+ q.wait_ip_for_status(ip, 'available')
32
+ p 'Associate IP..'
33
+ puts q.ass_ip(i, ip)
34
+ sleep 20
35
+
36
+ q.wait_ip_for_status(ip, 'associated')
37
+ p 'Disassociate IP..'
38
+ puts q.dis_ass_ip(ip)
39
+
40
+ sleep 20
41
+
42
+
43
+ p 'ShutDown...'
44
+ puts q.shut_instance(i)
45
+ q.wait_instance_for_status(i, 'stopped')
46
+
47
+ sleep 10
48
+ p 'Destroy ..'
49
+ puts q.des_instance(i)
50
+
51
+ q.wait_ip_for_status(ip, 'available')
52
+ p 'Release IP..'
53
+ puts q.release_ip(ip)
54
+
55
+ q.wait_ip_for_status(ip, 'released')
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qingyun-api-demo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - chen188
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: larrow-qingcloud
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Some use cases such as create/destroy instance, create/destroy Eip, etc.
70
+ email:
71
+ - fengzhishuiwo@163.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gemfile
77
+ - qingyun-api-demo.gemspec
78
+ - qingyun-api-demo.rb
79
+ - test.rb
80
+ homepage: https://github.com/Chen188/qingyun-api-demo.git
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: qingcloud's api demo.
104
+ test_files: []