Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for capabilities #109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :rubygems
source "https://rubygems.org"

gemspec

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.0
4.1.1
3 changes: 2 additions & 1 deletion lib/nexus_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module NexusCli
DEFAULT_ACCEPT_HEADER = {
"Accept" => "application/json"
}.freeze

DEFAULT_CONTENT_TYPE_HEADER = {
"Content-Type" => "application/json"
}.freeze
Expand All @@ -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'
Expand Down
31 changes: 29 additions & 2 deletions lib/nexus_cli/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def message
end
status_code(127)
end

class NexusHTTP404 < NexusCliError
def initialize(body)
@server_response = body
Expand All @@ -256,4 +256,31 @@ def message
end
status_code(128)
end
end


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
89 changes: 89 additions & 0 deletions lib/nexus_cli/mixins/capability_actions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'json'

module NexusCli
# @author Jonathan Morley <[email protected]>
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
48 changes: 24 additions & 24 deletions lib/nexus_cli/mixins/repository_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
module NexusCli
# @author Kyle Allan <[email protected]>
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
Expand All @@ -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)}"))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)}"))
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
end
3 changes: 2 additions & 1 deletion lib/nexus_cli/remote/oss_remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class OSSRemote < BaseRemote
include GlobalSettingsActions
include LoggingActions
include RepositoryActions
include CapabilityActions
include UserActions
end
end
end
31 changes: 31 additions & 0 deletions lib/nexus_cli/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion nexus_cli.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down