Search in sources :

Example 1 with SecurityGroupJoinVO

use of com.cloud.api.query.vo.SecurityGroupJoinVO in project cloudstack by apache.

the class ApiResponseHelper method createSecurityGroupResponseFromSecurityGroupRule.

@Override
public SecurityGroupResponse createSecurityGroupResponseFromSecurityGroupRule(List<? extends SecurityRule> securityRules) {
    SecurityGroupResponse response = new SecurityGroupResponse();
    Map<Long, Account> securiytGroupAccounts = new HashMap<Long, Account>();
    if ((securityRules != null) && !securityRules.isEmpty()) {
        SecurityGroupJoinVO securityGroup = ApiDBUtils.findSecurityGroupViewById(securityRules.get(0).getSecurityGroupId()).get(0);
        response.setId(securityGroup.getUuid());
        response.setName(securityGroup.getName());
        response.setDescription(securityGroup.getDescription());
        Account account = securiytGroupAccounts.get(securityGroup.getAccountId());
        if (securityGroup.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
            response.setProjectId(securityGroup.getProjectUuid());
            response.setProjectName(securityGroup.getProjectName());
        } else {
            response.setAccountName(securityGroup.getAccountName());
        }
        response.setDomainId(securityGroup.getDomainUuid());
        response.setDomainName(securityGroup.getDomainName());
        for (SecurityRule securityRule : securityRules) {
            SecurityGroupRuleResponse securityGroupData = new SecurityGroupRuleResponse();
            securityGroupData.setRuleId(securityRule.getUuid());
            securityGroupData.setProtocol(securityRule.getProtocol());
            if ("icmp".equalsIgnoreCase(securityRule.getProtocol())) {
                securityGroupData.setIcmpType(securityRule.getStartPort());
                securityGroupData.setIcmpCode(securityRule.getEndPort());
            } else {
                securityGroupData.setStartPort(securityRule.getStartPort());
                securityGroupData.setEndPort(securityRule.getEndPort());
            }
            Long allowedSecurityGroupId = securityRule.getAllowedNetworkId();
            if (allowedSecurityGroupId != null) {
                List<SecurityGroupJoinVO> sgs = ApiDBUtils.findSecurityGroupViewById(allowedSecurityGroupId);
                if (sgs != null && sgs.size() > 0) {
                    SecurityGroupJoinVO sg = sgs.get(0);
                    securityGroupData.setSecurityGroupName(sg.getName());
                    securityGroupData.setAccountName(sg.getAccountName());
                }
            } else {
                securityGroupData.setCidr(securityRule.getAllowedSourceIpCidr());
            }
            if (securityRule.getRuleType() == SecurityRuleType.IngressRule) {
                securityGroupData.setObjectName("ingressrule");
                response.addSecurityGroupIngressRule(securityGroupData);
            } else {
                securityGroupData.setObjectName("egressrule");
                response.addSecurityGroupEgressRule(securityGroupData);
            }
        }
        response.setObjectName("securitygroup");
    }
    return response;
}
Also used : ProjectAccount(com.cloud.projects.ProjectAccount) UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) HashMap(java.util.HashMap) SecurityGroupResponse(org.apache.cloudstack.api.response.SecurityGroupResponse) SecurityRule(com.cloud.network.security.SecurityRule) SecurityGroupRuleResponse(org.apache.cloudstack.api.response.SecurityGroupRuleResponse) SecurityGroupJoinVO(com.cloud.api.query.vo.SecurityGroupJoinVO)

Example 2 with SecurityGroupJoinVO

use of com.cloud.api.query.vo.SecurityGroupJoinVO in project cloudstack by apache.

the class SecurityGroupManagerImpl 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) Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair)

Example 3 with SecurityGroupJoinVO

use of com.cloud.api.query.vo.SecurityGroupJoinVO in project cloudstack by apache.

the class SecurityGroupJoinDaoImpl method setSecurityGroupResponse.

@Override
public SecurityGroupResponse setSecurityGroupResponse(SecurityGroupResponse vsgData, SecurityGroupJoinVO 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());
        }
        // add the tags to the rule data
        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");
            vsgData.addSecurityGroupIngressRule(ruleData);
        } else {
            ruleData.setObjectName("egressrule");
            vsgData.addSecurityGroupEgressRule(ruleData);
        }
    }
    // 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) {
            vsgData.addTag(ApiDBUtils.newResourceTagResponse(vtag, false));
        }
    }
    return vsgData;
}
Also used : ResourceTagResponse(org.apache.cloudstack.api.response.ResourceTagResponse) ResourceTagJoinVO(com.cloud.api.query.vo.ResourceTagJoinVO) SecurityGroupRuleResponse(org.apache.cloudstack.api.response.SecurityGroupRuleResponse) SecurityGroupJoinVO(com.cloud.api.query.vo.SecurityGroupJoinVO) HashSet(java.util.HashSet)

Example 4 with SecurityGroupJoinVO

use of com.cloud.api.query.vo.SecurityGroupJoinVO 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 5 with SecurityGroupJoinVO

use of com.cloud.api.query.vo.SecurityGroupJoinVO in project cloudstack by apache.

the class ViewResponseHelper method createSecurityGroupResponses.

public static List<SecurityGroupResponse> createSecurityGroupResponses(List<SecurityGroupJoinVO> securityGroups) {
    Account caller = CallContext.current().getCallingAccount();
    Hashtable<Long, SecurityGroupResponse> vrDataList = new Hashtable<Long, SecurityGroupResponse>();
    // Initialise the vrdatalist with the input data
    for (SecurityGroupJoinVO vr : securityGroups) {
        SecurityGroupResponse vrData = vrDataList.get(vr.getId());
        if (vrData == null) {
            // first time encountering this sg
            vrData = ApiDBUtils.newSecurityGroupResponse(vr, caller);
        } else {
            // update rules for 1 to many mapping fields
            vrData = ApiDBUtils.fillSecurityGroupDetails(vrData, vr);
        }
        vrDataList.put(vr.getId(), vrData);
    }
    return new ArrayList<SecurityGroupResponse>(vrDataList.values());
}
Also used : Account(com.cloud.user.Account) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) SecurityGroupResponse(org.apache.cloudstack.api.response.SecurityGroupResponse) SecurityGroupJoinVO(com.cloud.api.query.vo.SecurityGroupJoinVO)

Aggregations

SecurityGroupJoinVO (com.cloud.api.query.vo.SecurityGroupJoinVO)7 ArrayList (java.util.ArrayList)4 Account (com.cloud.user.Account)3 Pair (com.cloud.utils.Pair)3 Filter (com.cloud.utils.db.Filter)3 List (java.util.List)3 SecurityGroupResponse (org.apache.cloudstack.api.response.SecurityGroupResponse)3 SecurityGroupRuleResponse (org.apache.cloudstack.api.response.SecurityGroupRuleResponse)3 ResourceTagJoinVO (com.cloud.api.query.vo.ResourceTagJoinVO)2 SecurityGroupVMMapVO (com.cloud.network.security.SecurityGroupVMMapVO)2 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)2 UserVmVO (com.cloud.vm.UserVmVO)2 HashSet (java.util.HashSet)2 ResourceTagResponse (org.apache.cloudstack.api.response.ResourceTagResponse)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 SecurityRule (com.cloud.network.security.SecurityRule)1 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)1 ProjectAccount (com.cloud.projects.ProjectAccount)1 UserAccount (com.cloud.user.UserAccount)1 Ternary (com.cloud.utils.Ternary)1