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;
}
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;
}
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");
}
}
Aggregations