Search in sources :

Example 16 with LoadBalancerVO

use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method updateLBHealthCheckPolicy.

@Override
@ActionEvent(eventType = EventTypes.EVENT_LB_HEALTHCHECKPOLICY_UPDATE, eventDescription = "updating lb healthcheck policy", async = true)
public HealthCheckPolicy updateLBHealthCheckPolicy(long id, String customId, Boolean forDisplay) {
    LBHealthCheckPolicyVO policy = _lb2healthcheckDao.findById(id);
    if (policy == null) {
        throw new InvalidParameterValueException("Fail to find stickiness policy with " + id);
    }
    LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(policy.getLoadBalancerId()));
    if (loadBalancer == null) {
        throw new InvalidParameterException("Invalid Load balancer : " + policy.getLoadBalancerId() + " for Stickiness policy id: " + id);
    }
    _accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, loadBalancer);
    if (customId != null) {
        policy.setUuid(customId);
    }
    if (forDisplay != null) {
        policy.setDisplay(forDisplay);
    }
    _lb2healthcheckDao.update(id, policy);
    return _lb2healthcheckDao.findById(id);
}
Also used : InvalidParameterException(java.security.InvalidParameterException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LBHealthCheckPolicyVO(com.cloud.network.LBHealthCheckPolicyVO) ActionEvent(com.cloud.event.ActionEvent)

Example 17 with LoadBalancerVO

use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method getExistingDestinations.

@Override
public List<LbDestination> getExistingDestinations(long lbId) {
    List<LbDestination> dstList = new ArrayList<LbDestination>();
    List<LoadBalancerVMMapVO> lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lbId);
    LoadBalancerVO lb = _lbDao.findById(lbId);
    String dstIp = null;
    for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) {
        UserVm vm = _vmDao.findById(lbVmMap.getInstanceId());
        Nic nic = _nicDao.findByInstanceIdAndNetworkIdIncludingRemoved(lb.getNetworkId(), vm.getId());
        dstIp = lbVmMap.getInstanceIp() == null ? nic.getIPv4Address() : lbVmMap.getInstanceIp();
        LbDestination lbDst = new LbDestination(lb.getDefaultPortStart(), lb.getDefaultPortEnd(), dstIp, lbVmMap.isRevoke());
        dstList.add(lbDst);
    }
    return dstList;
}
Also used : UserVm(com.cloud.uservm.UserVm) ArrayList(java.util.ArrayList) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) Nic(com.cloud.vm.Nic) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination)

Example 18 with LoadBalancerVO

use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method updateLoadBalancerRule.

@Override
@ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_UPDATE, eventDescription = "updating load balancer", async = true)
public LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd) {
    Account caller = CallContext.current().getCallingAccount();
    Long lbRuleId = cmd.getId();
    String name = cmd.getLoadBalancerName();
    String description = cmd.getDescription();
    String algorithm = cmd.getAlgorithm();
    LoadBalancerVO lb = _lbDao.findById(lbRuleId);
    LoadBalancerVO lbBackup = _lbDao.findById(lbRuleId);
    String customId = cmd.getCustomId();
    Boolean forDisplay = cmd.getDisplay();
    if (lb == null) {
        throw new InvalidParameterValueException("Unable to find lb rule by id=" + lbRuleId);
    }
    // check permissions
    _accountMgr.checkAccess(caller, null, true, lb);
    if (name != null) {
        lb.setName(name);
    }
    if (description != null) {
        lb.setDescription(description);
    }
    if (algorithm != null) {
        lb.setAlgorithm(algorithm);
    }
    if (customId != null) {
        lb.setUuid(customId);
    }
    if (forDisplay != null) {
        lb.setDisplay(forDisplay);
    }
    // Validate rule in LB provider
    LoadBalancingRule rule = getLoadBalancerRuleToApply(lb);
    if (!validateLbRule(rule)) {
        throw new InvalidParameterValueException("Modifications in lb rule " + lbRuleId + " are not supported.");
    }
    boolean success = _lbDao.update(lbRuleId, lb);
    // If algorithm is changed, have to reapply the lb config
    if (algorithm != null) {
        try {
            lb.setState(FirewallRule.State.Add);
            _lbDao.persist(lb);
            applyLoadBalancerConfig(lbRuleId);
        } catch (ResourceUnavailableException e) {
            if (isRollBackAllowedForProvider(lb)) {
                /*
                     * NOTE : We use lb object to update db instead of lbBackup
                     * object since db layer will fail to update if there is no
                     * change in the object.
                     */
                if (lbBackup.getName() != null) {
                    lb.setName(lbBackup.getName());
                }
                if (lbBackup.getDescription() != null) {
                    lb.setDescription(lbBackup.getDescription());
                }
                if (lbBackup.getAlgorithm() != null) {
                    lb.setAlgorithm(lbBackup.getAlgorithm());
                }
                lb.setState(lbBackup.getState());
                _lbDao.update(lb.getId(), lb);
                _lbDao.persist(lb);
                s_logger.debug("LB Rollback rule id: " + lbRuleId + " while updating LB rule.");
            }
            s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e);
            success = false;
        }
    }
    if (!success) {
        throw new CloudRuntimeException("Failed to update load balancer rule: " + lbRuleId);
    }
    return lb;
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ActionEvent(com.cloud.event.ActionEvent)

