Search in sources :

Example 11 with ResourceTag

use of com.cloud.server.ResourceTag in project cloudstack by apache.

the class ApiResponseHelper method createFirewallResponse.

@Override
public FirewallResponse createFirewallResponse(FirewallRule fwRule) {
    FirewallResponse response = new FirewallResponse();
    response.setId(fwRule.getUuid());
    response.setProtocol(fwRule.getProtocol());
    if (fwRule.getSourcePortStart() != null) {
        response.setStartPort(fwRule.getSourcePortStart());
    }
    if (fwRule.getSourcePortEnd() != null) {
        response.setEndPort(fwRule.getSourcePortEnd());
    }
    List<String> cidrs = ApiDBUtils.findFirewallSourceCidrs(fwRule.getId());
    response.setCidrList(StringUtils.join(cidrs, ","));
    if (fwRule.getTrafficType() == FirewallRule.TrafficType.Ingress) {
        IpAddress ip = ApiDBUtils.findIpAddressById(fwRule.getSourceIpAddressId());
        response.setPublicIpAddressId(ip.getUuid());
        response.setPublicIpAddress(ip.getAddress().addr());
    }
    Network network = ApiDBUtils.findNetworkById(fwRule.getNetworkId());
    response.setNetworkId(network.getUuid());
    FirewallRule.State state = fwRule.getState();
    String stateToSet = state.toString();
    if (state.equals(FirewallRule.State.Revoke)) {
        stateToSet = "Deleting";
    }
    response.setIcmpCode(fwRule.getIcmpCode());
    response.setIcmpType(fwRule.getIcmpType());
    response.setForDisplay(fwRule.isDisplay());
    // set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.FirewallRule, fwRule.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    response.setTags(tagResponses);
    response.setState(stateToSet);
    response.setObjectName("firewallrule");
    return response;
}
Also used : ResourceTag(com.cloud.server.ResourceTag) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) ArrayList(java.util.ArrayList) ResourceTagResponse(org.apache.cloudstack.api.response.ResourceTagResponse) IpAddress(com.cloud.network.IpAddress) FirewallRule(com.cloud.network.rules.FirewallRule) FirewallResponse(org.apache.cloudstack.api.response.FirewallResponse)

Example 12 with ResourceTag

use of com.cloud.server.ResourceTag in project cloudstack by apache.

the class TaggedResourceManagerImpl method deleteTags.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TAGS_DELETE, eventDescription = "deleting resource tags")
public boolean deleteTags(List<String> resourceIds, ResourceObjectType resourceType, Map<String, String> tags) {
    Account caller = CallContext.current().getCallingAccount();
    SearchBuilder<ResourceTagVO> sb = _resourceTagDao.createSearchBuilder();
    sb.and().op("resourceId", sb.entity().getResourceId(), SearchCriteria.Op.IN);
    sb.or("resourceUuid", sb.entity().getResourceUuid(), SearchCriteria.Op.IN);
    sb.cp();
    sb.and("resourceType", sb.entity().getResourceType(), SearchCriteria.Op.EQ);
    SearchCriteria<ResourceTagVO> sc = sb.create();
    sc.setParameters("resourceId", resourceIds.toArray());
    sc.setParameters("resourceUuid", resourceIds.toArray());
    sc.setParameters("resourceType", resourceType);
    List<? extends ResourceTag> resourceTags = _resourceTagDao.search(sc, null);
    ;
    final List<ResourceTag> tagsToRemove = new ArrayList<ResourceTag>();
    // Finalize which tags should be removed
    for (ResourceTag resourceTag : resourceTags) {
        //1) validate the permissions
        Account owner = _accountMgr.getAccount(resourceTag.getAccountId());
        _accountMgr.checkAccess(caller, null, false, owner);
        //2) Only remove tag if it matches key value pairs
        if (tags != null && !tags.isEmpty()) {
            for (String key : tags.keySet()) {
                boolean canBeRemoved = false;
                if (resourceTag.getKey().equalsIgnoreCase(key)) {
                    String value = tags.get(key);
                    if (value != null) {
                        if (resourceTag.getValue().equalsIgnoreCase(value)) {
                            canBeRemoved = true;
                        }
                    } else {
                        canBeRemoved = true;
                    }
                    if (canBeRemoved) {
                        tagsToRemove.add(resourceTag);
                        break;
                    }
                }
            }
        } else {
            tagsToRemove.add(resourceTag);
        }
    }
    if (tagsToRemove.isEmpty()) {
        throw new InvalidParameterValueException("Unable to find tags by parameters specified");
    }
    //Remove the tags
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            for (ResourceTag tagToRemove : tagsToRemove) {
                _resourceTagDao.remove(tagToRemove.getId());
                s_logger.debug("Removed the tag " + tagToRemove);
            }
        }
    });
    return true;
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) ResourceTag(com.cloud.server.ResourceTag) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 13 with ResourceTag

use of com.cloud.server.ResourceTag in project cloudstack by apache.

the class CreateTagsCmd method execute.

@Override
public void execute() {
    List<ResourceTag> tags = _taggedResourceService.createTags(getResourceIds(), getResourceType(), getTags(), getCustomer());
    if (tags != null && !tags.isEmpty()) {
        SuccessResponse response = new SuccessResponse(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create tags");
    }
}
Also used : SuccessResponse(org.apache.cloudstack.api.response.SuccessResponse) ResourceTag(com.cloud.server.ResourceTag) ServerApiException(org.apache.cloudstack.api.ServerApiException)

Aggregations

ResourceTag (com.cloud.server.ResourceTag)13 ArrayList (java.util.ArrayList)12 ResourceTagResponse (org.apache.cloudstack.api.response.ResourceTagResponse)10 Network (com.cloud.network.Network)6 PhysicalNetwork (com.cloud.network.PhysicalNetwork)6 DataCenter (com.cloud.dc.DataCenter)5 FirewallRule (com.cloud.network.rules.FirewallRule)4 IpAddress (com.cloud.network.IpAddress)3 Vpc (com.cloud.network.vpc.Vpc)3 UserVm (com.cloud.uservm.UserVm)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ActionEvent (com.cloud.event.ActionEvent)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 Service (com.cloud.network.Network.Service)2 NetworkVO (com.cloud.network.dao.NetworkVO)2 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)2 NetworkACL (com.cloud.network.vpc.NetworkACL)2 Account (com.cloud.user.Account)2 VMInstanceVO (com.cloud.vm.VMInstanceVO)2