OSDN Git Service

Change storage pool allocation max and capacity max get method.
authorkeisuke fukawa <keisuke@karesansui-project.info>
Wed, 21 Apr 2010 07:29:56 +0000 (16:29 +0900)
committerkeisuke fukawa <keisuke@karesansui-project.info>
Wed, 21 Apr 2010 07:29:56 +0000 (16:29 +0900)
bin/create_storage_volume.py
karesansui/gadget/hostby1storagepoolby1storagevolume.py
karesansui/lib/storage.py [deleted file]

index cc3bcee..471d9cf 100755 (executable)
@@ -25,8 +25,8 @@ try:
     from karesansui import __version__
     from karesansui.lib.virt.virt import KaresansuiVirtConnection
     from karesansui.lib.utils import load_locale
-    from karesansui.lib.const import STORAGE_POOL_TYPE, STORAGE_VOLUME_FORMAT, STORAGE_VOLUME_SIZE_MIN_LENGTH
-    from karesansui.lib.storage import get_storage_volume_max_size_by_unit
+    from karesansui.lib.const import STORAGE_POOL_TYPE, STORAGE_VOLUME_FORMAT, \
+        STORAGE_VOLUME_SIZE_MIN_LENGTH, STORAGE_VOLUME_SIZE_MAX_LENGTH, STORAGE_VOLUME_UNIT
 except ImportError:
     print >>sys.stderr, "[Error] karesansui package was not found."
     sys.exit(1)
@@ -44,7 +44,7 @@ def getopts():
                     type="int", help=_('Allocation'), default=0)
     optp.add_option('-c', '--capacity', dest='capacity', type="int",
                     help=_('Capacity'), default=0)
-    optp.add_option('-u', '--unit', dest='unit', help=_('Volume Size Unit'))
+    optp.add_option('-u', '--unit', dest='unit', help=_('Volume Size Unit'), default=None)
     optp.add_option('-o', '--permission_owner', dest='permission_owner', help=_('Permission Owner'))
     optp.add_option('-g', '--permission_group', dest='permission_group', help=_('Permission Group'))
     optp.add_option('-m', '--permission_mode', dest='permission_mode', help=_('Permission Mode'))
@@ -63,13 +63,6 @@ def chkopts(opts):
     if not opts.format in STORAGE_VOLUME_FORMAT.values():
         raise KssCommandOptException('ERROR: Format is not available. '
                                      'raw or qcow2... is available. format=%s' % opts.format)
-
-    storage_volume_max_size = get_storage_volume_max_size_by_unit(opts.pool_name, opts.unit)
-    if opts.allocation < STORAGE_VOLUME_SIZE_MIN_LENGTH or storage_volume_max_size < opts.allocation:
-        raise KssCommandOptException('ERROR: Allocation "%s" is out of available range.' % opts.allocation)
-    if opts.capacity < STORAGE_VOLUME_SIZE_MIN_LENGTH or storage_volume_max_size < opts.capacity:
-        raise KssCommandOptException('ERROR: Capacity "%s" is out of available range.' % opts.capacity)
-
     # TODO permission('s) use
     pass
 
@@ -96,6 +89,20 @@ class CreateStorageVolume(KssCommand):
                         'We already have a storage volume. - pool=%s, vol=%s'
                         % (opts.pool_name, opts.name))
 
