// Add the given nics in the appropriate order
int i = 0;
if (ips != null) {
for (i = 0; i < ips.size(); i++) {
RESTLink source = LinkUtils.getSelfLink(ips.get(i).unwrap());
RESTLink link = new RESTLink("nic" + i, source.getHref());
link.setType(ips.get(i).unwrap().getBaseMediaType());
target.addLink(link);
}
}
// Add unmanaged network references, if given
if (unmanagedNetworks != null) {
for (UnmanagedNetwork unmanaged : unmanagedNetworks) {
RESTLink source = checkNotNull(unmanaged.unwrap().searchLink("ips"), ValidationErrors.MISSING_REQUIRED_LINK
+ "ips");
RESTLink link = new RESTLink("nic" + i, source.getHref());
link.setType(UnmanagedIpDto.BASE_MEDIA_TYPE);
target.addLink(link);
i++;
}
}
VirtualMachineTask task = update(true);
if (gatewayNetwork == null) {
return task;
}
// If there is a gateway network, we have to wait until the network
// configuration links are
// available
if (task != null) {
VirtualMachineState originalState = target.getState();
VirtualMachineMonitor monitor = context.utils().injector().getInstance(MonitoringService.class)
.getVirtualMachineMonitor();
monitor.awaitState(originalState, this);
}
// Set the new network configuration
// Refresh virtual machine, to get the new configuration links
refresh();
VMNetworkConfigurationsDto configs = context.getApi().getCloudApi().listNetworkConfigurations(target);
Iterables.removeIf(target.getLinks(), LinkPredicates.rel(ParentLinkName.NETWORK_GATEWAY));
for (VMNetworkConfigurationDto config : configs.getCollection()) {
if (config.getGateway().equalsIgnoreCase(gatewayNetwork.getGateway())) {
target.addLink(new RESTLink(ParentLinkName.NETWORK_GATEWAY, config.getEditLink().getHref()));
break;
}
}
return update(true);