|
27 | 27 | import org.zstack.header.message.APIMessage; |
28 | 28 | import org.zstack.header.message.Message; |
29 | 29 | import org.zstack.header.message.MessageReply; |
| 30 | +import static org.zstack.core.Platform.argerr; |
30 | 31 | import org.zstack.utils.CollectionUtils; |
31 | 32 | import org.zstack.utils.Utils; |
32 | 33 | import org.zstack.utils.logging.CLogger; |
@@ -316,11 +317,42 @@ private void handleApiMessage(APIMessage msg) { |
316 | 317 | handle((APIDeleteAccountMsg) msg); |
317 | 318 | } else if (msg instanceof APIGetAccountQuotaUsageMsg) { |
318 | 319 | handle((APIGetAccountQuotaUsageMsg) msg); |
| 320 | + } else if (msg instanceof APIChangeAccountTypeMsg) { |
| 321 | + handle((APIChangeAccountTypeMsg) msg); |
319 | 322 | } else { |
320 | 323 | bus.dealWithUnknownMessage(msg); |
321 | 324 | } |
322 | 325 | } |
323 | 326 |
|
| 327 | + private void handle(APIChangeAccountTypeMsg msg) { |
| 328 | + APIChangeAccountTypeEvent evt = new APIChangeAccountTypeEvent(msg.getId()); |
| 329 | + String accountUuid = msg.getUuid(); |
| 330 | + AccountVO account = dbf.findByUuid(accountUuid, AccountVO.class); |
| 331 | + if (account == null) { |
| 332 | + evt.setError(argerr("Cannot find account[uuid:%s]", accountUuid)); |
| 333 | + bus.publish(evt); |
| 334 | + return; |
| 335 | + } |
| 336 | + AccountType originalAccountType = account.getType(); |
| 337 | + AccountType targetAccountType = AccountType.valueOf(msg.getType()); |
| 338 | + List<AccountTypeChangedExtensionPoint> extensions = pluginRgty.getExtensionList(AccountTypeChangedExtensionPoint.class); |
| 339 | + for (AccountTypeChangedExtensionPoint extension : extensions) { |
| 340 | + ErrorCode errorCode = extension.preAccountTypeChange(accountUuid, originalAccountType, targetAccountType); |
| 341 | + if (errorCode != null) { |
| 342 | + evt.setError(errorCode); |
| 343 | + bus.publish(evt); |
| 344 | + return; |
| 345 | + } |
| 346 | + } |
| 347 | + CollectionUtils.forEach(extensions, ext -> ext.beforeAccountTypeChange(accountUuid, originalAccountType, targetAccountType)); |
| 348 | + |
| 349 | + account.setType(targetAccountType); |
| 350 | + account = dbf.updateAndRefresh(account); |
| 351 | + CollectionUtils.forEach(extensions, ext -> ext.afterAccountTypeChange(accountUuid, targetAccountType)); |
| 352 | + evt.setInventory(AccountInventory.valueOf(account)); |
| 353 | + bus.publish(evt); |
| 354 | + } |
| 355 | + |
324 | 356 | private void handle(APIGetAccountQuotaUsageMsg msg) { |
325 | 357 | APIGetAccountQuotaUsageReply reply = new APIGetAccountQuotaUsageReply(); |
326 | 358 |
|
|
0 commit comments