mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	Refactored EnableAccount api command
This commit is contained in:
		
							parent
							
								
									c928ec47f7
								
							
						
					
					
						commit
						b4adabe4a9
					
				| @ -25,23 +25,20 @@ import java.util.Map; | |||||||
| import org.apache.log4j.Logger; | import org.apache.log4j.Logger; | ||||||
| 
 | 
 | ||||||
| import com.cloud.api.BaseCmd; | import com.cloud.api.BaseCmd; | ||||||
|  | import com.cloud.api.Implementation; | ||||||
| import com.cloud.api.Parameter; | import com.cloud.api.Parameter; | ||||||
| import com.cloud.api.ServerApiException; | import com.cloud.api.ServerApiException; | ||||||
|  | import com.cloud.api.BaseCmd.Manager; | ||||||
| import com.cloud.user.Account; | import com.cloud.user.Account; | ||||||
| import com.cloud.utils.Pair; | import com.cloud.utils.Pair; | ||||||
| 
 | 
 | ||||||
|  | @Implementation(method="enableAccount", manager=Manager.ManagementServer) | ||||||
| public class EnableAccountCmd extends BaseCmd { | public class EnableAccountCmd extends BaseCmd { | ||||||
|     public static final Logger s_logger = Logger.getLogger(EnableAccountCmd.class.getName()); |     public static final Logger s_logger = Logger.getLogger(EnableAccountCmd.class.getName()); | ||||||
| 
 | 
 | ||||||
|     private static final String s_name = "enableaccountresponse"; |     private static final String s_name = "enableaccountresponse"; | ||||||
|     private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>(); |     private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>(); | ||||||
| 
 | 
 | ||||||
|     static { |  | ||||||
|         s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); |  | ||||||
|         s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT, Boolean.TRUE)); |  | ||||||
|         s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DOMAIN_ID, Boolean.TRUE)); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     ///////////////////////////////////////////////////// |     ///////////////////////////////////////////////////// | ||||||
|     //////////////// API parameters ///////////////////// |     //////////////// API parameters ///////////////////// | ||||||
|     ///////////////////////////////////////////////////// |     ///////////////////////////////////////////////////// | ||||||
| @ -75,41 +72,36 @@ public class EnableAccountCmd extends BaseCmd { | |||||||
|         return s_name; |         return s_name; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override | //    @Override | ||||||
|     public List<Pair<Enum, Boolean>> getProperties() { | //    public List<Pair<String, Object>> execute(Map<String, Object> params) { | ||||||
|         return s_properties; | //        Account adminAccount = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); | ||||||
|     } | //        Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); | ||||||
| 
 | //        String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); | ||||||
|     @Override | // | ||||||
|     public List<Pair<String, Object>> execute(Map<String, Object> params) { | //        if ((adminAccount != null) && !getManagementServer().isChildDomain(adminAccount.getDomainId(), domainId)) { | ||||||
|         Account adminAccount = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); | //            throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Failed to enable account " + accountName + " in domain " + domainId + ", permission denied."); | ||||||
|         Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); | //        } | ||||||
|         String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); | // | ||||||
| 
 | //        Account account = getManagementServer().findActiveAccount(accountName, domainId); | ||||||
|         if ((adminAccount != null) && !getManagementServer().isChildDomain(adminAccount.getDomainId(), domainId)) { | //        if (account == null) { | ||||||
|             throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Failed to enable account " + accountName + " in domain " + domainId + ", permission denied."); | //            throw new ServerApiException (BaseCmd.PARAM_ERROR, "Unable to find active account with name " + accountName + " in domain " + domainId); | ||||||
|         } | //        } | ||||||
| 
 | // | ||||||
|         Account account = getManagementServer().findActiveAccount(accountName, domainId); | //        // don't allow modify system account | ||||||
|         if (account == null) { | //        if (account.getId().longValue() == Account.ACCOUNT_ID_SYSTEM) { | ||||||
|             throw new ServerApiException (BaseCmd.PARAM_ERROR, "Unable to find active account with name " + accountName + " in domain " + domainId); | //            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "can not enable system account"); | ||||||
|         } | //        } | ||||||
| 
 | // | ||||||
|         // don't allow modify system account | //        boolean success = true; | ||||||
|         if (account.getId().longValue() == Account.ACCOUNT_ID_SYSTEM) { | //        try { | ||||||
|             throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "can not enable system account"); | //            success = getManagementServer().enableAccount(account.getId().longValue()); | ||||||
|         } | //        } catch (Exception ex) { | ||||||
| 
 | //            s_logger.error("error enabling account " + accountName + " in domain " + domainId, ex); | ||||||
