Search in sources :

Example 1 with SecurityRule

use of com.cloud.network.security.SecurityRule 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)

Aggregations

SecurityGroupJoinVO (com.cloud.api.query.vo.SecurityGroupJoinVO)1 SecurityRule (com.cloud.network.security.SecurityRule)1 ProjectAccount (com.cloud.projects.ProjectAccount)1 Account (com.cloud.user.Account)1 UserAccount (com.cloud.user.UserAccount)1 HashMap (java.util.HashMap)1 SecurityGroupResponse (org.apache.cloudstack.api.response.SecurityGroupResponse)1 SecurityGroupRuleResponse (org.apache.cloudstack.api.response.SecurityGroupRuleResponse)1