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 com.cloud.api.BaseCmd; | ||||
| import com.cloud.api.Implementation; | ||||
| import com.cloud.api.Parameter; | ||||
| import com.cloud.api.ServerApiException; | ||||
| import com.cloud.api.BaseCmd.Manager; | ||||
| import com.cloud.user.Account; | ||||
| import com.cloud.utils.Pair; | ||||
| 
 | ||||
| 
 | ||||
| @Implementation(method="enableAccount", manager=Manager.ManagementServer) | ||||
| public class EnableAccountCmd extends BaseCmd { | ||||
|     public static final Logger s_logger = Logger.getLogger(EnableAccountCmd.class.getName()); | ||||
| 
 | ||||
|     private static final String s_name = "enableaccountresponse"; | ||||
|     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 ///////////////////// | ||||
|     ///////////////////////////////////////////////////// | ||||
| @ -75,41 +72,36 @@ public class EnableAccountCmd extends BaseCmd { | ||||
|         return s_name; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public List<Pair<Enum, Boolean>> getProperties() { | ||||
|         return s_properties; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public List<Pair<String, Object>> execute(Map<String, Object> params) { | ||||
|         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()); | ||||
| 
 | ||||
|         if ((adminAccount != null) && !getManagementServer().isChildDomain(adminAccount.getDomainId(), domainId)) { | ||||
|             throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Failed to enable account " + accountName + " in domain " + domainId + ", permission denied."); | ||||
|         } | ||||
| 
 | ||||
|         Account account = getManagementServer().findActiveAccount(accountName, domainId); | ||||
|         if (account == null) { | ||||
|             throw new ServerApiException (BaseCmd.PARAM_ERROR, "Unable to find active account with name " + accountName + " in domain " + domainId); | ||||
|         } | ||||
| 
 | ||||
|         // don't allow modify system account | ||||
|         if (account.getId().longValue() == Account.ACCOUNT_ID_SYSTEM) { | ||||
|             throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "can not enable system account"); | ||||
|         } | ||||
| 
 | ||||
|         boolean success = true; | ||||
|         try { | ||||
|             success = getManagementServer().enableAccount(account.getId().longValue()); | ||||
|         } catch (Exception ex) { | ||||
|             s_logger.error("error enabling account " + accountName + " in domain " + domainId, ex); | ||||
|             throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error enabling account " + accountName + " in domain " + domainId); | ||||
|         } | ||||
| 
 | ||||
|         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; | ||||
|     } | ||||
| //    @Override | ||||
| //    public List<Pair<String, Object>> execute(Map<String, Object> params) { | ||||
| //        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()); | ||||
| // | ||||
| //        if ((adminAccount != null) && !getManagementServer().isChildDomain(adminAccount.getDomainId(), domainId)) { | ||||
| //            throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Failed to enable account " + accountName + " in domain " + domainId + ", permission denied."); | ||||
| //        } | ||||
| // | ||||
| //        Account account = getManagementServer().findActiveAccount(accountName, domainId); | ||||
| //        if (account == null) { | ||||
| //            throw new ServerApiException (BaseCmd.PARAM_ERROR, "Unable to find active account with name " + accountName + " in domain " + domainId); | ||||
| //        } | ||||
| // | ||||
| //        // don't allow modify system account | ||||
| //        if (account.getId().longValue() == Account.ACCOUNT_ID_SYSTEM) { | ||||
| //            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "can not enable system account"); | ||||
| //        } | ||||
| // | ||||
| //        boolean success = true; | ||||
| //        try { | ||||
| //            success = getManagementServer().enableAccount(account.getId().longValue()); | ||||
| //        } catch (Exception ex) { | ||||
| //            s_logger.error("error enabling account " + accountName + " in domain " + domainId, ex); | ||||
| //            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error enabling account " + accountName + " in domain " + domainId); | ||||
| //        } | ||||
| // | ||||
| //        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 com.cloud.alert.AlertVO; | ||||
| import com.cloud.api.commands.EnableAccountCmd; | ||||
| import com.cloud.api.commands.UpdateAccountCmd; | ||||
| import com.cloud.async.AsyncJobResult; | ||||
| import com.cloud.async.AsyncJobVO; | ||||
| @ -210,6 +211,14 @@ public interface ManagementServer { | ||||
|      * @return true if enable was successful, false otherwise | ||||
|      */ | ||||
|     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. | ||||
|  | ||||
| @ -70,6 +70,7 @@ import com.cloud.api.commands.DeleteIsoCmd; | ||||
| import com.cloud.api.commands.DeleteTemplateCmd; | ||||
| import com.cloud.api.commands.DeleteUserCmd; | ||||
| import com.cloud.api.commands.DeployVMCmd; | ||||
| import com.cloud.api.commands.EnableAccountCmd; | ||||
| import com.cloud.api.commands.PrepareForMaintenanceCmd; | ||||
| import com.cloud.api.commands.PreparePrimaryStorageForMaintenanceCmd; | ||||
| import com.cloud.api.commands.ReconnectHostCmd; | ||||
| @ -1205,6 +1206,23 @@ public class ManagementServerImpl implements ManagementServer { | ||||
|         success = _accountDao.update(Long.valueOf(accountId), acctForUpdate); | ||||
|         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 | ||||
|     public boolean lockAccount(long accountId) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user