Class: OpenNebula::Group

Inherits:
PoolElement show all
Defined in:
lib/OpenNebula/Group.rb

Constant Summary collapse

GROUP_METHODS =
{
    :info       => "group.info",
    :allocate   => "group.allocate",
    :delete     => "group.delete"
}
SELF =

Flag for requesting connected user’s group info

-1
GROUP_DEFAULT =
"/etc/one/group.default"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PoolElement

#id, #name, new_with_id, #to_str

Methods inherited from XMLElement

#[], #add_element, #attr, #delete_element, #each, #each_xpath, #has_elements?, #initialize_xml, #name, #retrieve_elements, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml

Constructor Details

#initialize(xml, client) ⇒ Group

Class constructor



61
62
63
# File 'lib/OpenNebula/Group.rb', line 61

def initialize(xml, client)
    super(xml,client)
end

Class Method Details

.build_xml(pe_id = nil) ⇒ Object

Creates a Group description with just its identifier this method should be used to create plain Group objects. id the id of the user

Example:

group = Group.new(Group.build_xml(3),rpc_client)


50
51
52
53
54
55
56
57
58
# File 'lib/OpenNebula/Group.rb', line 50

def Group.build_xml(pe_id=nil)
    if pe_id
        group_xml = "<GROUP><ID>#{pe_id}</ID></GROUP>"
    else
        group_xml = "<GROUP></GROUP>"
    end

    XMLElement.build_xml(group_xml,'GROUP')
end

Instance Method Details

#allocate(groupname) ⇒ Object

Allocates a new Group in OpenNebula

groupname A string containing the name of the Group.



114
115
116
# File 'lib/OpenNebula/Group.rb', line 114

def allocate(groupname)
    super(GROUP_METHODS[:allocate], groupname)
end

#contains(uid) ⇒ Object

Returns whether or not the user with id ‘uid’ is part of this group



128
129
130
131
132
133
134
# File 'lib/OpenNebula/Group.rb', line 128

def contains(uid)
    #This doesn't work in ruby 1.8.5
    #return self["USERS/ID[.=#{uid}]"] != nil

    id_array = retrieve_elements('USERS/ID')
    return id_array != nil && id_array.include?(uid.to_s)
end

#create_acls(filename = GROUP_DEFAULT) ⇒ Object

Creates ACLs for the group. The ACL rules are described in a file



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/OpenNebula/Group.rb', line 70

def create_acls(filename = GROUP_DEFAULT)
    if !File.readable?(filename)
        return -1, "Cannot read deafult ACL file for group"
    end

    msg = String.new

    File.open(filename).each_line{ |l|
        next if l.match(/^#/)

        rule  = "@#{@pe_id} #{l}"
        parse = OpenNebula::Acl.parse_rule(rule)

        if OpenNebula.is_error?(parse)
            return -1, "Error parsing rule #{rule}: #{parse.message}"
        end

        xml = OpenNebula::Acl.build_xml
        acl = OpenNebula::Acl.new(xml, @client)

        rc = acl.allocate(*parse)

        if OpenNebula.is_error?(rc)
            return -1, "Error creating rule #{rule}: #{rc.message}"
        else
            msg << "ACL_ID: #{acl.id}\n"
        end
    }

    return 0, msg
end

#deleteObject

Deletes the Group



119
120
121
# File 'lib/OpenNebula/Group.rb', line 119

def delete()
    super(GROUP_METHODS[:delete])
end

#infoObject

Retrieves the information of the given Group.



107
108
109
# File 'lib/OpenNebula/Group.rb', line 107

def info()
    super(GROUP_METHODS[:info], 'GROUP')
end

#user_idsObject

Returns an array with the numeric user ids



137
138
139
140
141
142
143
144
145
# File 'lib/OpenNebula/Group.rb', line 137

def user_ids
    array = Array.new

    self.each("USERS/ID") do |id|
        array << id.text.to_i
    end

    return array
end