lmc 0.8.0 → 0.12.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
- SHA1:
3
- metadata.gz: 31077763003e89d0d7d95ccf0882eca185921b9b
4
- data.tar.gz: 20f58644eeb3bbf19f9a20142f6603147f028a51
2
+ SHA256:
3
+ metadata.gz: ec4925e30542f46b5b9c991de79a672a9eabeffda07028620aaa6fa14e62c551
4
+ data.tar.gz: 0eb5b5654694a1eb90e920df6d00d67cf4b8dda7c42fa167760d9c329bd88341
5
5
  SHA512:
6
- metadata.gz: 7eb1f25f826afe31759b6a553acd5fbeee9aaef610a09a9e280441d21390fca1e169fc00a1aa77ba81d7567940ecb5b7de3730658ee4cc93b7262fb79be04bd6
7
- data.tar.gz: 2054e10d846866b107e70e12c6413adc863cf5a48a0b3399254db08c72e4f042b887bee326cb8d8efd3f2e5922c8fab2e372eabcf24ce644c9876548198c1035
6
+ metadata.gz: ebbb8a5bcb4eb5cdaf9f0236394f18572922a70e42f7218b9d942420e4234ff30071549fff0c37e5e6e82ffcf3cce61b8b351921915ca749f7e947a8b33e74f0
7
+ data.tar.gz: c4008e578e219956662b9b0239a543c95f921099cee0dca7553b8770cf4a986fd872b3867a506327a2c4ea522244984341e72dfb970193f1837729c71b078601
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="Encoding" addBOMForNewFiles="with NO BOM" />
4
+ </project>
@@ -1,7 +1,7 @@
1
1
  <component name="InspectionProjectProfileManager">
2
2
  <profile version="1.0">
3
3
  <option name="myName" value="Project Default" />
4
- <inspection_tool class="Rubocop" enabled="true" level="WARNING" enabled_by_default="true">
4
+ <inspection_tool class="Rubocop" enabled="false" level="WARNING" enabled_by_default="true">
5
5
  <scope name="Tests" level="ERROR" enabled="true" />
6
6
  </inspection_tool>
7
7
  <inspection_tool class="RubyInstanceMethodNamingConvention" enabled="true" level="WARNING" enabled_by_default="true">
@@ -1,2 +1,8 @@
1
- Layout/IndentHash:
2
- IndentationWidth: 4
1
+ Layout/TrailingEmptyLines:
2
+ EnforcedStyle: final_blank_line
3
+ Layout/FirstHashElementIndentation:
4
+ IndentationWidth: 4
5
+ Layout/LineLength:
6
+ Max: 110
7
+ Style/IfUnlessModifier:
8
+ Enabled: false
@@ -1 +1 @@
1
- 2.4.1
1
+ 2.7.1
@@ -0,0 +1,26 @@
1
+ # Should establish
2
+ ## URLs in ressource classes
3
+ Add methods to build urls
4
+
5
+ def url_configbuilder
6
+ ['cloud-service-config', 'configbuilder', 'accounts',
7
+ @account.id, 'devices', @device.id, 'ui']
8
+ end
9
+
10
+ Not decided yet if private or public
11
+
12
+ ## Parameter ordering for initialize methods
13
+
14
+ Library/API objects ordered from least to most specific.
15
+ Object specific data hashes go last.
16
+
17
+ ### Embedding the Cloud object in ressources
18
+ * First parameter in initalizer
19
+ * Saved in @cloud instance var
20
+ * Should be exposed via getter (attr_reader)
21
+
22
+ It' is also okay to use Cloud object from parent object if a reasonable one is passed.
23
+
24
+
25
+ # Unsolved, bad
26
+ * Cloud http methods (get, etc.) return response object. 99% of the time, have to call .body on it
File without changes
data/Rakefile CHANGED
@@ -1,17 +1,42 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
4
- require "rake/testtask"
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
5
  require 'rubocop/rake_task'
6
+ require 'rdoc/task'
6
7
 
7
8
  Rake::TestTask.new(:test) do |t|
8
- t.libs << "test"
9
- t.libs << "lib"
10
- t.test_files = FileList["test/**/*_test.rb"]
9
+ t.libs << 'test'
10
+ t.libs << 'lib'
11
+ t.test_files = FileList['test/**/*_test.rb']
11
12
  end
