Search in sources :

Example 6 with VirtualMachineInterface

use of net.juniper.contrail.api.types.VirtualMachineInterface in project cloudstack by apache.

the class InstanceIpModel method update.

@Override
public void update(ModelController controller) throws InternalErrorException, IOException {
    assert _vmiModel != null;
    ApiConnector api = controller.getApiAccessor();
    VirtualNetworkModel vnModel = _vmiModel.getVirtualNetworkModel();
    assert vnModel != null;
    VirtualMachineInterface vmi = _vmiModel.getVMInterface();
    VirtualNetwork vnet = vnModel.getVirtualNetwork();
    if (vnet == null) {
        vnet = (VirtualNetwork) api.findById(VirtualNetwork.class, _vmiModel.getNetworkUuid());
    }
    String ipid = api.findByName(InstanceIp.class, null, _name);
    if (ipid == null) {
        InstanceIp ip_obj = new InstanceIp();
        ip_obj.setName(_name);
        ip_obj.setVirtualNetwork(vnet);
        if (_ipAddress != null) {
            ip_obj.setAddress(_ipAddress);
        }
        ip_obj.setVirtualMachineInterface(vmi);
        if (!api.create(ip_obj)) {
            throw new InternalErrorException("Unable to create instance-ip " + _name);
        }
        api.read(ip_obj);
        _uuid = ip_obj.getUuid();
        if (_ipAddress == null) {
            if (!api.read(ip_obj)) {
                throw new InternalErrorException("Unable to read instance-ip " + _name);
            }
        }
        _ipAddress = ip_obj.getAddress();
    } else {
        // Ensure that the instance-ip has the correct value and is pointing at the VMI.
        InstanceIp ip_obj = (InstanceIp) api.findById(InstanceIp.class, ipid);
        if (ip_obj == null) {
            throw new InternalErrorException("Unable to read instance-ip " + _name);
        }
        boolean update = false;
        String ipnet_id = ObjectReference.getReferenceListUuid(ip_obj.getVirtualNetwork());
        if (ipnet_id == null || !ipnet_id.equals(_vmiModel.getNetworkUuid())) {
            ip_obj.setVirtualNetwork(vnet);
            update = true;
        }
        if (_ipAddress != null && !ip_obj.getAddress().equals(_ipAddress)) {
            ip_obj.setAddress(_ipAddress);
            update = true;
        }
        String vmi_id = ObjectReference.getReferenceListUuid(ip_obj.getVirtualMachineInterface());
        if (vmi_id == null || !vmi_id.equals(_vmiModel.getUuid())) {
            if (vmi != null) {
                ip_obj.setVirtualMachineInterface(vmi);
                update = true;
            }
        }
        if (update && !api.update(ip_obj)) {
            throw new InternalErrorException("Unable to update instance-ip: " + ip_obj.getName());
        }
        api.read(ip_obj);
        _uuid = ip_obj.getUuid();
        _ipAddress = ip_obj.getAddress();
    }
}
Also used : VirtualNetwork(net.juniper.contrail.api.types.VirtualNetwork) VirtualMachineInterface(net.juniper.contrail.api.types.VirtualMachineInterface) ApiConnector(net.juniper.contrail.api.ApiConnector) InstanceIp(net.juniper.contrail.api.types.InstanceIp) InternalErrorException(com.cloud.exception.InternalErrorException)

Aggregations

VirtualMachineInterface (net.juniper.contrail.api.types.VirtualMachineInterface)6 ApiConnector (net.juniper.contrail.api.ApiConnector)4 IOException (java.io.IOException)3 InternalErrorException (com.cloud.exception.InternalErrorException)2 NicVO (com.cloud.vm.NicVO)2 VMInstanceVO (com.cloud.vm.VMInstanceVO)2 ApiConnectorMock (net.juniper.contrail.api.ApiConnectorMock)2 InstanceIp (net.juniper.contrail.api.types.InstanceIp)2 MacAddressesType (net.juniper.contrail.api.types.MacAddressesType)2 VirtualNetwork (net.juniper.contrail.api.types.VirtualNetwork)2 Test (org.junit.Test)2 Domain (com.cloud.domain.Domain)1 CloudException (com.cloud.exception.CloudException)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 InsufficientVirtualNetworkCapacityException (com.cloud.exception.InsufficientVirtualNetworkCapacityException)1 Network (com.cloud.network.Network)1 NetworkDao (com.cloud.network.dao.NetworkDao)1 NetworkVO (com.cloud.network.dao.NetworkVO)1 UserVm (com.cloud.uservm.UserVm)1