Search in sources :

Example 1 with UserContext

use of com.cloud.user.UserContext in project CloudStack-archive by CloudStack-extras.

the class CreatePortForwardingRuleCmd method execute.

@Override
public void execute() throws ResourceUnavailableException {
    UserContext callerContext = UserContext.current();
    boolean success = true;
    PortForwardingRule rule = null;
    try {
        UserContext.current().setEventDetails("Rule Id: " + getEntityId());
        if (getOpenFirewall()) {
            success = success && _firewallService.applyFirewallRules(ipAddressId, callerContext.getCaller());
        }
        success = success && _rulesService.applyPortForwardingRules(ipAddressId, callerContext.getCaller());
        // State is different after the rule is applied, so get new object here
        rule = _entityMgr.findById(PortForwardingRule.class, getEntityId());
        FirewallRuleResponse fwResponse = new FirewallRuleResponse();
        if (rule != null) {
            fwResponse = _responseGenerator.createPortForwardingRuleResponse(rule);
            setResponseObject(fwResponse);
        }
        fwResponse.setResponseName(getCommandName());
    } finally {
        if (!success || rule == null) {
            if (getOpenFirewall()) {
                _firewallService.revokeRelatedFirewallRule(getEntityId(), true);
            }
            _rulesService.revokePortForwardingRule(getEntityId(), true);
            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to apply port forwarding rule");
        }
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) UserContext(com.cloud.user.UserContext) PortForwardingRule(com.cloud.network.rules.PortForwardingRule) FirewallRuleResponse(com.cloud.api.response.FirewallRuleResponse)

Example 2 with UserContext

use of com.cloud.user.UserContext in project CloudStack-archive by CloudStack-extras.

the class CreateFirewallRuleCmd method execute.

@Override
public void execute() throws ResourceUnavailableException {
    UserContext callerContext = UserContext.current();
    boolean success = false;
    FirewallRule rule = _entityMgr.findById(FirewallRule.class, getEntityId());
    try {
        UserContext.current().setEventDetails("Rule Id: " + getEntityId());
        success = _firewallService.applyFirewallRules(rule.getSourceIpAddressId(), callerContext.getCaller());
        // State is different after the rule is applied, so get new object here
        rule = _entityMgr.findById(FirewallRule.class, getEntityId());
        FirewallResponse fwResponse = new FirewallResponse();
        if (rule != null) {
            fwResponse = _responseGenerator.createFirewallResponse(rule);
            setResponseObject(fwResponse);
        }
        fwResponse.setResponseName(getCommandName());
    } finally {
        if (!success || rule == null) {
            _firewallService.revokeFirewallRule(getEntityId(), true);
            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create firewall rule");
        }
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) UserContext(com.cloud.user.UserContext) FirewallRule(com.cloud.network.rules.FirewallRule) FirewallResponse(com.cloud.api.response.FirewallResponse)

Example 3 with UserContext

use of com.cloud.user.UserContext in project CloudStack-archive by CloudStack-extras.

the class BaseAsyncCmd method saveCompletedEvent.

protected long saveCompletedEvent(String level, String eventType, String description, Long startEventId) {
    UserContext ctx = UserContext.current();
    Long userId = ctx.getCallerUserId();
    userId = (userId == null) ? User.UID_SYSTEM : userId;
    Long startEvent = startEventId;
    if (startEvent == null) {
        startEvent = 0L;
    }
    return _mgr.saveCompletedEvent((userId == null) ? User.UID_SYSTEM : userId, getEntityOwnerId(), level, eventType, description, startEvent);
}
Also used : UserContext(com.cloud.user.UserContext)

Example 4 with UserContext

use of com.cloud.user.UserContext in project CloudStack-archive by CloudStack-extras.

the class BaseAsyncCmd method saveStartedEvent.

protected long saveStartedEvent(String eventType, String description, Long startEventId) {
    UserContext ctx = UserContext.current();
    Long userId = ctx.getCallerUserId();
    userId = (userId == null) ? User.UID_SYSTEM : userId;
    Long startEvent = startEventId;
    if (startEvent == null) {
        startEvent = 0L;
    }
    return _mgr.saveStartedEvent((userId == null) ? User.UID_SYSTEM : userId, getEntityOwnerId(), eventType, description, startEvent);
}
Also used : UserContext(com.cloud.user.UserContext)

Example 5 with UserContext

use of com.cloud.user.UserContext in project CloudStack-archive by CloudStack-extras.

the class CreateLoadBalancerRuleCmd method execute.

@Override
public void execute() throws ResourceAllocationException, ResourceUnavailableException {
    UserContext callerContext = UserContext.current();
    boolean success = true;
    LoadBalancer rule = null;
    try {
        UserContext.current().setEventDetails("Rule Id: " + getEntityId());
        if (getOpenFirewall()) {
            success = success && _firewallService.applyFirewallRules(getSourceIpAddressId(), callerContext.getCaller());
        }
        // State might be different after the rule is applied, so get new object here
        rule = _entityMgr.findById(LoadBalancer.class, getEntityId());
        LoadBalancerResponse lbResponse = new LoadBalancerResponse();
        if (rule != null) {
            lbResponse = _responseGenerator.createLoadBalancerResponse(rule);
            setResponseObject(lbResponse);
        }
        lbResponse.setResponseName(getCommandName());
    } catch (Exception ex) {
        s_logger.warn("Failed to create LB rule due to exception ", ex);
    } finally {
        if (!success || rule == null) {
            if (getOpenFirewall()) {
                _firewallService.revokeRelatedFirewallRule(getEntityId(), true);
            }
            // no need to apply the rule on the backend as it exists in the db only
            _lbService.deleteLoadBalancerRule(getEntityId(), false);
            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create load balancer rule");
        }
    }
}
Also used : LoadBalancerResponse(com.cloud.api.response.LoadBalancerResponse) ServerApiException(com.cloud.api.ServerApiException) UserContext(com.cloud.user.UserContext) LoadBalancer(com.cloud.network.rules.LoadBalancer) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ServerApiException(com.cloud.api.ServerApiException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException)

Aggregations

UserContext (com.cloud.user.UserContext)5 ServerApiException (com.cloud.api.ServerApiException)3 FirewallResponse (com.cloud.api.response.FirewallResponse)1 FirewallRuleResponse (com.cloud.api.response.FirewallRuleResponse)1 LoadBalancerResponse (com.cloud.api.response.LoadBalancerResponse)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)1 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 FirewallRule (com.cloud.network.rules.FirewallRule)1 LoadBalancer (com.cloud.network.rules.LoadBalancer)1 PortForwardingRule (com.cloud.network.rules.PortForwardingRule)1