|         boolean success = true; | //            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error enabling account " + accountName + " in domain " + domainId); | ||||||
|         try { | //        } | ||||||
|             success = getManagementServer().enableAccount(account.getId().longValue()); | // | ||||||
|         } catch (Exception ex) { | //        List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>(); | ||||||
|             s_logger.error("error enabling account " + accountName + " in domain " + domainId, ex); | //        returnValues.add(new Pair<String, Object>(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(success).toString())); | ||||||
|             throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error enabling account " + accountName + " in domain " + domainId); | //        return returnValues; | ||||||
|         } | //    } | ||||||
| 
 |  | ||||||
|         List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>(); |  | ||||||
|         returnValues.add(new Pair<String, Object>(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(success).toString())); |  | ||||||
|         return returnValues; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -25,6 +25,7 @@ import java.util.List; | |||||||
| import java.util.Map; | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| import com.cloud.alert.AlertVO; | import com.cloud.alert.AlertVO; | ||||||
|  | import com.cloud.api.commands.EnableAccountCmd; | ||||||
| import com.cloud.api.commands.UpdateAccountCmd; | import com.cloud.api.commands.UpdateAccountCmd; | ||||||
| import com.cloud.async.AsyncJobResult; | import com.cloud.async.AsyncJobResult; | ||||||
| import com.cloud.async.AsyncJobVO; | import com.cloud.async.AsyncJobVO; | ||||||
| @ -211,6 +212,14 @@ public interface ManagementServer { | |||||||
|      */ |      */ | ||||||
|     boolean enableAccount(long accountId); |     boolean enableAccount(long accountId); | ||||||
|      |      | ||||||
|  |      | ||||||
|  |     /** | ||||||
|  |      * Enables an account by accountId | ||||||
|  |      * @param accountId | ||||||
|  |      * @return true if enable was successful, false otherwise | ||||||
|  |      */ | ||||||
|  |     boolean enableAccount(EnableAccountCmd cmd) throws InvalidParameterValueException; | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Locks an account by accountId.  A locked account cannot access the API, but will still have running VMs/IP addresses allocated/etc. |      * Locks an account by accountId.  A locked account cannot access the API, but will still have running VMs/IP addresses allocated/etc. | ||||||
|      * @param accountId |      * @param accountId | ||||||
|  | |||||||
| @ -70,6 +70,7 @@ import com.cloud.api.commands.DeleteIsoCmd; | |||||||
| import com.cloud.api.commands.DeleteTemplateCmd; | import com.cloud.api.commands.DeleteTemplateCmd; | ||||||
| import com.cloud.api.commands.DeleteUserCmd; | import com.cloud.api.commands.DeleteUserCmd; | ||||||
| import com.cloud.api.commands.DeployVMCmd; | import com.cloud.api.commands.DeployVMCmd; | ||||||
|  | import com.cloud.api.commands.EnableAccountCmd; | ||||||
| import com.cloud.api.commands.PrepareForMaintenanceCmd; | import com.cloud.api.commands.PrepareForMaintenanceCmd; | ||||||
| import com.cloud.api.commands.PreparePrimaryStorageForMaintenanceCmd; | import com.cloud.api.commands.PreparePrimaryStorageForMaintenanceCmd; | ||||||
| import com.cloud.api.commands.ReconnectHostCmd; | import com.cloud.api.commands.ReconnectHostCmd; | ||||||
| @ -1206,6 +1207,23 @@ public class ManagementServerImpl implements ManagementServer { | |||||||
|         return success; |         return success; | ||||||
|     } |     } | ||||||
|     	 |     	 | ||||||
|  |      | ||||||
|  |     @Override | ||||||
|  |     public boolean enableAccount(EnableAccountCmd cmd) throws InvalidParameterValueException{ | ||||||
|  |     	String accountName = cmd.getAccountName(); | ||||||
|  |     	Long domainId = cmd.getDomainId(); | ||||||
|  |         boolean success = false; | ||||||
|  |         Account account = _accountDao.findActiveAccount(accountName, domainId); | ||||||
|  | 
 | ||||||
|  |         //Check if account exists | ||||||
|  |         if (account == null) { | ||||||
|  |         	s_logger.error("Unable to find account " + accountName + " in domain " + domainId); | ||||||
|  |     		throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); | ||||||
|  |         } | ||||||
|  |         success = enableAccount(account.getId()); | ||||||
|  |         return success; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Override |     @Override | ||||||
|     public boolean lockAccount(long accountId) { |     public boolean lockAccount(long accountId) { | ||||||
|         boolean success = false; |         boolean success = false; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user