nexus_cli 0.7.3 → 0.8.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.7.3
1
+ 0.8.0
@@ -7,8 +7,4 @@ 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
14
10
  end
@@ -19,6 +19,7 @@ Feature: Use the Nexus CLI
19
19
  """
20
20
  And the exit status should be 0
21
21
 
22
+ @pull
22
23
  Scenario: Pull an artifact
23
24
  When I call the nexus "pull com.test:mytest:1.0.0:tgz" command
24
25
  Then the output should contain:
@@ -161,4 +162,29 @@ Feature: Use the Nexus CLI
161
162
  """
162
163
  <userId>cucumber</userId>
163
164
  """
165
+ And the exit status should be 0
166
+
167
+ Scenario: Get Logging Info
168
+ When I call the nexus "get_logging_info" command
169
+ Then the output should contain:
170
+ """
171
+ \"rootLoggerLevel\":\"INFO\"
172
+ """
173
+ And the exit status should be 0
174
+
175
+ Scenario: Change the logging level to DEBUG
176
+ When I call the nexus "set_logger_level debug" command
177
+ And I call the nexus "get_logging_info" command
178
+ Then the output should contain:
179
+ """
180
+ \"rootLoggerLevel\":\"DEBUG\"
181
+ """
182
+ And the exit status should be 0
183
+
184
+ Scenario: Change the logging level back to INFO
185
+ When I call the nexus "set_logger_level info" command
186
+ Then the output should contain:
187
+ """
188
+ The logging level of Nexus has been set to INFO
189
+ """
164
190
  And the exit status should be 0
@@ -9,6 +9,10 @@ require 'nexus_cli/configuration'
9
9
  require 'nexus_cli/n3_metadata'
10
10
 
11
11
  module NexusCli
12
+
13
+ DEFAULT_ACCEPT_HEADER = {"Accept" => "application/json"}
14
+ DEFAULT_CONTENT_TYPE_HEADER = {"Content-Type" => "application/json"}
15
+
12
16
  class << self
13
17
  def root
14
18
  @root ||= Pathname.new(File.expand_path('../', File.dirname(__FILE__)))
@@ -172,7 +172,7 @@ The output from the server was:
172
172
  end
173
173
  status_code(118)
174
174
  end
175
-
175
+
176
176
  class UpdateUserException < NexusCliError
177
177
  def initialize(body)
178
178
  @server_response = JSON.pretty_generate(JSON.parse(body))
@@ -209,4 +209,18 @@ The output from the server was:
209
209
  end
210
210
  status_code(122)
211
211
  end
212
+
213
+ class InvalidLoggingLevelException < NexusCliError
214
+ def message
215
+ "Logging level must be set to one of either INFO, DEBUG, or ERROR."
216
+ end
217
+ status_code(123)
218
+ end
219
+
220
+ class N3NotFoundException < NexusCliError
221
+ def message
222
+ "The artifact does not have any custom metadata added yet."
223
+ end
224
+ status_code(124)
225
+ end
212
226
  end
@@ -1,87 +1,70 @@
1
1
  require 'nokogiri'
2
+ require 'base64'
2
3
 
3
4
  module NexusCli
4
5
  module N3Metadata
5
6
  class << self
6
- def generate_n3_path(group_id, artifact_id, version, extension, repository)
7
- return "content/repositories/#{repository}/.meta/#{group_id.gsub(".", "/")}/#{artifact_id.gsub(".", "/")}/#{version}/#{artifact_id}-#{version}.#{extension}.n3"
8
- end
9
-
7
+ # Checks if the custom metadata key is valid.
8
+ # Valid characters are alphanumeric with no special characeters.
10
9
  def valid_n3_key?(element)
11
- return !element.match(/^[a-zA-Z0-9]+$/).nil? ? true : false
10
+ return !element.nil? && !element.match(/^[a-zA-Z0-9]+$/).nil? ? true : false
12
11
  end
13
12
 
13
+ # Checks if the custom metadata value is valid.
14
+ # Valid characters are anything but quotes.
14
15
  def valid_n3_value?(element)
15
- return !element.match(/^[^"'\\]*$/).nil? ? true : false
16
+ return !element.nil? && !element.match(/^[^"'\\]*$/).nil? ? true : false
16
17
  end
17
18
 
19
+ # Check if the custom metadata search type is valid.
18
20
  def valid_n3_search_type?(element)
19
- return ["equal", "notequal", "matches", "bounded"].include?(element)
21
+ return !element.nil? && ["equal", "notequal", "matches", "bounded"].include?(element)
20
22
  end
21
23
 
22
- # Generates the Nexus .n3 header for the tempfile that will be used to update an artifact's custom metadata.
23
- def generate_n3_header(group_id, artifact_id, version, extension)
24
- return "<urn:maven/artifact##{group_id}:#{artifact_id}:#{version}::#{extension}> a <urn:maven#artifact>"
24
+ # Creates a custom metadata subject for HTTP requests.
25
+ def create_base64_subject(group_id, artifact_id, version, extension)
26
+ return Base64.urlsafe_encode64("urn:maven/artifact##{group_id}:#{artifact_id}:#{version}::#{extension}")
25
27
  end
26
28
 
27
- # Generates a hash containing the Nexus .n3 contents for the tempfile that will be used to update an artifact's custom metadata.
28
- # If a hash of n3 user urns is provided, the contents will override existing key/value pairs.
29
- def generate_n3_urns_from_n3(contents, n3_user_urns={})
30
- contents.each_line do |line|
31
- if !line.match(/urn:nexus\/user#/).nil?
32
- tag, value = parse_n3_item(line)
33
- # Delete the nexus key if the local key has no value.
34
- if n3_user_urns.has_key?(tag) && value.empty?
35
- n3_user_urns.delete(tag)
36
- else
37
- n3_user_urns[tag] = generate_n3_item(tag, value) unless tag.empty? || value.empty?
38
- end
39
- end
29
+ # Parses the regular custom metadata xml into a simpler format containing only the custom metadata.
30
+ def convert_result_to_simple_xml(custom_metadata)
31
+ request = []
32
+ Nokogiri::XML(custom_metadata).root.search("//customMetadataResponse/data/customMetadata[namespace=\"urn:nexus/user#\"]").each do |row|
33
+ request.push(create_tag(row.at("key").text.strip, row.at("value").text.strip))
40
34
  end
41
- return n3_user_urns
35
+ return Nokogiri::XML("<artifact-resolution><data>#{request.join}</data></artifact-resolution>").root.to_xml(:indent => 4)
42
36
  end
43
37
 
44
- def generate_n3_urns_from_hash(contents, n3_user_urns={})
45
- contents.each do |tag, value|
46
- # Delete the nexus key if the local key has no value.
47
- if n3_user_urns.has_key?(tag) && value.empty?
48
- n3_user_urns.delete(tag)
49
- else
50
- n3_user_urns[tag] = generate_n3_item(tag, value) unless tag.empty? || value.empty?
51
- end
38
+ # Parses the regular custom metadata xml into a hash containing only the custom metadata.
39
+ def convert_result_to_hash(custom_metadata)
40
+ request = {}
41
+ Nokogiri::XML(custom_metadata).root.search("//customMetadataResponse/data/customMetadata[namespace=\"urn:nexus/user#\"]").each do |row|
42
+ request[row.at("key").text.strip] = row.at("value").text.strip
52
43
  end
53
- return n3_user_urns
44
+ return request
54
45
  end
55
46
 
56
- # Parses a hash of n3 user urns and returns it as an n3-formatted string.
57
- def parse_n3_hash(contents)
58
- return contents.values.count == 1 ? contents.values[0] + " ." : contents.values.join(" ;\n") + " ."
47
+ # Create the request from the specified list of custom metadata key:value pairs
48
+ # @info If the target hash contains empty values for a key that exist in source, the metadata will be deleted
49
+ # @param [Hash] source The source hash of custom metadata key:value pairs
50
+ # @param [Hash] target The target hash to merge with the source hash (optional)
51
+ # @result [Hash] The resulting merge of the source and target hashes
52
+ def create_metadata_hash(source, target={})
53
+ request = []
54
+ source.merge(target).each do |key, value|
55
+ request.push({:namespace => "urn:nexus/user#", :key => key, :value => value, :readOnly => false}) unless value.empty?
56
+ end
57
+ return request
59
58
  end
60
59
 
61
- # Returns n3 as XML.
62
- def n3_to_xml(n3)
63
- builder = Nokogiri::XML::Builder.new do |xml|
64
- xml.send("artifact-resolution") {
65
- xml.data {
66
- n3.each_line do |line|
67
- tag, value = parse_n3_item(line)
68
- xml.send(tag, value) unless tag.empty? || value.empty?
69
- end
70
- }
71
- }
72
- end
73
- return builder.doc.root.to_s
60
+ def missing_custom_metadata?(custom_metadata)
61
+ return !custom_metadata.match(/<data[ ]*\/>/).nil? ? true : false
74
62
  end
75
63
 
76
64
  private
77
- def parse_n3_item(line)
78
- tag = line.match(/#(\w*)>/) ? "#{$1}" : ""
79
- value = line.match(/"([^"]*)"/) ? "#{$1}" : ""
80
- return tag, value
81
- end
82
65
 
83
- def generate_n3_item(tag, value)
84
- return "\t<urn:nexus/user##{tag}> \"#{value}\""
66
+ def create_tag(tag, value)
67
+ return "<#{tag}>#{value}</#{tag}>"
85
68
  end
86
69
  end
87
70
  end
@@ -1,4 +1,4 @@
1
- require 'restclient'
1
+ require 'httpclient'
2
2
  require 'nokogiri'
3
3
  require 'yaml'
4
4
  require 'json'
@@ -6,7 +6,6 @@ require 'jsonpath'
6
6
 
7
7
  module NexusCli
8
8
  class OSSRemote
9
-
10
9
  def initialize(overrides)
11
10
  @configuration = Configuration::parse(overrides)
12
11
  end
@@ -16,85 +15,120 @@ module NexusCli
16
15
  end
17
16
 
18
17
  def nexus
19
- RestClient::Resource.new configuration["url"], :user => configuration["username"], :password => configuration["password"], :timeout => 1000000, :open_timeout => 1000000
18
+ client = HTTPClient.new
19
+ client.send_timeout = 600
20
+ client.receive_timeout = 600
21
+ # https://github.com/nahi/httpclient/issues/63
22
+ client.set_auth(nil, configuration['username'], configuration['password'])
23
+ client.www_auth.basic_auth.challenge(configuration['url'])
24
+ return client
25
+ end
26
+
27
+ def nexus_url(url)
28
+ File.join(configuration['url'], url)
20
29
  end
21
30
 
22
31
  def status
23
- doc = Nokogiri::XML(nexus['service/local/status'].get).xpath("/status/data")
24
- data = Hash.new
25
- data['app_name'] = doc.xpath("appName")[0].text
26
- data['version'] = doc.xpath("version")[0].text
27
- data['edition_long'] = doc.xpath("editionLong")[0].text
28
- data['state'] = doc.xpath("state")[0].text
29
- data['started_at'] = doc.xpath("startedAt")[0].text
30
- data['base_url'] = doc.xpath("baseUrl")[0].text
31
- return data
32
+ response = nexus.get(nexus_url("service/local/status"))
33
+ case response.status
34
+ when 200
35
+ doc = Nokogiri::XML(response.content).xpath("/status/data")
36
+ data = Hash.new
37
+ data['app_name'] = doc.xpath("appName")[0].text
38
+ data['version'] = doc.xpath("version")[0].text
39
+ data['edition_long'] = doc.xpath("editionLong")[0].text
40
+ data['state'] = doc.xpath("state")[0].text
41
+ data['started_at'] = doc.xpath("startedAt")[0].text
42
+ data['base_url'] = doc.xpath("baseUrl")[0].text
43
+ return data
44
+ when 401
45
+ raise PermissionsException
46
+ when 503
47
+ raise CouldNotConnectToNexusException
48
+ else
49
+ raise UnexpectedStatusCodeException.new(response.status)
50
+ end
51
+ end
52
+
53
+ def running_nexus_pro?
54
+ status['edition_long'] == "Professional"
32
55
  end
33
56
 
34
57
  def pull_artifact(artifact, destination)
35
- # Using net/http because restclient dies on large files.
36
58
  group_id, artifact_id, version, extension = parse_artifact_string(artifact)
37
- uri = URI(File.join(configuration["url"], "service/local/artifact/maven/redirect"))
38
- params = {:g => group_id, :a => artifact_id, :v => version, :e => extension, :r => configuration["repository"]}.collect {|k,v| "#{k}=#{URI::escape(v.to_s)}"}.join("&")
39
59
  version = Nokogiri::XML(get_artifact_info(artifact)).xpath("//version").first.content() if version.casecmp("latest")
40
60
  destination = File.join(File.expand_path(destination || "."), "#{artifact_id}-#{version}.#{extension}")
41
- Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
42
- request = Net::HTTP::Get.new(URI(http.request_get(uri.request_uri + "?" + params)["location"]).request_uri)
43
- request.basic_auth(configuration["username"], configuration["password"])
44
- http.request(request) do |response|
45
- case response.code.to_i
46
- when 404
47
- raise ArtifactNotFoundException
48
- end
49
- artifact_file = File.open(destination, "wb") do |io|
50
- response.read_body do |chunk|
51
- io.write(chunk)
52
- end
61
+ response = nexus.get(nexus_url("service/local/artifact/maven/redirect"), :query => {:g => group_id, :a => artifact_id, :v => version, :e => extension, :r => configuration['repository']})
62
+ case response.status
63
+ when 301
64
+ # Follow redirect and stream in chunks.
65
+ artifact_file = File.open(destination, "wb") do |io|
66
+ nexus.get(response.content.gsub(/If you are not automatically redirected use this url: /, "")) do |chunk|
67
+ io.write(chunk)
53
68
  end
54
69
  end
70
+ when 404
71
+ raise ArtifactNotFoundException
72
+ else
73
+ raise UnexpectedStatusCodeException.new(response.status)
55
74
  end
56
75
  File.expand_path(destination)
57
76
  end
58
77
 
59
78
  def push_artifact(artifact, file)
60
79
  group_id, artifact_id, version, extension = parse_artifact_string(artifact)
61
- nexus['service/local/artifact/maven/content'].post(:hasPom => false, :g => group_id, :a => artifact_id, :v => version, :e => extension, :p => extension, :r => configuration['repository'],
62
- :file => File.new(file)) do |response|
63
- case response.code
64
- when 400
65
- raise BadUploadRequestException
66
- when 401
67
- raise PermissionsException
68
- when 403
69
- raise PermissionsException
70
- when 404
71
- raise CouldNotConnectToNexusException
72
- end
80
+ response = nexus.post(nexus_url("service/local/artifact/maven/content"), {:hasPom => false, :g => group_id, :a => artifact_id, :v => version, :e => extension, :p => extension, :r => configuration['repository'], :file => File.open(file)})
81
+ case response.status
82
+ when 201
83
+ return true
84
+ when 400
85
+ raise BadUploadRequestException
86
+ when 401
87
+ raise PermissionsException
88
+ when 403
89
+ raise PermissionsException
90
+ when 404
91
+ raise CouldNotConnectToNexusException
92
+ else
93
+ raise UnexpectedStatusCodeException.new(response.status)
73
94
  end
74
95
  end
75
96
 
76
97
  def delete_artifact(artifact)
77
98
  group_id, artifact_id, version = parse_artifact_string(artifact)
78
- delete_string = "content/repositories/releases/#{group_id.gsub(".", "/")}/#{artifact_id.gsub(".", "/")}/#{version}"
79
- Kernel.quietly {`curl --request DELETE #{File.join(configuration['url'], delete_string)} -u #{configuration['username']}:#{configuration['password']}`}
99
+ response = nexus.delete(nexus_url("content/repositories/#{configuration['repository']}/#{group_id.gsub(".", "/")}/#{artifact_id.gsub(".", "/")}/#{version}"))
100
+ case response.status
101
+ when 204
102
+ return true
103
+ else
104
+ raise UnexpectedStatusCodeException.new(response.status)
105
+ end
80
106
  end
81
107
 
82
108
  def get_artifact_info(artifact)
83
109
  group_id, artifact_id, version, extension = parse_artifact_string(artifact)
84
- begin
85
- nexus['service/local/artifact/maven/resolve'].get(:params => {:r => configuration['repository'], :g => group_id, :a => artifact_id, :v => version, :e => extension})
86
- rescue Errno::ECONNREFUSED => e
87
- raise CouldNotConnectToNexusException
88
- rescue RestClient::ResourceNotFound => e
110
+ response = nexus.get(nexus_url("service/local/artifact/maven/resolve"), :query => {:g => group_id, :a => artifact_id, :v => version, :e => extension, :r => configuration['repository']})
111
+ case response.status
112
+ when 200
113
+ return response.content
114
+ when 404
89
115
  raise ArtifactNotFoundException
116
+ when 503
117
+ raise CouldNotConnectToNexusException
118
+ else
119
+ raise UnexpectedStatusCodeException.new(response.status)
90
120
  end
91
121
  end
92
122
 
93
123
  def search_for_artifacts(artifact)
94
124
  group_id, artifact_id = artifact.split(":")
95
- nexus['service/local/data_index'].get(:params => {:g => group_id, :a => artifact_id}) do |response|
96
- doc = Nokogiri::XML(response.body)
125
+ response = nexus.get(nexus_url("service/local/data_index"), :query => {:g => group_id, :a => artifact_id})
126
+ case response.status
127
+ when 200
128
+ doc = Nokogiri::XML(response.content)
97
129
  return format_search_results(doc, group_id, artifact_id)
130
+ else
131
+ raise UnexpectedStatusCodeException.new(response.status)
98
132
  end
99
133
  end
100
134
 
@@ -108,6 +142,16 @@ module NexusCli
108
142
  end
109
143
  end
110
144
 
145
+ def get_global_settings_json
146
+ response = nexus.get(nexus_url("service/local/global_settings/current"), :header => DEFAULT_ACCEPT_HEADER)
147
+ case response.status
148
+ when 200
149
+ return response.content
150
+ else
151
+ raise UnexpectedStatusCodeException.new(response.status)
152
+ end
153
+ end
154
+
111
155
  def upload_global_settings(json=nil)
112
156
  global_settings = nil
113
157
  if json == nil
@@ -115,78 +159,95 @@ module NexusCli
115
159
  else
116
160
  global_settings = json
117
161
  end
118
- nexus['service/local/global_settings/current'].put(global_settings, {:content_type => "application/json"}) do |response|
119
- case response.code
120
- when 400
121
- raise BadSettingsException.new(response.body)
122
- end
162
+ response = nexus.put(nexus_url("service/local/global_settings/current"), :body => global_settings, :header => DEFAULT_CONTENT_TYPE_HEADER)
163
+ case response.status
164
+ when 204
165
+ return true
166
+ when 400
167
+ raise BadSettingsException.new(response.content)
123
168
  end
124
169
  end
125
170
 
126
- def get_global_settings_json
127
- nexus['service/local/global_settings/current'].get(:accept => "application/json")
128
- end
129
-
130
171
  def reset_global_settings
131
- default_json = nexus['service/local/global_settings/default'].get(:accept => "application/json")
132
- nexus['service/local/global_settings/current'].put(default_json, :content_type => "application/json")
172
+ response = nexus.get(nexus_url("service/local/global_settings/default"), :header => DEFAULT_ACCEPT_HEADER)
173
+ case response.status
174
+ when 200
175
+ default_json = response.content
176
+ else
177
+ raise UnexpectedStatusCodeException.new(response.status)
178
+ end
179
+
180
+ response = nexus.put(nexus_url("service/local/global_settings/current"), :body => default_json, :header => DEFAULT_CONTENT_TYPE_HEADER)
181
+ case response.status
182
+ when 204
183
+ return true
184
+ else
185
+ raise UnexpectedStatusCodeException.new(response.status)
186
+ end
133
187
  end
134
188
 
135
189
  def create_repository(name, proxy, url)
136
190
  json = if proxy
137
- create_proxy_repository_json(name, url)
138
- else
139
- create_hosted_repository_json(name)
140
- end
141
- nexus['service/local/repositories'].post(json, :content_type => "application/json") do |response|
142
- case response.code
143
- when 400
144
- raise CreateRepsitoryException.new(response.body)
145
- when 201
146
- return true
147
- else
148
- raise UnexpectedStatusCodeException.new(response.code)
149
- end
191
+ create_proxy_repository_json(name, url)
192
+ else
193
+ create_hosted_repository_json(name)
194
+ end
195
+ response = nexus.post(nexus_url("service/local/repositories"), :body => json, :header => DEFAULT_CONTENT_TYPE_HEADER)
196
+ case response.status
197
+ when 201
198
+ return true
199
+ when 400
200
+ raise CreateRepsitoryException.new(response.content)
201
+ else
202
+ raise UnexpectedStatusCodeException.new(response.status)
150
203
  end
151
204
  end
152
205
 
153
206
  def delete_repository(name)
154
- nexus["service/local/repositories/#{name.downcase}"].delete do |response|
155
- case response.code
156
- when 404
157
- raise RepositoryDoesNotExistException
158
- when 204
159
- return true
160
- else
161
- raise UnexpectedStatusCodeException.new(response.code)
162
- end
207
+ response = nexus.delete(nexus_url("service/local/repositories/#{name.downcase}"))
208
+ case response.status
209
+ when 204
210
+ return true
211
+ when 404
212
+ raise RepositoryDoesNotExistException
213
+ else
214
+ raise UnexpectedStatusCodeException.new(response.status)
163
215
  end
164
216
  end
165
217
 
166
218
  def get_repository_info(name)
167
- begin
168
- nexus["service/local/repositories/#{name.gsub(" ", "_").downcase}"].get
169
- rescue Errno::ECONNREFUSED => e
170
- raise CouldNotConnectToNexusException
171
- rescue RestClient::ResourceNotFound => e
219
+ response = nexus.get(nexus_url("service/local/repositories/#{name.gsub(" ", "_").downcase}"))
220
+ case response.status
221
+ when 200
222
+ return response.content
223
+ when 404
172
224
  raise RepositoryNotFoundException
225
+ when 503
226
+ raise CouldNotConnectToNexusException
227
+ else
228
+ raise UnexpectedStatusCodeException.new(response.status)
173
229
  end
174
230
  end
175
231
 
176
232
  def get_users
177
- nexus["service/local/users"].get
233
+ response = nexus.get(nexus_url("service/local/users"))
234
+ case response.status
235
+ when 200
236
+ return response.content
237
+ else
238
+ raise UnexpectedStatusCodeException.new(response.status)
239
+ end
178
240
  end
179
241
 
180
242
  def create_user(params)
181
- nexus["service/local/users"].post(create_user_json(params), :content_type => "application/json") do |response|
182
- case response.code
183
- when 201
184
- return true
185
- when 400
186
- raise CreateUserException.new(response.body)
187
- else
188
- raise UnexpectedStatusCodeException.new(reponse.code)
189
- end
243
+ response = nexus.post(nexus_url("service/local/users"), :body => create_user_json(params), :header => DEFAULT_CONTENT_TYPE_HEADER)
244
+ case response.status
245
+ when 201
246
+ return true
247
+ when 400
248
+ raise CreateUserException.new(response.content)
249
+ else
250
+ raise UnexpectedStatusCodeException.new(reponse.code)
190
251
  end
191
252
  end
192
253
 
@@ -198,116 +259,136 @@ module NexusCli
198
259
  params.each do |key, value|
199
260
  modified_json.gsub!("$..#{key}"){|v| value} unless key == "userId" || value.blank?
200
261
  end
201
-
202
- nexus["service/local/users/#{params[:userId]}"].put(JSON.dump(modified_json.to_hash), :content_type => "application/json") do |response|
203
- case response.code
204
- when 200
205
- return true
206
- when 400
207
- raise UpdateUserException.new(response.body)
208
- else
209
- raise UnexpectedStatusCodeException.new(response.code)
210
- end
262
+
263
+ response = nexus.put(nexus_url("service/local/users/#{params[:userId]}"), :body => JSON.dump(modified_json.to_hash), :header => DEFAULT_CONTENT_TYPE_HEADER)
264
+ case response.status
265
+ when 200
266
+ return true
267
+ when 400
268
+ raise UpdateUserException.new(response.content)
269
+ else
270
+ raise UnexpectedStatusCodeException.new(response.status)
211
271
  end
212
272
  end
213
273
 
214
274
  def get_user(user)
215
- nexus["service/local/users/#{user}"].get(:accept => "application/json") do |response|
216
- case response.code
217
- when 200
218
- return JSON.parse(response.body)
219
- when 404
220
- raise UserNotFoundException.new(user)
221
- else
222
- raise UnexpectedStatusCodeException.new(response.code)
223
- end
275
+ response = nexus.get(nexus_url("service/local/users/#{user}"), :header => DEFAULT_ACCEPT_HEADER)
276
+ case response.status
277
+ when 200
278
+ return JSON.parse(response.content)
279
+ when 404
280
+ raise UserNotFoundException.new(user)
281
+ else
282
+ raise UnexpectedStatusCodeException.new(response.status)
224
283
  end
225
284
  end
226
285
 
227
286
  def change_password(params)
228
- nexus["service/local/users_changepw"].post(create_change_password_json(params), :content_type => "application/json") do |response|
229
- case response.code
230
- when 202
231
- return true
232
- when 400
233
- raise InvalidCredentialsException
234
- end
287
+ response = nexus.post(nexus_url("service/local/users_changepw"), :body => create_change_password_json(params), :header => DEFAULT_CONTENT_TYPE_HEADER)
288
+ case response.status
289
+ when 202
290
+ return true
291
+ when 400
292
+ raise InvalidCredentialsException
293
+ else
294
+ raise UnexpectedStatusCodeException.new(response.status)
235
295
  end
236
296
  end
237
297
 
238
298
  def delete_user(user_id)
239
- nexus["service/local/users/#{user_id}"].delete do |response|
240
- case response.code
241
- when 204
242
- return true
243
- when 404
244
- raise UserNotFoundException.new(user_id)
245
- else
246
- raise UnexpectedStatusCodeException.new(response.code)
247
- end
299
+ response = nexus.delete(nexus_url("service/local/users/#{user_id}"))
300
+ case response.status
301
+ when 204
302
+ return true
303
+ when 404
304
+ raise UserNotFoundException.new(user_id)
305
+ else
306
+ raise UnexpectedStatusCodeException.new(response.status)
248
307
  end
249
308
  end
250
309
 
251
- def running_nexus_pro?
252
- status['edition_long'] == "Professional"
310
+ def get_logging_info
311
+ response = nexus.get(nexus_url("service/local/log/config"), :header => DEFAULT_ACCEPT_HEADER)
312
+ case response.status
313
+ when 200
314
+ return response.content
315
+ else
316
+ raise UnexpectedStatusCodeException.new(response.status)
317
+ end
318
+ end
319
+
320
+ def set_logger_level(level)
321
+ raise InvalidLoggingLevelException unless ["INFO", "DEBUG", "ERROR"].include?(level.upcase)
322
+ response = nexus.put(nexus_url("service/local/log/config"), :body => create_logger_level_json(level), :header => DEFAULT_CONTENT_TYPE_HEADER)
323
+ case response.status
324
+ when 200
325
+ return true
326
+ else
327
+ raise UnexpectedStatusCodeException.new(response.status)
328
+ end
253
329
  end
254
330
 
255
331
  private
256
332
 
257
- def format_search_results(doc, group_id, artifact_id)
258
- versions = doc.xpath("//version").inject([]) {|array,node| array << "#{node.content()}"}
259
- indent_size = versions.max{|a,b| a.length <=> b.length}.size+4
260
- formated_results = ['Found Versions:']
261
- versions.inject(formated_results) do |array,version|
262
- temp_version = version + ":"
263
- array << "#{temp_version.ljust(indent_size)} `nexus-cli pull #{group_id}:#{artifact_id}:#{version}:tgz`"
264
- end
333
+ def format_search_results(doc, group_id, artifact_id)
334
+ versions = doc.xpath("//version").inject([]) {|array,node| array << "#{node.content()}"}
335
+ indent_size = versions.max{|a,b| a.length <=> b.length}.size+4
336
+ formated_results = ['Found Versions:']
337
+ versions.inject(formated_results) do |array,version|
338
+ temp_version = version + ":"
339
+ array << "#{temp_version.ljust(indent_size)} `nexus-cli pull #{group_id}:#{artifact_id}:#{version}:tgz`"
265
340
  end
341
+ end
266
342
 
267
- def parse_artifact_string(artifact)
268
- split_artifact = artifact.split(":")
269
- if(split_artifact.size < 4)
270
- raise ArtifactMalformedException
271
- end
272
- group_id, artifact_id, version, extension = split_artifact
273
- version.upcase! if version.casecmp("latest")
274
- return group_id, artifact_id, version, extension
343
+ def parse_artifact_string(artifact)
344
+ split_artifact = artifact.split(":")
345
+ if(split_artifact.size < 4)
346
+ raise ArtifactMalformedException
275
347
  end
348
+ group_id, artifact_id, version, extension = split_artifact
349
+ version.upcase! if version.casecmp("latest")
350
+ return group_id, artifact_id, version, extension
351
+ end
276
352
 
277
- def create_hosted_repository_json(name)
278
- params = {:provider => "maven2"}
279
- params[:providerRole] = "org.sonatype.nexus.proxy.repository.Repository"
280
- params[:exposed] = true
281
- params[:repoType] = "hosted"
282
- params[:repoPolicy] = "RELEASE"
283
- params[:name] = name
284
- params[:id] = name.gsub(" ", "_").downcase
285
- params[:format] = "maven2"
286
- JSON.dump(:data => params)
287
- end
353
+ def create_hosted_repository_json(name)
354
+ params = {:provider => "maven2"}
355
+ params[:providerRole] = "org.sonatype.nexus.proxy.repository.Repository"
356
+ params[:exposed] = true
357
+ params[:repoType] = "hosted"
358
+ params[:repoPolicy] = "RELEASE"
359
+ params[:name] = name
360
+ params[:id] = name.gsub(" ", "_").downcase
361
+ params[:format] = "maven2"
362
+ JSON.dump(:data => params)
363
+ end
288
364
 
289
- def create_proxy_repository_json(name, url)
290
- params = {:provider => "maven2"}
291
- params[:providerRole] = "org.sonatype.nexus.proxy.repository.Repository"
292
- params[:exposed] = true
293
- params[:repoType] = "proxy"
294
- params[:repoPolicy] = "RELEASE"
295
- params[:checksumPolicy] = "WARN"
296
- params[:writePolicy] = "READ_ONLY"
297
- params[:downloadRemoteIndexes] = true
298
- params[:autoBlockActive] = true
299
- params[:name] = name
300
- params[:id] = name.gsub(" ", "_").downcase
301
- params[:remoteStorage] = {:remoteStorageUrl => url.nil? ? "http://change-me.com/" : url}
302
- JSON.dump(:data => params)
303
- end
365
+ def create_proxy_repository_json(name, url)
366
+ params = {:provider => "maven2"}
367
+ params[:providerRole] = "org.sonatype.nexus.proxy.repository.Repository"
368
+ params[:exposed] = true
369
+ params[:repoType] = "proxy"
370
+ params[:repoPolicy] = "RELEASE"
371
+ params[:checksumPolicy] = "WARN"
372
+ params[:writePolicy] = "READ_ONLY"
373
+ params[:downloadRemoteIndexes] = true
374
+ params[:autoBlockActive] = true
375
+ params[:name] = name
376
+ params[:id] = name.gsub(" ", "_").downcase
377
+ params[:remoteStorage] = {:remoteStorageUrl => url.nil? ? "http://change-me.com/" : url}
378
+ JSON.dump(:data => params)
379
+ end
304
380
 
305
- def create_user_json(params)
306
- JSON.dump(:data => params)
307
- end
381
+ def create_user_json(params)
382
+ JSON.dump(:data => params)
383
+ end
308
384
 
309
- def create_change_password_json(params)
310
- JSON.dump(:data => params)
311
- end
385
+ def create_change_password_json(params)
386
+ JSON.dump(:data => params)
387
+ end
388
+
389
+ def create_logger_level_json(level)
390
+ params = {:rootLoggerLevel => level.upcase}
391
+ JSON.dump(:data => params)
392
+ end
312
393
  end
313
394
  end