+                pool_obj = conn.search_kvn_storage_pools(opts.pool_name)[0]
+                pool_info = pool_obj.get_info()
+                if not pool_info['allocation'] or pool_info['allocation'] == 0:
+                    storage_volume_max_size = STORAGE_VOLUME_SIZE_MAX_LENGTH
+                elif opts.unit is None:
+                    storage_volume_max_size = long(pool_info['allocation'])
+                else:
+                    storage_volume_max_size = long(pool_info['allocation']) / STORAGE_VOLUME_UNIT.get(opts.unit, 1)
+
+                if opts.allocation < STORAGE_VOLUME_SIZE_MIN_LENGTH or storage_volume_max_size < opts.allocation:
+                    raise KssCommandException('Allocation "%s" is out of available range.' % opts.allocation)
+                if opts.capacity < STORAGE_VOLUME_SIZE_MIN_LENGTH or storage_volume_max_size < opts.capacity:
+                    raise KssCommandException('Capacity "%s" is out of available range.' % opts.capacity)
+
                 if conn.create_storage_volume(opts.name, opts.pool_name,
                                               opts.format,
                                               use=opts.use,
index 8cba33e..0ee4d9a 100644 (file)
@@ -20,7 +20,8 @@ from karesansui.lib.rest import Rest, auth
 from karesansui.lib.virt.virt import KaresansuiVirtConnection
 from karesansui.lib.const import VIRT_COMMAND_CREATE_STORAGE_VOLUME, \
      VIRT_COMMAND_DELETE_STORAGE_VOLUME, STORAGE_VOLUME_SIZE_MIN_LENGTH, \
-     STORAGE_VOLUME_SIZE_MAX_LENGTH, STORAGE_VOLUME_FORMAT, STORAGE_VOLUME_PWD
+     STORAGE_VOLUME_SIZE_MAX_LENGTH, STORAGE_VOLUME_FORMAT, STORAGE_VOLUME_PWD, \
+     STORAGE_VOLUME_UNIT
 
 from karesansui.db.access.machine import findbyhost1
 from karesansui.db.access._2pysilhouette import save_job_collaboration
@@ -29,14 +30,13 @@ from karesansui.db.model._2pysilhouette import JobGroup, Job
 
 from pysilhouette.command import dict2command
 
-from karesansui.lib.storage import get_storage_volume_max_size_by_unit
 from karesansui.lib.utils import is_param
 from karesansui.lib.checker import Checker, CHECK_EMPTY, CHECK_ONLYSPACE, CHECK_VALID, \
     CHECK_MIN, CHECK_MAX
 
 
 # validate
-def validates_volume(obj, now_pools):
+def validates_volume(obj, now_pools, pool_info):
     checker = Checker()
     check = True
 
@@ -88,10 +88,12 @@ def validates_volume(obj, now_pools):
 
 
     if is_param(obj.input, 'volume_capacity'):
-        if is_param(obj.input, 'volume_pool_name') and is_param(obj.input, 'volume_unit'):
-            storage_volume_capacity_size_max = get_storage_volume_max_size_by_unit(obj.input.volume_pool_name, obj.input.volume_unit)
-        else:
+        if not pool_info['capacity'] or pool_info['capacity'] == 0:
             storage_volume_capacity_size_max = STORAGE_VOLUME_SIZE_MAX_LENGTH
+        elif is_param(obj.input, 'volume_unit'):
+            storage_volume_capacity_size_max = long(pool_info['capacity']) / STORAGE_VOLUME_UNIT.get(obj.input.volume_unit, 1)
+        else:
+            storage_volume_capacity_size_max = long(pool_info['capacity'])
 
         check = checker.check_number(_('Storagepool Volume Capacity'),
                                      obj.input.volume_capacity,
@@ -104,10 +106,12 @@ def validates_volume(obj, now_pools):
         checker.add_error(_('"%s" is required.') % _('Storagepool Volume Capacity'))
 
     if is_param(obj.input, 'volume_allocation'):
-        if is_param(obj.input, 'volume_pool_name') and is_param(obj.input, 'volume_unit'):
-            storage_volume_allocation_size_max = get_storage_volume_max_size_by_unit(obj.input.volume_pool_name, obj.input.volume_unit)
-        else:
+        if not pool_info['allocation'] or pool_info['allocation'] == 0:
             storage_volume_allocation_size_max = STORAGE_VOLUME_SIZE_MAX_LENGTH
+        elif is_param(obj.input, 'volume_unit'):
+            storage_volume_allocation_size_max = long(pool_info['allocation']) / STORAGE_VOLUME_UNIT.get(obj.input.volume_unit, 1)
+        else:
+            storage_volume_allocation_size_max = long(pool_info['allocation'])
 
         check = checker.check_number(_('Storagepool Volume Allocation'),
                                      obj.input.volume_allocation,
@@ -240,12 +244,13 @@ class HostBy1StoragePoolBy1StorageVolume(Rest):
             pools_obj = kvc.get_storage_pool_UUIDString2kvn_storage_pool(param[1])
             if len(pools_obj) <= 0:
                 return web.notfound()
+            pool_info = pools_obj[0].get_info()
         finally:
             kvc.close()
 
         model = findbyhost1(self.orm, host_id)
 
-        if not validates_volume(self, pools):
+        if not validates_volume(self, pools, pool_info):
             return web.badrequest(self.view.alert)
         extra_opts = {}
         if create_volume_job(self,
diff --git a/karesansui/lib/storage.py b/karesansui/lib/storage.py
deleted file mode 100644 (file)
index 0b73bca..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-#
-# This file is part of Karesansui Core.
-#
-# Copyright (C) 2010 HDE, Inc.
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
-#
-
-
-from karesansui.lib.virt.virt import KaresansuiVirtConnection
-from karesansui.lib.const import STORAGE_VOLUME_SIZE_MAX_LENGTH, STORAGE_VOLUME_UNIT
-
-
-def get_storage_volume_max_size_by_unit(pool_name, unit=None):
-    try:
-        kvc = KaresansuiVirtConnection()
-        pool_obj = kvc.search_kvn_storage_pools(pool_name)[0]
-        pool_info = pool_obj.get_info()
-    finally:
-        kvc.close()
-
-    if not pool_info['allocation'] or pool_info['allocation'] == 0:
-        storage_volume_max_size = STORAGE_VOLUME_SIZE_MAX_LENGTH
-    elif unit is None:
-        storage_volume_max_size = long(pool_info['allocation'])
-    else:
-        storage_volume_max_size = long(pool_info['allocation']) / STORAGE_VOLUME_UNIT.get(unit, 1)
-
-    return storage_volume_max_size
-