Class: OpenNebula::Group

Inherits:
PoolElement show all
Defined in:
lib/opennebula/group.rb

Constant Summary collapse

GROUP_METHODS =

Constants and Class Methods

{
    :info           => "group.info",
    :allocate       => "group.allocate",
    :update         => "group.update",
    :delete         => "group.delete",
    :quota          => "group.quota",
    :add_admin      => "group.addadmin",
    :del_admin      => "group.deladmin",
}
SELF =

Flag for requesting connected user’s group info

-1
GROUP_DEFAULT_ACLS =

Default resource ACL’s for group users (create)

"VM+IMAGE+TEMPLATE+DOCUMENT+SECGROUP+VROUTER+VMGROUP+BACKUPJOB"
GROUP_ADMIN_SUNSTONE_VIEWS =

The default view for group and group admins, must be defined in sunstone_views.yaml

"groupadmin"
GROUP_SUNSTONE_VIEWS =
"cloud"

Instance Attribute Summary

Attributes inherited from PoolElement

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PoolElement

#id, new_with_id, #replace, #to_str

Methods inherited from XMLElement

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

Constructor Details

#initialize(xml, client) ⇒ Group

Class constructor



65
66
67
# File 'lib/opennebula/group.rb', line 65

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)


54
55
56
57
58
59
60
61
62
# File 'lib/opennebula/group.rb', line 54

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

#add_admin(user_id) ⇒ nil, OpenNebula::Error

Adds a User to the Group administrators set

Parameters:

  • user_id (Integer)

    User ID

Returns:



256
257
258
# File 'lib/opennebula/group.rb', line 256

def add_admin(user_id)
    return call(GROUP_METHODS[:add_admin], @pe_id, user_id.to_i)
end

#admin_idsObject

Returns an array with the numeric admin user ids



301
302
303
304
305
306
307
# File 'lib/opennebula/group.rb', line 301

def admin_ids
    ids = self.retrieve_elements("ADMINS/ID")

    return [] if ids.nil?

    return ids.collect! {|x| x.to_i}
end

#allocate(groupname) ⇒ Object

Allocates a new Group in OpenNebula

groupname A string containing the name of the Group.



216
217
218
# File 'lib/opennebula/group.rb', line 216

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



274
275
276
277
278
279
280
# File 'lib/opennebula/group.rb', line 274

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

#contains_admin(uid) ⇒ Object

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



283
284
285
286
287
288
289
# File 'lib/opennebula/group.rb', line 283

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

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

#create(group_hash) ⇒ Object

Creates a group based in a group definition hash

group_hash[:name] the group name
group_hash[:group_admin] the admin user definition hash, see def
create_admin_user function description for details.
group_hash[:views] Array of sunstone view names, to be stored
    in SUNSTONE_VIEWS
group_hash[:default_view] Default sunstone view name, to be stored
    in DEFAULT_VIEW
group_hash[:admin_views] Array of sunstone view names, to be stored
    in GROUP_ADMIN_VIEWS
group_hash[:default_admin_view] Default sunstone view name, to be stored
    in DEFAULT_ADMIN_DEFAULT_VIEW


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/opennebula/group.rb', line 94

