1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-20 10:50:08 +03:00

Feature #1304: Add generic Document & DocumentPool classes to Ruby OCA

This commit is contained in:
Carlos Martín 2012-06-08 18:37:45 +02:00
parent 3b8b36dbf0
commit e196843c9b
4 changed files with 356 additions and 31 deletions

View File

@ -994,6 +994,8 @@ RUBY_OPENNEBULA_LIB_FILES="src/oca/ruby/OpenNebula/Host.rb \
src/oca/ruby/OpenNebula/ImagePool.rb \
src/oca/ruby/OpenNebula/Template.rb \
src/oca/ruby/OpenNebula/TemplatePool.rb \
src/oca/ruby/OpenNebula/Document.rb \
src/oca/ruby/OpenNebula/DocumentPool.rb \
src/oca/ruby/OpenNebula/Group.rb \
src/oca/ruby/OpenNebula/GroupPool.rb \
src/oca/ruby/OpenNebula/Acl.rb \

View File

@ -0,0 +1,214 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'OpenNebula/Pool'
module OpenNebula
# All subclasses must define the Document::TYPE constant.
#
# @example
# require 'OpenNebula/Document'
#
# module OpenNebula
# class CustomObject < Document
#
# Document::TYPE = 400
#
# end
# end
class Document < PoolElement
#######################################################################
# Constants and Class Methods
#######################################################################
DOCUMENT_METHODS = {
:allocate => "document.allocate",
:delete => "document.delete",
:info => "document.info",
:update => "document.update",
:chown => "document.chown",
:chmod => "document.chmod",
:clone => "document.clone",
}
# Creates a Document Object description with just its identifier
# this method should be used to create plain Document objects.
# @param [Integer] pe_id the id of the object
#
# @return [Nokogiri::XML::Node, REXML::Element] the empty xml
def Document.build_xml(pe_id=nil)
if pe_id
obj_xml = "<DOCUMENT><ID>#{pe_id}</ID></DOCUMENT>"
else
obj_xml = "<DOCUMENT></DOCUMENT>"
end
XMLElement.build_xml(obj_xml,'DOCUMENT')
end
# Class constructor
#
# @param [Nokogiri::XML::Node, REXML::Element] xml string
# created by the build_xml() method
# @param [OpenNebula::Client] client the xml-rpc client
#
# @return [Document] the new object
#
# @example
# doc = Document.new(Document.build_xml(3),rpc_client)
def initialize(xml, client)
super(xml,client)
end
#######################################################################
# XML-RPC Methods for the Document Object
#######################################################################
# Retrieves the information of the given Document.
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def info()
super(DOCUMENT_METHODS[:info], 'DOCUMENT', TYPE)
end
# Allocates a new Document in OpenNebula
#
# @param description [String] The contents of the Document.
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def allocate(description)
super(DOCUMENT_METHODS[:allocate], description, TYPE)
end
# Deletes the Document
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def delete()
return call(DOCUMENT_METHODS[:delete], @pe_id, TYPE)
end
# Replaces the template contents
#
# @param [String] new_template new template contents
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def update(new_template)
super(DOCUMENT_METHODS[:update], new_template, TYPE)
end
# Publishes the Document, to be used by other users
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def publish
set_publish(true)
end
# Unplubishes the Document
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def unpublish
set_publish(false)
end
# Changes the owner/group
#
# @param [Integer] uid the new owner id. Set to -1 to leave the current one
# @param [Integer] gid the new group id. Set to -1 to leave the current one
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chown(uid, gid)
super(DOCUMENT_METHODS[:chown], uid, gid, TYPE)
end
# Changes the Document permissions.
#
# @param octet [String] Permissions octed , e.g. 640
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chmod_octet(octet)
super(DOCUMENT_METHODS[:chmod], octet, TYPE)
end
# Changes the Document permissions.
# Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
other_m, other_a)
super(DOCUMENT_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
group_m, group_a, other_u, other_m, other_a, TYPE)
end
# Clones this Document into a new one
#
# @param name [String] Name for the new Document.
#
# @return [Integer, OpenNebula::Error] The new Document ID in case
# of success, Error otherwise
def clone(name)
return Error.new('ID not defined') if !@pe_id
rc = @client.call(DOCUMENT_METHODS[:clone], @pe_id, name, TYPE)
return rc
end
#######################################################################
# Helpers to get Document information
#######################################################################
# Returns the group identifier
# @return [Integer] the element's group ID
def gid
self['GID'].to_i
end
# Returns the owner user ID
# @return [Integer] the element's owner user ID
def owner_id
self['UID'].to_i
end
# Returns true if the GROUP_U permission bit is set
# @return [true, false] true if the GROUP_U permission bit is set
def public?
if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
true
else
false
end
end
private
def set_publish(published)
group_u = published ? 1 : 0
chmod(-1, -1, -1, group_u, -1, -1, -1, -1, -1)
end
end
end

