Revision c70d5593

View differences:

src/cli/one_helper/onegroup_helper.rb
158 158

  
159 159
        group_hash = group.to_hash
160 160

  
161
        OneQuotaHelper.format_quota(group_hash['GROUP'])
161
        system = System.new(@client)
162
        default_quotas = system.get_group_quotas()
163

  
164
        if OpenNebula::is_error?(default_quotas)
165
            # TODO: cannot do 
166
            # return -1, default_quotas.message
167
            # because OneHelper::show_resource ignores this method's output
168

  
169
            default_quotas = nil
170
        end
171

  
172
        helper = OneQuotaHelper.new
173
        helper.format_quota(group_hash['GROUP'], default_quotas)
162 174
    end
163 175
end
src/cli/one_helper/onequota_helper.rb
20 20

  
21 21
    EDITOR_PATH='/usr/bin/vi'
22 22

  
23
    #---------------------------------------------------------------------------
24
    #  Tables to format user quotas
25
    #---------------------------------------------------------------------------
26
    TABLE_DS = CLIHelper::ShowTable.new(nil, self) do
27
        column :"DATASTORE ID", "", :size=>12 do |d|
28
            d["ID"] if !d.nil?
29
        end
30

  
31
        column :"IMAGES", "", :right, :size=>20 do |d|
32
            "%8d / %8d" % [d["IMAGES_USED"], d["IMAGES"]] if !d.nil?
33
        end
34

  
35
        column :"SIZE", "", :right, :size=>19 do |d|
36
            "%8s / %8s" % [OpenNebulaHelper.unit_to_str(d["SIZE_USED"].to_i,{},"M"),
37
                OpenNebulaHelper.unit_to_str(d["SIZE"].to_i,{},"M")] if !d.nil?
38
        end
39
    end
40

  
41
    TABLE_NET = CLIHelper::ShowTable.new(nil, self) do
42
        column :"NETWORK ID", "", :size=>12 do |d|
43
            d["ID"] if !d.nil?
44
        end
45

  
46
        column :"LEASES", "", :right, :size=>20 do |d|
47
            "%8d / %8d" % [d["LEASES_USED"], d["LEASES"]] if !d.nil?
48
        end
49
    end
50

  
51
    TABLE_VM = CLIHelper::ShowTable.new(nil, self) do
52

  
53
        column :"NUMBER OF VMS", "", :right, :size=>20 do |d|
54
            "%8d / %8d" % [d["VMS_USED"], d["VMS"]] if !d.nil?
55
        end
56

  
57
        column :"MEMORY", "", :right, :size=>20 do |d|
58
            "%8s / %8s" % [OpenNebulaHelper.unit_to_str(d["MEMORY_USED"].to_i,{},"M"),
59
                OpenNebulaHelper.unit_to_str(d["MEMORY"].to_i,{},"M")] if !d.nil?
60
        end
61

  
62
        column :"CPU", "", :right, :size=>20 do |d|
63
            "%8.2f / %8.2f" % [d["CPU_USED"], d["CPU"]] if !d.nil?
64
        end
65
    end
66

  
67
    TABLE_IMG = CLIHelper::ShowTable.new(nil, self) do
68
        column :"IMAGE ID", "", :size=>12 do |d|
69
            d["ID"] if !d.nil?
70
        end
71

  
72
        column :"RUNNING VMS", "", :right, :size=>20 do |d|
73
            "%8d / %8d" % [d["RVMS_USED"], d["RVMS"]] if !d.nil?
74
        end
75
    end
76

  
77 23
    HELP_QUOTA = <<-EOT.unindent
78 24
        #-----------------------------------------------------------------------
79 25
        # Supported quota limits:
......
217 163

  
218 164
    #  Outputs formated quota information to stdout
219 165
    #  @param qh [Hash] with the quotas for a given resource
166
    #  @param default_quotas_hash [XMLElement] with the default quota limits
220 167
    #
221
    def self.format_quota(qh)
168
    def format_quota(qh, default_quotas)
222 169
        str_h1="%-80s"
223 170

  
224 171
        puts
......
227 174

  
228 175
        puts
229 176

  
177
        @default_quotas = default_quotas
178

  
230 179
        vm_quotas = [qh['VM_QUOTA']['VM']].flatten
180

  
231 181
        if !vm_quotas[0].nil?
232
            TABLE_VM.show(vm_quotas, {})
182
            CLIHelper::ShowTable.new(nil, self) do
183
                column :"NUMBER OF VMS", "", :right, :size=>20 do |d|
184
                    if !d.nil?
185
                        elem = 'VMS'
186
                        limit = d[elem]
187
                        limit = helper.get_default_limit(
188
                            limit, "VM_QUOTA/VM/#{elem}")
189

  
190
                        "%8d / %8d" % [d["VMS_USED"], limit]
191
                    end
192
                end
193

  
194
                column :"MEMORY", "", :right, :size=>20 do |d|
195
                    if !d.nil?
196
                        elem = 'MEMORY'
197
                        limit = d[elem]
198
                        limit = helper.get_default_limit(
199
                            limit, "VM_QUOTA/VM/#{elem}")
