|
18 | 18 | package com.cloud.resourcelimit; |
19 | 19 |
|
20 | 20 | import java.util.ArrayList; |
| 21 | +import java.util.EnumMap; |
21 | 22 | import java.util.List; |
22 | 23 | import java.util.Map; |
23 | 24 | import java.util.Set; |
@@ -115,7 +116,9 @@ public class ResourceLimitManagerImpl implements ResourceLimitService, Manager{ |
115 | 116 | protected SearchBuilder<ResourceCountVO> ResourceCountSearch; |
116 | 117 | ScheduledExecutorService _rcExecutor; |
117 | 118 | long _resourceCountCheckInterval=0; |
118 | | - |
| 119 | + Map<ResourceType, Long> accountResourceLimitMap = new EnumMap<ResourceType, Long>(ResourceType.class); |
| 120 | + Map<ResourceType, Long> projectResourceLimitMap = new EnumMap<ResourceType, Long>(ResourceType.class); |
| 121 | + |
119 | 122 | @Override |
120 | 123 | public String getName() { |
121 | 124 | return _name; |
@@ -148,6 +151,19 @@ public boolean configure(final String name, final Map<String, Object> params) th |
148 | 151 | if (_resourceCountCheckInterval > 0){ |
149 | 152 | _rcExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("ResourceCountChecker")); |
150 | 153 | } |
| 154 | + |
| 155 | + projectResourceLimitMap.put(Resource.ResourceType.public_ip, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectPublicIPs.key()))); |
| 156 | + projectResourceLimitMap.put(Resource.ResourceType.snapshot, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectSnapshots.key()))); |
| 157 | + projectResourceLimitMap.put(Resource.ResourceType.template, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectTemplates.key()))); |
| 158 | + projectResourceLimitMap.put(Resource.ResourceType.user_vm, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectUserVms.key()))); |
| 159 | + projectResourceLimitMap.put(Resource.ResourceType.volume, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectVolumes.key()))); |
| 160 | + |
| 161 | + accountResourceLimitMap.put(Resource.ResourceType.public_ip, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountPublicIPs.key()))); |
| 162 | + accountResourceLimitMap.put(Resource.ResourceType.snapshot, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountSnapshots.key()))); |
| 163 | + accountResourceLimitMap.put(Resource.ResourceType.template, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountTemplates.key()))); |
| 164 | + accountResourceLimitMap.put(Resource.ResourceType.user_vm, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountUserVms.key()))); |
| 165 | + accountResourceLimitMap.put(Resource.ResourceType.volume, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountVolumes.key()))); |
| 166 | + |
151 | 167 | return true; |
152 | 168 | } |
153 | 169 |
|
@@ -183,50 +199,24 @@ public void decrementResourceCount(long accountId, ResourceType type, Long... de |
183 | 199 |
|
184 | 200 | @Override |
185 | 201 | public long findCorrectResourceLimitForAccount(Account account, ResourceType type) { |
186 | | - long max = -1; |
187 | | - |
| 202 | + |
| 203 | + long max = -1; //if resource limit is not found, then we treat it as unlimited |
188 | 204 | ResourceLimitVO limit = _resourceLimitDao.findByOwnerIdAndType(account.getId(), ResourceOwnerType.Account, type); |
189 | 205 |
|
190 | 206 | // Check if limit is configured for account |
191 | 207 | if (limit != null) { |
192 | 208 | max = limit.getMax().longValue(); |
193 | 209 | } else { |
194 | | - // If the account has an no limit set, then return global default account limits |
195 | | - try { |
196 | | - if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) { |
197 | | - if (type == Resource.ResourceType.public_ip) { |
198 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectPublicIPs.key())); |
199 | | - } else if (type == ResourceType.snapshot) { |
200 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectSnapshots.key())); |
201 | | - } else if (type == ResourceType.template) { |
202 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectTemplates.key())); |
203 | | - } else if (type == ResourceType.user_vm) { |
204 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectUserVms.key())); |
205 | | - } else if (type == ResourceType.volume) { |
206 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectVolumes.key())); |
207 | | - } else { |
208 | | - //if resource limit is not supported, then we treat it as unlimited |
209 | | - return -1; |
210 | | - } |
211 | | - } else { |
212 | | - if (type == Resource.ResourceType.public_ip) { |
213 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountPublicIPs.key())); |
214 | | - } else if (type == ResourceType.snapshot) { |
215 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountSnapshots.key())); |
216 | | - } else if (type == ResourceType.template) { |
217 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountTemplates.key())); |
218 | | - } else if (type == ResourceType.user_vm) { |
219 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountUserVms.key())); |
220 | | - } else if (type == ResourceType.volume) { |
221 | | - max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountVolumes.key())); |
222 | | - } else { |
223 | | - //if resource limit is not supported, then we treat it as unlimited |
224 | | - return -1; |
225 | | - } |
226 | | - } |
227 | | - } catch (NumberFormatException nfe) { |
228 | | - s_logger.error("Invalid value is set for the default account limit."); |
229 | | - } |
| 210 | + // If the account has an no limit set, then return global default account limits |
| 211 | + Long value = null; |
| 212 | + if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) { |
| 213 | + value = projectResourceLimitMap.get(type); |
| 214 | + } else { |
| 215 | + value = accountResourceLimitMap.get(type); |
| 216 | + } |
| 217 | + if (value != null){ |
| 218 | + return value; |
| 219 | + } |
230 | 220 | } |
231 | 221 |
|
232 | 222 | return max; |
|
0 commit comments