magellan-cli 0.2.7 → 0.2.8

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: 73d1f40fdfc7506bd3896f983666f347ee1dbb53
4
- data.tar.gz: 7bdf741e09e11d6daf57dd229faf3e12bd731956
3
+ metadata.gz: f84015af091e8e39e5a63466e4518e74ad3d9b65
4
+ data.tar.gz: ceae355f009d372955e052d0035a6ebbccc90a6b
5
5
  SHA512:
6
- metadata.gz: cc40cac7fead1cc17822544c2a138f68837d760868b69e326d9cf90140e834d3556630354bd4658f99bf3ffd168841941c5aa4c0b8c3e1e92d5c999f6db61e9e
7
- data.tar.gz: e90258063827ea1c377d56f85a8d574e3f60a6016ec9aa7f9b054e4c061ef4443dc4b0ba5d763e6ee92e27cc5fa1cf4791071f22e8d8de61b9b593406c6be0d7
6
+ metadata.gz: 3fef2a90a5b822b287ef8a8341b27ccd833602ca15105a656e747e09eafa83b2a21dddb5f3f316488e164cf6e404bc6f8030aac26cf2ab29af28a0cf502813ea
7
+ data.tar.gz: 094d538e70201384bd2af1c20c4f5895431631e001484c11d82de88936dc28c2380c9c6db9cbfd1e055db191042f579ef9c938a1ddcc4ef4b792925bd49cd650
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- magellan-cli (0.2.7)
4
+ magellan-cli (0.2.8)
5
5
  activesupport (~> 4.1.4)
6
6
  httpclient
7
7
  nokogiri
@@ -6,7 +6,6 @@ require 'active_support/core_ext/string/inflections'
6
6
  module Magellan
7
7
  module Cli
8
8
  class Command < Base
9
- include Magellan::Cli::FileAccess
10
9
 
