lmc 0.6.2 → 0.7.0

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: de2309803c8cca606f470eeb197c6e8fb45e39ae
4
- data.tar.gz: f78ff690a0a5e95524c203a33df9711f11d2a80e
3
+ metadata.gz: 1b427aa6fb475e2cae56564144444cc2e4ca76e7
4
+ data.tar.gz: 6a780cc2a84e4417fc0172eb30d2b5d4a7d73284
5
5
  SHA512:
6
- metadata.gz: 0f3ac4a105e3176947c34761e398e94d094de93bc07f4b51a153a04dfc623341b635240f657c74320890dde31084e058011cb42a54a244dae967e4ec553fd8dd
7
- data.tar.gz: aac984d524ce6d9c76ac4f75cd1b97680d96c899ddd4c08b0f3117ebdb9fc8821b5d56e7aaf5e62a945c3389dcaf12c93371beb32c1fad3655e4544e2854538d
6
+ metadata.gz: 4e3ee7fd714001efcdd496a246d694a616bd025b494816c34285a29333c3c5322a86f62c5c8b7529f2541a2f8f59e0640a14dddeb4ca62eec9e590215c3a43fc
7
+ data.tar.gz: f42450556c13a9cf95c6d5e8d0dfb9731b6050b4fec980b83b44f52d5c356533e608d4f97109c3ae1409be9f78836cc5f43c0ea43d8a45f41af447fd5f96fed4
data/.gitignore CHANGED
@@ -5,10 +5,10 @@
5
5
  /pkg/
6
6
  /spec/reports/
7
7
  /tmp/
