Search in sources :

Example 36 with Account

use of com.cloud.user.Account in project cloudstack by apache.

the class VpcManagerImpl method startVpc.

@Override
public boolean startVpc(final long vpcId, final boolean destroyOnFailure) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    final CallContext ctx = CallContext.current();
    final Account caller = ctx.getCallingAccount();
    final User callerUser = _accountMgr.getActiveUser(ctx.getCallingUserId());
    // check if vpc exists
    final Vpc vpc = getActiveVpc(vpcId);
    if (vpc == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC by id specified");
        ex.addProxyObject(String.valueOf(vpcId), "VPC");
        throw ex;
    }
    // permission check
    _accountMgr.checkAccess(caller, null, false, vpc);
    final DataCenter dc = _entityMgr.findById(DataCenter.class, vpc.getZoneId());
    final DeployDestination dest = new DeployDestination(dc, null, null, null);
    final ReservationContext context = new ReservationContextImpl(null, null, callerUser, _accountMgr.getAccount(vpc.getAccountId()));
    boolean result = true;
    try {
        if (!startVpc(vpc, dest, context)) {
            s_logger.warn("Failed to start vpc " + vpc);
            result = false;
        }
    } catch (final Exception ex) {
        s_logger.warn("Failed to start vpc " + vpc + " due to ", ex);
        result = false;
    } finally {
        // do cleanup
        if (!result && destroyOnFailure) {
            s_logger.debug("Destroying vpc " + vpc + " that failed to start");
            if (destroyVpc(vpc, caller, callerUser.getId())) {
                s_logger.warn("Successfully destroyed vpc " + vpc + " that failed to start");
            } else {
                s_logger.warn("Failed to destroy vpc " + vpc + " that failed to start");
            }
        }
    }
    return result;
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DeployDestination(com.cloud.deploy.DeployDestination) CallContext(org.apache.cloudstack.context.CallContext) ReservationContextImpl(com.cloud.vm.ReservationContextImpl) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ReservationContext(com.cloud.vm.ReservationContext)

Example 37 with Account

use of com.cloud.user.Account in project cloudstack by apache.

the class VpcManagerImpl method createVpc.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VPC_CREATE, eventDescription = "creating vpc", create = true)
public Vpc createVpc(final long zoneId, final long vpcOffId, final long vpcOwnerId, final String vpcName, final String displayText, final String cidr, String networkDomain, final Boolean displayVpc) throws ResourceAllocationException {
    final Account caller = CallContext.current().getCallingAccount();
    final Account owner = _accountMgr.getAccount(vpcOwnerId);
    // Verify that caller can perform actions in behalf of vpc owner
    _accountMgr.checkAccess(caller, null, false, owner);
    // check resource limit
    _resourceLimitMgr.checkResourceLimit(owner, ResourceType.vpc);
    // Validate vpc offering
    final VpcOfferingVO vpcOff = _vpcOffDao.findById(vpcOffId);
    if (vpcOff == null || vpcOff.getState() != State.Enabled) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find vpc offering in " + State.Enabled + " state by specified id");
        if (vpcOff == null) {
            ex.addProxyObject(String.valueOf(vpcOffId), "vpcOfferingId");
        } else {
            ex.addProxyObject(vpcOff.getUuid(), "vpcOfferingId");
        }
        throw ex;
    }
    final boolean isRegionLevelVpcOff = vpcOff.offersRegionLevelVPC();
    if (isRegionLevelVpcOff && networkDomain == null) {
        throw new InvalidParameterValueException("Network domain must be specified for region level VPC");
    }
    // Validate zone
    final DataCenter zone = _entityMgr.findById(DataCenter.class, zoneId);
    if (zone == null) {
        throw new InvalidParameterValueException("Can't find zone by id specified");
    }
    if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getId())) {
        // See DataCenterVO.java
        final PermissionDeniedException ex = new PermissionDeniedException("Cannot perform this operation since specified Zone is currently disabled");
        ex.addProxyObject(zone.getUuid(), "zoneId");
        throw ex;
    }
    if (networkDomain == null) {
        // 1) Get networkDomain from the corresponding account
        networkDomain = _ntwkModel.getAccountNetworkDomain(owner.getId(), zoneId);
        // global config variables
        if (networkDomain == null) {
            networkDomain = "cs" + Long.toHexString(owner.getId()) + NetworkOrchestrationService.GuestDomainSuffix.valueIn(zoneId);
        }
    }
    final boolean useDistributedRouter = vpcOff.supportsDistributedRouter();
    final VpcVO vpc = new VpcVO(zoneId, vpcName, displayText, owner.getId(), owner.getDomainId(), vpcOffId, cidr, networkDomain, useDistributedRouter, isRegionLevelVpcOff, vpcOff.getRedundantRouter());
    return createVpc(displayVpc, vpc);
}
Also used : Account(com.cloud.user.Account) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ActionEvent(com.cloud.event.ActionEvent)

Example 38 with Account

use of com.cloud.user.Account in project cloudstack by apache.