11
10
  {
12
11
  "organization" => "Organization",
@@ -34,8 +33,8 @@ module Magellan
34
33
  print "password: "
35
34
  password = STDIN.noecho(&:gets).chomp
36
35
  puts ""
37
- update_selections("login" => {"email" => email, "password" => password})
38
- Magellan::Cli::Http.new.login
36
+
37
+ Magellan::Cli::Http.new.login!(email, password)
39
38
  end
40
39
  end
41
40
  end
@@ -7,9 +7,18 @@ module Magellan
7
7
 
8
8
  no_commands do
9
9
 
10
- def login
10
+ def login!(email, password)
11
+ cli = Cli::Login.new
12
+ logined = cli.api_login!(email, password)
13
+ if logined
14
+ success("OK")
15
+ else
16
+ error("Login Failure.")
17
+ end
18
+ end
19
+
20
+ def access_api
11
21
  cli = Cli::Login.new
12
- cli.login!
13
22
  return block_given? ? yield(cli) : success("OK")
14
23
  end
15
24
 
@@ -33,9 +42,10 @@ module Magellan
33
42
  # @param [String] rel_path cli.base_url からの相対パス
34
43
  # @param [Hash] params クエリ文字列
35
44
  # @return [Object] レスポンスのBODYをJSONとしてパースした結果オブジェクト
36
- def get_json(rel_path, params = nil)
37
- login do |cli|
38
- url = "#{cli.base_url}#{rel_path}"
45
+ def get_json(rel_path, params = {})
46
+ access_api do |api|
47
+ url = "#{api.base_url}#{rel_path}"
48
+ params = api.login_auth.merge(params)
39
49
  if params && !params.empty?
40
50
  url << '?' << params.map{|k,v| "%s=%s" % [CGI.escape(k.to_s), CGI.escape(v.to_s)] }.join("&")
41
51
  end
@@ -43,7 +53,7 @@ module Magellan
43
53
  # "Unknown key: max-age = 0" というメッセージを表示させないために$stderrを一時的に上書き
44
54
  $stderr, bak = StringIO.new, $stderr
45
55
  begin
46
- res = cli.httpclient.get(url)
56
+ res = api.httpclient.get(url)
47
57
  check_response(res)
48
58
  ensure
49
59
  $stderr = bak
@@ -56,9 +66,9 @@ module Magellan
56
66
  # @param [Hash] params POSTで渡されるパラメータ
57
67
  # @return nil
58
68
  def post(rel_path, params)
59
- login do |cli|
60
- params = {authenticity_token: cli.auth_token}.update(params || {})
61
- process_res(cli, :post, rel_path, params)
69
+ access_api do |api|
70
+ params = api.login_auth.update(params || {})
71
+ process_res(api, :post, rel_path, params)
62
72
  end
63
73
  end
64
74
 
@@ -67,9 +77,9 @@ module Magellan
67
77
  # @param [Hash] params POSTで渡されるパラメータ
68
78
  # @return nil
69
79
  def post_json(rel_path, params)
70
- login do |cli|
71
- params = {authenticity_token: cli.auth_token}.update(params || {})
72
- process_res(cli, :post, rel_path, params.to_json, JSON_HEADER)
80
+ access_api do |api|
81
+ params = api.login_auth.update(params || {})
82
+ process_res(api, :post, rel_path, params.to_json, JSON_HEADER)
73
83
  end
74
84
  end
75
85
 
@@ -78,9 +88,9 @@ module Magellan
78
88
  # @param [Hash] params PUTで渡されるパラメータ
79
89
  # @return nil
80
90
  def put(rel_path, params)
81
- login do |cli|
82
- params = {authenticity_token: cli.auth_token}.update(params || {})
83
- process_res(cli, :put, rel_path, params)
91
+ access_api do |api|
92
+ params = cli.login_auth.update(params || {})
93
+ process_res(api, :put, rel_path, params)
84
94
  end
85
95
  end
86
96
 
@@ -89,9 +99,9 @@ module Magellan
89
99
  # @param [Hash] params PUTで渡されるパラメータ
90
100
  # @return nil
91
101
  def put_json(rel_path, params)
92
- login do |cli|
93
- params = {authenticity_token: cli.auth_token}.update(params || {})
94
- process_res(cli, :put, rel_path, params.to_json, JSON_HEADER)
102
+ access_api do |api|
103
+ params = api.login_auth.update(params || {})
104
+ process_res(api, :put, rel_path, params.to_json, JSON_HEADER)
95
105
  end
96
106
  end
97
107
 
@@ -99,16 +109,16 @@ module Magellan
99
109
  # @param [String] rel_path cli.base_url からの相対パス
100
110
  # @return nil
101
111
  def delete(rel_path)
102
- login do |cli|
103
- params = {authenticity_token: cli.auth_token}
104
- process_res(cli, :delete, rel_path, params)
112
+ access_api do |api|
113
+ params = api.login_auth
114
+ process_res(api, :delete, rel_path, params)
105
115
  end
106
116
  end
107
117
 
108
- def process_res(cli, http_method, rel_path, *args)
109
- url = "#{cli.base_url}#{rel_path}"
118
+ def process_res(api, http_method, rel_path, *args)
119
+ url = "#{api.base_url}#{rel_path}"
110
120
  verbose("%s %s\n%s" % [http_method, url, args])
111
- res = cli.httpclient.send(http_method, url, *args)
121
+ res = api.httpclient.send(http_method, url, *args)
112
122
  check_response(res)
113
123
  end
114
124
 
@@ -23,6 +23,7 @@ module Magellan
23
23
  attr_reader :base_url
24
24
 
25
25
  attr_reader :auth_token
26
+ attr_reader :login_auth
26
27
 
27
28
  # Magellan::Cli::Loginのコンストラクタです。
28
29
  #
@@ -45,6 +46,7 @@ module Magellan
45
46
 
46
47
  @httpclient = HTTPClient.new
47
48
  @httpclient.ssl_config.verify_mode = nil # 自己署名の証明書をOKにする
49
+ @login_auth = load_selection("login")
48
50
  end
49
51
 
50
52
  def login_form_url
@@ -54,46 +56,30 @@ module Magellan
54
56
  @login_url ||= base_url + "/api/sign_in.json"
55
57
  end
56
58
 
57
- # magellan-apiサーバに接続してログインの検証と処理を行います。
59
+ # magellan-apiサーバに接続してログインの検証とアクセストークンの保存を行います。
58
60
  #
59
- # @param [Hash] extra オプション
60
- # @return [Integer] サーバが返したレスポンスのステータスを返します
61
- # @return [String] サーバが返したレスポンスのbodyを返します
62
- def login_and_status
61
+ # @return [boolean] login成功/失敗
62
+ def api_login!(email, password)
63
63
  @auth_token ||= get_auth_token
64
64
  params = {
65
- "user" => load_selection("login"),
65
+ "user" => {
66
+ "email" => email,
67
+ "password" => password
68
+ },
66
69
  "authenticity_token" => @auth_token
67
70
  }.to_json
68
71
  res2 = Ssl.retry_on_ssl_error("login"){ @httpclient.post(login_url, params, JSON_HEADER) }
69
- return res2.status, res2.body
70
- end
71
72
 
72
- # GSSサーバに接続してログインの検証と処理を行います。
73
- #
74
- # @param [Hash] extra オプション
75
- # @option extra [Integer] :device_type デバイス種別
76
- # @option extra [Integer] :device_id デバイス識別子
77
- # @return [Boolean] ログインに成功した場合はtrue、失敗した場合はfalse
78
- def login
79
- status, _ = login_and_status
80
- case status
81
- when 200...300 then true
82
- else false
73
+ case res2.status
74
+ when 200...300 then logined = true
75
+ else logined = false
83
76
  end
84
- end
85
77
 
86
- # GSSサーバに接続してログインの検証と処理を行います。
87
- #
88
- # @param [Hash] extra オプション
89
- # @see #login
90
- # @return ログインに成功した場合は自身のオブジェクト返します。失敗した場合はLibgss::Network::Errorがraiseされます。
91
- def login!
92
- status, body = login_and_status
93
- case status
94
- when 200...300 then return self
95
- else raise LoginError, "status: #{status} #{body}"
78
+ if logined
79
+ body = JSON.parse res2.body
80
+ update_selections("login" => {"email" => email, "token" => body["token"] })
96
81
  end
82
+ logined
97
83
  end
98
84
 
99
85
  def get_auth_token
@@ -42,6 +42,7 @@ module Magellan
42
42
  q[f] = r["id"]
43
43
  end
44
44
  return build_query(q)
45
+
45
46
  end
46
47
 
47
48
  DEFAULT_SELECTION_FIELDS = %w[id name].freeze
@@ -64,7 +65,7 @@ module Magellan
64
65
  next unless i
65
66
  fields[i] = obj[:name]
66
67
  res = obj[:resource] || ((k = Resources.const_get(obj[:class])) ? k.resource_name : nil)
67
- res2 = get_json("/admin/#{res}.json?compact=true")
68
+ res2 = get_json("/admin/#{res}.json", {"compact" => true})
68
69
  associations[f] = res2.each_with_object({}){|r,d| d[ r["id"].to_i ] = r["label"] }
69
70
  end
70
71
  return associations
@@ -49,6 +49,14 @@ module Magellan
49
49
  put_json("/admin/#{resource_name}/#{w["id"]}/edit.js", params)
50
50
  end
51
51
 
52
+ desc "prepare_images", "prepare worker images"
53
+ def prepare_images
54
+ s = load_selection("functions_worker")
55
+ id = s["id"]
56
+ r = post_json("/admin/functions~worker/#{id}/simple_method_call.json", {method_name: "prepare_images"})
57
+ Image.new.show_list(r["result"])
58
+ end
59
+
52
60
  end
53
61
 
54
62
  end
@@ -1,5 +1,5 @@
1
1
  module Magellan
2
2
  module Cli
3
- VERSION = "0.2.7"
3
+ VERSION = "0.2.8"
4
4
  end
5
5
  end
@@ -36,7 +36,7 @@ describe Magellan::Cli::Login do
36
36
  allow(res).to receive(:status).and_return(200)
37
37
  allow(res).to receive(:body).and_return({"success" => true}.to_json)
38
38
  allow(res).to receive(:body_encoding).and_return(Encoding.find("UTF-8"))
39
- expect(cli.login!).to eq cli # 自身を返す
39
+ expect(cli.api_login!(email, password)).to eq true
40
40
  end
41
41
 
42
42
  it :login_failure do
@@ -45,7 +45,7 @@ describe Magellan::Cli::Login do
45
45
  allow(res).to receive(:status).and_return(401) # Unauthorized
46
46
  allow(res).to receive(:body).and_return({"success" => false, "message" => "Error with your login or password"}.to_json)
47
47
  allow(res).to receive(:body_encoding).and_return(Encoding.find("UTF-8"))
48
- expect{cli.login!}.to raise_error(Magellan::Cli::LoginError, 'status: 401 {"success":false,"message":"Error with your login or password"}')
48
+ expect(cli.api_login!(email, password)).to eq false
49
49
  end
50
50
  end
51
51
 
@@ -72,7 +72,7 @@ describe Magellan::Cli::Login do
72
72
  allow(res).to receive(:status).and_return(200)
73
73
  allow(res).to receive(:body).and_return({"success" => true}.to_json)
74
74
  allow(res).to receive(:body_encoding).and_return(Encoding.find("UTF-8"))
75
- expect(cli.login!).to eq cli # 自身を返す
75
+ expect(cli.api_login!(email, password)).to eq true
76
76
  end
77
77
  end
78
78
  end
@@ -6,22 +6,20 @@ describe Magellan::Cli::Resources::Project do
6
6
  let(:base_url){ "https://localhost:3000" }
7
7
  let(:httpclient){ double(:httpclient) }
8
8
  let(:cli) do
9
- cli = double(:login, base_url: base_url)
9
+ cli = double(:access_api, base_url: base_url, auth_token: {}, login_auth: {})
10
10
  allow(cli).to receive(:httpclient).and_return(httpclient)
11
11
  cli
12
12
  end
13
- let(:res){ double(:res) }
13
+ let(:res){ double(:res, status: 200, body: {}.to_json) }
14
14
 
15
15
  let(:cmd){ Magellan::Cli::Resources::Project.new }
16
16
 
17
17
  before do
18
- allow(cmd).to receive(:login).and_yield(cli)
18
+ allow(cmd).to receive(:access_api).and_yield(cli)
19
19
  end
20
20
 
21
21
  describe :list do
22
22
  it do
23
- allow(res).to receive(:body).and_return("[]")
24
- allow(res).to receive(:status).and_return(200)
25
23
  expect(httpclient).to receive(:get).with("#{base_url}/admin/project.json").and_return(res)
26
24
  expect($stdout).to receive(:puts)
27
25
  cmd.list
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magellan-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm2000
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-18 00:00:00.000000000 Z
11
+ date: 2014-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient