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