12
13
 
13
-
14
14
  RuboCop::RakeTask.new(:autocop) do |t|
15
- t.options = ['--only', 'Style/FrozenStringLiteralComment', '--auto-correct', 'lib', 'test', 'Rakefile', 'lmc.gemspec']
15
+ autofix = [
16
+ 'Layout/EmptyLines',
17
+ 'Layout/EmptyLinesAroundClassBody',
18
+ 'Layout/EmptyLinesAroundModuleBody',
19
+ 'Layout/EmptyLineBetweenDefs',
20
+ 'Layout/LeadingCommentSpace',
21
+ 'Layout/SpaceAroundOperators',
22
+ 'Layout/SpaceInsideBlockBraces',
23
+ 'Layout/SpaceInsideHashLiteralBraces',
24
+ 'Layout/TrailingEmptyLines',
25
+ 'Style/CommentAnnotation',
26
+ 'Style/FrozenStringLiteralComment',
27
+ 'Style/MethodDefParentheses',
28
+ 'Style/RedundantSelf',
29
+ 'Style/RedundantReturn',
30
+ 'Style/StringLiterals',
31
+ 'Style/StringLiteralsInInterpolation',
32
+ 'Style/StabbyLambdaParentheses',
33
+ ]
34
+ t.options = ['--only', autofix.join(','), '--auto-correct', 'lib', 'test', 'Rakefile', 'lmc.gemspec']
16
35
  end
17
36
  task :default => :test
37
+
38
+ RDoc::Task.new do |rdoc|
39
+ rdoc.main = 'README.rdoc'
40
+ rdoc.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
41
+ end
42
+
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "result": {
3
- "covered_percent": 84.99
3
+ "covered_percent": 91.53
4
4
  }
5
5
  }
data/lib/lmc.rb CHANGED
@@ -11,7 +11,7 @@ module LMC
11
11
  'cloud-service-licenses']
12
12
 
13
13
  def self.useful
14
- return true
14
+ true
15
15
  end
16
16
  end
17
17
 
@@ -23,7 +23,7 @@ Dir.glob(File.expand_path('../lmc/*.rb', __FILE__)).each do |file|
23
23
  require file
24
24
  end
25
25
 
26
- ['exceptions', 'auth', 'monitoring'].each do |folder|
26
+ ['exceptions', 'auth', 'config', 'monitoring', 'preferences'].each do |folder|
27
27
  Dir.glob(File.expand_path("../lmc/#{folder}/*.rb", __FILE__)).each do |file|
28
28
  require file
29
29
  end
@@ -10,22 +10,22 @@ module LMC
10
10
 
11
11
  def self.get(id)
12
12
  cloud = Cloud.instance
13
- result = cloud.get ["cloud-service-auth", "accounts", id.to_s]
14
- return Account.new(result.body)
13
+ result = cloud.get ['cloud-service-auth', 'accounts', id.to_s]
14
+ Account.new(cloud, result.body)
15
15
  end
16
16
 
17
- def self.get_by_uuid uuid
18
- raise "Missing argument" if uuid.nil?
19
- return self.get uuid
17
+ def self.get_by_uuid(uuid)
18
+ raise 'Missing argument' if uuid.nil?
19
+ get uuid
20
20
  end
21
21
 
22
22
  def self.get_by_name(name, type = nil)
23
- raise "Missing argument" if name.nil?
23
+ raise 'Missing argument' if name.nil?
24
24
  accounts = Cloud.instance.get_accounts_objects.select do |a|
25
25
  (name.nil? || a.name == name) && (type.nil? || a.type == type)
26
26
  end
27
27
  if accounts.length == 1
28
- return accounts[0]
28
+ accounts[0]
29
29
  elsif accounts.length == 0
30
30
  raise 'Did not find account'
31
31
  else
@@ -33,21 +33,21 @@ module LMC
33
33
  end
34
34
  end
35
35
 
36
- def initialize(data)
37
- @cloud = LMC::Cloud.instance
36
+ def initialize(cloud, data)
37
+ @cloud = cloud
38
38
  apply_data(data)
