Search in sources :

Example 1 with SecurityGroupResponse

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;
}
Also used : NicSecondaryIpResponse(org.apache.cloudstack.api.response.NicSecondaryIpResponse) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) AffinityGroupResponse(org.apache.cloudstack.affinity.AffinityGroupResponse) ArrayList(java.util.ArrayList) SecurityGroupResponse(org.apache.cloudstack.api.response.SecurityGroupResponse) NicResponse(org.apache.cloudstack.api.response.NicResponse)

Example 2 with SecurityGroupResponse

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;
}
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 3 with SecurityGroupResponse

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)");
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) SecurityGroupResponse(org.apache.cloudstack.api.response.SecurityGroupResponse)

Example 4 with SecurityGroupResponse

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)");
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) SecurityGroupResponse(org.apache.cloudstack.api.response.SecurityGroupResponse)

Example 5 with SecurityGroupResponse

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());
}
Also used : Field(java.lang.reflect.Field) Set(java.util.Set) SecurityGroupResponse(org.apache.cloudstack.api.response.SecurityGroupResponse) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

SecurityGroupResponse (org.apache.cloudstack.api.response.SecurityGroupResponse)14 Field (java.lang.reflect.Field)6 Test (org.junit.Test)6 SecurityGroupJoinVO (com.cloud.api.query.vo.SecurityGroupJoinVO)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 ServerApiException (org.apache.cloudstack.api.ServerApiException)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Account (com.cloud.user.Account)2 NicSecondaryIpVO (com.cloud.vm.dao.NicSecondaryIpVO)2 HashMap (java.util.HashMap)2 AffinityGroupResponse (org.apache.cloudstack.affinity.AffinityGroupResponse)2 NicResponse (org.apache.cloudstack.api.response.NicResponse)2 NicSecondaryIpResponse (org.apache.cloudstack.api.response.NicSecondaryIpResponse)2 SecurityGroupRuleResponse (org.apache.cloudstack.api.response.SecurityGroupRuleResponse)2 ResourceTagJoinVO (com.cloud.api.query.vo.ResourceTagJoinVO)1 SecurityGroup (com.cloud.network.security.SecurityGroup)1 SecurityGroupVMMapVO (com.cloud.network.security.SecurityGroupVMMapVO)1 SecurityRule (com.cloud.network.security.SecurityRule)1 ProjectAccount (com.cloud.projects.ProjectAccount)1