def create(group_hash)
    # Check arguments
    if !group_hash[:name]
        return OpenNebula::Error.new("Group name not defined")
    end

    if group_hash[:group_admin]
        if group_hash[:group_admin][:name] && !group_hash[:group_admin][:password]
            error_msg = "Admin user password not defined"
            return OpenNebula::Error.new(error_msg)
        end
    end

    # Allocate group
    rc = allocate(group_hash[:name])
    return rc if OpenNebula.is_error?(rc)

    # Set group ACLs to create resources
    rc, msg = create_default_acls(group_hash[:resources])

    if OpenNebula.is_error?(rc)
        delete
        error_msg =  "Error creating group ACL's: #{rc.message}"
        return OpenNebula::Error.new(error_msg)
    end

    # Set group ACLs to share resources
    if group_hash[:shared_resources]
        acls = Array.new
        acls << "@#{id} #{group_hash[:shared_resources]}/@#{id} USE"

        rc, msg = create_group_acls(acls)

        if OpenNebula.is_error?(rc)
            self.delete
            error_msg =  "Error creating group ACL's: #{rc.message}"
            return OpenNebula::Error.new(error_msg)
        end
    end

    # Create associated group admin if needed
    rc = create_admin_user(group_hash)

    if OpenNebula.is_error?(rc)
        delete
        error_msg =  "Error creating admin user: #{rc.message}"
        return OpenNebula::Error.new(error_msg)
    end

    sunstone_attrs = []

    # Add Sunstone views for the group
    if group_hash[:views]
        sunstone_attrs << "VIEWS=\"#{group_hash[:views].join(",")}\""
    else
        sunstone_attrs << "VIEWS=\"#{GROUP_SUNSTONE_VIEWS}\""
    end

    if group_hash[:default_view]
        sunstone_attrs << "DEFAULT_VIEW=\"#{group_hash[:default_view]}\""
    else
        sunstone_attrs << "DEFAULT_VIEW=\"#{GROUP_SUNSTONE_VIEWS}\""
    end

    # And the admin views
    if group_hash[:admin_views]
        sunstone_attrs << "GROUP_ADMIN_VIEWS=\"#{group_hash[:admin_views].join(",")}\""
    else
        sunstone_attrs << "GROUP_ADMIN_VIEWS=#{GROUP_ADMIN_SUNSTONE_VIEWS}"
    end

    if group_hash[:default_admin_view]
        sunstone_attrs << "GROUP_ADMIN_DEFAULT_VIEW=\"#{group_hash[:default_admin_view]}\""
    else
        sunstone_attrs << "GROUP_ADMIN_DEFAULT_VIEW=#{GROUP_ADMIN_SUNSTONE_VIEWS}"
    end

    do_update = false

    if sunstone_attrs.length > 0
        do_update = true

        update_str = "FIREEDGE=[#{sunstone_attrs.join(",\n")}]\n"
    end

    opennebula_attrs = []

    # Persistency attributes for new images
    if group_hash[:opennebula]
        if group_hash[:opennebula][:default_image_persistent]
            opennebula_attrs << "DEFAULT_IMAGE_PERSISTENT=\""\
                "#{group_hash[:opennebula][:default_image_persistent]}\""
        end

        if group_hash[:opennebula][:default_image_persistent_new]
            opennebula_attrs << "DEFAULT_IMAGE_PERSISTENT_NEW=\""\
                "#{group_hash[:opennebula][:default_image_persistent_new]}\""
        end
    end

    if opennebula_attrs.length > 0
        do_update = true

        update_str += "OPENNEBULA=[#{opennebula_attrs.join(",\n")}]\n"
    end

    if do_update
        rc = update(update_str, true)

        if OpenNebula.is_error?(rc)
            delete
            error_msg =  "Error updating group template: #{rc.message}"
            return OpenNebula::Error.new(error_msg)
        end
    end

    return 0
end

#del_admin(user_id) ⇒ nil, OpenNebula::Error

Removes a User from the Group administrators set

Parameters:

  • user_id (Integer)

    User ID

Returns:



265
266
267
# File 'lib/opennebula/group.rb', line 265

def del_admin(user_id)
    return call(GROUP_METHODS[:del_admin], @pe_id, user_id.to_i)
end

#deleteObject

Deletes the Group



233
234
235
# File 'lib/opennebula/group.rb', line 233

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

#infoObject Also known as: info!

Retrieves the information of the given Group.



75
76
77
# File 'lib/opennebula/group.rb', line 75

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

#set_quota(quota) ⇒ nil, OpenNebula::Error

Sets the group quota limits

Parameters:

  • quota (String)

    a template (XML or txt) with the new quota limits

Returns:



242
243
244
245
246
247
248
249
# File 'lib/opennebula/group.rb', line 242

def set_quota(quota)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(GROUP_METHODS[:quota],@pe_id, quota)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#update(new_template = nil, append = false) ⇒ nil, OpenNebula::Error

Replaces the template contents

Parameters:

  • new_template (String) (defaults to: nil)

    New template contents

  • append (true, false) (defaults to: false)

    True to append new attributes instead of replace the whole template

Returns:



228
229
230
# File 'lib/opennebula/group.rb', line 228

def update(new_template=nil, append=false)
    super(GROUP_METHODS[:update], new_template, append ? 1 : 0)
end

#user_idsObject

Returns an array with the numeric user ids



292
293
294
295
296
297
298
# File 'lib/opennebula/group.rb', line 292

def user_ids
    ids = self.retrieve_elements("USERS/ID")

    return [] if ids.nil?

    return ids.collect! {|x| x.to_i}
end