Damans227 commented on code in PR #13478: URL: https://github.com/apache/cloudstack/pull/13478#discussion_r4519959832
########## server/src/main/java/com/cloud/alert/ResourceAlertServiceImpl.java: ########## @@ -0,0 +1,641 @@ +// 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); Review Comment: This uses the same buildACLSearchParameters path as the other list APIs, so ResourceAdmin with listall behaves the same here as it does elsewhere. If that scoping needs changing, it would be a core change across all list APIs rather than in this PR. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
