|
16 | 16 | // under the License. |
17 | 17 | package org.apache.cloudstack.iam; |
18 | 18 |
|
19 | | -import java.sql.PreparedStatement; |
20 | | -import java.sql.ResultSet; |
21 | | -import java.sql.SQLException; |
22 | 19 | import java.util.ArrayList; |
23 | 20 | import java.util.Collections; |
24 | 21 | import java.util.HashMap; |
|
114 | 111 | import com.cloud.utils.component.ManagerBase; |
115 | 112 | import com.cloud.utils.db.DB; |
116 | 113 | import com.cloud.utils.db.EntityManager; |
117 | | -import com.cloud.utils.db.Transaction; |
118 | | -import com.cloud.utils.db.TransactionCallbackNoReturn; |
119 | | -import com.cloud.utils.db.TransactionLegacy; |
120 | | -import com.cloud.utils.db.TransactionStatus; |
121 | | -import com.cloud.utils.exception.CloudRuntimeException; |
122 | 114 | import com.cloud.vm.InstanceGroupVO; |
123 | 115 | import com.cloud.vm.VMInstanceVO; |
124 | 116 | import com.cloud.vm.dao.NicIpAliasVO; |
@@ -199,10 +191,6 @@ public class IAMApiServiceImpl extends ManagerBase implements IAMApiService, Man |
199 | 191 | @Override |
200 | 192 | public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException { |
201 | 193 |
|
202 | | - // populate group <-> account association if not present for CS admin |
203 | | - // and system accounts |
204 | | - populateIAMGroupAdminAccountMap(); |
205 | | - |
206 | 194 | _messageBus.subscribe(AccountManager.MESSAGE_ADD_ACCOUNT_EVENT, new MessageSubscriber() { |
207 | 195 | @Override |
208 | 196 | public void onPublishMessage(String senderAddress, String subject, Object obj) { |
@@ -351,86 +339,22 @@ public void onPublishMessage(String senderAddress, String subject, Object obj) { |
351 | 339 | return super.configure(name, params); |
352 | 340 | } |
353 | 341 |
|
354 | | - private void populateIAMGroupAdminAccountMap() { |
355 | | - |
356 | | - Transaction.execute(new TransactionCallbackNoReturn() { |
357 | | - @Override |
358 | | - public void doInTransactionWithoutResult(TransactionStatus status) { |
359 | | - TransactionLegacy txn = TransactionLegacy.currentTxn(); |
360 | | - |
361 | | - String searchQuery = "Select id from `cloud`.`iam_group_account_map` where account_id = ? and removed is null"; |
362 | | - ResultSet rs = null; |
363 | | - PreparedStatement acctQuery = null; |
364 | | - PreparedStatement acctInsert = null; |
365 | | - // find if the system account is present in the map |
366 | | - try { |
367 | | - acctQuery = txn.prepareAutoCloseStatement(searchQuery); |
368 | | - acctQuery.setLong(1, Account.ACCOUNT_ID_SYSTEM); |
369 | | - |
370 | | - rs = acctQuery.executeQuery(); |
371 | | - if (!rs.next()) { |
372 | | - acctInsert = txn |
373 | | - .prepareAutoCloseStatement("INSERT INTO `cloud`.`iam_group_account_map` (group_id, account_id, created) values(?, ?, Now())"); |
374 | | - // insert entry in iam_group_account_map table |
375 | | - acctInsert.setLong(1, Account.ACCOUNT_TYPE_ADMIN + 1); |
376 | | - acctInsert.setLong(2, Account.ACCOUNT_ID_SYSTEM); |
377 | | - acctInsert.executeUpdate(); |
378 | | - } |
379 | | - } catch (SQLException ex) { |
380 | | - String msg = "Unable to populate iam_group_account_map for SYSTEM account." + ex.getMessage(); |
381 | | - s_logger.error(msg); |
382 | | - throw new CloudRuntimeException(msg, ex); |
383 | | - } finally { |
384 | | - try { |
385 | | - if (acctInsert != null) { |
386 | | - acctInsert.close(); |
387 | | - } |
388 | | - if (rs != null) { |
389 | | - rs.close(); |
390 | | - } |
391 | | - if (acctQuery != null) { |
392 | | - acctQuery.close(); |
393 | | - } |
394 | | - } catch (SQLException e) { |
395 | | - } |
396 | | - } |
| 342 | + @Override |
| 343 | + public boolean start() { |
| 344 | + s_logger.info("Populating IAM group and account association for default accounts..."); |
397 | 345 |
|
398 | | - // find if the admin account is present in the map |
399 | | - try { |
400 | | - acctQuery = txn.prepareAutoCloseStatement(searchQuery); |
401 | | - acctQuery.setLong(1, Account.ACCOUNT_ID_SYSTEM + 1); |
402 | | - |
403 | | - rs = acctQuery.executeQuery(); |
404 | | - if (!rs.next()) { |
405 | | - acctInsert = txn |
406 | | - .prepareAutoCloseStatement("INSERT INTO `cloud`.`iam_group_account_map` (group_id, account_id, created) values(?, ?, Now())"); |
407 | | - // insert entry in iam_group_account_map table |
408 | | - acctInsert.setLong(1, Account.ACCOUNT_TYPE_ADMIN + 1); |
409 | | - acctInsert.setLong(2, Account.ACCOUNT_ID_SYSTEM + 1); |
410 | | - acctInsert.executeUpdate(); |
411 | | - } |
412 | | - } catch (SQLException ex) { |
413 | | - String msg = "Unable to populate iam_group_account_map for Admin account." + ex.getMessage(); |
414 | | - s_logger.error(msg); |
415 | | - throw new CloudRuntimeException(msg, ex); |
416 | | - } finally { |
417 | | - try { |
418 | | - if (acctInsert != null) { |
419 | | - acctInsert.close(); |
420 | | - } |
421 | | - if (rs != null) { |
422 | | - rs.close(); |
423 | | - } |
424 | | - if (acctQuery != null) { |
425 | | - acctQuery.close(); |
426 | | - } |
427 | | - } catch (SQLException e) { |
428 | | - } |
429 | | - } |
| 346 | + // populate group <-> account association if not present for CS admin |
| 347 | + // and system accounts |
| 348 | + populateIAMGroupAdminAccountMap(); |
430 | 349 |
|
431 | | - } |
432 | | - }); |
| 350 | + return true; |
| 351 | + } |
433 | 352 |
|
| 353 | + private void populateIAMGroupAdminAccountMap() { |
| 354 | + List<Long> sysAccts = new ArrayList<Long>(); |
| 355 | + sysAccts.add(Account.ACCOUNT_ID_SYSTEM); |
| 356 | + sysAccts.add(Account.ACCOUNT_ID_SYSTEM + 1); |
| 357 | + _iamSrv.addAccountsToGroup(sysAccts, new Long(Account.ACCOUNT_TYPE_ADMIN + 1)); |
434 | 358 | } |
435 | 359 |
|
436 | 360 | private void addDomainWideResourceAccess(Map<String, Object> params) { |
|
0 commit comments