4 # Copyright (C) 2010 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """User-id pool related functions.
24 The user-id pool is cluster-wide configuration option.
25 It is stored as a list of user-id ranges.
26 This module contains functions used for manipulating the
27 user-id pool parameter and for requesting/returning user-ids
32 from ganeti import errors
33 from ganeti import constants
34 from ganeti import utils
37 def ParseUidPool(value, separator=None):
38 """Parse a user-id pool definition.
40 @param value: string representation of the user-id pool.
41 The accepted input format is a list of integer ranges.
42 The boundaries are inclusive.
43 Example: '1000-5000,8000,9000-9010'.
44 @param separator: the separator character between the uids/uid-ranges.
46 @return: a list of integer pairs (lower, higher range boundaries)
53 for range_def in value.split(separator):
57 boundaries = range_def.split("-")
58 n_elements = len(boundaries)
60 raise errors.OpPrereqError(
61 "Invalid user-id range definition. Only one hyphen allowed: %s"
64 lower = int(boundaries[0])
65 except (ValueError, TypeError), err:
66 raise errors.OpPrereqError("Invalid user-id value for lower boundary of"
68 % str(err), errors.ECODE_INVAL)
70 higher = int(boundaries[n_elements - 1])
71 except (ValueError, TypeError), err:
72 raise errors.OpPrereqError("Invalid user-id value for higher boundary of"
74 % str(err), errors.ECODE_INVAL)
76 ranges.append((lower, higher))
82 def AddToUidPool(uid_pool, add_uids):
83 """Add a list of user-ids/user-id ranges to a user-id pool.
85 @param uid_pool: a user-id pool (list of integer tuples)
86 @param add_uids: user-id ranges to be added to the pool
87 (list of integer tuples)
90 for uid_range in add_uids:
91 if uid_range not in uid_pool:
92 uid_pool.append(uid_range)
96 def RemoveFromUidPool(uid_pool, remove_uids):
97 """Remove a list of user-ids/user-id ranges from a user-id pool.
99 @param uid_pool: a user-id pool (list of integer tuples)
100 @param remove_uids: user-id ranges to be removed from the pool
101 (list of integer tuples)
104 for uid_range in remove_uids:
105 if uid_range not in uid_pool:
106 raise errors.OpPrereqError(
107 "User-id range to be removed is not found in the current"
108 " user-id pool: %s" % uid_range, errors.ECODE_INVAL)
109 uid_pool.remove(uid_range)
112 def _FormatUidRange(lower, higher):
113 """Convert a user-id range definition into a string.
118 return "%s-%s" % (lower, higher)
121 def FormatUidPool(uid_pool):
122 """Convert the internal representation of the user-id pool into a string.
124 The output format is also accepted by ParseUidPool()
126 @param uid_pool: a list of integer pairs representing UID ranges
127 @return: a string with the formatted results
130 return utils.CommaJoin([_FormatUidRange(lower, higher)
131 for lower, higher in uid_pool])
134 def CheckUidPool(uid_pool):
135 """Sanity check user-id pool range definition values.
137 @param uid_pool: a list of integer pairs (lower, higher range boundaries)
140 for lower, higher in uid_pool:
142 raise errors.OpPrereqError(
143 "Lower user-id range boundary value (%s)"
144 " is larger than higher boundary value (%s)" %
145 (lower, higher), errors.ECODE_INVAL)
146 if lower < constants.UIDPOOL_UID_MIN:
147 raise errors.OpPrereqError(
148 "Lower user-id range boundary value (%s)"
149 " is smaller than UIDPOOL_UID_MIN (%s)." %
150 (lower, constants.UIDPOOL_UID_MIN),
152 if higher > constants.UIDPOOL_UID_MAX:
153 raise errors.OpPrereqError(
154 "Higher user-id boundary value (%s)"
155 " is larger than UIDPOOL_UID_MAX (%s)." %
156 (higher, constants.UIDPOOL_UID_MAX),
160 def ExpandUidPool(uid_pool):
161 """Expands a uid-pool definition to a list of uids.
163 @param uid_pool: a list of integer pairs (lower, higher range boundaries)
164 @return: a list of integers
168 for lower, higher in uid_pool:
169 uids.update(range(lower, higher + 1))