200

  
201
                        "%8s / %8s" % [
202
                            OpenNebulaHelper.unit_to_str(d["MEMORY_USED"].to_i,{},"M"),
203
                            OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")
204
                        ]
205
                    end
206
                end
207

  
208
                column :"CPU", "", :right, :size=>20 do |d|
209
                    if !d.nil?
210
                        elem = 'CPU'
211
                        limit = d[elem]
212
                        limit = helper.get_default_limit(
213
                            limit, "VM_QUOTA/VM/#{elem}")
214

  
215
                        "%8.2f / %8.2f" % [d["CPU_USED"], limit]
216
                    end
217
                end
218
            end.show(vm_quotas, {})
219

  
233 220
            puts
234 221
        end
235 222

  
236 223
        ds_quotas = [qh['DATASTORE_QUOTA']['DATASTORE']].flatten
224

  
237 225
        if !ds_quotas[0].nil?
238
            TABLE_DS.show(ds_quotas, {})
226
            CLIHelper::ShowTable.new(nil, self) do
227
                column :"DATASTORE ID", "", :size=>12 do |d|
228
                    d["ID"] if !d.nil?
229
                end
230

  
231
                column :"IMAGES", "", :right, :size=>20 do |d|
232
                    if !d.nil?
233
                        elem = 'IMAGES'
234
                        limit = d[elem]
235
                        limit = helper.get_default_limit(
236
                            limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}")
237

  
238
                        "%8d / %8d" % [d["IMAGES_USED"], limit]
239
                    end
240
                end
241

  
242
                column :"SIZE", "", :right, :size=>19 do |d|
243
                    if !d.nil?
244
                        elem = 'SIZE'
245
                        limit = d[elem]
246
                        limit = helper.get_default_limit(
247
                            limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}")
248

  
249
                        "%8s / %8s" % [
250
                            OpenNebulaHelper.unit_to_str(d["SIZE_USED"].to_i,{},"M"),
251
                            OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")
252
                        ]
253
                    end
254
                end
255
            end.show(ds_quotas, {})
256

  
239 257
            puts
240 258
        end
241 259

  
242 260
        net_quotas = [qh['NETWORK_QUOTA']['NETWORK']].flatten
261

  
243 262
        if !net_quotas[0].nil?
244
            TABLE_NET.show(net_quotas, {})
263
            CLIHelper::ShowTable.new(nil, self) do
264
                column :"NETWORK ID", "", :size=>12 do |d|
265
                    d["ID"] if !d.nil?
266
                end
267

  
268
                column :"LEASES", "", :right, :size=>20 do |d|
269
                    if !d.nil?
270
                        elem = 'LEASES'
271
                        limit = d[elem]
272
                        limit = helper.get_default_limit(
273
                            limit, "NETWORK_QUOTA/NETWORK[ID=#{d['ID']}]/#{elem}")
274

  
275
                        "%8d / %8d" % [d["LEASES_USED"], limit]
276
                    end
277
                end
278
            end.show(net_quotas, {})
279

  
245 280
            puts
246 281
        end
247 282

  
248 283
        image_quotas = [qh['IMAGE_QUOTA']['IMAGE']].flatten
284

  
249 285
        if !image_quotas[0].nil?
250
            TABLE_IMG.show(image_quotas, {})
286
            CLIHelper::ShowTable.new(nil, self) do
287
                column :"IMAGE ID", "", :size=>12 do |d|
288
                    d["ID"] if !d.nil?
289
                end
290

  
291
                column :"RUNNING VMS", "", :right, :size=>20 do |d|
292
                    if !d.nil?
293
                        elem = 'RVMS'
294
                        limit = d[elem]
295
                        limit = helper.get_default_limit(
296
                            limit, "IMAGE_QUOTA/IMAGE[ID=#{d['ID']}]/RVMS")
297

  
298
                        "%8d / %8d" % [d["RVMS_USED"], limit]
299
                    end
300
                end
301
            end.show(image_quotas, {})
251 302
        end
252 303
    end
304

  
305
    def get_default_limit(limit, xpath)
306
        if limit == "-1"
307
            if !@default_quotas.nil?
308
                limit = @default_quotas[xpath]
309

  
310
                limit = "0" if limit.nil? || limit == ""
311
            else
312
                limit = "0"
313
            end
314
        end
315

  
316
        return limit
317
    end
253 318
end
src/cli/one_helper/oneuser_helper.rb
265 265

  
266 266
        user_hash = user.to_hash
267 267

  
268
        OneQuotaHelper.format_quota(user_hash['USER'])
268
        system = System.new(@client)
269
        default_quotas = system.get_user_quotas()
270

  
271
        if OpenNebula::is_error?(default_quotas)
272
            # TODO: cannot do 
273
            # return -1, default_quotas.message
274
            # because OneHelper::show_resource ignores this method's output
275

  
276
            default_quotas = nil
277
        end
278

  
279
        helper = OneQuotaHelper.new
280
        helper.format_quota(user_hash['USER'], default_quotas)
269 281
    end
270 282
end

Also available in: Unified diff