39
39
  end
40
40
 
41
- #returns itself, allows chaining
41
+ # returns itself, allows chaining
42
42
  def save
43
43
  response = if @id.nil?
44
44
  @cloud.auth_for_accounts [@parent]
45
- @cloud.post ["cloud-service-auth", "accounts"], self
45
+ @cloud.post ['cloud-service-auth', 'accounts'], self
46
46
  else
47
47
  @cloud.post path, self
48
48
  end
49
49
  apply_data(response.body)
50
- return self
50
+ self
51
51
  end
52
52
 
53
53
  def delete!
@@ -56,11 +56,11 @@ module LMC
56
56
  delete_action = AuthAction.new @cloud
57
57
  delete_action.type = AuthAction::ACCOUNT_DELETE
58
58
  delete_action.name = Cloud.user
59
- delete_action.data = {'password' => Cloud.password,
60
- 'accountId' => @id}
59
+ delete_action.data = { 'password' => Cloud.password,
60
+ 'accountId' => @id }
61
61
  delete_action.post
62
62
  @id = nil
63
- return true
63
+ true
64
64
  end
65
65
  end
66
66
 
@@ -71,32 +71,39 @@ module LMC
71
71
  end
72
72
 
73
73
  def members
74
- ids = Cloud.instance.get ["cloud-service-auth", "accounts", @id, 'members'], {"select" => "id"}
74
+ ids = Cloud.instance.get ['cloud-service-auth', 'accounts', @id, 'members'], 'select' => 'id'
75
75
  puts ids.inspect if Cloud.debug
76
76
  principals = ids.map do |principal_id|
77
- response = Cloud.instance.get ["cloud-service-auth", "accounts", @id, 'members', principal_id]
77
+ response = Cloud.instance.get ['cloud-service-auth', 'accounts', @id, 'members', principal_id]
78
78
  principal = response.body
79
79
  puts principal.inspect if Cloud.debug
80
80
  principal
81
81
  end
82
- return principals
82
+ principals
83
83
  end
84
84
 
85
- def find_member_by_name name
86
- members.find {|m| m.name == name}
85
+ def find_member_by_name(name)
86
+ if name.nil?
87
+ raise 'No member name given'
88
+ end
89
+ member = members.find { |m| m.name == name }
90
+ unless member
91
+ raise "Member named #{name} not found in account #{self}"
92
+ end
93
+ member
87
94
  end
88
95
 
89
- #def update_member(principal_id, data)
96
+ # def update_member(principal_id, data)
90
97
  # response = @cloud.post ["cloud-service-auth", "accounts", id, 'members', principal_id], data
91
98
  # return response
92
- #end
99
+ # end
93
100
 
94
101
  def remove_membership(member_id)
95
- @cloud.delete ["cloud-service-auth", "accounts", id, "members", member_id]
102
+ @cloud.delete ['cloud-service-auth', 'accounts', id, 'members', member_id]
96
103
  end
97
104
 
98
105
  def remove_membership_self
99
- @cloud.delete ["cloud-service-auth", "accounts", id, "members", "self"]
106
+ @cloud.delete ['cloud-service-auth', 'accounts', id, 'members', 'self']
100
107
  end
101
108
 
102
109
  def authority(authority_id)
@@ -112,30 +119,29 @@ module LMC
112
119
  authorities = response.body.map do |r|
113
120
  Authority.new r, self
114
121
  end
115
- return authorities
122
+ authorities
116
123
  end
117
124
 
118
125
  def children
119
- @cloud.auth_for_accounts([self.id, ROOT_ACCOUNT_UUID])
126
+ @cloud.auth_for_accounts([id, ROOT_ACCOUNT_UUID])
120
127
  response = @cloud.get ['cloud-service-auth', 'accounts', id, 'children']
121
- response.map {|child| Account.new child}
128
+ response.map { |child| Account.new @cloud, child }
122
129
  end
123
130
 
124
-
125
131
  def logs
126
132
  # https://lmctest/cloud-service-logging/accounts/6392b234-b11c-498a-a077-a5f5b23c54a0/logs?lang=DE
127
133
  cloud = Cloud.instance
