Class: OpenNebula::User

Inherits:
Object
  • Object
show all
Defined in:
service/objects/user.rb

Overview

OpenNebula::User class

Defined Under Namespace

Classes: UserNotExistsError

Instance Method Summary collapse

Instance Method Details

#alertObject

Returns true if user balance is less than user alert point



38
39
40
41
# File 'service/objects/user.rb', line 38

def alert
    alert_at = (info! || self['TEMPLATE/ALERT']) || $db[:settings].where(:name => 'ALERT').to_a.last[:body]
    return balance.to_f <= alert_at.to_f, alert_at.to_f
end

#alert=(at) ⇒ Object

Sets users alert point



43
44
45
# File 'service/objects/user.rb', line 43

def alert= at
    update("ALERT = #{at}", true)
end

#balanceObject

Returns users actual balance



29
30
31
# File 'service/objects/user.rb', line 29

def balance
    info! || self['TEMPLATE/BALANCE']
end

#balance=(num) ⇒ Object

Sets users balance

Parameters:

  • num (Fixnum)


34
35
36
# File 'service/objects/user.rb', line 34

def balance= num
    update("BALANCE = #{num}", true)
end

#exists?Boolean

Checks if user exists

Returns:

  • (Boolean)


54
55
56
# File 'service/objects/user.rb', line 54

def exists?
    self.info! == nil
end

#name!Object

Returns users actual name



24
25
26
27
# File 'service/objects/user.rb', line 24

def name!
    info!
    name
end

#update_quota_by_vm(spec = {}) ⇒ Object

Note:

Method sets quota to 'used' values by default

Sets user quota by his existing VMs and/or appends new vm specs to it

Parameters:

  • spec (Hash) (defaults to: {})

Options Hash (spec):

  • 'append' (Boolean)

    Set it true if you wish to append specs

  • 'cpu' (Integer | String)

    CPU quota limit to append

  • 'ram' (Integer | String)

    RAM quota limit to append

Returns:

  • nil



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'service/objects/user.rb', line 10

def update_quota_by_vm(spec = {})
    quota = self.to_hash!['USER']['VM_QUOTA']['VM']
    if quota.nil? then
        quota = Hash.new
    end
    self.set_quota(
        "VM=[
            CPU=\"#{(spec['cpu'].to_i + quota['CPU_USED'].to_i).to_s}\", 
            MEMORY=\"#{(spec['ram'].to_i + quota['MEMORY_USED'].to_i).to_s}\", 
            SYSTEM_DISK_SIZE=\"#{spec['drive'].to_i + quota['SYSTEM_DISK_SIZE_USED'].to_i}\", 
            VMS=\"#{spec['append'].nil? ? quota['VMS_USED'].to_s : (quota['VMS_USED'].to_i + 1).to_s}\" ]"
    )
end

#vms(db) ⇒ Array<OpenNebula::VirtualMachine>

Returns users VMs objects array



48
49
50
51
52
# File 'service/objects/user.rb', line 48

def vms db
    db[:vm_pool].where(uid: @pe_id).exclude(:state => 6).select(:oid).to_a.map { |row| onblock(:vm, row[:oid]){|vm| vm.info! || vm} }
rescue
    nil
end