|
45 | 45 | import org.apache.cloudstack.api.response.AccountResponse; |
46 | 46 |
|
47 | 47 | import com.cloud.api.query.ViewResponseHelper; |
| 48 | +import com.cloud.api.query.vo.AccountJoinVO; |
48 | 49 | import com.cloud.api.query.vo.ControlledViewEntity; |
49 | 50 | import com.cloud.api.query.vo.DomainRouterJoinVO; |
50 | 51 | import com.cloud.api.query.vo.EventJoinVO; |
@@ -275,162 +276,13 @@ public UserResponse createUserResponse(User user) { |
275 | 276 | // this method is used for response generation via createAccount (which creates an account + user) |
276 | 277 | @Override |
277 | 278 | public AccountResponse createUserAccountResponse(UserAccount user) { |
278 | | - return createAccountResponse(ApiDBUtils.findAccountById(user.getAccountId())); |
| 279 | + return ApiDBUtils.newAccountResponse(ApiDBUtils.findAccountViewById(user.getAccountId())); |
279 | 280 | } |
280 | 281 |
|
281 | 282 | @Override |
282 | 283 | public AccountResponse createAccountResponse(Account account) { |
283 | | - boolean accountIsAdmin = (account.getType() == Account.ACCOUNT_TYPE_ADMIN); |
284 | | - AccountResponse accountResponse = new AccountResponse(); |
285 | | - accountResponse.setId(account.getUuid()); |
286 | | - accountResponse.setName(account.getAccountName()); |
287 | | - accountResponse.setAccountType(account.getType()); |
288 | | - Domain domain = ApiDBUtils.findDomainById(account.getDomainId()); |
289 | | - if (domain != null) { |
290 | | - accountResponse.setDomainId(domain.getUuid()); |
291 | | - accountResponse.setDomainName(domain.getName()); |
292 | | - } |
293 | | - accountResponse.setState(account.getState().toString()); |
294 | | - accountResponse.setNetworkDomain(account.getNetworkDomain()); |
295 | | - DataCenter dc = ApiDBUtils.findZoneById(account.getDefaultZoneId()); |
296 | | - if (dc != null) { |
297 | | - accountResponse.setDefaultZone(dc.getUuid()); |
298 | | - } |
299 | | - |
300 | | - // get network stat |
301 | | - List<UserStatisticsVO> stats = ApiDBUtils.listUserStatsBy(account.getId()); |
302 | | - if (stats == null) { |
303 | | - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error searching for user stats"); |
304 | | - } |
305 | | - |
306 | | - Long bytesSent = 0L; |
307 | | - Long bytesReceived = 0L; |
308 | | - for (UserStatisticsVO stat : stats) { |
309 | | - Long rx = stat.getNetBytesReceived() + stat.getCurrentBytesReceived(); |
310 | | - Long tx = stat.getNetBytesSent() + stat.getCurrentBytesSent(); |
311 | | - bytesReceived = bytesReceived + Long.valueOf(rx); |
312 | | - bytesSent = bytesSent + Long.valueOf(tx); |
313 | | - } |
314 | | - accountResponse.setBytesReceived(bytesReceived); |
315 | | - accountResponse.setBytesSent(bytesSent); |
316 | | - |
317 | | - // Get resource limits and counts |
318 | | - |
319 | | - Long vmLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.user_vm, account.getId()); |
320 | | - String vmLimitDisplay = (accountIsAdmin || vmLimit == -1) ? "Unlimited" : String.valueOf(vmLimit); |
321 | | - Long vmTotal = ApiDBUtils.getResourceCount(ResourceType.user_vm, account.getId()); |
322 | | - String vmAvail = (accountIsAdmin || vmLimit == -1) ? "Unlimited" : String.valueOf(vmLimit - vmTotal); |
323 | | - accountResponse.setVmLimit(vmLimitDisplay); |
324 | | - accountResponse.setVmTotal(vmTotal); |
325 | | - accountResponse.setVmAvailable(vmAvail); |
326 | | - |
327 | | - Long ipLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.public_ip, account.getId()); |
328 | | - String ipLimitDisplay = (accountIsAdmin || ipLimit == -1) ? "Unlimited" : String.valueOf(ipLimit); |
329 | | - Long ipTotal = ApiDBUtils.getResourceCount(ResourceType.public_ip, account.getId()); |
330 | | - |
331 | | - Long ips = ipLimit - ipTotal; |
332 | | - // check how many free ips are left, and if it's less than max allowed number of ips from account - use this |
333 | | - // value |
334 | | - Long ipsLeft = ApiDBUtils.countFreePublicIps(); |
335 | | - boolean unlimited = true; |
336 | | - if (ips.longValue() > ipsLeft.longValue()) { |
337 | | - ips = ipsLeft; |
338 | | - unlimited = false; |
339 | | - } |
340 | | - |
341 | | - String ipAvail = ((accountIsAdmin || ipLimit == -1) && unlimited) ? "Unlimited" : String.valueOf(ips); |
342 | | - |
343 | | - accountResponse.setIpLimit(ipLimitDisplay); |
344 | | - accountResponse.setIpTotal(ipTotal); |
345 | | - accountResponse.setIpAvailable(ipAvail); |
346 | | - |
347 | | - Long volumeLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.volume, account.getId()); |
348 | | - String volumeLimitDisplay = (accountIsAdmin || volumeLimit == -1) ? "Unlimited" : String.valueOf(volumeLimit); |
349 | | - Long volumeTotal = ApiDBUtils.getResourceCount(ResourceType.volume, account.getId()); |
350 | | - String volumeAvail = (accountIsAdmin || volumeLimit == -1) ? "Unlimited" : String.valueOf(volumeLimit - volumeTotal); |
351 | | - accountResponse.setVolumeLimit(volumeLimitDisplay); |
352 | | - accountResponse.setVolumeTotal(volumeTotal); |
353 | | - accountResponse.setVolumeAvailable(volumeAvail); |
354 | | - |
355 | | - Long snapshotLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.snapshot, account.getId()); |
356 | | - String snapshotLimitDisplay = (accountIsAdmin || snapshotLimit == -1) ? "Unlimited" : String.valueOf(snapshotLimit); |
357 | | - Long snapshotTotal = ApiDBUtils.getResourceCount(ResourceType.snapshot, account.getId()); |
358 | | - String snapshotAvail = (accountIsAdmin || snapshotLimit == -1) ? "Unlimited" : String.valueOf(snapshotLimit - snapshotTotal); |
359 | | - accountResponse.setSnapshotLimit(snapshotLimitDisplay); |
360 | | - accountResponse.setSnapshotTotal(snapshotTotal); |
361 | | - accountResponse.setSnapshotAvailable(snapshotAvail); |
362 | | - |
363 | | - Long templateLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.template, account.getId()); |
364 | | - String templateLimitDisplay = (accountIsAdmin || templateLimit == -1) ? "Unlimited" : String.valueOf(templateLimit); |
365 | | - Long templateTotal = ApiDBUtils.getResourceCount(ResourceType.template, account.getId()); |
366 | | - String templateAvail = (accountIsAdmin || templateLimit == -1) ? "Unlimited" : String.valueOf(templateLimit - templateTotal); |
367 | | - accountResponse.setTemplateLimit(templateLimitDisplay); |
368 | | - accountResponse.setTemplateTotal(templateTotal); |
369 | | - accountResponse.setTemplateAvailable(templateAvail); |
370 | | - |
371 | | - // Get stopped and running VMs |
372 | | - int vmStopped = 0; |
373 | | - int vmRunning = 0; |
374 | | - |
375 | | - List<Long> permittedAccounts = new ArrayList<Long>(); |
376 | | - permittedAccounts.add(account.getId()); |
377 | | - |
378 | | - List<UserVmJoinVO> virtualMachines = ApiDBUtils.searchForUserVMs(new Criteria(), permittedAccounts); |
379 | | - |
380 | | - // get Running/Stopped VMs |
381 | | - for (Iterator<UserVmJoinVO> iter = virtualMachines.iterator(); iter.hasNext();) { |
382 | | - // count how many stopped/running vms we have |
383 | | - UserVmJoinVO vm = iter.next(); |
384 | | - |
385 | | - if (vm.getState() == State.Stopped) { |
386 | | - vmStopped++; |
387 | | - } else if (vm.getState() == State.Running) { |
388 | | - vmRunning++; |
389 | | - } |
390 | | - } |
391 | | - |
392 | | - accountResponse.setVmStopped(vmStopped); |
393 | | - accountResponse.setVmRunning(vmRunning); |
394 | | - accountResponse.setObjectName("account"); |
395 | | - |
396 | | - //get resource limits for projects |
397 | | - Long projectLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.project, account.getId()); |
398 | | - String projectLimitDisplay = (accountIsAdmin || projectLimit == -1) ? "Unlimited" : String.valueOf(projectLimit); |
399 | | - Long projectTotal = ApiDBUtils.getResourceCount(ResourceType.project, account.getId()); |
400 | | - String projectAvail = (accountIsAdmin || projectLimit == -1) ? "Unlimited" : String.valueOf(projectLimit - projectTotal); |
401 | | - accountResponse.setProjectLimit(projectLimitDisplay); |
402 | | - accountResponse.setProjectTotal(projectTotal); |
403 | | - accountResponse.setProjectAvailable(projectAvail); |
404 | | - |
405 | | - //get resource limits for networks |
406 | | - Long networkLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.network, account.getId()); |
407 | | - String networkLimitDisplay = (accountIsAdmin || networkLimit == -1) ? "Unlimited" : String.valueOf(networkLimit); |
408 | | - Long networkTotal = ApiDBUtils.getResourceCount(ResourceType.network, account.getId()); |
409 | | - String networkAvail = (accountIsAdmin || networkLimit == -1) ? "Unlimited" : String.valueOf(networkLimit - networkTotal); |
410 | | - accountResponse.setNetworkLimit(networkLimitDisplay); |
411 | | - accountResponse.setNetworkTotal(networkTotal); |
412 | | - accountResponse.setNetworkAvailable(networkAvail); |
413 | | - |
414 | | - //get resource limits for vpcs |
415 | | - Long vpcLimit = ApiDBUtils.findCorrectResourceLimit(ResourceType.vpc, account.getId()); |
416 | | - String vpcLimitDisplay = (accountIsAdmin || vpcLimit == -1) ? "Unlimited" : String.valueOf(vpcLimit); |
417 | | - Long vpcTotal = ApiDBUtils.getResourceCount(ResourceType.vpc, account.getId()); |
418 | | - String vpcAvail = (accountIsAdmin || vpcLimit == -1) ? "Unlimited" : String.valueOf(vpcLimit - vpcTotal); |
419 | | - accountResponse.setNetworkLimit(vpcLimitDisplay); |
420 | | - accountResponse.setNetworkTotal(vpcTotal); |
421 | | - accountResponse.setNetworkAvailable(vpcAvail); |
422 | | - |
423 | | - // adding all the users for an account as part of the response obj |
424 | | - List<UserVO> usersForAccount = ApiDBUtils.listUsersByAccount(account.getAccountId()); |
425 | | - List<UserResponse> userResponseList = new ArrayList<UserResponse>(); |
426 | | - for (UserVO user : usersForAccount) { |
427 | | - UserResponse userResponse = createUserResponse(user); |
428 | | - userResponseList.add(userResponse); |
429 | | - } |
430 | | - |
431 | | - accountResponse.setUsers(userResponseList); |
432 | | - accountResponse.setDetails(ApiDBUtils.getAccountDetails(account.getId())); |
433 | | - return accountResponse; |
| 284 | + AccountJoinVO vUser = ApiDBUtils.newAccountView(account); |
| 285 | + return ApiDBUtils.newAccountResponse(vUser); |
434 | 286 | } |
435 | 287 |
|
436 | 288 |
|
|
0 commit comments