diff --git a/Gemfile b/Gemfile index 5964f19..8515c1d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source :rubygems +source "https://rubygems.org" gemspec diff --git a/VERSION b/VERSION index 99eba4d..627a3f4 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.1.0 \ No newline at end of file +4.1.1 diff --git a/lib/nexus_cli.rb b/lib/nexus_cli.rb index fa9c374..10a487f 100755 --- a/lib/nexus_cli.rb +++ b/lib/nexus_cli.rb @@ -9,7 +9,7 @@ module NexusCli DEFAULT_ACCEPT_HEADER = { "Accept" => "application/json" }.freeze - + DEFAULT_CONTENT_TYPE_HEADER = { "Content-Type" => "application/json" }.freeze @@ -28,6 +28,7 @@ module NexusCli autoload :GlobalSettingsActions, 'nexus_cli/mixins/global_settings_actions' autoload :UserActions, 'nexus_cli/mixins/user_actions' autoload :RepositoryActions, 'nexus_cli/mixins/repository_actions' + autoload :CapabilityActions, 'nexus_cli/mixins/capability_actions' autoload :LoggingActions, 'nexus_cli/mixins/logging_actions' autoload :CustomMetadataActions, 'nexus_cli/mixins/pro/custom_metadata_actions' autoload :SmartProxyActions, 'nexus_cli/mixins/pro/smart_proxy_actions' diff --git a/lib/nexus_cli/errors.rb b/lib/nexus_cli/errors.rb index 25066e7..b45276b 100644 --- a/lib/nexus_cli/errors.rb +++ b/lib/nexus_cli/errors.rb @@ -244,7 +244,7 @@ def message end status_code(127) end - + class NexusHTTP404 < NexusCliError def initialize(body) @server_response = body @@ -256,4 +256,31 @@ def message end status_code(128) end -end \ No newline at end of file + + + class CreateCapabilityException < NexusCliError + def initialize(body) + @server_response = JSON.pretty_generate(JSON.parse(body)) + end + + def message + %{Your create capability command failed due to the following: +#{@server_response}} + end + status_code(129) + end + + class CapabilityDoesNotExistException < NexusCliError + def message + "The capability you are trying to delete does not exist." + end + status_code(130) + end + + class CapabilityNotFoundException < NexusCliError + def message + "The capability you provided could not be found. Please ensure the capability exists." + end + status_code(131) + end +end diff --git a/lib/nexus_cli/mixins/capability_actions.rb b/lib/nexus_cli/mixins/capability_actions.rb new file mode 100644 index 0000000..9649e5e --- /dev/null +++ b/lib/nexus_cli/mixins/capability_actions.rb @@ -0,0 +1,89 @@ +require 'json' + +module NexusCli + # @author Jonathan Morley + module CapabilityActions + + # Creates a capability that Nexus uses. + # + # @param type [String] the typeId of the capability to create + # @param enabled [Boolean] true if this capability is enabled + # @param properties [Hash] hash of the properties for the capability + # + # @return [Int] returns id of the on success + def create_capability(type, enabled, properties) + json = create_capability_json(type, enabled, properties) + response = nexus.post(nexus_url("service/siesta/capabilities"), :body => json, :header => DEFAULT_CONTENT_TYPE_HEADER) + case response.status + when 200 + return JSON.parse(response.content)["capability"]["id"] + when 400 + raise CreateCapabilityException.new(response.content) + else + raise UnexpectedStatusCodeException.new(response.status) + end + end + + # Deletes the given capability + # + # @param id [Int] the id of the capability to delete. + # + # @return [Boolean] true if the capability is deleted, false otherwise. + def delete_capability(id) + response = nexus.delete(nexus_url("service/siesta/capabilities/#{id}")) + case response.status + when 204 + return true + when 404 + raise CapabilityDoesNotExistException + else + raise UnexpectedStatusCodeException.new(response.status) + end + end + + # Find information about the capability with the given [id]. + # + # @param id [Int] the id of the capability. + # + # @return [Hash] A Ruby hash with information about the desired capability. + def get_capability_info(id) + response = nexus.get(nexus_url("service/siesta/capabilities/#{id}"), :header => DEFAULT_ACCEPT_HEADER) + case response.status + when 200 + return JSON.parse(response.content) + when 404 + raise CapabilityNotFoundException + when 503 + raise CouldNotConnectToNexusException + else + raise UnexpectedStatusCodeException.new(response.status) + end + end + + # Get information about all capabilities. + # + # @return [Hash] A Ruby hash with information about all capabilities. + def get_capabilities_info() + response = nexus.get(nexus_url("service/siesta/capabilities"), :header => DEFAULT_ACCEPT_HEADER) + case response.status + when 200 + return JSON.parse(response.content) + when 503 + raise CouldNotConnectToNexusException + else + raise UnexpectedStatusCodeException.new(response.status) + end + end + + private + + def create_capability_json(type, enabled, properties) + params = { + :typeId => type, + :enabled => enabled.nil? ? true : enabled, + :properties => properties.collect{|k,v| {:key => k, :value => v} } + } + JSON.dump(params) + end + end +end diff --git a/lib/nexus_cli/mixins/repository_actions.rb b/lib/nexus_cli/mixins/repository_actions.rb index ad1b743..c3691ed 100644 --- a/lib/nexus_cli/mixins/repository_actions.rb +++ b/lib/nexus_cli/mixins/repository_actions.rb @@ -3,16 +3,16 @@ module NexusCli # @author Kyle Allan module RepositoryActions - + # Creates a repository that the Nexus uses to hold artifacts. - # + # # @param name [String] the name of the repository to create # @param proxy [Boolean] true if this is a proxy repository # @param url [String] the url for the proxy repository to point to - # @param id [String] the id of repository + # @param id [String] the id of repository # @param policy [String] repository policy (RELEASE|SNAPSHOT) # @param provider [String] repo provider (maven2 by default) - # + # # @return [Boolean] returns true on success def create_repository(name, proxy, url, id, policy, provider) json = if proxy @@ -32,10 +32,10 @@ def create_repository(name, proxy, url, id, policy, provider) end # Deletes the given repository - # + # # @param name [String] the name of the repositroy to delete, transformed # into an id. - # + # # @return [Boolean] true if the repository is deleted, false otherwise. def delete_repository(name) response = nexus.delete(nexus_url("service/local/repositories/#{sanitize_for_id(name)}")) @@ -51,10 +51,10 @@ def delete_repository(name) # Find information about the repository with the given # [name]. - # + # # @param name [String] the name of the repository, transformed # into an id. - # + # # @return [String] A String of XML with information about the desired # repository. def get_repository_info(name) @@ -72,11 +72,11 @@ def get_repository_info(name) end # Creates a group repository with the given name. - # + # # @param name [String] the name to give the new repository # @param id [String] an alternative id to use for the new repository # @param provider [String] the type of Maven provider for this repository - # + # # @return [Boolean] true if the group repository is created, false otherwise def create_group_repository(name, id, provider) response = nexus.post(nexus_url("service/local/repo_groups"), :body => create_group_repository_json(name, id, provider), :header => DEFAULT_CONTENT_TYPE_HEADER) @@ -92,9 +92,9 @@ def create_group_repository(name, id, provider) # Gets information about the given group repository with # the given [group_id]. - # + # # @param group_id [String] the id of the group repository to find - # + # # @return [String] a JSON String of information about the given group repository def get_group_repository(group_id) response = nexus.get(nexus_url("service/local/repo_groups/#{sanitize_for_id(group_id)}"), :header => DEFAULT_ACCEPT_HEADER) @@ -110,10 +110,10 @@ def get_group_repository(group_id) # Checks if a the given [repository_to_check] is a member # of the given group repository - [group_ip]. - # + # # @param group_id [String] the group repository to look in # @param repository_to_check [String] the repository that might be a member of the group - # + # # @return [Boolean] true if the [repository_to_check] is a member of group repository, false otherwise def repository_in_group?(group_id, repository_to_check) group_repository = JSON.parse(get_group_repository(group_id)) @@ -124,10 +124,10 @@ def repository_in_group?(group_id, repository_to_check) # Adds the given [repository_to_add_id] to the given group repository, # [group_id]. - # + # # @param group_id [String] the group repository to add to # @param repository_to_add_id [String] the repository to added to the group - # + # # @return [Boolean] true if the repository is successfully added, false otherwise def add_to_group_repository(group_id, repository_to_add_id) raise RepositoryInGroupException if repository_in_group?(group_id, repository_to_add_id) @@ -144,10 +144,10 @@ def add_to_group_repository(group_id, repository_to_add_id) # Removes the given [repository_to_remove_id] from the group repository, # [group_id]. - # + # # @param group_id [String] the group repository to remove from # @param repository_to_remove_id [String] the repository to remove from the group - # + # # @return [Boolean] true if the repisotory is successfully remove, false otherwise def remove_from_group_repository(group_id, repository_to_remove_id) raise RepositoryNotInGroupException unless repository_in_group?(group_id, repository_to_remove_id) @@ -161,9 +161,9 @@ def remove_from_group_repository(group_id, repository_to_remove_id) end # Deletes the given group repository. - # + # # @param group_id [String] the group repository to delete - # + # # @return [Boolean] true if the group repository is deleted, false otherwise def delete_group_repository(group_id) response = nexus.delete(nexus_url("service/local/repo_groups/#{sanitize_for_id(group_id)}")) @@ -186,7 +186,7 @@ def create_hosted_repository_json(name, id, policy, provider) params[:browseable] = true params[:indexable] = true params[:repoType] = "hosted" - params[:repoPolicy] = policy.nil? ? "RELEASE" : ["RELEASE", "SNAPSHOT"].include?(policy) ? policy : "RELEASE" + params[:repoPolicy] = policy.nil? ? "RELEASE" : ["RELEASE", "SNAPSHOT"].include?(policy) ? policy : "RELEASE" params[:name] = name params[:id] = id.nil? ? sanitize_for_id(name) : sanitize_for_id(id) params[:format] = "maven2" @@ -200,7 +200,7 @@ def create_proxy_repository_json(name, url, id, policy, provider) params[:browseable] = true params[:indexable] = true params[:repoType] = "proxy" - params[:repoPolicy] = policy.nil? ? "RELEASE" : ["RELEASE", "SNAPSHOT"].include?(policy) ? policy : "RELEASE" + params[:repoPolicy] = policy.nil? ? "RELEASE" : ["RELEASE", "SNAPSHOT"].include?(policy) ? policy : "RELEASE" params[:checksumPolicy] = "WARN" params[:writePolicy] = "READ_ONLY" params[:downloadRemoteIndexes] = true @@ -235,11 +235,11 @@ def create_remove_from_group_repository_json(group_id, repository_to_remove_id) repositories = group_repository_json["data"]["repositories"] repositories.delete(repository_in_group?(group_id, repository_to_remove_id)) - + params = {:repositories => repositories} params[:id] = group_repository_json["data"]["id"] params[:name] = group_repository_json["data"]["name"] JSON.dump(:data => params) end end -end \ No newline at end of file +end diff --git a/lib/nexus_cli/remote/oss_remote.rb b/lib/nexus_cli/remote/oss_remote.rb index bdbe6a8..22e2ccc 100644 --- a/lib/nexus_cli/remote/oss_remote.rb +++ b/lib/nexus_cli/remote/oss_remote.rb @@ -6,6 +6,7 @@ class OSSRemote < BaseRemote include GlobalSettingsActions include LoggingActions include RepositoryActions + include CapabilityActions include UserActions end -end \ No newline at end of file +end diff --git a/lib/nexus_cli/tasks.rb b/lib/nexus_cli/tasks.rb index 97dc9d7..72bc341 100755 --- a/lib/nexus_cli/tasks.rb +++ b/lib/nexus_cli/tasks.rb @@ -161,6 +161,37 @@ def get_repository_info(name) say nexus_remote.get_repository_info(name), :green end + method_option :enabled, + :type => :boolean, + :desc => "Whether the capability is enabled or not, true by default." + method_option :properties, + :type => :hash, + :desc => "Json array of properties to use for the capability" + desc "create_capability type", "Creates a new capability with the provided type." + def create_capability(type) + id = nexus_remote.create_capability(type, options[:enabled], options[:properties]) + if id + say "A new Capability with an id of #{id} has been created.", :blue + end + end + + desc "delete_capability id", "Deletes a capability with the provided id." + def delete_capability(id) + if nexus_remote.delete_capability(id) + say "The Repository named #{id} has been deleted.", :blue + end + end + + desc "get_capability_info id", "Finds and returns information about the provided capability." + def get_capability_info(id) + say nexus_remote.get_capability_info(id), :green + end + + desc "get_capability_info id", "Finds and returns information about the provided capability." + def get_capabilities_info() + say nexus_remote.get_capabilities_info(), :green + end + desc "get_users", "Returns XML representing the users in Nexus." def get_users say nexus_remote.get_users, :green diff --git a/nexus_cli.gemspec b/nexus_cli.gemspec index 0257f2f..4cb3f39 100644 --- a/nexus_cli.gemspec +++ b/nexus_cli.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |s| s.add_dependency 'highline' s.add_dependency 'jsonpath' s.add_runtime_dependency 'chozo', '>= 0.6.0' - s.add_runtime_dependency 'activesupport', '~> 3.2.0' + s.add_runtime_dependency 'activesupport', '~> 4.0.0' s.add_development_dependency 'rspec' s.add_development_dependency 'aruba', "= 0.5.0"