Class: OpenNebula::User

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

Overview

Extensions for OpenNebula::User class

Defined Under Namespace

Classes: ShowbackError, UserNotExistsError

Instance Method Summary collapse

Instance Method Details

#admin?Boolean Also known as: is_admin?, is_admin

Note:

Admin means user is a part of oneadmin group

Checks if user is admin

Returns:

  • (Boolean)


133
134
135
136
137
138
# File 'service/objects/user.rb', line 133

def admin?
  info!
  (groups << gid).include? 0
rescue
  false
end

#alertObject

Returns true if user balance is less than user alert point



36
37
38
39
# File 'service/objects/user.rb', line 36

def alert
  alert_at = (info! || self['TEMPLATE/ALERT']) || IONe::Settings['ALERT']
  return balance.to_f <= alert_at.to_f, alert_at.to_f
end

#alert=(at) ⇒ Object

Sets users alert point



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

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

#balanceObject

Returns users actual balance



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

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

#balance=(num) ⇒ Object

Sets users balance

Parameters:

  • num (Integer)


31
32
33
# File 'service/objects/user.rb', line 31

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

#billable_vnsObject

Returns users VNets being billed



61
62
63
# File 'service/objects/user.rb', line 61

def billable_vns
  AR.where(owner: @pe_id, etime: nil).all
end

#calculate_networking_showback(stime_req, etime_req) ⇒ Hash

Calculates VNs Showback

Parameters:

  • stime_req (Integer)
    • Point from which calculation starts(timestamp)

  • etime_req (Integer)
    • Point at which calculation stops(timestamp)

Returns:

Raises:



69
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
101
102
103
104
105
106
107
108
109
# File 'service/objects/user.rb', line 69

def calculate_networking_showback stime_req, etime_req
  raise ShowbackError, ["Wrong Time-period given", stime_req, etime_req] if stime_req >= etime_req

  info!

  vnp = AR.where(owner: @pe_id).where { (etime == nil) | (etime >= etime_req) }.all

  vnp.inject({ 'TOTAL' => 0 }) do | showback, rec |
    first = rec.stime

    stime, etime = stime_req, etime_req

    stime = rec.stime if rec.stime > stime
    etime = rec.etime if !rec.etime.nil? && rec.etime < etime

    stime   = Time.at(stime).to_datetime
    current = Time.at(first).to_datetime
    etime   = Time.at(etime).to_datetime

    while current < stime do
      current = current >> 1
    end

    periods = 0
    while current <= etime do
      periods += 1
      current = current >> 1
    end

    ip, r = onblock(:vn, rec.vnid).ar_record(rec.arid, periods)

    if showback[ip].nil? then
      showback[ip] = r
    else
      showback[ip] += r
    end

    showback['TOTAL'] += r
    showback
  end
end

#exists?Boolean

Checks if user exists

Returns:

  • (Boolean)


112
113
114
# File 'service/objects/user.rb', line 112

def exists?
  info! == nil
end

#langObject

Returns User sunstone language



117
118
119
# File 'service/objects/user.rb', line 117

def lang
  info! || self['//LANG']
end

#lang=(lang) ⇒ Object

Sets User sunstone language

Parameters:

  • l (String)
    • lang code, like en_US/ru_RU/etc



123
124
125
126
127
128
# File 'service/objects/user.rb', line 123

def lang= lang
  sunstone = to_hash!['USER']['TEMPLATE']['SUNSTONE']
  sunstone['LANG'] = lang

  update({ "SUNSTONE" => sunstone }.to_one_template, true)
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)}\",
      MEMORY=\"#{(spec['ram'].to_i + quota['MEMORY_USED'].to_i)}\",
      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

#vns(db) ⇒ Array<OpenNebula::VirtualNetwork>

Returns users VNets objects array



56
57
58
# File 'service/objects/user.rb', line 56

def vns db
  db[:network_pool].where(uid: @pe_id).select(:oid).to_a.map { |row| onblock(:vn, row[:oid]) { |vn| vn.info! || vn } }
end