the class RemoteAccessVpnManagerImpl method startRemoteAccessVpn.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_REMOTE_ACCESS_VPN_CREATE, eventDescription = "creating remote access vpn", async = true)
public RemoteAccessVpnVO startRemoteAccessVpn(long ipAddressId, boolean openFirewall) throws ResourceUnavailableException {
    Account caller = CallContext.current().getCallingAccount();
    final RemoteAccessVpnVO vpn = _remoteAccessVpnDao.findByPublicIpAddress(ipAddressId);
    if (vpn == null) {
        throw new InvalidParameterValueException("Unable to find your vpn: " + ipAddressId);
    }
    if (vpn.getVpcId() != null) {
        openFirewall = false;
    }
    _accountMgr.checkAccess(caller, null, true, vpn);
    boolean started = false;
    try {
        boolean firewallOpened = true;
        if (openFirewall) {
            firewallOpened = _firewallMgr.applyIngressFirewallRules(vpn.getServerAddressId(), caller);
        }
        if (firewallOpened) {
            for (RemoteAccessVPNServiceProvider element : _vpnServiceProviders) {
                if (element.startVpn(vpn)) {
                    started = true;
                    break;
                }
            }
        }
        return vpn;
    } finally {
        if (started) {
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    vpn.setState(RemoteAccessVpn.State.Running);
                    _remoteAccessVpnDao.update(vpn.getId(), vpn);
                    // Start billing of existing VPN users in ADD and Active state
                    List<VpnUserVO> vpnUsers = _vpnUsersDao.listByAccount(vpn.getAccountId());
                    for (VpnUserVO user : vpnUsers) {
                        if (user.getState() != VpnUser.State.Revoke) {
                            UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VPN_USER_ADD, user.getAccountId(), 0, user.getId(), user.getUsername(), user.getClass().getName(), user.getUuid());
                        }
                    }
                }
            });
        }
    }
}
Also used : Account(com.cloud.user.Account) RemoteAccessVPNServiceProvider(com.cloud.network.element.RemoteAccessVPNServiceProvider) RemoteAccessVpnVO(com.cloud.network.dao.RemoteAccessVpnVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VpnUserVO(com.cloud.network.VpnUserVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) List(java.util.List) ArrayList(java.util.ArrayList) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 39 with Account

use of com.cloud.user.Account in project cloudstack by apache.

the class RemoteAccessVpnManagerImpl method searchForVpnUsers.

@Override
public Pair<List<? extends VpnUser>, Integer> searchForVpnUsers(ListVpnUsersCmd cmd) {
    String username = cmd.getUsername();
    Long id = cmd.getId();
    String keyword = cmd.getKeyword();
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(VpnUserVO.class, "username", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchBuilder<VpnUserVO> sb = _vpnUsersDao.createSearchBuilder();
    _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("username", sb.entity().getUsername(), SearchCriteria.Op.EQ);
    sb.and("keyword", sb.entity().getUsername(), SearchCriteria.Op.LIKE);
    sb.and("state", sb.entity().getState(), Op.IN);
    SearchCriteria<VpnUserVO> sc = sb.create();
    _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    //list only active users
    sc.setParameters("state", State.Active, State.Add);
    if (keyword != null) {
        sc.setParameters("keyword", "%" + keyword + "%");
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (username != null) {
        sc.setParameters("username", username);
    }
    Pair<List<VpnUserVO>, Integer> result = _vpnUsersDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends VpnUser>, Integer>(result.first(), result.second());
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) VpnUserVO(com.cloud.network.VpnUserVO) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) Filter(com.cloud.utils.db.Filter) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair)

Example 40 with Account

use of com.cloud.user.Account in project cloudstack by apache.

the class VpcManagerImpl method updateVpc.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VPC_UPDATE, eventDescription = "updating vpc")
public Vpc updateVpc(final long vpcId, final String vpcName, final String displayText, final String customId, final Boolean displayVpc) {
    CallContext.current().setEventDetails(" Id: " + vpcId);
    final Account caller = CallContext.current().getCallingAccount();
    // Verify input parameters
    final VpcVO vpcToUpdate = _vpcDao.findById(vpcId);
    if (vpcToUpdate == null) {
        throw new InvalidParameterValueException("Unable to find vpc by id " + vpcId);
    }
    _accountMgr.checkAccess(caller, null, false, vpcToUpdate);
    final VpcVO vpc = _vpcDao.createForUpdate(vpcId);
    if (vpcName != null) {
        vpc.setName(vpcName);
    }
    if (displayText != null) {
        vpc.setDisplayText(displayText);
    }
    if (customId != null) {
        vpc.setUuid(customId);
    }
    if (displayVpc != null) {
        vpc.setDisplay(displayVpc);
    }
    if (_vpcDao.update(vpcId, vpc)) {
        s_logger.debug("Updated VPC id=" + vpcId);
        return _vpcDao.findById(vpcId);
    } else {
        return null;
    }
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

Account (com.cloud.user.Account)566 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)254 ArrayList (java.util.ArrayList)152 ActionEvent (com.cloud.event.ActionEvent)114 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)98 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)82 List (java.util.List)80 Test (org.junit.Test)73 User (com.cloud.user.User)66 AccountVO (com.cloud.user.AccountVO)64 DB (com.cloud.utils.db.DB)61 Network (com.cloud.network.Network)60 Pair (com.cloud.utils.Pair)52 DataCenter (com.cloud.dc.DataCenter)49 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)46 Filter (com.cloud.utils.db.Filter)46 CallContext (org.apache.cloudstack.context.CallContext)45 DomainVO (com.cloud.domain.DomainVO)44 TransactionStatus (com.cloud.utils.db.TransactionStatus)44 NetworkVO (com.cloud.network.dao.NetworkVO)43