@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();
}
}