128
134
  cloud.auth_for_accounts [id]
129
- cloud.get(["cloud-service-logging", "accounts", id, "logs?lang=DE"]).body
135
+ cloud.get(['cloud-service-logging', 'accounts', id, 'logs?lang=DE']).body
130
136
  end
131
137
 
132
138
  def sites
133
139
  # private clouds can not have sites
134
- return [] if @type == "PRIVATE_CLOUD"
140
+ return [] if @type == 'PRIVATE_CLOUD'
135
141
  @cloud.auth_for_accounts([id])
136
- response = @cloud.get ["cloud-service-devices", "accounts", id, "sites"]
137
- return response.body.map {|data|
138
- Site.new(data, self)
142
+ response = @cloud.get ['cloud-service-devices', 'accounts', id, 'sites'], :select => :id
143
+ response.body.map { |data|
144
+ Site.new(UUID.new(data), self)
139
145
  }
140
146
  end
141
147
 
@@ -145,16 +151,16 @@ module LMC
145
151
 
146
152
  def config_updatestates
147
153
  @cloud.auth_for_accounts([id])
148
- response = @cloud.get ["cloud-service-config", "configdevice", "accounts", id, "updatestates"]
149
- return LMC::Configstates.new response.body
154
+ response = @cloud.get ['cloud-service-config', 'configdevice', 'accounts', id, 'updatestates']
155
+ LMC::Configstates.new response.body
150
156
  end
151
157
 
152
158
  def to_json(*a)
153
159
  {
154
- "name" => @name,
155
- "state" => @state,
156
- "type" => @type,
157
- "parent" => @parent
160
+ 'name' => @name,
161
+ 'state' => @state,
162
+ 'type' => @type,
163
+ 'parent' => @parent
158
164
  }.to_json(*a)
159
165
  end
160
166
 
@@ -162,21 +168,25 @@ module LMC
162
168
  "#{name}"
163
169
  end
164
170
 
171
+ def summary
172
+ "\"#{@name}\" (#{@type}) ID: #{@id}"
173
+ end
174
+
165
175
  private
166
176
 
167
177
  ## should be put into entity or such
168
178
  def path
169
- ["cloud-service-auth", "accounts", @id].join("/")
179
+ ['cloud-service-auth', 'accounts', @id].join('/')
170
180
  end
171
181
 
172
182
  def apply_data(data)
173
- @id = data["id"]
174
- @parent = data["parent"]
175
- @name = data["name"]
176
- @state = data["state"]
177
- @type = data["type"]
178
- @identifier = data["identifier"]
183
+ @id = data['id']
184
+ @parent = data['parent']
185
+ @name = data['name']
186
+ @state = data['state']
187
+ @type = data['type']
188
+ @identifier = data['identifier']
179
189
  end
180
-
181
190
  end
182
191
  end
192
+
@@ -12,9 +12,10 @@ module LMC
12
12
  Cloud.verify_tls = true
13
13
  end
14
14
 
15
- def self.instance(opts = {authorize: true})
16
- @@inst ||= self.new(@cloud_host, @user, @password, opts[:authorize])
15
+ def self.instance(opts = { authorize: true })
16
+ @@inst ||= new(@cloud_host, @user, @password, opts[:authorize])
17
17
  end
18
+
18
19
  attr_reader :auth_ok, :cloud_host, :user, :password
19
20
 
20
21
  def initialize(cloud_host, user, pass, auth = true)
@@ -47,21 +48,26 @@ module LMC
47
48
  result = get ['cloud-service-auth', 'accounts']
48
49
  if result.code == 200
49
50
  accounts = result.map do |aj|
50
- Account.new(aj)
51
+ Account.new(self, aj)
51
52
  end
52
53
  else
53
54
  raise "Unable to fetch accounts: #{result.body.message}"
54
55
  end
55
56
 
56
- return accounts
57
+ accounts
57
58
  end
58
59
 
59
60
  def invite_user_to_account(email, account_id, type, authorities = [])
60
- body = {name: email, state: 'ACTIVE', type: type}
61
+ body = { name: email, state: 'ACTIVE', type: type }
61
62
  body['authorities'] = authorities
62
63
  post ['cloud-service-auth', 'accounts', account_id, 'members'], body
63
64
  end
64
65
 
66
+ # @param section Array of String to indicate section to access. Example: ['principal', 'self', 'ui']
67
+ def preferences(section)
68
+ LMC::Preferences.new cloud: self, section: section
69
+ end
70
+
65
71
  def get(path, params = nil)
66
72
  prepared_headers = headers
67
73
  prepared_headers[:params] = params
@@ -73,31 +79,37 @@ module LMC
73
79
  execute_request args
74
80
  end
75
81
 
76
- def put(path, body_object)
82
+ def put(path, body_object, params = nil)
83
+ prepared_headers = headers
84
+ prepared_headers[:params] = params
77
85
  args = {
78
86
  :method => :put,
79
87
  :url => build_url(path),
80
- :payload => body_object.to_json
81
-
88
+ :payload => body_object.to_json,
89
+ :headers => prepared_headers
82
90
  }
83
91
  execute_request args
84
92
  end
85
93
 
86
- def post(path, body_object)
94
+ def post(path, body_object, params=nil )
95
+ prepared_headers = headers
96
+ prepared_headers[:params] = params
87
97
  args = {
88
98
  :method => :post,
89
99
  :url => build_url(path),
90
- :payload => body_object.to_json
100
+ :payload => body_object.to_json,
101
+ :headers => prepared_headers
91
102
  }
92
103
  execute_request args
93
104
  end
94
105
 
95
- def delete(path, body_object = nil)
106
+ def delete(path, params = nil)
107
+ prepared_headers = headers
108
+ prepared_headers[:params] = params
96
109
  args = {
97
110
  :method => :delete,
98
111
  :url => build_url(path),
99
- :payload => body_object.to_json,
100
- :headers => headers
112
+ :headers => prepared_headers
101
113
  }
102
114
  execute_request args
103
115
  end
@@ -117,27 +129,31 @@ module LMC
117
129
  ["#{protocol}://#{@cloud_host}", path_components].flatten.compact.join('/')
118
130
  end
119
131
 
120
- def auth_for_accounts(account_ids)
121
- puts 'Authorizing for accounts: ' + account_ids.to_s if Cloud.debug
122
- authorize(account_ids)
132
+ def auth_for_accounts(accounts)
133
+ authorize(accounts)
123
134
  end
124
135
 
125
136
  def auth_for_account(account)
126
- auth_for_accounts([account.id])
137
+ authorize([account])
127
138
  end
128
139
 
129
140
  def accept_tos(tos)
130
141
  authorize([], tos)
131
142
  end
132
143
 
133
-
134
144
  private
135
145
 
136
- def authorize(account_ids = [], tos = [])
146
+ def authorize(accounts = [], tos = [])
147
+ account_ids = accounts.map { |a|
148
+ if a.respond_to? :id
149
+ a.id
150
+ else
151
+ a
152
+ end
153
+ }
137
154
  if account_ids != @last_authorized_account_ids
138
155
  begin
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
156
+ reply = post(['cloud-service-auth', 'auth'], name: @user, password: @password, accountIds: account_ids, termsOfUse: tos)
141
157
  @last_authorized_account_ids = account_ids
142
158
  @auth_token = reply
143
159
  @auth_ok = true
@@ -160,7 +176,7 @@ module LMC
160
176
  if @auth_ok
161
177
  headers[:Authorization] = auth_bearer
162
178
  end
163
- return headers
179
+ headers
164
180
  end
165
181
 
166
182
  def rest_options
@@ -175,17 +191,16 @@ module LMC
175
191
  internal_args.merge! args
176
192
  begin
177
193
  resp = RestClient::Request.execute internal_args
178
- return LMCResponse.new(resp)
194
+ LMCResponse.new(resp)
179
195
  rescue RestClient::ExceptionWithResponse => e
180
- print_exception e if Cloud.debug
196
+ if Cloud.debug
197
+ puts 'EXCEPTION: ' + e.to_s
198
+ puts 'EX.response: ' + e.response.to_s
199
+ puts JSON.parse(e.response)['message']
200
+ end
181
201
  raise e
182
202
  end
183
203
  end
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
204
  end
191
205
  end
206
+