Search in sources :

Example 6 with IPAddressResponse

use of com.cloud.api.response.IPAddressResponse in project cosmic by MissionCriticalCloud.

the class ListPublicIpAddressesCmd method execute.

@Override
public void execute() {
    final Pair<List<? extends IpAddress>, Integer> result = _mgr.searchForIPAddresses(this);
    final ListResponse<IPAddressResponse> response = new ListResponse<>();
    final List<IPAddressResponse> ipAddrResponses = new ArrayList<>();
    for (final IpAddress ipAddress : result.first()) {
        final IPAddressResponse ipResponse = _responseGenerator.createIPAddressResponse(ResponseView.Restricted, ipAddress);
        ipResponse.setObjectName("publicipaddress");
        ipAddrResponses.add(ipResponse);
    }
    response.setResponses(ipAddrResponses, result.second());
    response.setResponseName(getCommandName());
    setResponseObject(response);
}
Also used : ListResponse(com.cloud.api.response.ListResponse) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IpAddress(com.cloud.network.IpAddress) IPAddressResponse(com.cloud.api.response.IPAddressResponse)

Example 7 with IPAddressResponse

use of com.cloud.api.response.IPAddressResponse in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createIPAddressResponse.

@Override
public IPAddressResponse createIPAddressResponse(final ResponseView view, final IpAddress ipAddr) {
    final VlanVO vlan = ApiDBUtils.findVlanById(ipAddr.getVlanId());
    final boolean forVirtualNetworks = vlan.getVlanType().equals(VlanType.VirtualNetwork);
    final long zoneId = ipAddr.getDataCenterId();
    final IPAddressResponse ipResponse = new IPAddressResponse();
    ipResponse.setId(ipAddr.getUuid());
    ipResponse.setIpAddress(ipAddr.getAddress().toString());
    if (ipAddr.getAllocatedTime() != null) {
        ipResponse.setAllocated(ipAddr.getAllocatedTime());
    }
    final DataCenter zone = ApiDBUtils.findZoneById(ipAddr.getDataCenterId());
    if (zone != null) {
        ipResponse.setZoneId(zone.getUuid());
        ipResponse.setZoneName(zone.getName());
    }
    ipResponse.setSourceNat(ipAddr.isSourceNat());
    ipResponse.setIsSystem(ipAddr.getSystem());
    // get account information
    if (ipAddr.getAllocatedToAccountId() != null) {
        populateOwner(ipResponse, ipAddr);
    }
    ipResponse.setForVirtualNetwork(forVirtualNetworks);
    ipResponse.setStaticNat(ipAddr.isOneToOneNat());
    if (ipAddr.getAssociatedWithVmId() != null) {
        final UserVm vm = ApiDBUtils.findUserVmById(ipAddr.getAssociatedWithVmId());
        if (vm != null) {
            ipResponse.setVirtualMachineId(vm.getUuid());
            ipResponse.setVirtualMachineName(vm.getHostName());
            if (vm.getDisplayName() != null) {
                ipResponse.setVirtualMachineDisplayName(vm.getDisplayName());
            } else {
                ipResponse.setVirtualMachineDisplayName(vm.getHostName());
            }
        }
    }
    if (ipAddr.getVmIp() != null) {
        ipResponse.setVirtualMachineIp(ipAddr.getVmIp());
    }
    if (ipAddr.getAssociatedWithNetworkId() != null) {
        final Network ntwk = ApiDBUtils.findNetworkById(ipAddr.getAssociatedWithNetworkId());
        if (ntwk != null) {
            ipResponse.setAssociatedNetworkId(ntwk.getUuid());
            ipResponse.setAssociatedNetworkName(ntwk.getName());
        }
    }
    if (ipAddr.getVpcId() != null) {
        final Vpc vpc = ApiDBUtils.findVpcById(ipAddr.getVpcId());
        if (vpc != null) {
            ipResponse.setVpcId(vpc.getUuid());
        }
    }
    // Network id the ip is associated with (if associated networkId is
    // null, try to get this information from vlan)
    final Long vlanNetworkId = ApiDBUtils.getVlanNetworkId(ipAddr.getVlanId());
    // Network id the ip belongs to
    final Long networkId;
    if (vlanNetworkId != null) {
        networkId = vlanNetworkId;
    } else {
        networkId = ApiDBUtils.getPublicNetworkIdByZone(zoneId);
    }
    if (networkId != null) {
        final NetworkVO nw = ApiDBUtils.findNetworkById(networkId);
        if (nw != null) {
            ipResponse.setNetworkId(nw.getUuid());
            ipResponse.setAssociatedNetworkName(nw.getName());
        }
    }
    ipResponse.setState(ipAddr.getState().toString());
    final NetworkACL acl = ApiDBUtils.findByNetworkACLId(ipAddr.getIpACLId());
    if (acl != null) {
        ipResponse.setAclId(acl.getUuid());
        ipResponse.setAclName(acl.getName());
    }
    if (ipAddr.getPhysicalNetworkId() != null) {
        final PhysicalNetworkVO pnw = ApiDBUtils.findPhysicalNetworkById(ipAddr.getPhysicalNetworkId());
        if (pnw != null) {
            ipResponse.setPhysicalNetworkId(pnw.getUuid());
        }
    }
    // show this info to full view only
    if (view == ResponseView.Full) {
        final VlanVO vl = ApiDBUtils.findVlanById(ipAddr.getVlanId());
        if (vl != null) {
            ipResponse.setVlanId(vl.getUuid());
            ipResponse.setVlanName(vl.getVlanTag());
        }
    }
    if (ipAddr.getSystem()) {
        if (ipAddr.isOneToOneNat()) {
            ipResponse.setPurpose(IpAddress.Purpose.StaticNat.toString());
        } else {
            ipResponse.setPurpose(IpAddress.Purpose.Lb.toString());
        }
    }
    ipResponse.setForDisplay(ipAddr.isDisplay());
    // set tag information
    final List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.PublicIpAddress, ipAddr.getId());
    final List<ResourceTagResponse> tagResponses = new ArrayList<>();
    for (final ResourceTag tag : tags) {
        final ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        if (tagResponse != null) {
            tagResponses.add(tagResponse);
        }
    }
    ipResponse.setTags(tagResponses);
    ipResponse.setObjectName("ipaddress");
    return ipResponse;
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) Vpc(com.cloud.legacymodel.network.vpc.Vpc) ArrayList(java.util.ArrayList) NetworkACL(com.cloud.legacymodel.network.vpc.NetworkACL) UserVm(com.cloud.uservm.UserVm) DataCenter(com.cloud.legacymodel.dc.DataCenter) ResourceTag(com.cloud.server.ResourceTag) PhysicalNetwork(com.cloud.network.PhysicalNetwork) Network(com.cloud.legacymodel.network.Network) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) ResourceTagResponse(com.cloud.api.response.ResourceTagResponse) VlanVO(com.cloud.dc.VlanVO) IPAddressResponse(com.cloud.api.response.IPAddressResponse)