View File

@ -0,0 +1,93 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'OpenNebula/Pool'
module OpenNebula
# All subclasses must define the DocumentPool::TYPE constant
# and the factory method.
#
# @example
# require 'OpenNebula/DocumentPool'
#
# module OpenNebula
# class CustomObjectPool < DocumentPool
#
# DocumentPool::TYPE = 400
#
# def factory(element_xml)
# OpenNebula::CustomObject.new(element_xml, @client)
# end
# end
# end
class DocumentPool < Pool
#######################################################################
# Constants and Class attribute accessors
#######################################################################
DOCUMENT_POOL_METHODS = {
:info => "documentpool.info"
}
#######################################################################
# Class constructor & Pool Methods
#######################################################################
# Class constructor
#
# @param [OpenNebula::Client] client the xml-rpc client
# @param [Integer] user_id the filter flag, see
# http://opennebula.org/documentation:rel3.6:api
#
# @return [DocumentPool] the new object
def initialize(client, user_id=-1)
super('DOCUMENT_POOL','DOCUMENT',client)
@user_id = user_id
end
#######################################################################
# XML-RPC Methods for the Document Object
#######################################################################
# Retrieves all or part of the Documents in the pool.
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def info(*args)
case args.size
when 0
info_filter(DOCUMENT_POOL_METHODS[:info],@user_id,-1,-1, TYPE)
when 3
info_filter(DOCUMENT_POOL_METHODS[:info],args[0],args[1],args[2], TYPE)
end
end
def info_all()
return super(DOCUMENT_POOL_METHODS[:info], TYPE)
end
def info_mine()
return super(DOCUMENT_POOL_METHODS[:info], TYPE)
end
def info_group()
return super(DOCUMENT_POOL_METHODS[:info], TYPE)
end
end
end

View File

