conoha 0.0.9 → 0.0.10

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: 95877d4435f34c4d78ca9d4382dd8ec52c49feb6
4
- data.tar.gz: 840162fa998780e54041dd10e59772c8afa604e3
3
+ metadata.gz: 940f0b5bd5020ab62e5aa2cfe9a67e1baed25f82
4
+ data.tar.gz: 9c943723642d672bf2c4a34bbc8d11b4ecd74f49
5
5
  SHA512:
6
- metadata.gz: 0482293873d148b329bd621d6c1482b5abf88ca9bbed26519f50503fda27cac46b6e5dfb7076e00774d69171cec09099b8a947e13244bd42e67587df19b78b43
7
- data.tar.gz: 66f015bc630b17d1becaea701b62798a452ec4b7e8995bdb18ddb7ec38fb3cb57db37bff68ef4cae20a4a9b33902ae70c9617200e94c55955813cad5f51f6287
6
+ metadata.gz: 3d4d73d75a0e9bb1d6c01e3342a0b4f45bb4fae55334968d8749728e45f867db5f6ceeac20092608c9648854f2cb13ea011e272cd908e3fb6fc7c0d545d7724d
7
+ data.tar.gz: 6c29147df0553bf4ccc9087c2f5a751b277df0a2da3e9f2c7ebe4772da2c0df92c5e31eae1f948eceea43c7b732d5461b3dc3a35d7657920cdba4c8c14dc80b7
@@ -1,7 +1,5 @@
1
- require "conoha/version"
2
-
3
- require 'net/https'
4
- require 'uri'
1
+ require 'conoha/version'
2
+ require 'conoha/util'
5
3
  require 'json'
6
4
 
7
5
  class Conoha
@@ -10,35 +8,24 @@ class Conoha
10
8
  end
11
9
 
12
10
  def self.authenticate!
13
- uri = URI.parse 'https://identity.tyo1.conoha.io/v2.0/tokens'
14
- https = Net::HTTP.new(uri.host, uri.port)
15
- https.use_ssl = true
16
- req = Net::HTTP::Post.new(uri.request_uri)
17
- req['Content-Type'] = 'application/json'
11
+ uri = 'https://identity.tyo1.conoha.io/v2.0/tokens'
18
12
  payload = {
19
- auth: {
20
- passwordCredentials: {
21
- username: @@username,
22
- password: @@password
23
- },
24
- tenantId: @@tenant_id
13
+ auth: {
14
+ passwordCredentials: {
15
+ username: @@username,
16
+ password: @@password
17
+ },
18
+ tenantId: @@tenant_id
19
+ }
25
20
  }
26
- }.to_json
27
- req.body = payload
28
- res = https.request(req)
21
+ res = https_post uri, payload, nil
29
22
  @@authtoken = JSON.parse(res.body)["access"]["token"]["id"]
30
23
  save_config!
31
24
  end
32
25
 
33
26
  def self.servers
34
- uri = URI.parse "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers"
35
- https = Net::HTTP.new(uri.host, uri.port)
36
- https.use_ssl = true
37
- req = Net::HTTP::Get.new(uri.path)
38
- req['Content-Type'] = 'application/json'
39
- req['Accept'] = 'application/json'
40
- req['X-Auth-Token'] = authtoken
41
- res = https.request(req)
27
+ uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers"
28
+ res = https_get uri, authtoken
42
29
  JSON.parse(res.body)["servers"]
43
30
  end
44
31
 
@@ -47,13 +34,7 @@ class Conoha
47
34
  end
48
35
 
49
36
  def self.create(os, ram)
50
- uri = URI.parse "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers"
51
- https = Net::HTTP.new(uri.host, uri.port)
52
- https.use_ssl = true
53
- req = Net::HTTP::Post.new(uri.request_uri)
54
- req['Content-Type'] = 'application/json'
55
- req['Accept'] = 'application/json'
56
- req['X-Auth-Token'] = authtoken
37
+ uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers"
57
38
  payload = {
58
39
  server: {
59
40
  adminPass: randstr,
@@ -65,107 +46,55 @@ class Conoha
65
46
  {name: 'gncs-ipv4-all'}
66
47
  ]
67
48
  }