8
+ .idea/*.iml
9
+ .idea/modules.xml
8
10
  .idea/workspace.xml
9
11
  .idea/tasks.xml
10
- .idea/misc.xml
11
- .idea/modules.xml
12
12
  .idea/.rakeTasks
13
13
  #When developing a gem, use the gemspec method in your Gemfile to avoid duplication. In general, a gem's Gemfile should contain the Rubygems source and a single gemspec line. Do not check your Gemfile.lock into version control, since it enforces precision that does not exist in the gem command, which is used to install gems in practice.
14
14
  Gemfile.lock
@@ -1,8 +1,8 @@
1
1
  <component name="ProjectRunConfigurationManager">
2
- <configuration default="false" name="tests" type="TestUnitRunConfigurationType" factoryName="Test::Unit/Shoulda/Minitest">
2
+ <configuration default="false" name="tests" type="TestUnitRunConfigurationType" factoryName="Test::Unit/Shoulda/Minitest" show_console_on_std_err="true" show_console_on_std_out="true">
3
3
  <predefined_log_file id="RUBY_TESTUNIT" enabled="true" />
4
- <module name="lmc" />
5
- <RTEST_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)" />
4
+ <module name="ruby-lmc" />
5
+ <RTEST_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="-I test" />
6
6
  <RTEST_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="" />
7
7
  <RTEST_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
8
8
  <RTEST_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
@@ -23,6 +23,6 @@
23
23
  <RTEST_RUN_CONFIG_SETTINGS_ID NAME="ZEUS" VALUE="false" />
24
24
  <RTEST_RUN_CONFIG_SETTINGS_ID NAME="SPRING" VALUE="false" />
25
25
  <RTEST_RUN_CONFIG_SETTINGS_ID NAME="RUNNER_OPTIONS" VALUE="" />
26
- <method />
26
+ <method v="2" />
27
27
  </configuration>
28
28
  </component>
@@ -0,0 +1,2 @@
1
+ Layout/IndentHash:
2
+ IndentationWidth: 4
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Lmc
1
+ # ruby-lmc
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lmc`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ This gem provides access to select lmc functionality.
6
4
 
7
5
  ## Installation
8
6
 
@@ -32,6 +30,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
30
  ### Running a single test file
33
31
  rake test TEST=test/file_with_tests.rb
34
32
 
33
+ ### Running a single test
34
+ rake test TESTOPTS="--name=test_foobar1"
35
35
  ### Tests against real LMC instances
36
36
 
37
37
  #### credentials file
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "result": {
3
- "covered_percent": 75.79
3
+ "covered_percent": 80.54
4
4
  }
5
5
  }
@@ -31,15 +31,6 @@ module LMC
31
31
  end
32
32
  end
33
33
 
34
- def self.get_children_from_root account
35
- cloud = Cloud.instance
36
- cloud.auth_for_accounts [ROOT_ACCOUNT_UUID]
37
- response = cloud.get ['cloud-service-auth', 'accounts', account.id, 'children'],
38
- {"parent.id" => account.id}
39
- response.map {|child| Account.new child}
40
- end
41
-
42
-
43
34
  def initialize(data)
44
35
  @cloud = LMC::Cloud.instance
45
36
  apply_data(data)
@@ -66,12 +57,8 @@ module LMC
66
57
  delete_action.data = {'password' => Cloud.password,
67
58
  'accountId' => @id}
68
59
  deleted = delete_action.post
69
- if deleted.code == 200
70
- @id = nil
71
- return true
72
- else
73
- raise "unable to delete account: #{deleted.body.message}"
74
- end
60
+ @id = nil
61
+ return true
75
62
  end
76
63
  end
77
64
 
@@ -86,11 +73,7 @@ module LMC
86
73
  puts ids.inspect if Cloud.debug
87
74
  principals = ids.map do |principal_id|
88
75
  response = Cloud.instance.get ["cloud-service-auth", "accounts", @id, 'members', principal_id]
89
- if response.code == 200
90
- principal = response.body
91
- else
92
- raise "ERROR: #{response.code} #{response.body.message}"
93
- end
76
+ principal = response.body
94
77
  puts principal.inspect if Cloud.debug
95
78
  principal
96
79
  end
@@ -114,6 +97,13 @@ module LMC
114
97
  @cloud.delete ["cloud-service-auth", "accounts", id, "members", "self"]
115
98
  end
116
99
 
100
+ def authority(authority_id)
101
+ response = @cloud.get(
102
+ ['cloud-service-auth', 'accounts', id, 'authorities', authority_id]
103
+ )
104
+ Authority.new(response, self)
105
+ end
106
+
117
107
  def authorities
118
108
  response = @cloud.get ['cloud-service-auth', 'accounts', id, 'authorities']
119
109
  raise 'Unable to get authorities' unless response.code == 200
@@ -124,7 +114,7 @@ module LMC
124
114
  end
125
115
 
126
116
  def children
127
- @cloud.auth_for_account self
117
+ @cloud.auth_for_accounts([self.id, ROOT_ACCOUNT_UUID])
128
118
  response = @cloud.get ['cloud-service-auth', 'accounts', id, 'children']
129
119
  response.map {|child| Account.new child}
130
120
  end
@@ -142,13 +132,13 @@ module LMC
142
132
  return [] if @type == "PRIVATE_CLOUD"
143
133
  @cloud.auth_for_accounts([id])
144
134
  response = @cloud.get ["cloud-service-devices", "accounts", id, "sites"]
145
- if response.code == 200
146
- return response.body.map {|data|
147
- Site.new(data, self)
148
- }
149
- elsif response.code == 404
150
- return []
151
- end
135
+ return response.body.map {|data|
136
+ Site.new(data, self)
137
+ }
138
+ end
139
+
140
+ def devices
141
+ Device.get_for_account self
152
142
  end
153
143
 
154
144
  def config_updatestates
@@ -1,27 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'base64'
2
4
  require 'json'
3
5
  require 'restclient'
4
6
 
5
7
  module LMC
6
8
  class Cloud
7
- #include ActionView::Helpers::DateHelper
8
-
9
9
  class << self
10
10
  attr_accessor :cloud_host, :user, :password, :verbose, :debug, :verify_tls, :use_tls
11
11
  Cloud.use_tls = true
12
12
  Cloud.verify_tls = true
13
13
  end
14
14
 
15
- # def self.cloud_host=(cloud_host)
16
- # @@cloud_host = cloud_host
17
- # end
18
-
19
15
  def self.instance(opts = {authorize: true})
20
16
  @@inst ||= self.new(@cloud_host, @user, @password, opts[:authorize])
21
17
  end
22
-
23
-
24
- attr_reader :auth_ok, :cloud_host
18
+ attr_reader :auth_ok, :cloud_host, :user, :password
25
19
 
26
20
  def initialize(cloud_host, user, pass, auth = true)
27
21
  @auth_ok = false
@@ -30,6 +24,9 @@ module LMC
30
24
  @password = pass
31
25
  @verify_tls = Cloud.verify_tls
32
26
  @last_authorized_account_ids = nil
27
+ @logger ||= ::LMC::Logger.new(STDOUT) if Cloud.debug
28
+ @logger.cloud = self if Cloud.debug
29
+ RestClient.log = @logger if Cloud.debug
33
30
  authorize if auth
34
31
  end
35
32
 
@@ -39,15 +36,15 @@ module LMC
39
36
  end
40
37
 
41
38
  def get_backstage_serviceinfos
42
- get "cloud-service-backstage/serviceinfos"
39
+ get 'cloud-service-backstage/serviceinfos'
43
40
  end
44
41
 
45
42
  def get_accounts
46
- get "cloud-service-auth/accounts"
43
+ get 'cloud-service-auth/accounts'
47
44
  end
48
45
 
49
46
  def get_accounts_objects
50
- result = get ["cloud-service-auth", "accounts"]
47
+ result = get ['cloud-service-auth', 'accounts']
51
48
  if result.code == 200
52
49
  accounts = result.map do |aj|
53
50
  Account.new(aj)
@@ -59,125 +56,69 @@ module LMC
59
56
  return accounts
60
57
  end
61
58
 
62
- # functionality should be moved to Account class
63
- #def get_account(name, type = nil)
64
- # accounts = get_accounts_objects.select do |a|
65
- # (name.nil? || a.name == name) && (type.nil? || a.type == type)
66
- # end
67
- # if accounts.length == 1
68
- # return accounts[0]
69
- # else
70
- # raise "Did not specify exactly one account"
71
- # end
72
- #end
73
-
74
59
  def invite_user_to_account(email, account_id, type, authorities = [])
75
- body = {name: email, state: "ACTIVE", type: type}
76
- body["authorities"] = authorities
77
- post ["cloud-service-auth", "accounts", account_id, 'members'], body
60
+ body = {name: email, state: 'ACTIVE', type: type}
61
+ body['authorities'] = authorities
62
+ post ['cloud-service-auth', 'accounts', account_id, 'members'], body
78
63
  end
79
64
 
80
65
  def get(path, params = nil)
81
- RestClient.log = ("stdout") if Cloud.debug
82
- begin
83
- prepared_headers = headers
84
- prepared_headers[:params] = params
85
- args = {
86
- :method => :get,
87
- :url => build_url(path),
88
- :headers => prepared_headers,
89
- }
90
- args.merge!(rest_options)
91
- resp = RestClient::Request.execute args
92
- return LMCResponse.new(resp)
93
- rescue RestClient::ExceptionWithResponse => e
94
- puts "EXCEPTION: " + e.to_s if Cloud.debug
95
- puts "EX.response: " + e.response.to_s if Cloud.debug
96
- puts JSON.parse(e.response)["message"] if Cloud.debug
97
- raise e
98
- #return LMCResponse.new(e.response)
99
- end
66
+ prepared_headers = headers
67
+ prepared_headers[:params] = params
68
+ args = {
69
+ :method => :get,
70
+ :url => build_url(path),
71
+ :headers => prepared_headers
72
+ }
73
+ execute_request args
100
74
  end
101
75
 
102
76
  def put(path, body_object)
103
- RestClient.log = ("stdout") if Cloud.debug
104
- begin
105
- args = {
106
- :method => :put,
107
- :url => build_url(path),
108
- :payload => body_object.to_json,
109
- :headers => headers
110
-
111
- }
112
- args.merge!(rest_options)
113
- resp = RestClient::Request.execute args
114
- return LMCResponse.new(resp)
115
- rescue RestClient::ExceptionWithResponse => e
116
- puts "EXCEPTION: " + e.to_s if Cloud.debug
117
- puts "EX.response: " + e.response.to_s if Cloud.debug
118
- puts JSON.parse(e.response)["message"] if Cloud.debug
119
- return LMCResponse.new(e.response)
120
- end
77
+ args = {
78
+ :method => :put,
79
+ :url => build_url(path),
80
+ :payload => body_object.to_json
81
+
82
+ }
83
+ execute_request args
121
84
  end
122
85
 
123
86
  def post(path, body_object)
124
- RestClient.log = ("stdout") if Cloud.debug
125
- begin
126
- args = {
127
- :method => :post,
128
- :url => build_url(path),
129
- :payload => body_object.to_json,
130
- :headers => headers
131
-
132
- }
133
- args.merge!(rest_options)
134
- resp = RestClient::Request.execute args
135
- return LMCResponse.new(resp)
136
- rescue RestClient::ExceptionWithResponse => e
137
- puts "EXCEPTION: " + e.to_s if Cloud.debug
138
- puts "EX.response: " + e.response.to_s if Cloud.debug
139
- puts JSON.parse(e.response)["message"] if Cloud.debug
140
- raise e
141
- end
87
+ args = {
88
+ :method => :post,
89
+ :url => build_url(path),
90
+ :payload => body_object.to_json
91
+ }
92
+ execute_request args
142
93
  end
143
94
 
144
95
  def delete(path, body_object = nil)
145
- RestClient.log = ("stdout") if Cloud.debug
146
- begin
147
- args = {
148
- :method => :delete,
149
- :url => build_url(path),
150
- :payload => body_object.to_json,
151
- :headers => headers
152
- }
153
- args.merge!(rest_options)
154
- resp = RestClient::Request.execute args
155
- return LMCResponse.new(resp)
156
- rescue RestClient::ExceptionWithResponse => e
157
- puts "EXCEPTION: " + e.to_s if Cloud.debug
158
- puts "EX.response: " + e.response.to_s if Cloud.debug
159
- puts JSON.parse(e.response)["message"] if Cloud.debug
160
- return LMCResponse.new(e.response)
161
- end
96
+ args = {
97
+ :method => :delete,
98
+ :url => build_url(path),
99
+ :payload => body_object.to_json,
100
+ :headers => headers
101
+ }
102
+ execute_request args
162
103
  end
163
104
 
164
105
  ##
165
106
  # public accessors
166
107
  ##
167
108
  def session_token
168
- @auth_token["value"]
109
+ @auth_token['value']
169
110
  end
170
111
 
171
112
  def build_url(*path_components)
172
- protocol = "https"
113
+ protocol = 'https'
173
114
  if !Cloud.use_tls
174
- protocol = "http"
115
+ protocol = 'http'
175
116
  end
176
- ["#{protocol}://#{@cloud_host}", path_components].flatten.compact.join("/")
117
+ ["#{protocol}://#{@cloud_host}", path_components].flatten.compact.join('/')
177
118
  end
178
119
 
179
120
  def auth_for_accounts(account_ids)
180
- puts "Authorizing for accounts: " + account_ids.to_s if Cloud.debug
121
+ puts 'Authorizing for accounts: ' + account_ids.to_s if Cloud.debug
181
122
  authorize(account_ids)
182
123
  end
183
124
 
@@ -195,8 +136,8 @@ module LMC
195
136
  def authorize(account_ids = [], tos = [])
196
137
  if account_ids != @last_authorized_account_ids
197
138
  begin
198
- reply = post(["cloud-service-auth", "auth"], {name: @user, password: @password, accountIds: account_ids, termsOfUse: tos})
199
- puts "authorize reply " + reply.inspect if Cloud.debug
139
+ reply = post(['cloud-service-auth', 'auth'], {name: @user, password: @password, accountIds: account_ids, termsOfUse: tos})
140
+ puts 'authorize reply ' + reply.inspect if Cloud.debug
200
141
  @last_authorized_account_ids = account_ids
201
142
  @auth_token = reply
202
143
  @auth_ok = true
@@ -210,7 +151,7 @@ module LMC
210
151
  end
211
152
 
212
153
  def auth_bearer
213
- "Bearer " + session_token
154
+ 'Bearer ' + session_token
214
155
  end
215
156
 
216
157
  def headers
@@ -224,11 +165,27 @@ module LMC
224
165
 
225
166
  def rest_options
226
167
  options = {}
227
- if !@verify_tls
228
- options[:verify_ssl] = false
168
+ options[:verify_ssl] = false unless @verify_tls
169
+ options
170
+ end
171
+
172
+ def execute_request(args)
173
+ internal_args = { headers: headers }
174
+ internal_args.merge! rest_options
175
+ internal_args.merge! args
176
+ begin
177
+ resp = RestClient::Request.execute internal_args
178
+ return LMCResponse.new(resp)
179
+ rescue RestClient::ExceptionWithResponse => e
180
+ print_exception e if Cloud.debug
181
+ raise e
229
182
  end
230
- return options
231
183
  end
232
- end
233
184
 
185
+ def print_exception(execption)
186
+ puts 'EXCEPTION: ' + execption.to_s
187
+ puts 'EX.response: ' + execption.response.to_s
188
+ puts JSON.parse(execption.response)['message']
189
+ end
190
+ end
234
191
  end
@@ -1,8 +1,8 @@
1
1
  module LMC
2
2
  class AuthAction
3
3
  include LMC::ServiceResource
4
- include LMC::JSONAble
5
4
  resource_attrs :name, :type, :data
5
+ include LMC::JSONAble
6
6
  # action types
7
7
  ACCOUNT_INVITE = 'ACCOUNT_INVITE'
8
8
  PASSWORD_RESET = 'PASSWORD_RESET'
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LMC
4
+ # Custom logger that hides the cloud password in restclient output
5
+ class Logger < ::Logger
6
+ @cloud = nil
7
+ attr_accessor :cloud
8
+ def <<(line)
9
+ super line.gsub @cloud.password, '********'
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module LMC
2
- VERSION = "0.6.2"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -0,0 +1,4 @@
1
+ require_relative 'lib/lmc.rb'
2
+ LMC::Cloud.debug=true
3
+ c = LMC::Cloud.new "cloud.lancom.de", 'foo', 'bar'
4
+ c.auth_for_accounts ['asdf']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lmc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - erpel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-19 00:00:00.000000000 Z
11
+ date: 2018-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -147,6 +147,7 @@ files:
147
147
  - ".idea/inspectionProfiles/Project_Default.xml"
148
148
  - ".idea/runConfigurations/tests.xml"
149
149
  - ".idea/vcs.xml"
150
+ - ".rubocop.yml"
150
151
  - ".ruby-version"
151
152
  - ".travis.yml"
152
153
  - Gemfile
@@ -171,12 +172,14 @@ files:
171
172
  - lib/lmc/device_config_state.rb
172
173
  - lib/lmc/entity.rb
173
174
  - lib/lmc/exceptions/lmc_outdated_terms_of_use_exception.rb
175
+ - lib/lmc/logger.rb
174
176
  - lib/lmc/membership.rb
175
177
  - lib/lmc/mixins/json_able.rb
176
178
  - lib/lmc/mixins/service_resource.rb
177
179
  - lib/lmc/principal.rb
178
180
  - lib/lmc/version.rb
179
181
  - lmc.gemspec
182
+ - misc/debug_log_experiment.rb
180
183
  homepage:
181
184
  licenses:
182
185
  - BSD-3-Clause