Example 8 with IPAddressResponse

use of com.cloud.api.response.IPAddressResponse in project cosmic by MissionCriticalCloud.

the class AssociateIPAddrCmdByAdmin method execute.

@Override
public void execute() throws ResourceUnavailableException, ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
    CallContext.current().setEventDetails("Ip Id: " + getEntityId());
    IpAddress result = null;
    if (getVpcId() != null) {
        result = _vpcService.associateIPToVpc(getEntityId(), getVpcId());
    } else if (getNetworkId() != null) {
        result = _networkService.associateIPToNetwork(getEntityId(), getNetworkId());
    }
    if (result != null) {
        final IPAddressResponse ipResponse = _responseGenerator.createIPAddressResponse(ResponseView.Full, result);
        ipResponse.setResponseName(getCommandName());
        setResponseObject(ipResponse);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to assign ip address");
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) IpAddress(com.cloud.network.IpAddress) IPAddressResponse(com.cloud.api.response.IPAddressResponse)

Aggregations

IPAddressResponse (com.cloud.api.response.IPAddressResponse)8 IpAddress (com.cloud.network.IpAddress)7 ArrayList (java.util.ArrayList)4 ServerApiException (com.cloud.api.ServerApiException)3 ListResponse (com.cloud.api.response.ListResponse)3 List (java.util.List)2 ResourceTagResponse (com.cloud.api.response.ResourceTagResponse)1 VlanVO (com.cloud.dc.VlanVO)1 DataCenter (com.cloud.legacymodel.dc.DataCenter)1 Network (com.cloud.legacymodel.network.Network)1 NetworkACL (com.cloud.legacymodel.network.vpc.NetworkACL)1 Vpc (com.cloud.legacymodel.network.vpc.Vpc)1 PhysicalNetwork (com.cloud.network.PhysicalNetwork)1 NetworkVO (com.cloud.network.dao.NetworkVO)1 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)1 ResourceTag (com.cloud.server.ResourceTag)1 UserVm (com.cloud.uservm.UserVm)1