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

Already on GitHub? Sign in to your account

Filtering categories in collections #35

Merged
merged 2 commits into from Jan 3, 2013
Jump to file or symbol
Failed to load files and symbols.
+197 −94
Split
View
@@ -47,7 +47,7 @@ if options.interactive
Occi::Log.debug "Checking for endpoint and auth changes ..."
options.endpoint = ask("What endpoint should I use? ") {
- |q| q.default = options.endpoint
+ |q| q.default = options.endpoint
}
# separate menus
@@ -70,7 +70,7 @@ if options.auth[:password].nil? or options.auth[:user_cert_password].nil? or opt
say("\n")
options.auth[:user_cert_password] = ask("Enter a password or an auth. token: ") {
- |q| q.echo = false
+ |q| q.echo = false
} unless options.auth[:type] == "none"
options.auth[:token] = options.auth[:password] = options.auth[:user_cert_password]
@@ -121,12 +121,14 @@ if options.dump_model
exit!
end
+ collection = model.get options.filter
+
# iterate through available instance variables
- model.instance_variables.each do |inst_var_sym|
+ collection.instance_variables.each do |inst_var_sym|
puts "#"*79
puts "Dumping #{inst_var_sym.to_s}:"
- inst_var = model.instance_variable_get(inst_var_sym)
+ inst_var = collection.instance_variable_get(inst_var_sym)
next unless inst_var.respond_to? :each
# iterate through collection elements
@@ -195,7 +197,7 @@ begin
}
# describe action requires a resource type or a resource location
- menu.choice(:describe) {
+ menu.choice(:describe) {
options.action = :describe
# separate menus from each other
@@ -260,7 +262,7 @@ begin
if options.action == :create
options.resource_title = ask("What name should I give to the new resource? ")
number_of_mixins = ask("How many mixins do you wish me to mix into this resource? ",
- Integer) { |q| q.in = 0..2 }
+ Integer) { |q| q.in = 0..2 }
options.mixin = {}
(1..number_of_mixins).each do |mixin_number|
@@ -336,47 +338,47 @@ begin
# call the appropriate helper and then format its output
case options.action
- when :list
- found = helper_list options
+ when :list
+ found = helper_list options
- valid = Occi::Bin::ResourceOutputFactory.allowed_resource_types.include? options.resource.to_sym
- Occi::Log.error "Not printing, the resource type is not supported!" unless valid
+ valid = Occi::Bin::ResourceOutputFactory.allowed_resource_types.include? options.resource.to_sym
+ Occi::Log.error "Not printing, the resource type is not supported!" unless valid
- puts output.format(found, :locations, options.resource.to_sym) if valid
- when :describe
- found = helper_describe options
+ puts output.format(found, :locations, options.resource.to_sym) if valid
+ when :describe
+ found = helper_describe options
- if options.resource.start_with? options.endpoint
- resource_type = options.resource.split("/")[3].to_sym
- elsif mixin_types.include? options.resource.split('#').first
- resource_type = options.resource.split('#').first.to_sym
- else
- resource_type = options.resource.to_sym
- end
+ if options.resource.start_with? options.endpoint
+ resource_type = options.resource.split("/")[3].to_sym
+ elsif mixin_types.include? options.resource.split('#').first
+ resource_type = options.resource.split('#').first.to_sym
+ else
+ resource_type = options.resource.to_sym
+ end
+
+ valid = Occi::Bin::ResourceOutputFactory.allowed_resource_types.include? resource_type
+ Occi::Log.error "Not printing, the resource type is not supported!" unless valid
- valid = Occi::Bin::ResourceOutputFactory.allowed_resource_types.include? resource_type
- Occi::Log.error "Not printing, the resource type is not supported!" unless valid
-
- puts output.format(found, :resources, resource_type) if valid
- when :create
- location = helper_create options
- puts location
- when :delete
- result = helper_delete options
-
- if result
- puts "Resource #{options.resource} successfully removed!"
+ puts output.format(found, :resources, resource_type) if valid
+ when :create
+ location = helper_create options
+ puts location
+ when :delete
+ result = helper_delete options
+
+ if result
+ puts "Resource #{options.resource} successfully removed!"
+ else
+ puts "Failed to remove resource #{options.resource}!"
+ end
+ when :trigger
+ helper_trigger options
+ when :refresh
+ refresh
+ when :skip
+ Occi::Log.info "Skipping this action, probably not implemented yet!"
else
- puts "Failed to remove resource #{options.resource}!"
- end
- when :trigger
- helper_trigger options
- when :refresh
- refresh
- when :skip
- Occi::Log.info "Skipping this action, probably not implemented yet!"
- else
- raise "Unknown action [#{options.action}]!"
+ raise "Unknown action [#{options.action}]!"
end
rescue Errno::ECONNREFUSED
@@ -388,7 +390,7 @@ rescue Exception => ex
# something went wrong during the execution
# hide the stack trace in non-debug modes
Occi::Log.error "An error occurred! Message: #{ex.message}"
-
+
raise ex if options.debug
exit!
end while options.interactive
@@ -142,6 +142,45 @@ def get_resource(resource_type)
end
+ # Retrieves all entity type identifiers related to a given type identifier
+ #
+ # @example
+ # client.get_entity_type_identifiers_related_to 'network'
+ # # => [ "http://schemas.ogf.org/occi/infrastructure#network",
+ # # "http://schemas.ogf.org/occi/infrastructure#ipnetwork" ]
+ #
+ # @param [String] type_identifier
+ # @return [Array<String>] list of available entity type identifiers related to given type identifier in a human-readable format
+ def get_entity_types_related_to(type_identifier)
+ Occi::Log.debug("Getting entity type identifiers related to #{type_identifier}")
+ collection = @model.get type_identifier
+ collection.kinds.collect { |kind| kind.type_identifier }
+ end
+
+ # Retrieves all available entity types.
+ #
+ # @example
+ # client.get_entity_types # => [ "compute", "storage", "network" ]
+ #
+ # @return [Array<String>] list of available entity types in a human-readable format
+ def get_entity_types
+ Occi::Log.debug("Getting entity types ...")
+ @model.kinds.collect { |kind| kind.term }
+ end
+
+ # Retrieves all available entity type identifiers.
+ #
+ # @example
+ # client.get_entity_type_identifiers
+ # # => [ "http://schemas.ogf.org/occi/infrastructure#compute",
+ # # "http://schemas.ogf.org/occi/infrastructure#storage",
+ # # "http://schemas.ogf.org/occi/infrastructure#network" ]
+ #
+ # @return [Array<String>] list of available entity types in a OCCI ID format
+ def get_entity_type_identifiers
+ get_entity_types_related_to Occi::Core::Entity.kind.type_identifier
+ end
+
# Retrieves all available resource types.
#
# @example
@@ -150,25 +189,50 @@ def get_resource(resource_type)
# @return [Array<String>] list of available resource types in a human-readable format
def get_resource_types
Occi::Log.debug("Getting resource types ...")
- @model.kinds.collect { |kind| kind.term }
+ collection = @model.get Occi::Core::Resource.kind
+ collection.kinds.collect { |kind| kind.term }
end
# Retrieves all available resource type identifiers.
#
# @example
- # client.get_resource_type_identifiers
+ # client.get_resource_type_identifiers
# # => [ "http://schemas.ogf.org/occi/infrastructure#compute",
# # "http://schemas.ogf.org/occi/infrastructure#storage",
# # "http://schemas.ogf.org/occi/infrastructure#network" ]
#
# @return [Array<String>] list of available resource types in a Occi ID format
def get_resource_type_identifiers
- Occi::Log.debug("Getting resource identifiers ...")
- @model.kinds.collect { |kind| kind.type_identifier }
+ get_entity_types_related_to Occi::Core::Resource.kind.type_identifier
+
+ end
+
+ # Retrieves all available link types.
+ #
+ # @example
+ # client.get_link_types # => [ "storagelink", "networkinterface" ]
+ #
+ # @return [Array<String>] list of available link types in a human-readable format
+ def get_link_types
+ Occi::Log.debug("Getting link types ...")
+ collection = @model.get Occi::Core::Link.kind
+ collection.kinds.collect { |kind| kind.term }
+ end
+
+ # Retrieves all available link type identifiers.
+ #
+ # @example
+ # client.get_link_type_identifiers
+ # # => [ "http://schemas.ogf.org/occi/infrastructure#storagelink",
+ # # "http://schemas.ogf.org/occi/infrastructure#networkinterface" ]
+ #
+ # @return [Array<String>] list of available link types in a OCCI ID format
+ def get_link_type_identifiers
+ get_entity_types_related_to Occi::Core::Link.kind.type_identifier
end
# Looks up a mixin using its name and, optionally, a type as well.
- # Will return mixin's full location (a link) or a description.
+ # Will return mixin's full location (a link) or a description.
#
# @example
# client.find_mixin "debian6"
@@ -286,7 +350,7 @@ def get_mixin_types
# @example
# client.get_mixin_type_identifiers
# # => ['http://schemas.ogf.org/occi/infrastructure#os_tpl',
- # # 'http://schemas.ogf.org/occi/infrastructure#resource_tpl']
+ # # 'http://schemas.ogf.org/occi/infrastructure#resource_tpl']
#
# @return [Array<String>] list of available mixin type identifiers
def get_mixin_type_identifiers
@@ -345,7 +409,7 @@ def list(resource_type_identifier=nil)
# Retrieves descriptions for available resources specified by a type
# identifier or resource location. If no type identifier or location
# is specified, all available resources in all available resource types
- # will be described.
+ # will be described.
#
# @example
# client.describe
@@ -397,7 +461,7 @@ def describe(resource_type_identifier=nil)
# Creates a new resource on the server. Resource must be provided
# as an instance of Occi::Core::Entity, e.g. instantiated using
- # the get_resource method.
+ # the get_resource method.
#
# @example
# res = client.get_resource "compute"
@@ -477,7 +541,7 @@ def deploy(location)
# @example
# client.delete "compute" # => true
# client.delete "http://schemas.ogf.org/occi/infrastructure#compute" # => true
- # client.delete "http://localhost:3300/compute/245j42594...98s9df8s9f" # => true
+ # client.delete "http://localhost:3300/compute/245j42594...98s9df8s9f" # => true
#
# @param [String] resource type identifier, type name or location
# @return [Boolean] status
@@ -552,7 +616,7 @@ def set_logger(log_options)
end
# Sets auth method and appropriate httparty attributes. Supported auth methods
- # are: ["basic", "digest", "x509", "none"]
+ # are: ["basic", "digest", "x509", "none"]
#
# @example
# change_auth { :type => "none" }
@@ -601,7 +665,7 @@ def change_auth(auth_options)
# certs_to_file_ary "~/.globus/usercert.pem"
# # => [#<String>, #<String>, ...]
#
- # @param [String] Path to a PEM file containing certificates
+ # @param [String] Path to a PEM file containing certificates
# @return [Array<String>] An array of read certificates
def certs_to_file_ary(ca_file)
# TODO: read and separate multiple certificates
@@ -756,7 +820,7 @@ def del(path, filter=nil)
# @param [String] type identifier of the linked resource
# @param [Occi::Core::Attributes] link attributes
# @param [Array<String>] link mixins
- # @return [Occi::Core::Link] link instance
+ # @return [Occi::Core::Link] link instance
def link(kind, source, target_location, target_kind, attributes=Occi::Core::Attributes.new, mixins=[])
link = Occi::Core::Link.new(kind)
link.mixins = mixins
@@ -774,10 +838,10 @@ def link(kind, source, target_location, target_kind, attributes=Occi::Core::Attr
# Checks whether the given endpoint URI is valid and adds a trailing
# slash if necessary.
#
- # @example
+ # @example
# prepare_endpoint "http://localhost:3300" # => "http://localhost:3300/"
#
- # @param [String] endpoint URI in a non-canonical string
+ # @param [String] endpoint URI in a non-canonical string
# @return [String] canonical endpoint URI in a string, with a trailing slash
def prepare_endpoint(endpoint)
raise 'Endpoint not a valid URI' if (endpoint =~ URI::ABS_URI).nil?
@@ -891,7 +955,7 @@ def set_media_type
end
end
- # Generates a human-readable response message based on the HTTP response code.
+ # Generates a human-readable response message based on the HTTP response code.
#
# @example
# response_message self.class.delete(@endpoint + path)
@@ -23,6 +23,7 @@ def self.parse(args)
options.log[:out] = STDERR
options.log[:level] = Occi::Log::WARN
+ options.filter = nil
options.dump_model = false
options.interactive = false
@@ -94,6 +95,11 @@ def self.parse(args)
options.auth[:ca_file] = ca_file
end
+ opts.on("-F",
+ "--filter CATEGORY", String, "Category type identifier to filter categories from model. Must be used together with the -m option") do |filter|
+ options.filter = filter
+ end
+
opts.on("-x",
"--user-cred PATH",
String,
Oops, something went wrong.