| // Licensed to the Apache Software Foundation (ASF) under one | |
| // or more contributor license agreements. See the NOTICE file | |
| // distributed with this work for additional information | |
| // regarding copyright ownership. The ASF licenses this file | |
| // to you under the Apache License, Version 2.0 (the | |
| // "License"); you may not use this file except in compliance | |
| // with the License. You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, | |
| // software distributed under the License is distributed on an | |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| // KIND, either express or implied. See the License for the | |
| // specific language governing permissions and limitations | |
| // under the License. | |
| package com.cloud.alert; | |
| import org.apache.cloudstack.alert.ResourceAlertService; | |
| import java.util.ArrayList; | |
| import java.util.Date; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Objects; | |
| import java.util.stream.Collectors; | |
| import javax.inject.Inject; | |
| import org.apache.cloudstack.acl.ControlledEntity; | |
| import org.apache.cloudstack.api.InternalIdentity; | |
| import org.apache.cloudstack.api.response.ListResponse; | |
| import org.apache.cloudstack.api.command.user.resourcealert.CreateResourceAlertRuleCmd; | |
| import org.apache.cloudstack.api.command.user.resourcealert.DeleteResourceAlertRuleCmd; | |
| import org.apache.cloudstack.api.command.user.resourcealert.ListResourceAlertRulesCmd; | |
| import org.apache.cloudstack.api.command.user.resourcealert.ListResourceAlertsCmd; | |
| import org.apache.cloudstack.api.command.user.resourcealert.UpdateResourceAlertRuleCmd; | |
| import org.apache.cloudstack.api.response.ResourceAlertResponse; | |
| import org.apache.cloudstack.api.response.ResourceAlertRuleResponse; | |
| import com.cloud.alert.dao.ResourceAlertDao; | |
| import com.cloud.alert.dao.ResourceAlertRuleDao; | |
| import com.cloud.api.query.dao.ResourceAlertRuleJoinDao; | |
| import com.cloud.alert.dao.ResourceAlertRuleWebhookDao; | |
| import com.cloud.api.query.vo.ResourceAlertRuleJoinVO; | |
| import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; | |
| import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; | |
| import org.apache.cloudstack.webhook.WebhookHelper; | |
| import org.apache.commons.lang3.EnumUtils; | |
| import org.springframework.beans.factory.NoSuchBeanDefinitionException; | |
| import org.apache.commons.lang3.StringUtils; | |
| import com.cloud.event.ActionEvent; | |
| import com.cloud.event.EventTypes; | |
| import com.cloud.exception.InvalidParameterValueException; | |
| import com.cloud.exception.PermissionDeniedException; | |
| import com.cloud.host.HostVO; | |
| import com.cloud.host.dao.HostDao; | |
| import com.cloud.hypervisor.Hypervisor.HypervisorType; | |
| import com.cloud.projects.Project; | |
| import com.cloud.storage.VolumeVO; | |
| import com.cloud.storage.dao.VolumeDao; | |
| import com.cloud.user.Account; | |
| import com.cloud.user.AccountManager; | |
| import com.cloud.utils.Pair; | |
| import com.cloud.utils.Ternary; | |
| import com.cloud.utils.component.ComponentContext; | |
| import com.cloud.utils.component.ManagerBase; | |
| import com.cloud.utils.db.Filter; | |
| import com.cloud.utils.db.GlobalLock; | |
| import com.cloud.utils.db.SearchBuilder; | |
| import com.cloud.utils.db.SearchCriteria; | |
| import com.cloud.utils.exception.CloudRuntimeException; | |
| import com.cloud.vm.UserVmVO; | |
| import com.cloud.vm.dao.UserVmDao; | |
| import org.apache.cloudstack.context.CallContext; | |
| public class ResourceAlertServiceImpl extends ManagerBase implements ResourceAlertService { | |
| static final int OWNER_LOCK_WAIT_SECONDS = 30; | |
| @Inject | |
| AccountManager accountManager; | |
| @Inject | |
| ResourceAlertRuleDao ruleDao; | |
| @Inject | |
| ResourceAlertRuleJoinDao ruleJoinDao; | |
| @Inject | |
| ResourceAlertDao alertDao; | |
| @Inject | |
| ResourceAlertRuleWebhookDao ruleWebhookDao; | |
| @Inject | |
| UserVmDao userVmDao; | |
| @Inject | |
| VolumeDao volumeDao; | |
| @Inject | |
| HostDao hostDao; | |
| @Inject | |
| PrimaryDataStoreDao storagePoolDao; | |
| @Override | |
| @ActionEvent(eventType = EventTypes.EVENT_RESOURCE_ALERT_RULE_CREATE, eventDescription = "creating resource alert rule") | |
| public ResourceAlertRuleResponse createResourceAlertRule(CreateResourceAlertRuleCmd cmd) { | |
| ResourceAlertRule.ResourceType resourceType = parseResourceType(cmd.getResourceType()); | |
| AlertCondition condition = parseCondition(cmd.getCondition()); | |
| AlertSeverity severity = parseSeverity(cmd.getSeverity()); | |
| ResourceAlertMetric metric = parseMetric(cmd.getMetric(), resourceType); | |
| validateThreshold(metric, cmd.getThreshold()); | |
| validateResetInterval(cmd.getResetInterval()); | |
| int resetInterval = cmd.getResetInterval() != null ? cmd.getResetInterval() : ResourceAlertManagerImpl.DEFAULT_RESET_INTERVAL.value(); | |
| boolean email = cmd.getEmail() != null && cmd.getEmail(); | |
| Account caller = CallContext.current().getCallingAccount(); | |
| checkInfrastructureAccess(caller, resourceType); | |
| checkEmailAccess(caller, email); | |
| Account owner = accountManager.finalizeOwner(caller, cmd.getAccountName(), cmd.getDomainId(), cmd.getProjectId()); | |
| long domainId = owner.getDomainId(); | |
| InternalIdentity resource = findResourceOrFail(resourceType, cmd.getResourceId()); | |
| checkResourceReportsMetrics(resource); | |
| if (resource instanceof ControlledEntity) { | |
| accountManager.checkAccess(owner, null, false, (ControlledEntity) resource); | |
| } | |
| Long resourceId = resource != null ? resource.getId() : null; | |
| List<Long> webhookIds = resolveWebhookIds(owner, cmd.getWebhookIds()); | |
| // Held across management servers so parallel creates can't go over the limit or reuse a name. | |
| GlobalLock lock = getOwnerLock(owner.getId()); | |
| if (!lock.lock(OWNER_LOCK_WAIT_SECONDS)) { | |
| throw new CloudRuntimeException("Unable to create the resource alert rule as another rule is being created for the account, please try again"); | |
| } | |
| ResourceAlertRuleVO rule; | |
| try { | |
| int limit = ResourceAlertManagerImpl.RULES_PER_ACCOUNT_LIMIT.valueIn(owner.getId()); | |
| if (limit >= 0 && ruleDao.countActiveByAccountId(owner.getId()) >= limit) { | |
| throw new InvalidParameterValueException( | |
| "Account has reached the maximum of " + limit + " resource alert rules"); | |
| } | |
| String name = validateName(owner.getId(), cmd.getName(), null); | |
| rule = new ResourceAlertRuleVO( | |
| name, resourceType, resourceId, | |
| owner.getId(), domainId, | |
| metric.name(), condition, cmd.getThreshold(), severity, | |
| cmd.getMessage(), email, resetInterval); | |
| ruleDao.persist(rule); | |
| if (!webhookIds.isEmpty()) { | |
| ruleWebhookDao.replaceWebhooksForRule(rule.getId(), webhookIds); | |
| } | |
| } finally { | |
| lock.unlock(); | |
| lock.releaseRef(); | |
| } | |
| CallContext.current().setEventResourceId(rule.getId()); | |
| CallContext.current().setEventDetails("Rule: " + rule.getName()); | |
| return toRuleResponse(ruleJoinDao.findById(rule.getId())); | |
| } | |
| @Override | |
| public ListResponse<ResourceAlertRuleResponse> listResourceAlertRules(ListResourceAlertRulesCmd cmd) { | |
| ResourceAlertRule.ResourceType resourceType = StringUtils.isNotBlank(cmd.getResourceType()) ? | |
| parseResourceType(cmd.getResourceType()) : null; | |
| Long resourceId = null; | |
| if (StringUtils.isNotBlank(cmd.getResourceId())) { | |
| Pair<ResourceAlertRule.ResourceType, Long> resource = findRuleResourceOrFail(resourceType, cmd.getResourceId()); | |
| resourceType = resource.first(); | |
| resourceId = resource.second(); | |
| } | |
| String metric = StringUtils.isNotBlank(cmd.getMetric()) ? parseMetricName(cmd.getMetric()) : null; | |
| AlertSeverity severity = StringUtils.isNotBlank(cmd.getSeverity()) ? parseSeverity(cmd.getSeverity()) : null; | |
| Account caller = CallContext.current().getCallingAccount(); | |
| List<Long> permittedAccounts = new ArrayList<>(); | |
| Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = | |
| new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null); | |
| accountManager.buildACLSearchParameters(caller, cmd.getId(), cmd.getAccountName(), cmd.getProjectId(), | |
| permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false); | |
| SearchBuilder<ResourceAlertRuleJoinVO> sb = createAclSearchBuilder(domainIdRecursiveListProject, permittedAccounts); | |
| sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ); | |
| sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ); | |
| sb.and("keyword", sb.entity().getName(), SearchCriteria.Op.LIKE); | |
| sb.and("resourceType", sb.entity().getResourceType(), SearchCriteria.Op.EQ); | |
| sb.and("resourceId", sb.entity().getResourceId(), SearchCriteria.Op.EQ); | |
| sb.and("metric", sb.entity().getMetric(), SearchCriteria.Op.EQ); | |
| sb.and("severity", sb.entity().getSeverity(), SearchCriteria.Op.EQ); | |
| SearchCriteria<ResourceAlertRuleJoinVO> sc = createAclSearchCriteria(sb, domainIdRecursiveListProject, permittedAccounts); | |
| if (cmd.getId() != null) sc.setParameters("id", cmd.getId()); | |
| if (StringUtils.isNotBlank(cmd.getRuleName())) sc.setParameters("name", cmd.getRuleName()); | |
| if (StringUtils.isNotBlank(cmd.getKeyword())) sc.setParameters("keyword", "%" + cmd.getKeyword() + "%"); | |
| if (resourceType != null) sc.setParameters("resourceType", resourceType); | |
| if (resourceId != null) sc.setParameters("resourceId", resourceId); | |
| if (metric != null) sc.setParameters("metric", metric); | |
| if (severity != null) sc.setParameters("severity", severity); | |
| Filter filter = new Filter(ResourceAlertRuleJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal()); | |
| Pair<List<ResourceAlertRuleJoinVO>, Integer> rules = ruleJoinDao.searchAndCount(sc, filter); | |
| List<ResourceAlertRuleResponse> responses = rules.first().stream() | |
| .map(this::toRuleResponse) | |
| .collect(Collectors.toList()); | |
| ListResponse<ResourceAlertRuleResponse> response = new ListResponse<>(); | |
| response.setResponses(responses, rules.second()); | |
| return response; | |
| } | |
| @Override | |
| @ActionEvent(eventType = EventTypes.EVENT_RESOURCE_ALERT_RULE_UPDATE, eventDescription = "updating resource alert rule") | |
| public ResourceAlertRuleResponse updateResourceAlertRule(UpdateResourceAlertRuleCmd cmd) { | |
| ResourceAlertRuleVO rule = findRuleForCaller(cmd.getId()); | |
| checkEmailAccess(CallContext.current().getCallingAccount(), Boolean.TRUE.equals(cmd.getEmail())); | |
| // Same lock as create, so a rename can't reuse a name taken at the same time. | |
| GlobalLock lock = null; | |
| if (cmd.getName() != null) { | |
| lock = getOwnerLock(rule.getAccountId()); | |
| if (!lock.lock(OWNER_LOCK_WAIT_SECONDS)) { | |
| throw new CloudRuntimeException("Unable to rename the resource alert rule as another rule is being changed for the account, please try again"); | |
| } | |
| } | |
| try { | |
| return applyRuleUpdate(rule, cmd); | |
| } finally { | |
| if (lock != null) { | |
| lock.unlock(); | |
| lock.releaseRef(); | |
| } | |
| } | |
| } | |
| private ResourceAlertRuleResponse applyRuleUpdate(ResourceAlertRuleVO rule, UpdateResourceAlertRuleCmd cmd) { | |
| if (cmd.getName() != null) rule.setName(validateName(rule.getAccountId(), cmd.getName(), rule.getId())); | |
| if (StringUtils.isNotBlank(cmd.getCondition())) rule.setCondition(parseCondition(cmd.getCondition())); | |
| if (cmd.getThreshold() != null) { | |
| validateThreshold(ResourceAlertMetric.valueOf(rule.getMetric()), cmd.getThreshold()); | |
| rule.setThreshold(cmd.getThreshold()); | |
| } | |
| if (StringUtils.isNotBlank(cmd.getSeverity())) rule.setSeverity(parseSeverity(cmd.getSeverity())); | |
| if (cmd.getMessage() != null) rule.setMessage(cmd.getMessage()); | |
| if (cmd.getEmail() != null) rule.setEmail(cmd.getEmail()); | |
| if (cmd.getResetInterval() != null) { | |
| validateResetInterval(cmd.getResetInterval()); | |
| rule.setResetInterval(cmd.getResetInterval()); | |
| } | |
| if (StringUtils.isNotBlank(cmd.getState())) rule.setState(parseState(cmd.getState())); | |
| rule.setUpdated(new Date()); | |
| if (cmd.isCleanupWebhooks()) { | |
| ruleWebhookDao.replaceWebhooksForRule(rule.getId(), new ArrayList<>()); | |
| } else if (cmd.getWebhookIds() != null) { | |
| Account owner = accountManager.getAccount(rule.getAccountId()); | |
| ruleWebhookDao.replaceWebhooksForRule(rule.getId(), resolveWebhookIds(owner, cmd.getWebhookIds())); | |
| } | |
| ruleDao.update(rule.getId(), rule); | |
| return toRuleResponse(ruleJoinDao.findById(rule.getId())); | |
| } | |
| @Override | |
| @ActionEvent(eventType = EventTypes.EVENT_RESOURCE_ALERT_RULE_DELETE, eventDescription = "deleting resource alert rule") | |
| public boolean deleteResourceAlertRule(DeleteResourceAlertRuleCmd cmd) { | |
| findRuleForCaller(cmd.getId()); | |
| // Rule first, so an alert fired by a run in progress is cleared with the history. | |
| boolean removed = ruleDao.remove(cmd.getId()); | |
| alertDao.removeByAlertRuleId(cmd.getId()); | |
| return removed; | |
| } | |
| @Override | |
| public ListResponse<ResourceAlertResponse> listResourceAlerts(ListResourceAlertsCmd cmd) { | |
| Long resourceId = resolveResourceIdFilter(cmd.getResourceType(), cmd.getResourceId()); | |
| List<Long> alertRuleIds = null; | |
| if (cmd.getAlertRuleId() != null) { | |
| ResourceAlertRuleVO rule = ruleDao.findByUuid(cmd.getAlertRuleId()); | |
| if (rule == null) { | |
| throw new InvalidParameterValueException("Alert rule not found: " + cmd.getAlertRuleId()); | |
| } | |
| accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, rule); | |
| alertRuleIds = new ArrayList<>(List.of(rule.getId())); | |
| } | |
| List<Long> visibleRuleIds = listVisibleRuleIds(cmd); | |
| if (visibleRuleIds != null) { | |
| if (alertRuleIds == null) { | |
| alertRuleIds = visibleRuleIds; | |
| } else { | |
| alertRuleIds.retainAll(visibleRuleIds); | |
| } | |
| } | |
| if (StringUtils.isNotBlank(cmd.getResourceType())) { | |
| List<Long> typeRuleIds = ruleDao.listIdsByResourceType(parseResourceType(cmd.getResourceType())); | |
| if (alertRuleIds == null) { | |
| alertRuleIds = new ArrayList<>(typeRuleIds); | |
| } else { | |
| alertRuleIds.retainAll(typeRuleIds); | |
| } | |
| } | |
| if (StringUtils.isNotBlank(cmd.getKeyword())) { | |
| List<Long> matchingRuleIds = ruleDao.listIdsByNameLike(cmd.getKeyword()); | |
| if (alertRuleIds == null) { | |
| alertRuleIds = new ArrayList<>(matchingRuleIds); | |
| } else { | |
| alertRuleIds.retainAll(matchingRuleIds); | |
| } | |
| } | |
| String severity = StringUtils.isNotBlank(cmd.getSeverity()) ? parseSeverity(cmd.getSeverity()).name() : null; | |
| if (alertRuleIds != null && alertRuleIds.isEmpty()) { | |
| ListResponse<ResourceAlertResponse> empty = new ListResponse<>(); | |
| empty.setResponses(new ArrayList<>(), 0); | |
| return empty; | |
| } | |
| Pair<List<ResourceAlertVO>, Integer> alerts = alertDao.searchAndCountByFilters( | |
| alertRuleIds, resourceId, severity, cmd.getStartDate(), cmd.getEndDate(), | |
| cmd.getStartIndex(), cmd.getPageSizeVal()); | |
| Map<Long, ResourceAlertRuleVO> rules = new HashMap<>(); | |
| List<ResourceAlertResponse> responses = alerts.first().stream() | |
| .map(alert -> toAlertResponse(alert, rules.computeIfAbsent(alert.getAlertRuleId(), ruleDao::findByIdIncludingRemoved))) | |
| .collect(Collectors.toList()); | |
| ListResponse<ResourceAlertResponse> response = new ListResponse<>(); | |
| response.setResponses(responses, alerts.second()); | |
| return response; | |
| } | |
| @Override | |
| public List<Class<?>> getCommands() { | |
| List<Class<?>> cmds = new ArrayList<>(); | |
| cmds.add(CreateResourceAlertRuleCmd.class); | |
| cmds.add(ListResourceAlertRulesCmd.class); | |
| cmds.add(UpdateResourceAlertRuleCmd.class); | |
| cmds.add(DeleteResourceAlertRuleCmd.class); | |
| cmds.add(ListResourceAlertsCmd.class); | |
| return cmds; | |
| } | |
| private ResourceAlertRuleResponse toRuleResponse(ResourceAlertRuleJoinVO vo) { | |
| if (vo == null) return null; | |
| ResourceAlertRuleResponse r = new ResourceAlertRuleResponse(); | |
| r.setObjectName("resourcealertrule"); | |
| r.setId(vo.getUuid()); | |
| r.setName(vo.getName()); | |
| r.setResourceType(vo.getResourceType() != null ? vo.getResourceType().name() : null); | |
| Pair<String, String> resource = describeResource(vo.getResourceType(), vo.getResourceId()); | |
| r.setResourceId(resource != null ? resource.first() : null); | |
| r.setResourceName(resource != null ? resource.second() : null); | |
| r.setMetric(vo.getMetric()); | |
| r.setCondition(vo.getCondition() != null ? vo.getCondition().name() : null); | |
| r.setThreshold(vo.getThreshold()); | |
| r.setSeverity(vo.getSeverity() != null ? vo.getSeverity().name() : null); | |
| r.setMessage(vo.getMessage()); | |
| r.setEmail(vo.isEmail()); | |
| r.setResetInterval(vo.getResetInterval()); | |
| r.setState(vo.getState() != null ? vo.getState().name() : null); | |
| List<Pair<String, String>> webhooks = describeWebhooks(vo.getId()); | |
| r.setWebhookIds(webhooks.stream().map(Pair::first).collect(Collectors.toList())); | |
| // Left out when empty so the UI hides it | |
| r.setWebhookNames(webhooks.isEmpty() ? null : webhooks.stream().map(Pair::second).collect(Collectors.toList())); | |
| r.setAccountName(vo.getAccountName()); | |
| r.setDomainId(vo.getDomainUuid()); | |
| r.setDomainName(vo.getDomainName()); | |
| r.setProjectId(vo.getProjectUuid()); | |
| r.setProjectName(vo.getProjectName()); | |
| r.setCreated(vo.getCreated()); | |
| return r; | |
| } | |
| private ResourceAlertResponse toAlertResponse(ResourceAlertVO vo, ResourceAlertRuleVO rule) { | |
| ResourceAlertResponse r = new ResourceAlertResponse(); | |
| r.setObjectName("resourcealert"); | |
| r.setId(vo.getUuid()); | |
| r.setAlertRuleId(rule != null ? rule.getUuid() : null); | |
| Pair<String, String> resource = rule != null ? describeResource(rule.getResourceType(), vo.getResourceId()) : null; | |
| r.setResourceId(resource != null ? resource.first() : null); | |
| r.setResourceName(resource != null ? resource.second() : null); | |
| r.setResourceType(rule != null ? rule.getResourceType().name() : null); | |
| r.setAlertRuleName(rule != null ? rule.getName() : null); | |
| r.setMetricType(vo.getMetricType()); | |
| r.setMetricValue(vo.getMetricValue()); | |
| r.setSeverity(vo.getSeverity() != null ? vo.getSeverity().name() : null); | |
| r.setMessage(vo.getMessage()); | |
| r.setAlertTimestamp(vo.getAlertTimestamp()); | |
| return r; | |
| } | |
| private InternalIdentity findResource(ResourceAlertRule.ResourceType type, String uuid) { | |
| switch (type) { | |
| case VirtualMachine: | |
| return userVmDao.findByUuid(uuid); | |
| case Volume: | |
| return volumeDao.findByUuid(uuid); | |
| case Host: | |
| return hostDao.findByUuid(uuid); | |
| case StoragePool: | |
| return storagePoolDao.findByUuid(uuid); | |
| default: | |
| return null; | |
| } | |
| } | |
| private InternalIdentity findResourceOrFail(ResourceAlertRule.ResourceType type, String uuid) { | |
| if (StringUtils.isBlank(uuid)) { | |
| return null; | |
| } | |
| InternalIdentity resource = findResource(type, uuid); | |
| if (resource == null) { | |
| throw new InvalidParameterValueException("Unable to find " + type.name() + " with ID " + uuid); | |
| } | |
| return resource; | |
| } | |
| // Extensions do not report VM or host usage, so a rule on one of their VMs or hosts would never fire. | |
| private void checkResourceReportsMetrics(InternalIdentity resource) { | |
| HypervisorType hypervisorType = null; | |
| if (resource instanceof UserVmVO) { | |
| hypervisorType = ((UserVmVO) resource).getHypervisorType(); | |
| } else if (resource instanceof HostVO) { | |
| hypervisorType = ((HostVO) resource).getHypervisorType(); | |
| } | |
| if (HypervisorType.External.equals(hypervisorType)) { | |
| throw new InvalidParameterValueException( | |
| "Resource alert rules are not supported on VMs and hosts managed by an extension, as they do not report usage"); | |
| } | |
| } | |
| private Long resolveResourceIdFilter(String resourceType, String uuid) { | |
| if (StringUtils.isBlank(uuid)) { | |
| return null; | |
| } | |
| if (StringUtils.isBlank(resourceType)) { | |
| throw new InvalidParameterValueException("resourcetype is required when resourceid is specified"); | |
| } | |
| return findResourceOrFail(parseResourceType(resourceType), uuid).getId(); | |
| } | |
| // The type is optional here, so the list can be filtered by a resource picked from any type. | |
| private Pair<ResourceAlertRule.ResourceType, Long> findRuleResourceOrFail(ResourceAlertRule.ResourceType type, String uuid) { | |
| if (type != null) { | |
| return new Pair<>(type, findResourceOrFail(type, uuid).getId()); | |
| } | |
| for (ResourceAlertRule.ResourceType candidate : ResourceAlertRule.ResourceType.values()) { | |
| InternalIdentity resource = findResource(candidate, uuid); | |
| if (resource != null) { | |
| return new Pair<>(candidate, resource.getId()); | |
| } | |
| } | |
| throw new InvalidParameterValueException("Unable to find a resource with ID " + uuid); | |
| } | |
| // Returns the resource's uuid and display name, or null when the type or id is not set. | |
| private Pair<String, String> describeResource(ResourceAlertRule.ResourceType type, Long id) { | |
| if (type == null || id == null) { | |
| return null; | |
| } | |
| switch (type) { | |
| case VirtualMachine: { | |
| UserVmVO vm = userVmDao.findByIdIncludingRemoved(id); | |
| return vm == null ? null : new Pair<>(vm.getUuid(), | |
| StringUtils.isNotBlank(vm.getDisplayName()) ? vm.getDisplayName() : vm.getHostName()); | |
| } | |
| case Volume: { | |
| VolumeVO volume = volumeDao.findByIdIncludingRemoved(id); | |
| return volume == null ? null : new Pair<>(volume.getUuid(), volume.getName()); | |
| } | |
| case Host: { | |
| HostVO host = hostDao.findByIdIncludingRemoved(id); | |
| return host == null ? null : new Pair<>(host.getUuid(), host.getName()); | |
| } | |
| case StoragePool: { | |
| StoragePoolVO pool = storagePoolDao.findByIdIncludingRemoved(id); | |
| return pool == null ? null : new Pair<>(pool.getUuid(), pool.getName()); | |
| } | |
| default: | |
| return null; | |
| } | |
| } | |
| protected WebhookHelper getWebhookHelper() { | |
| try { | |
| return ComponentContext.getDelegateComponentOfType(WebhookHelper.class); | |
| } catch (NoSuchBeanDefinitionException e) { | |
| return null; | |
| } | |
| } | |
| private List<Long> resolveWebhookIds(Account owner, List<String> webhookUuids) { | |
| List<Long> ids = new ArrayList<>(); | |
| if (webhookUuids == null || webhookUuids.isEmpty()) { | |
| return ids; | |
| } | |
| WebhookHelper webhookHelper = getWebhookHelper(); | |
| if (webhookHelper == null) { | |
| throw new InvalidParameterValueException("Webhooks are not available, the webhook plugin is not enabled"); | |
| } | |
| for (String uuid : webhookUuids) { | |
| ControlledEntity webhook = webhookHelper.findWebhookByUuid(uuid); | |
| if (!(webhook instanceof InternalIdentity)) { | |
| throw new InvalidParameterValueException("Unable to find webhook with ID " + uuid); | |
| } | |
| accountManager.checkAccess(owner, null, false, webhook); | |
| long id = ((InternalIdentity) webhook).getId(); | |
| if (!ids.contains(id)) { | |
| ids.add(id); | |
| } | |
| } | |
| return ids; | |
| } | |
| private List<Pair<String, String>> describeWebhooks(long ruleId) { | |
| List<Long> ids = ruleWebhookDao.listWebhookIdsByRule(ruleId); | |
| WebhookHelper webhookHelper = ids.isEmpty() ? null : getWebhookHelper(); | |
| if (webhookHelper == null) { | |
| return new ArrayList<>(); | |
| } | |
| return ids.stream().map(webhookHelper::describeWebhook).filter(Objects::nonNull).collect(Collectors.toList()); | |
| } | |
| private void validateThreshold(ResourceAlertMetric metric, Double threshold) { | |
| if (threshold == null || threshold < 0) { | |
| throw new InvalidParameterValueException("threshold must be zero or more"); | |
| } | |
| if (metric.isPercentage() && threshold > 100) { | |
| throw new InvalidParameterValueException("threshold for " + metric.name() + " is a percentage and must be 100 or less"); | |
| } | |
| } | |
| GlobalLock getOwnerLock(long accountId) { | |
| return GlobalLock.getInternLock("ResourceAlertRules.Account." + accountId); | |
| } | |
| // Rules are picked by name in the UI and in alerts, so a name must be set and unique for its owner. | |
| private String validateName(long accountId, String name, Long ruleId) { | |
| String trimmed = StringUtils.trimToEmpty(name); | |
| if (trimmed.isEmpty()) { | |
| throw new InvalidParameterValueException("name cannot be blank"); | |
| } | |
| ResourceAlertRuleVO existing = ruleDao.findActiveByAccountIdAndName(accountId, trimmed); | |
| if (existing != null && (ruleId == null || existing.getId() != ruleId)) { | |
| throw new InvalidParameterValueException("A resource alert rule named " + trimmed + " already exists for this account"); | |
| } | |
| return trimmed; | |
| } | |
| private void validateResetInterval(Integer resetInterval) { | |
| if (resetInterval != null && resetInterval < 0) { | |
| throw new InvalidParameterValueException("resetinterval must be zero or more"); | |
| } | |
| } | |
| private void checkInfrastructureAccess(Account caller, ResourceAlertRule.ResourceType resourceType) { | |
| boolean infra = resourceType == ResourceAlertRule.ResourceType.Host | |
| || resourceType == ResourceAlertRule.ResourceType.StoragePool; | |
| if (infra && !accountManager.isRootAdmin(caller.getId())) { | |
| throw new PermissionDeniedException("Only root admins can create alert rules for " + resourceType.name()); | |
| } | |
| } | |
| private void checkEmailAccess(Account caller, boolean email) { | |
| if (email && !accountManager.isRootAdmin(caller.getId())) { | |
| throw new PermissionDeniedException("Only root admins can enable email for alert rules"); | |
| } | |
| } | |
| private ResourceAlertRuleVO findRuleForCaller(long id) { | |
| ResourceAlertRuleVO rule = ruleDao.findById(id); | |
| if (rule == null || rule.getRemoved() != null) { | |
| throw new InvalidParameterValueException("Alert rule not found"); | |
| } | |
| accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, rule); | |
| CallContext.current().setEventResourceId(rule.getId()); | |
| CallContext.current().setEventDetails("Rule: " + rule.getName()); | |
| return rule; | |
| } | |
| private SearchBuilder<ResourceAlertRuleJoinVO> createAclSearchBuilder( | |
| Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject, List<Long> permittedAccounts) { | |
| SearchBuilder<ResourceAlertRuleJoinVO> sb = ruleJoinDao.createSearchBuilder(); | |
| accountManager.buildACLSearchBuilder(sb, domainIdRecursiveListProject.first(), domainIdRecursiveListProject.second(), | |
| permittedAccounts, domainIdRecursiveListProject.third()); | |
| return sb; | |
| } | |
| private SearchCriteria<ResourceAlertRuleJoinVO> createAclSearchCriteria(SearchBuilder<ResourceAlertRuleJoinVO> sb, | |
| Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject, List<Long> permittedAccounts) { | |
| SearchCriteria<ResourceAlertRuleJoinVO> sc = sb.create(); | |
| accountManager.buildACLSearchCriteria(sc, domainIdRecursiveListProject.first(), domainIdRecursiveListProject.second(), | |
| permittedAccounts, domainIdRecursiveListProject.third()); | |
| return sc; | |
| } | |
| // Returns null when the caller can see alerts of every rule. | |
| private List<Long> listVisibleRuleIds(ListResourceAlertsCmd cmd) { | |
| Account caller = CallContext.current().getCallingAccount(); | |
| List<Long> permittedAccounts = new ArrayList<>(); | |
| Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = | |
| new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null); | |
| accountManager.buildACLSearchParameters(caller, null, cmd.getAccountName(), cmd.getProjectId(), | |
| permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false); | |
| if (permittedAccounts.isEmpty() && domainIdRecursiveListProject.first() == null | |
| && domainIdRecursiveListProject.third() == null) { | |
| return null; | |
| } | |
| SearchBuilder<ResourceAlertRuleJoinVO> sb = createAclSearchBuilder(domainIdRecursiveListProject, permittedAccounts); | |
| SearchCriteria<ResourceAlertRuleJoinVO> sc = createAclSearchCriteria(sb, domainIdRecursiveListProject, permittedAccounts); | |
| return ruleJoinDao.searchIncludingRemoved(sc, null, null, false).stream() | |
| .map(ResourceAlertRuleJoinVO::getId) | |
| .collect(Collectors.toList()); | |
| } | |
| private ResourceAlertRule.ResourceType parseResourceType(String value) { | |
| ResourceAlertRule.ResourceType type = EnumUtils.getEnumIgnoreCase(ResourceAlertRule.ResourceType.class, value); | |
| if (type == null) { | |
| throw new InvalidParameterValueException("Invalid resourcetype: " + value + ". Valid values: VirtualMachine, Volume, Host, StoragePool"); | |
| } | |
| return type; | |
| } | |
| private ResourceAlertRule.State parseState(String value) { | |
| ResourceAlertRule.State state = EnumUtils.getEnumIgnoreCase(ResourceAlertRule.State.class, value); | |
| if (state == null) { | |
| throw new InvalidParameterValueException("Invalid state: " + value + ". Valid values: Enabled, Disabled"); | |
| } | |
| return state; | |
| } | |
| private AlertCondition parseCondition(String value) { | |
| AlertCondition cond = EnumUtils.getEnum(AlertCondition.class, value != null ? value.toUpperCase() : null); | |
| if (cond == null) { | |
| throw new InvalidParameterValueException("Invalid condition: " + value + ". Valid values: GT, GTE, LT, LTE, EQ"); | |
| } | |
| return cond; | |
| } | |
| private AlertSeverity parseSeverity(String value) { | |
| AlertSeverity sev = EnumUtils.getEnum(AlertSeverity.class, value != null ? value.toUpperCase() : null); | |
| if (sev == null) { | |
| throw new InvalidParameterValueException("Invalid severity: " + value + ". Valid values: CRITICAL, HIGH, MEDIUM, LOW"); | |
| } | |
| return sev; | |
| } | |
| private String parseMetricName(String value) { | |
| ResourceAlertMetric metric = EnumUtils.getEnum(ResourceAlertMetric.class, value.toUpperCase()); | |
| if (metric == null) { | |
| throw new InvalidParameterValueException("Invalid metric: " + value); | |
| } | |
| return metric.name(); | |
| } | |
| private ResourceAlertMetric parseMetric(String value, ResourceAlertRule.ResourceType resourceType) { | |
| ResourceAlertMetric metric = EnumUtils.getEnum(ResourceAlertMetric.class, value != null ? value.toUpperCase() : null); | |
| if (metric == null) { | |
| throw new InvalidParameterValueException("Invalid metric: " + value); | |
| } | |
| if (!metric.appliesTo(resourceType)) { | |
| throw new InvalidParameterValueException( | |
| "Metric " + metric.name() + " does not apply to resource type " + resourceType.name()); | |
| } | |
| return metric; | |
| } | |
| } |