68
- }.to_json
69
- req.body = payload
70
- res = https.request(req)
49
+ }
50
+ res = https_post uri, payload, authtoken
71
51
  JSON.parse(res.body)["server"]["id"]
72
52
  end
73
53
 
74
54
  def self.delete(server_id)
75
- uri = URI.parse "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}"
76
- https = Net::HTTP.new(uri.host, uri.port)
77
- https.use_ssl = true
78
- req = Net::HTTP::Delete.new(uri.request_uri)
79
- req['Content-Type'] = 'application/json'
80
- req['Accept'] = 'application/json'
81
- req['X-Auth-Token'] = authtoken
82
- res = https.request(req)
55
+ uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}"
56
+ res = https_delete uri, authtoken
83
57
  res.code == '204' ? 'OK' : 'Error'
84
58
  end
85
59
 
86
60
  def self.ip_address_of(server_id)
87
- uri = URI.parse "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}"
88
- https = Net::HTTP.new(uri.host, uri.port)
89
- https.use_ssl = true
90
- req = Net::HTTP::Get.new(uri.path)
91
- req['Content-Type'] = 'application/json'
92
- req['Accept'] = 'application/json'
93
- req['X-Auth-Token'] = authtoken
94
- res = https.request(req)
61
+ uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}"
62
+ res = https_get uri, authtoken
95
63
  JSON.parse(res.body)["server"]["addresses"].values[0].map{ |e| e["addr"] }
96
64
  end
97
65
 
98
66
  def self.boot(server_id)
99
- uri = URI.parse "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action"
100
- https = Net::HTTP.new(uri.host, uri.port)
101
- https.use_ssl = true
102
- req = Net::HTTP::Post.new(uri.request_uri)
103
- req['Content-Type'] = 'application/json'
104
- req['Accept'] = 'application/json'
105
- req['X-Auth-Token'] = authtoken
106
- req.body = {"os-start": nil}.to_json
107
- res = https.request(req)
67
+ uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action"
68
+ res = https_post uri, {"os-start": nil}, authtoken
108
69
  res.code == '202' ? 'OK' : 'Error'
109
70
  end
110
71
 
111
72
  def self.shutdown(server_id)
112
- uri = URI.parse "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action"
113
- https = Net::HTTP.new(uri.host, uri.port)
114
- https.use_ssl = true
115
- req = Net::HTTP::Post.new(uri.request_uri)
116
- req['Content-Type'] = 'application/json'
117
- req['Accept'] = 'application/json'
118
- req['X-Auth-Token'] = authtoken
119
- req.body = {"os-stop": nil}.to_json
120
- res = https.request(req)
73
+ uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action"
74
+ res = https_post uri, {"os-stop": nil}, authtoken
121
75
  res.code == '202' ? 'OK' : 'Error'
122
76
  end
123
77
 
124
78
  def self.images
125
- uri = URI.parse "https://compute.tyo1.conoha.io/v2/#{tenant_id}/images"
126
- https = Net::HTTP.new(uri.host, uri.port)
127
- https.use_ssl = true
128
- req = Net::HTTP::Get.new(uri.path)
129
- req['Content-Type'] = 'application/json'
130
- req['Accept'] = 'application/json'
131
- req['X-Auth-Token'] = authtoken
132
- res = https.request(req)
79
+ uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/images"
80
+ res = https_get uri, authtoken
133
81
  JSON.parse(res.body)["images"].map { |e| [e["name"], e["id"]] }
134
82
  end
135
83
 
136
84
  def self.create_image(server_id, name)
137
- uri = URI.parse "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action"
138
- https = Net::HTTP.new(uri.host, uri.port)
139
- https.use_ssl = true
140
- req = Net::HTTP::Post.new(uri.request_uri)
141
- req['Content-Type'] = 'application/json'
142
- req['Accept'] = 'application/json'
143
- req['X-Auth-Token'] = authtoken
144
- req.body = {"createImage": {"name": name}}.to_json
145
- res = https.request(req)
85
+ uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action"
86
+ res = https_post uri, {"createImage": {"name": name}}, authtoken
146
87
  res.code == '202' ? 'OK' : 'Error'
