nexus_cli 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
@@ -7,4 +7,8 @@ begin
7
7
  rescue NexusCli::NexusCliError => e
8
8
  NexusCli.ui.say e.message, :red
9
9
  exit e.status_code
10
+ rescue RestClient::Unauthorized => e
11
+ e = NexusCli::PermissionsException.new
12
+ NexusCli.ui.say e.message, :red
13
+ exit e.status_code
10
14
  end
@@ -126,4 +126,39 @@ Feature: Use the Nexus CLI
126
126
  The repository you requested information could not be found. Please ensure the repository exists.
127
127
  """
128
128
  And the exit status should be 114
129
-
129
+
130
+ Scenario: Create a new user
131
+ When I call the nexus "create_user --username=cucumber --first_name=John --last_name=Smith --email=jsmith@nexus-cli.com --enabled --roles=nx-admin --password=pass" command
132
+ And I call the nexus "get_users" command
133
+ Then the output should contain:
134
+ """
135
+ <userId>cucumber</userId>
136
+ """
137
+ And the exit status should be 0
138
+
139
+ Scenario: Change a users information
140
+ When I call the nexus "update_user cucumber --first_name=Mike --last_name=Ditka --email= --enabled --roles=" command
141
+ And I call the nexus "get_users" command
142
+ Then the output should contain:
143
+ """
144
+ <lastName>Ditka</lastName>
145
+ """
146
+ And the exit status should be 0
147
+
148
+ Scenario: Change a users password
149
+ When I call the nexus "change_password cucumber --oldPassword=pass --newPassword=foo" command
150
+ And I call the nexus "get_users" command as the "cucumber" user with password "wrongPassword"
151
+ Then the output should contain:
152
+ """
153
+ Your request was denied by the Nexus server due to a permissions error
154
+ """
155
+ And the exit status should be 106
156
+
157
+ Scenario: Delete a user
158
+ When I call the nexus "delete_user cucumber" command
159
+ And I call the nexus "get_users" command
160
+ Then the output should not contain:
161
+ """
162
+ <userId>cucumber</userId>
163
+ """
164
+ And the exit status should be 0
@@ -7,6 +7,12 @@ When /^I call the nexus "(.*?)" command$/ do |command|
7
7
  step "I run `nexus-cli #{command} --overrides=#{get_overrides_string}`"
8
8
  end
9
9
 
10
+ When /^I call the nexus "(.*?)" command as the "(.*?)" user with password "(.*?)"$/ do |command, username, password|
11
+ overrides = get_overrides_string.gsub('username:admin', "username:#{username}")
12
+ overrides.gsub!('password:admin123', "password:#{password}")
13
+ step "I run `nexus-cli #{command} --overrides=#{overrides}`"
14
+ end
15
+
10
16
  When /^I push an artifact with the GAV of "(.*)"$/ do |gav|
11
17
  groupId, artifact_id, version, extension = gav.split(":")
12
18
  file = File.new(File.join(temp_dir, "#{artifact_id}-#{version}.#{extension}"), 'w')
@@ -101,7 +101,7 @@ This could mean several things:
101
101
  def message
102
102
  %{Your global_settings.json file is malformed and could not be uploaded to Nexus.
103
103
  The output from the server was:
104
- #{@server_response}}
104
+ #{@server_response}}
105
105
  end
106
106
  status_code(111)
107
107
  end
@@ -113,7 +113,7 @@ The output from the server was:
113
113
 
114
114
  def message
115
115
  %{Your create repository command failed due to the following:
116
- #{@server_response}}
116
+ #{@server_response}}
117
117
  end
118
118
  status_code(112)
119
119
  end
@@ -149,4 +149,46 @@ The output from the server was:
149
149
  end
150
150
  status_code(116)
151
151
  end
152
- end
152
+
153
+ class CreateUserException < NexusCliError
154
+ def initialize(body)
155
+ @server_response = JSON.pretty_generate(JSON.parse(body))
156
+ end
157
+
158
+ def message
159
+ %{Your create user command failed due to the following:
160
+ #{@server_response}}
161
+ end
162
+ status_code(117)
163
+ end
164
+
165
+ class UserNotFoundException < NexusCliError
166
+ def initialize(id)
167
+ @id = id
168
+ end
169
+
170
+ def message
171
+ "A user with the ID of #{@id} could not be found. Please ensure it exists."
172
+ end
173
+ status_code(118)
174
+ end
175
+
176
+ class UpdateUserException < NexusCliError
177
+ def initialize(body)
178
+ @server_response = JSON.pretty_generate(JSON.parse(body))
179
+ end
180
+
181
+ def message
182
+ %{Your update user command failed due to the following:
183
+ #{@server_response}}
184
+ end
185
+ status_code(119)
186
+ end
187
+
188
+ class InvalidCredentialsException < NexusCliError
189
+ def message
190
+ "Invalid Credentials were supplied. Please make sure you are passing the correct values."
191
+ end
192
+ status_code(120)
193
+ end
194
+ end
@@ -2,6 +2,7 @@ require 'restclient'
2
2
  require 'nokogiri'
3
3
  require 'yaml'
4
4
  require 'json'
5
+ require 'jsonpath'
5
6
 
6
7
  module NexusCli
7
8
  class OSSRemote
@@ -167,42 +168,126 @@ module NexusCli
167
168
  end
168
169
  end
169
170
 
171
+ def get_users
172
+ nexus["service/local/users"].get
173
+ end
174
+
175
+ def create_user(params)
176
+ nexus["service/local/users"].post(create_user_json(params), :content_type => "application/json") do |response|
177
+ case response.code
178
+ when 201
179
+ return true
180
+ when 400
181
+ raise CreateUserException.new(response.body)
182
+ else
183
+ raise UnexpectedStatusCodeException.new(reponse.code)
184
+ end
185
+ end
186
+ end
187
+
188
+ def update_user(params)
189
+ params[:roles] = [] if params[:roles] == [""]
190
+ user_json = get_user(params[:userId])
191
+
192
+ modified_json = JsonPath.for(user_json)
193
+ params.each do |key, value|
194
+ modified_json.gsub!("$..#{key}"){|v| value} unless key == "userId" || value.blank?
195
+ end
196
+
197
+ nexus["service/local/users/#{params[:userId]}"].put(JSON.dump(modified_json.to_hash), :content_type => "application/json") do |response|
198
+ case response.code
199
+ when 200
200
+ return true
201
+ when 400
202
+ raise UpdateUserException.new(response.body)
203
+ else
204
+ raise UnexpectedStatusCodeException.new(response.code)
205
+ end
206
+ end
207
+ end
208
+
209
+ def get_user(user)
210
+ nexus["service/local/users/#{user}"].get(:accept => "application/json") do |response|
211
+ case response.code
212
+ when 200
213
+ return JSON.parse(response.body)
214
+ when 404
215
+ raise UserNotFoundException.new(user)
216
+ else
217
+ raise UnexpectedStatusCodeException.new(response.code)
218
+ end
219
+ end
220
+ end
221
+
222
+ def change_password(params)
223
+ nexus["service/local/users_changepw"].post(create_change_password_json(params), :content_type => "application/json") do |response|
224
+ case response.code
225
+ when 202
226
+ return true
227
+ when 400
228
+ raise InvalidCredentialsException
229
+ end
230
+ end
231
+ end
232
+
233
+ def delete_user(user_id)
234
+ nexus["service/local/users/#{user_id}"].delete do |response|
235
+ case response.code
236
+ when 204
237
+ return true
238
+ when 404
239
+ raise UserNotFoundException.new(user_id)
240
+ else
241
+ raise UnexpectedStatusCodeException.new(response.code)
242
+ end
243
+ end
244
+ end
245
+
170
246
  private
171
- def format_search_results(doc, group_id, artifact_id)
172
- versions = doc.xpath("//version").inject([]) {|array,node| array << "#{node.content()}"}
173
- indent_size = versions.max{|a,b| a.length <=> b.length}.size+4
174
- formated_results = ['Found Versions:']
175
- versions.inject(formated_results) do |array,version|
176
- temp_version = version + ":"
177
- array << "#{temp_version.ljust(indent_size)} `nexus-cli pull #{group_id}:#{artifact_id}:#{version}:tgz`"
178
- end
179
- end
180
-
181
- def parse_artifact_string(artifact)
182
- split_artifact = artifact.split(":")
183
- if(split_artifact.size < 4)
184
- raise ArtifactMalformedException
185
- end
186
- group_id, artifact_id, version, extension = split_artifact
187
- version.upcase! if version.casecmp("latest")
188
- return group_id, artifact_id, version, extension
189
- end
190
-
191
- def create_repository_json(name)
192
- %{
193
- {
194
- "data" : {
195
- "provider" : "maven2",
196
- "providerRole" : "org.sonatype.nexus.proxy.repository.Repository",
197
- "exposed" : true,
198
- "repoType" : "hosted",
199
- "repoPolicy" : "RELEASE",
200
- "name" : #{name},
201
- "id" : #{name.downcase},
202
- "format" : "maven2"
247
+
248
+ def format_search_results(doc, group_id, artifact_id)
249
+ versions = doc.xpath("//version").inject([]) {|array,node| array << "#{node.content()}"}
250
+ indent_size = versions.max{|a,b| a.length <=> b.length}.size+4
251
+ formated_results = ['Found Versions:']
252
+ versions.inject(formated_results) do |array,version|
253
+ temp_version = version + ":"
254
+ array << "#{temp_version.ljust(indent_size)} `nexus-cli pull #{group_id}:#{artifact_id}:#{version}:tgz`"
255
+ end
256
+ end
257
+
258
+ def parse_artifact_string(artifact)
259
+ split_artifact = artifact.split(":")
260
+ if(split_artifact.size < 4)
261
+ raise ArtifactMalformedException
262
+ end
263
+ group_id, artifact_id, version, extension = split_artifact
264
+ version.upcase! if version.casecmp("latest")
265
+ return group_id, artifact_id, version, extension
266
+ end
267
+
268
+ def create_repository_json(name)
269
+ %{
270
+ {
271
+ "data" : {
272
+ "provider" : "maven2",
273
+ "providerRole" : "org.sonatype.nexus.proxy.repository.Repository",
274
+ "exposed" : true,
275
+ "repoType" : "hosted",
276
+ "repoPolicy" : "RELEASE",
277
+ "name" : #{name},
278
+ "id" : #{name.downcase},
279
+ "format" : "maven2"
280
+ }
203
281
  }
204
282
  }
205
- }
206
- end
283
+ end
284
+
285
+ def create_user_json(params)
286
+ JSON.dump(:data => params)
287
+ end
288
+
289
+ def create_change_password_json(params)
290
+ JSON.dump(:data => params)
291
+ end
207
292
  end
208
293
  end
@@ -1,4 +1,5 @@
1
1
  require 'thor'
2
+ require 'highline'
2
3
 
3
4
  module NexusCli
4
5
  module Tasks
@@ -155,6 +156,169 @@ module NexusCli
155
156
  def get_repository_info(name)
156
157
  say @nexus_remote.get_repository_info(name), :green
157
158
  end
159
+
160
+ desc "get_users", "Returns XML representing the users in Nexus."
161
+ def get_users
162
+ say @nexus_remote.get_users
163
+ end
164
+
165
+ method_option :username,
166
+ :type => :string,
167
+ :default => nil,
168
+ :desc => "The username."
169
+ method_option :first_name,
170
+ :type => :string,
171
+ :default => nil,
172
+ :desc => "The first name."
173
+ method_option :last_name,
174
+ :type => :string,
175
+ :default => nil,
176
+ :desc => "The last name."
177
+ method_option :email,
178
+ :type => :string,
179
+ :default => nil,
180
+ :desc => "The email."
181
+ method_option :password,
182
+ :type => :string,
183
+ :default => nil,
184
+ :desc => "The password."
185
+ method_option :enabled,
186
+ :type => :boolean,
187
+ :default => nil,
188
+ :desc => "Whether this new user is enabled or disabled."
189
+ method_option :roles,
190
+ :type => :array,
191
+ :default => [],
192
+ :require => false,
193
+ :desc => "An array of roles."
194
+ desc "create_user", "Creates a new user"
195
+ def create_user
196
+ params = ask_user(options)
197
+
198
+ if @nexus_remote.create_user(params)
199
+ say "A user with the ID of #{params[:userId]} has been created.", :blue
200
+ end
201
+ end
202
+
203
+ method_option :username,
204
+ :type => :string,
205
+ :default => nil,
206
+ :desc => "The username."
207
+ method_option :first_name,
208
+ :type => :string,
209
+ :default => nil,
210
+ :desc => "The first name."
211
+ method_option :last_name,
212
+ :type => :string,
213
+ :default => nil,
214
+ :desc => "The last name."
215
+ method_option :email,
216
+ :type => :string,
217
+ :default => nil,
218
+ :desc => "The email."
219
+ method_option :enabled,
220
+ :type => :boolean,
221
+ :default => nil,
222
+ :desc => "Whether this new user is enabled or disabled."
223
+ method_option :roles,
224
+ :type => :array,
225
+ :default => [],
226
+ :require => false,
227
+ :desc => "An array of roles."
228
+ desc "update_user user_id", "Updates a user's details. Leave fields blank for them to remain their current values."
229
+ def update_user(user_id)
230
+ params = ask_user(options, false, false)
231
+ params[:userId] = user_id
232
+
233
+ if @nexus_remote.update_user(params)
234
+ say "User #{user_id} has been updated.", :blue
235
+ end
236
+ end
237
+
238
+ desc "delete_user user_id", "Deletes the user with the given id."
239
+ def delete_user(user_id)
240
+ if @nexus_remote.delete_user(user_id)
241
+ say "User #{user_id} has been deleted.", :blue
242
+ end
243
+ end
244
+
245
+ method_option :oldPassword,
246
+ :type => :string,
247
+ :default => nil,
248
+ :desc => ""
249
+ method_option :newPassword,
250
+ :type => :string,
251
+ :default => nil,
252
+ :desc => ""
253
+ desc "change_password user_id", "Changes the given user's passwords to a new one."
254
+ def change_password(user_id)
255
+
256
+ oldPassword = options[:oldPassword]
257
+ newPassword = options[:newPassword]
258
+
259
+ if oldPassword.nil?
260
+ oldPassword = ask_password("Please enter your old password:")
261
+ end
262
+ if newPassword.nil?
263
+ newPassword = ask_password("Please enter your new password:")
264
+ end
265
+
266
+ params = {:userId => user_id}
267
+ params[:oldPassword] = oldPassword
268
+ params[:newPassword] = newPassword
269
+ if @nexus_remote.change_password(params)
270
+ say "The password for user #{user_id} has been updated."
271
+ end
272
+ end
273
+
274
+ private
275
+
276
+ def ask_user(params, ask_username=true, ask_password=true)
277
+ username = params[:username]
278
+ first_name = params[:first_name]
279
+ last_name = params[:last_name]
280
+ email = params[:email]
281
+ enabled = params[:enabled]
282
+ password = params[:password]
283
+ roles = params[:roles]
284
+ status = enabled
285
+
286
+ if username.nil? && ask_username
287
+ username = ask "Please enter the username:"
288
+ end
289
+ if first_name.nil?
290
+ first_name = ask "Please enter the first name:"
291
+ end
292
+ if last_name.nil?
293
+ last_name = ask "Please enter the last name:"
294
+ end
295
+ if email.nil?
296
+ email = ask "Please enter the email:"
297
+ end
298
+ if enabled.nil?
299
+ status = ask "Is this user enabled for use?", :limited_to => ["true", "false"]
300
+ end
301
+ if password.nil? && ask_password
302
+ password = ask_password("Please enter a password:")
303
+ end
304
+ if roles.size == 0
305
+ roles = ask "Please enter the roles:"
306
+ end
307
+ params = {:userId => username}
308
+ params[:firstName] = first_name
309
+ params[:lastName] = last_name
310
+ params[:email] = email
311
+ params[:status] = status == true ? "active" : "disabled"
312
+ params[:password] = password
313
+ params[:roles] = roles.kind_of?(Array) ? roles : roles.split(' ')
314
+ params
315
+ end
316
+
317
+ def ask_password(message)
318
+ HighLine.new.ask(message) do |q|
319
+ q.echo = false
320
+ end
321
+ end
158
322
  end
159
323
  end
160
324
  end
@@ -21,10 +21,12 @@ Gem::Specification.new do |s|
21
21
  s.add_dependency 'nokogiri'
22
22
  s.add_dependency 'extlib'
23
23
  s.add_dependency 'json'
24
+ s.add_dependency 'highline'
25
+ s.add_dependency 'jsonpath'
24
26
 
25
27
  s.add_development_dependency 'rspec'
26
28
  s.add_development_dependency 'aruba'
27
29
  s.add_development_dependency 'cucumber'
28
30
  s.add_development_dependency 'rake'
29
- s.add_development_dependency 'jsonpath'
31
+ s.add_development_dependency 'webmock'
30
32
  end
@@ -1,6 +1,6 @@
1
- require 'nexus_cli'
1
+ require 'spec_helper'
2
2
 
3
- remote = NexusCli::OSSRemote.new(nil)
3
+ remote = NexusCli::OSSRemote.new({'url' => 'http://localhost:8081/nexus', 'repository' => 'releases', 'username' => 'admin', 'password' => 'admin123'})
4
4
 
5
5
  describe NexusCli do
6
6
  it "gives you errors when you attempt to pull an artifact don't give a valid artifact name" do
@@ -20,4 +20,11 @@ describe NexusCli do
20
20
  RestClient::Resource.any_instance.stub(:get).and_raise(RestClient::ResourceNotFound)
21
21
  expect {remote.get_artifact_info "com.something:something:1.0.0:tgz"}.to raise_error(NexusCli::ArtifactNotFoundException)
22
22
  end
23
+
24
+ it "gives you an error when you try to update a user that doesnt exist" do
25
+ stub_request(:get, "http://admin:admin123@localhost:8081/nexus/service/local/users/qwertyasdf").
26
+ with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip, deflate', 'User-Agent'=>'Ruby'}).
27
+ to_return(:status => 404, :body => "", :headers => {})
28
+ expect {remote.update_user(:userId => "qwertyasdf")}.to raise_error(NexusCli::UserNotFoundException)
29
+ end
23
30
  end
@@ -0,0 +1,2 @@
1
+ require 'nexus_cli'
2
+ require 'webmock/rspec'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexus_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-14 00:00:00.000000000 Z
12
+ date: 2012-08-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -91,6 +91,38 @@ dependencies:
91
91
  - - ! '>='
92
92
  - !ruby/object:Gem::Version
93
93
  version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: highline
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: jsonpath
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
94
126
  - !ruby/object:Gem::Dependency
95
127
  name: rspec
96
128
  requirement: !ruby/object:Gem::Requirement
@@ -156,7 +188,7 @@ dependencies:
156
188
  - !ruby/object:Gem::Version
157
189
  version: '0'
158
190
  - !ruby/object:Gem::Dependency
159
- name: jsonpath
191
+ name: webmock
160
192
  requirement: !ruby/object:Gem::Requirement
161
193
  none: false
162
194
  requirements:
@@ -205,6 +237,7 @@ files:
205
237
  - spec/configuration_spec.rb
206
238
  - spec/oss_remote_spec.rb
207
239
  - spec/pro_remote_spec.rb
240
+ - spec/spec_helper.rb
208
241
  homepage: https://github.com/RiotGames/nexus_cli
209
242
  licenses: []
210
243
  post_install_message:
@@ -219,7 +252,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
219
252
  version: '0'
220
253
  segments:
221
254
  - 0
222
- hash: 2849503457088068545
255
+ hash: -1391615836890304252
223
256
  required_rubygems_version: !ruby/object:Gem::Requirement
224
257
  none: false
225
258
  requirements:
@@ -228,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
261
  version: '0'
229
262
  segments:
230
263
  - 0
231
- hash: 2849503457088068545
264
+ hash: -1391615836890304252
232
265
  requirements: []
233
266
  rubyforge_project:
234
267
  rubygems_version: 1.8.21
@@ -242,3 +275,4 @@ test_files:
242
275
  - spec/configuration_spec.rb
243
276
  - spec/oss_remote_spec.rb
244
277
  - spec/pro_remote_spec.rb
278
+ - spec/spec_helper.rb