Example 19 with LoadBalancerVO

use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method assignSSLCertToLoadBalancerRule.

@Override
public boolean assignSSLCertToLoadBalancerRule(Long lbId, String certName, String publicCert, String privateKey) {
    s_logger.error("Calling the manager for LB");
    LoadBalancerVO loadBalancer = _lbDao.findById(lbId);
    //TODO
    return false;
}
Also used : LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO)

Example 20 with LoadBalancerVO

use of com.cloud.network.dao.LoadBalancerVO in project cloudstack by apache.

the class UsageServiceImpl method getUsageRecords.

@Override
public Pair<List<? extends Usage>, Integer> getUsageRecords(GetUsageRecordsCmd cmd) {
    Long accountId = cmd.getAccountId();
    Long domainId = cmd.getDomainId();
    String accountName = cmd.getAccountName();
    Account userAccount = null;
    Account caller = CallContext.current().getCallingAccount();
    Long usageType = cmd.getUsageType();
    Long projectId = cmd.getProjectId();
    String usageId = cmd.getUsageId();
    if (projectId != null) {
        if (accountId != null) {
            throw new InvalidParameterValueException("Projectid and accountId can't be specified together");
        }
        Project project = _projectMgr.getProject(projectId);
        if (project == null) {
            throw new InvalidParameterValueException("Unable to find project by id " + projectId);
        }
        accountId = project.getProjectAccountId();
    }
    //if accountId is not specified, use accountName and domainId
    if ((accountId == null) && (accountName != null) && (domainId != null)) {
        if (_domainDao.isChildDomain(caller.getDomainId(), domainId)) {
            Filter filter = new Filter(AccountVO.class, "id", Boolean.FALSE, null, null);
            List<AccountVO> accounts = _accountDao.listAccounts(accountName, domainId, filter);
            if (accounts.size() > 0) {
                userAccount = accounts.get(0);
            }
            if (userAccount != null) {
                accountId = userAccount.getId();
            } else {
                throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
            }
        } else {
            throw new PermissionDeniedException("Invalid Domain Id or Account");
        }
    }
    boolean isAdmin = false;
    boolean isDomainAdmin = false;
    //If accountId couldn't be found using accountName and domainId, get it from userContext
    if (accountId == null) {
        accountId = caller.getId();
        //If account_id or account_name is explicitly mentioned, list records for the specified account only even if the caller is of type admin
        if (_accountService.isRootAdmin(caller.getId())) {
            isAdmin = true;
        } else if (_accountService.isDomainAdmin(caller.getId())) {
            isDomainAdmin = true;
        }
        s_logger.debug("Account details not available. Using userContext accountId: " + accountId);
    }
    Date startDate = cmd.getStartDate();
    Date endDate = cmd.getEndDate();
    if (startDate.after(endDate)) {
        throw new InvalidParameterValueException("Incorrect Date Range. Start date: " + startDate + " is after end date:" + endDate);
    }
    TimeZone usageTZ = getUsageTimezone();
    Date adjustedStartDate = computeAdjustedTime(startDate, usageTZ);
    Date adjustedEndDate = computeAdjustedTime(endDate, usageTZ);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("getting usage records for account: " + accountId + ", domainId: " + domainId + ", between " + adjustedStartDate + " and " + adjustedEndDate + ", using pageSize: " + cmd.getPageSizeVal() + " and startIndex: " + cmd.getStartIndex());
    }
    Filter usageFilter = new Filter(UsageVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    SearchCriteria<UsageVO> sc = _usageDao.createSearchCriteria();
    if (accountId != -1 && accountId != Account.ACCOUNT_ID_SYSTEM && !isAdmin && !isDomainAdmin) {
        sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);
    }
    if (isDomainAdmin) {
        SearchCriteria<DomainVO> sdc = _domainDao.createSearchCriteria();
        sdc.addOr("path", SearchCriteria.Op.LIKE, _domainDao.findById(caller.getDomainId()).getPath() + "%");
        List<DomainVO> domains = _domainDao.search(sdc, null);
        List<Long> domainIds = new ArrayList<Long>();
        for (DomainVO domain : domains) domainIds.add(domain.getId());
        sc.addAnd("domainId", SearchCriteria.Op.IN, domainIds.toArray());
    }
    if (domainId != null) {
        sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
    }
    if (usageType != null) {
        sc.addAnd("usageType", SearchCriteria.Op.EQ, usageType);
    }
    if (usageId != null) {
        if (usageType == null) {
            throw new InvalidParameterValueException("Usageid must be specified together with usageType");
        }
        Long usageDbId = null;
        switch(usageType.intValue()) {
            case UsageTypes.NETWORK_BYTES_RECEIVED:
            case UsageTypes.NETWORK_BYTES_SENT:
            case UsageTypes.RUNNING_VM:
            case UsageTypes.ALLOCATED_VM:
            case UsageTypes.VM_SNAPSHOT:
                VMInstanceVO vm = _vmDao.findByUuidIncludingRemoved(usageId);
                if (vm != null) {
                    usageDbId = vm.getId();
                }
                if (vm == null && (usageType == UsageTypes.NETWORK_BYTES_RECEIVED || usageType == UsageTypes.NETWORK_BYTES_SENT)) {
                    HostVO host = _hostDao.findByUuidIncludingRemoved(usageId);
                    if (host != null) {
                        usageDbId = host.getId();
                    }
                }
                break;
            case UsageTypes.SNAPSHOT:
                SnapshotVO snap = _snapshotDao.findByUuidIncludingRemoved(usageId);
                if (snap != null) {
                    usageDbId = snap.getId();
                }
                break;
            case UsageTypes.TEMPLATE:
            case UsageTypes.ISO:
                VMTemplateVO tmpl = _vmTemplateDao.findByUuidIncludingRemoved(usageId);
                if (tmpl != null) {
                    usageDbId = tmpl.getId();
                }
                break;
            case UsageTypes.LOAD_BALANCER_POLICY:
                LoadBalancerVO lb = _lbDao.findByUuidIncludingRemoved(usageId);
                if (lb != null) {
                    usageDbId = lb.getId();
                }
                break;
            case UsageTypes.PORT_FORWARDING_RULE:
                PortForwardingRuleVO pf = _pfDao.findByUuidIncludingRemoved(usageId);
                if (pf != null) {
                    usageDbId = pf.getId();
                }
                break;
            case UsageTypes.VOLUME:
            case UsageTypes.VM_DISK_IO_READ:
            case UsageTypes.VM_DISK_IO_WRITE:
            case UsageTypes.VM_DISK_BYTES_READ:
            case UsageTypes.VM_DISK_BYTES_WRITE:
                VolumeVO volume = _volumeDao.findByUuidIncludingRemoved(usageId);
                if (volume != null) {
                    usageDbId = volume.getId();
                }
                break;
            case UsageTypes.VPN_USERS:
                VpnUserVO vpnUser = _vpnUserDao.findByUuidIncludingRemoved(usageId);
                if (vpnUser != null) {
                    usageDbId = vpnUser.getId();
                }
                break;
            case UsageTypes.SECURITY_GROUP:
                SecurityGroupVO sg = _sgDao.findByUuidIncludingRemoved(usageId);
                if (sg != null) {
                    usageDbId = sg.getId();
                }
                break;
            case UsageTypes.IP_ADDRESS:
                IPAddressVO ip = _ipDao.findByUuidIncludingRemoved(usageId);
                if (ip != null) {
                    usageDbId = ip.getId();
                }
                break;
            default:
                break;
        }
        if (usageDbId != null) {
            sc.addAnd("usageId", SearchCriteria.Op.EQ, usageDbId);
        } else {
            // return an empty list if usageId was not found
            return new Pair<List<? extends Usage>, Integer>(new ArrayList<Usage>(), new Integer(0));
        }
    }
    if ((adjustedStartDate != null) && (adjustedEndDate != null) && adjustedStartDate.before(adjustedEndDate)) {
        sc.addAnd("startDate", SearchCriteria.Op.BETWEEN, adjustedStartDate, adjustedEndDate);
        sc.addAnd("endDate", SearchCriteria.Op.BETWEEN, adjustedStartDate, adjustedEndDate);
    } else {
        // return an empty list if we fail to validate the dates
        return new Pair<List<? extends Usage>, Integer>(new ArrayList<Usage>(), new Integer(0));
    }
    Pair<List<UsageVO>, Integer> usageRecords = null;
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
    try {
        usageRecords = _usageDao.searchAndCountAllRecords(sc, usageFilter);
    } finally {
        txn.close();
        // switch back to VMOPS_DB
        TransactionLegacy swap = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
        swap.close();
    }
    return new Pair<List<? extends Usage>, Integer>(usageRecords.first(), usageRecords.second());
}
Also used : Account(com.cloud.user.Account) VpnUserVO(com.cloud.network.VpnUserVO) ArrayList(java.util.ArrayList) VMTemplateVO(com.cloud.storage.VMTemplateVO) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) AccountVO(com.cloud.user.AccountVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair) PortForwardingRuleVO(com.cloud.network.rules.PortForwardingRuleVO) Usage(org.apache.cloudstack.usage.Usage) SecurityGroupVO(com.cloud.network.security.SecurityGroupVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) Date(java.util.Date) HostVO(com.cloud.host.HostVO) Project(com.cloud.projects.Project) DomainVO(com.cloud.domain.DomainVO) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) TimeZone(java.util.TimeZone) SnapshotVO(com.cloud.storage.SnapshotVO) Filter(com.cloud.utils.db.Filter) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) IPAddressVO(com.cloud.network.dao.IPAddressVO)

Aggregations

LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)56 ArrayList (java.util.ArrayList)29 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)22 Account (com.cloud.user.Account)19 ActionEvent (com.cloud.event.ActionEvent)17 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)17 DB (com.cloud.utils.db.DB)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)15 CallContext (org.apache.cloudstack.context.CallContext)12 LoadBalancerVMMapVO (com.cloud.network.dao.LoadBalancerVMMapVO)11 FirewallRule (com.cloud.network.rules.FirewallRule)11 Ip (com.cloud.utils.net.Ip)11 InvalidParameterException (java.security.InvalidParameterException)11 NetworkVO (com.cloud.network.dao.NetworkVO)10 Network (com.cloud.network.Network)9 IPAddressVO (com.cloud.network.dao.IPAddressVO)9 LoadBalancerDao (com.cloud.network.dao.LoadBalancerDao)9 TransactionStatus (com.cloud.utils.db.TransactionStatus)9 List (java.util.List)9 LbHealthCheckPolicy (com.cloud.network.lb.LoadBalancingRule.LbHealthCheckPolicy)8