use of org.apache.cloudstack.api.response.SecurityGroupResponse in project cloudstack by apache.
the class UserVmJoinDaoImpl method setUserVmResponse.
@Override
public UserVmResponse setUserVmResponse(ResponseView view, UserVmResponse userVmData, UserVmJoinVO uvo) {
Long securityGroupId = uvo.getSecurityGroupId();
if (securityGroupId != null && securityGroupId.longValue() != 0) {
SecurityGroupResponse resp = new SecurityGroupResponse();
resp.setId(uvo.getSecurityGroupUuid());
resp.setName(uvo.getSecurityGroupName());
resp.setDescription(uvo.getSecurityGroupDescription());
resp.setObjectName("securitygroup");
if (uvo.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
resp.setProjectId(uvo.getProjectUuid());
resp.setProjectName(uvo.getProjectName());
} else {
resp.setAccountName(uvo.getAccountName());
}
userVmData.addSecurityGroup(resp);
}
long nic_id = uvo.getNicId();
if (nic_id > 0) {
NicResponse nicResponse = new NicResponse();
nicResponse.setId(uvo.getNicUuid());
nicResponse.setIpaddress(uvo.getIpAddress());
nicResponse.setGateway(uvo.getGateway());
nicResponse.setNetmask(uvo.getNetmask());
nicResponse.setNetworkid(uvo.getNetworkUuid());
nicResponse.setNetworkName(uvo.getNetworkName());
nicResponse.setMacAddress(uvo.getMacAddress());
nicResponse.setIp6Address(uvo.getIp6Address());
nicResponse.setIp6Gateway(uvo.getIp6Gateway());
nicResponse.setIp6Cidr(uvo.getIp6Cidr());
if (uvo.getBroadcastUri() != null) {
nicResponse.setBroadcastUri(uvo.getBroadcastUri().toString());
}
if (uvo.getIsolationUri() != null) {
nicResponse.setIsolationUri(uvo.getIsolationUri().toString());
}
if (uvo.getTrafficType() != null) {
nicResponse.setTrafficType(uvo.getTrafficType().toString());
}
if (uvo.getGuestType() != null) {
nicResponse.setType(uvo.getGuestType().toString());
}
nicResponse.setIsDefault(uvo.isDefaultNic());
List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(uvo.getNicId());
if (secondaryIps != null) {
List<NicSecondaryIpResponse> ipList = new ArrayList<NicSecondaryIpResponse>();
for (NicSecondaryIpVO ip : secondaryIps) {
NicSecondaryIpResponse ipRes = new NicSecondaryIpResponse();
ipRes.setId(ip.getUuid());
ipRes.setIpAddr(ip.getIp4Address());
ipList.add(ipRes);
}
nicResponse.setSecondaryIps(ipList);
}
nicResponse.setObjectName("nic");
userVmData.addNic(nicResponse);
}
long tag_id = uvo.getTagId();
if (tag_id > 0 && !userVmData.containTag(tag_id)) {
addTagInformation(uvo, userVmData);
}
Long affinityGroupId = uvo.getAffinityGroupId();
if (affinityGroupId != null && affinityGroupId.longValue() != 0) {
AffinityGroupResponse resp = new AffinityGroupResponse();
resp.setId(uvo.getAffinityGroupUuid());
resp.setName(uvo.getAffinityGroupName());
resp.setDescription(uvo.getAffinityGroupDescription());
resp.setObjectName("affinitygroup");
resp.setAccountName(uvo.getAccountName());
userVmData.addAffinityGroup(resp);
}
return userVmData;
}
use of org.apache.cloudstack.api.response.SecurityGroupResponse 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;
}
use of org.apache.cloudstack.api.response.SecurityGroupResponse in project cloudstack by apache.
the class AuthorizeSecurityGroupEgressCmd method execute.
@Override
public void execute() {
List<? extends SecurityRule> egressRules = _securityGroupService.authorizeSecurityGroupEgress(this);
if (egressRules != null && !egressRules.isEmpty()) {
SecurityGroupResponse response = _responseGenerator.createSecurityGroupResponseFromSecurityGroupRule(egressRules);
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to authorize security group egress rule(s)");
}
}
use of org.apache.cloudstack.api.response.SecurityGroupResponse in project cloudstack by apache.
the class AuthorizeSecurityGroupIngressCmd method execute.
@Override
public void execute() {
List<? extends SecurityRule> ingressRules = _securityGroupService.authorizeSecurityGroupIngress(this);
if (ingressRules != null && !ingressRules.isEmpty()) {
SecurityGroupResponse response = _responseGenerator.createSecurityGroupResponseFromSecurityGroupRule(ingressRules);
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to authorize security group ingress rule(s)");
}
}
use of org.apache.cloudstack.api.response.SecurityGroupResponse in project cloudstack by apache.
the class SecurityGroupJoinDaoImplTest method virtualMachineIDsEmptyTest.
@Test
public void virtualMachineIDsEmptyTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
when(vsg.getId()).thenReturn(1L);
SecurityGroupResponse securityGroupResponse = _securityGroupJoinDaoImpl.newSecurityGroupResponse(vsg, caller);
Field fieldVirtualMachineIds = securityGroupResponse.getClass().getDeclaredField("virtualMachineIds");
fieldVirtualMachineIds.setAccessible(true);
Set<String> virtualMachineIds = (Set<String>) fieldVirtualMachineIds.get(securityGroupResponse);
assertEquals(0, virtualMachineIds.size());
}
Aggregations