@ -54,20 +54,20 @@ module OpenNebula
return xmlrpc_info(xml_method)
end
def info_all(xml_method)
return xmlrpc_info(xml_method,INFO_ALL,-1,-1)
def info_all(xml_method, *args)
return xmlrpc_info(xml_method,INFO_ALL,-1,-1, *args)
end
def info_mine(xml_method)
return xmlrpc_info(xml_method,INFO_MINE,-1,-1)
def info_mine(xml_method, *args)
return xmlrpc_info(xml_method,INFO_MINE,-1,-1, *args)
end
def info_group(xml_method)
return xmlrpc_info(xml_method,INFO_GROUP,-1,-1)
def info_group(xml_method, *args)
return xmlrpc_info(xml_method,INFO_GROUP,-1,-1, *args)
end
def info_filter(xml_method, who, start_id, end_id)
return xmlrpc_info(xml_method,who, start_id, end_id)
def info_filter(xml_method, who, start_id, end_id, *args)
return xmlrpc_info(xml_method,who, start_id, end_id, *args)
end
# Retrieves the monitoring data for all the Objects in the pool
@ -199,13 +199,14 @@ module OpenNebula
#
# @param [String] xml_method the name of the XML-RPC method
# @param [String] root_element Base XML element name
# @param [Array] args additional arguments
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def info(xml_method, root_element)
def info(xml_method, root_element, *args)
return Error.new('ID not defined') if !@pe_id
rc = @client.call(xml_method, @pe_id)
rc = @client.call(xml_method, @pe_id, *args)
if !OpenNebula.is_error?(rc)
initialize_xml(rc, root_element)
@ -220,10 +221,12 @@ module OpenNebula
# Calls to the corresponding allocate method to create a new element
# in the OpenNebula core
# xml_method:: _String_ the name of the XML-RPC method
# args:: _Array_ additional arguments including the template for the
# new element
# [return] nil in case of success or an Error object
#
# @param [String] xml_method the name of the XML-RPC method
# @param [Array] args any extra arguments for the xml-rpc method
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def allocate(xml_method, *args)
rc = @client.call(xml_method, *args)
@ -237,31 +240,42 @@ module OpenNebula
# Calls to the corresponding update method to modify
# the object's template
# xml_method:: _String_ the name of the XML-RPC method
# new_template:: _String_ the new template contents
# [return] nil in case of success or an Error object
def update(xml_method, new_template)
#
# @param [String] xml_method the name of the XML-RPC method
# @param [String] new_template the new template contents
# @param [Array] args any extra arguments for the xml-rpc method
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def update(xml_method, new_template, *args)
new_template ||= template_xml
return call(xml_method, @pe_id, new_template)
return call(xml_method, @pe_id, new_template, *args)
end
# Calls to the corresponding delete method to remove this element
# from the OpenNebula core
# xml_method:: _String_ the name of the XML-RPC method
# [return] nil in case of success or an Error object
#
# @param [String] xml_method the name of the XML-RPC method
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def delete(xml_method)
return call(xml_method,@pe_id)
end
# Calls to the corresponding chown method to modify
# the object's owner and group
# xml_method:: _String_ the name of the XML-RPC method
# uid:: _Integer_ the new owner id. Set to -1 to leave the current one
# gid:: _Integer_ the new group id. Set to -1 to leave the current one
# [return] nil in case of success or an Error object
def chown(xml_method, uid, gid)
return call(xml_method, @pe_id, uid, gid)
#
# @param [String] xml_method the name of the XML-RPC method
# @param [Integer] uid the new owner id. Set to -1 to leave the current one
# @param [Integer] gid the new goup id. Set to -1 to leave the current one
# @param [Array] args any extra arguments for the xml-rpc method
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chown(xml_method, uid, gid, *args)
return call(xml_method, @pe_id, uid, gid, *args)
end
# Calls to the corresponding chmod method to modify
@ -269,9 +283,10 @@ module OpenNebula
#
# @param xml_method [String] the name of the XML-RPC method
# @param octet [String] Permissions octed , e.g. 640
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chmod_octet(xml_method, octet)
def chmod_octet(xml_method, octet, *args)
owner_u = octet[0..0].to_i & 4 != 0 ? 1 : 0
owner_m = octet[0..0].to_i & 2 != 0 ? 1 : 0
owner_a = octet[0..0].to_i & 1 != 0 ? 1 : 0
@ -283,7 +298,7 @@ module OpenNebula
other_a = octet[2..2].to_i & 1 != 0 ? 1 : 0
chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
other_m, other_a)
other_m, other_a, *args)
end
# Calls to the corresponding chmod method to modify
@ -291,13 +306,14 @@ module OpenNebula
# Each [Integer] parameter must be 1 to allow, 0 deny, -1 do not change
#
# @param xml_method [String] the name of the XML-RPC method
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def chmod(xml_method, owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
other_m, other_a)
other_m, other_a, *args)
return call(xml_method, @pe_id, owner_u, owner_m,
owner_a, group_u, group_m, group_a, other_u,
other_m, other_a)
other_m, other_a, *args)
end