147
88
  end
148
89
 
149
90
  def self.delete_image(image_ref)
150
- uri = URI.parse "https://image-service.tyo1.conoha.io/v2/images/#{image_ref}"
151
- https = Net::HTTP.new(uri.host, uri.port)
152
- https.use_ssl = true
153
- req = Net::HTTP::Delete.new(uri.request_uri)
154
- req['Content-Type'] = 'application/json'
155
- req['Accept'] = 'application/json'
156
- req['X-Auth-Token'] = authtoken
157
- res = https.request(req)
91
+ uri = "https://image-service.tyo1.conoha.io/v2/images/#{image_ref}"
92
+ res = https_delete uri, authtoken
158
93
  res.code == '204' ? 'OK' : 'Error'
159
94
  end
160
95
 
161
96
  def self.create_from_image(image_ref, ram)
162
- uri = URI.parse "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers"
163
- https = Net::HTTP.new(uri.host, uri.port)
164
- https.use_ssl = true
165
- req = Net::HTTP::Post.new(uri.request_uri)
166
- req['Content-Type'] = 'application/json'
167
- req['Accept'] = 'application/json'
168
- req['X-Auth-Token'] = authtoken
97
+ uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers"
169
98
  payload = {
170
99
  server: {
171
100
  adminPass: randstr,
@@ -177,9 +106,8 @@ class Conoha
177
106
  {name: 'gncs-ipv4-all'}
178
107
  ]
179
108
  }
180
- }.to_json
181
- req.body = payload
182
- res = https.request(req)
109
+ }
110
+ res = https_post uri, payload, authtoken
183
111
  JSON.parse(res.body)["server"]["id"]
184
112
  end
185
113
 
@@ -1,3 +1,50 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+
4
+ # @return [Net::HTTPResponse]
5
+ # @params [String] uri_string URI string
6
+ # @params [String] authtoken
7
+ def https_get(uri_string, authtoken)
8
+ uri = URI.parse uri_string
9
+ https = Net::HTTP.new(uri.host, uri.port)
10
+ https.use_ssl = true
11
+ req = Net::HTTP::Get.new(uri.path)
12
+ req['Content-Type'] = 'application/json'
13
+ req['Accept'] = 'application/json'
14
+ req['X-Auth-Token'] = authtoken
15
+ https.request(req)
16
+ end
17
+
18
+ # @return [Net::HTTPResponse]
19
+ # @params [String] uri_string URI string
20
+ # @params [Hash] payload HTTP request body
21
+ # @params [String] authtoken
22
+ def https_post(uri_string, payload, authtoken)
23
+ uri = URI.parse uri_string
24
+ https = Net::HTTP.new(uri.host, uri.port)
25
+ https.use_ssl = true
26
+ req = Net::HTTP::Post.new(uri.request_uri)
27
+ req['Content-Type'] = 'application/json'
28
+ req['Accept'] = 'application/json'
29
+ req['X-Auth-Token'] = authtoken
30
+ req.body = payload.to_json
31
+ https.request(req)
32
+ end
33
+
34
+ # @return [Net::HTTPResponse]
35
+ # @params [String] uri_string URI string
36
+ # @params [String] authtoken
37
+ def https_delete(uri_string, authtoken)
38
+ uri = URI.parse uri_string
39
+ https = Net::HTTP.new(uri.host, uri.port)
40
+ https.use_ssl = true
41
+ req = Net::HTTP::Delete.new(uri.request_uri)
42
+ req['Content-Type'] = 'application/json'
43
+ req['Accept'] = 'application/json'
44
+ req['X-Auth-Token'] = authtoken
45
+ https.request(req)
46
+ end
47
+
1
48
  # @params [Array<String>]
2
49
  # The return value of `ip_address_of` method. It is either
3
50
  #
@@ -1,3 +1,3 @@
1
1
  module ConohaVersion
2
- ITSELF = "0.0.9"
2
+ ITSELF = "0.0.10"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conoha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - ka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-14 00:00:00.000000000 Z
11
+ date: 2015-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler