Search in sources :

Example 1 with SecurityGroupVMMapVO

use of com.cloud.network.security.SecurityGroupVMMapVO in project cloudstack by apache.

the class SecurityGroupJoinDaoImpl method newSecurityGroupResponse.

@Override
public SecurityGroupResponse newSecurityGroupResponse(SecurityGroupJoinVO vsg, Account caller) {
    SecurityGroupResponse sgResponse = new SecurityGroupResponse();
    sgResponse.setId(vsg.getUuid());
    sgResponse.setName(vsg.getName());
    sgResponse.setDescription(vsg.getDescription());
    ApiResponseHelper.populateOwner(sgResponse, vsg);
    Long rule_id = vsg.getRuleId();
    if (rule_id != null && rule_id.longValue() > 0) {
        SecurityGroupRuleResponse ruleData = new SecurityGroupRuleResponse();
        ruleData.setRuleId(vsg.getRuleUuid());
        ruleData.setProtocol(vsg.getRuleProtocol());
        if ("icmp".equalsIgnoreCase(vsg.getRuleProtocol())) {
            ruleData.setIcmpType(vsg.getRuleStartPort());
            ruleData.setIcmpCode(vsg.getRuleEndPort());
        } else {
            ruleData.setStartPort(vsg.getRuleStartPort());
            ruleData.setEndPort(vsg.getRuleEndPort());
        }
        if (vsg.getRuleAllowedNetworkId() != null) {
            List<SecurityGroupJoinVO> sgs = this.searchByIds(vsg.getRuleAllowedNetworkId());
            if (sgs != null && sgs.size() > 0) {
                SecurityGroupJoinVO sg = sgs.get(0);
                ruleData.setSecurityGroupName(sg.getName());
                ruleData.setAccountName(sg.getAccountName());
            }
        } else {
            ruleData.setCidr(vsg.getRuleAllowedSourceIpCidr());
        }
        // list the tags by rule uuid
        List<ResourceTagJoinVO> tags = _resourceTagJoinDao.listBy(vsg.getRuleUuid(), ResourceTag.ResourceObjectType.SecurityGroupRule);
        Set<ResourceTagResponse> tagResponse = new HashSet<ResourceTagResponse>();
        for (ResourceTagJoinVO tag : tags) {
            tagResponse.add(ApiDBUtils.newResourceTagResponse(tag, false));
        }
        // add the tags to the rule data
        ruleData.setTags(tagResponse);
        if (vsg.getRuleType() == SecurityRuleType.IngressRule) {
            ruleData.setObjectName("ingressrule");
            sgResponse.addSecurityGroupIngressRule(ruleData);
        } else {
            ruleData.setObjectName("egressrule");
            sgResponse.addSecurityGroupEgressRule(ruleData);
        }
    }
    List<SecurityGroupVMMapVO> securityGroupVmMap = _securityGroupVMMapDao.listBySecurityGroup(vsg.getId());
    s_logger.debug("newSecurityGroupResponse() -> virtualmachine count: " + securityGroupVmMap.size());
    sgResponse.setVirtualMachineCount(securityGroupVmMap.size());
    for (SecurityGroupVMMapVO securityGroupVMMapVO : securityGroupVmMap) {
        final UserVmVO userVmVO = _userVmDao.findById(securityGroupVMMapVO.getInstanceId());
        if (userVmVO != null) {
            sgResponse.addVirtualMachineId(userVmVO.getUuid());
        }
    }
    // update tag information
    Long tag_id = vsg.getTagId();
    if (tag_id != null && tag_id.longValue() > 0) {
        ResourceTagJoinVO vtag = ApiDBUtils.findResourceTagViewById(tag_id);
        if (vtag != null) {
            sgResponse.addTag(ApiDBUtils.newResourceTagResponse(vtag, false));
        }
    }
    // set async job
    if (vsg.getJobId() != null) {
        sgResponse.setJobId(vsg.getJobUuid());
        sgResponse.setJobStatus(vsg.getJobStatus());
    }
    sgResponse.setObjectName("securitygroup");
    return sgResponse;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) SecurityGroupResponse(org.apache.cloudstack.api.response.SecurityGroupResponse) SecurityGroupRuleResponse(org.apache.cloudstack.api.response.SecurityGroupRuleResponse) SecurityGroupJoinVO(com.cloud.api.query.vo.SecurityGroupJoinVO) ResourceTagResponse(org.apache.cloudstack.api.response.ResourceTagResponse) ResourceTagJoinVO(com.cloud.api.query.vo.ResourceTagJoinVO) SecurityGroupVMMapVO(com.cloud.network.security.SecurityGroupVMMapVO) HashSet(java.util.HashSet)

Example 2 with SecurityGroupVMMapVO

use of com.cloud.network.security.SecurityGroupVMMapVO in project cloudstack by apache.

the class QueryManagerImpl method listSecurityGroupRulesByVM.

private Pair<List<SecurityGroupJoinVO>, Integer> listSecurityGroupRulesByVM(long vmId, long pageInd, long pageSize) {
    Filter sf = new Filter(SecurityGroupVMMapVO.class, null, true, pageInd, pageSize);
    Pair<List<SecurityGroupVMMapVO>, Integer> sgVmMappingPair = _securityGroupVMMapDao.listByInstanceId(vmId, sf);
    Integer count = sgVmMappingPair.second();
    if (count.intValue() == 0) {
        // handle empty result cases
        return new Pair<List<SecurityGroupJoinVO>, Integer>(new ArrayList<SecurityGroupJoinVO>(), count);
    }
    List<SecurityGroupVMMapVO> sgVmMappings = sgVmMappingPair.first();
    Long[] sgIds = new Long[sgVmMappings.size()];
    int i = 0;
    for (SecurityGroupVMMapVO sgVm : sgVmMappings) {
        sgIds[i++] = sgVm.getSecurityGroupId();
    }
    List<SecurityGroupJoinVO> sgs = _securityGroupJoinDao.searchByIds(sgIds);
    return new Pair<List<SecurityGroupJoinVO>, Integer>(sgs, count);
}
Also used : SecurityGroupJoinVO(com.cloud.api.query.vo.SecurityGroupJoinVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) List(java.util.List) SecurityGroupVMMapVO(com.cloud.network.security.SecurityGroupVMMapVO) Pair(com.cloud.utils.Pair)

Aggregations

SecurityGroupJoinVO (com.cloud.api.query.vo.SecurityGroupJoinVO)2 SecurityGroupVMMapVO (com.cloud.network.security.SecurityGroupVMMapVO)2 ResourceTagJoinVO (com.cloud.api.query.vo.ResourceTagJoinVO)1 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)1 Pair (com.cloud.utils.Pair)1 Filter (com.cloud.utils.db.Filter)1 UserVmVO (com.cloud.vm.UserVmVO)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ResourceTagResponse (org.apache.cloudstack.api.response.ResourceTagResponse)1 SecurityGroupResponse (org.apache.cloudstack.api.response.SecurityGroupResponse)1 SecurityGroupRuleResponse (org.apache.cloudstack.api.response.SecurityGroupRuleResponse)1