@Override public void execute(CloudStackClient cloudStackClient, Pool pool, DelegateExecution execution) { if (execution.getVariable(ProcessVariables.NETWORK_ID) != null) { LOG.warn("Network process variable ({}) will be overwritten!", ProcessVariables.NETWORK_ID); } Network network; final String existingNetwork = pool.getNetwork().getOption(NetworkOptions.EXISTING_NETWORK_ID); if (existingNetwork != null) { network = checkNotNull(cloudStackClient.getNetworkClient().getNetwork(existingNetwork), "Network with id " + existingNetwork + " does not exist"); } else { final String networkName = Networks.formatNameFromBusinessKey(execution.getProcessBusinessKey()); final String zoneId = pool.getProvider().getOption(ProviderOptions.ZONE_ID); final String networkOfferingId = pool.getProvider().getOption(ProviderOptions.NETWORK_OFFERING_ID); try { network = Networks.getByName(cloudStackClient, networkName); LOG.info("Network with name {} exists.", networkName); } catch (NoSuchElementException e) { LOG.info(String.format("Creating network %s in zone %s with offering %s", networkName, zoneId, networkOfferingId)); network = cloudStackClient.getNetworkClient().createNetworkInZone(zoneId, networkOfferingId, networkName, networkName, CreateNetworkOptions.NONE); } } LOG.info("Storing network id {} in process variable {}", network.getId(), ProcessVariables.NETWORK_ID); execution.setVariable(ProcessVariables.NETWORK_ID, network.getId()); } }
@Test public void testListNetworks() throws Exception { if (!networksSupported) return; Set<Network> response = client.getNetworkClient().listNetworks( accountInDomain(user.getAccount(), user.getDomainId())); assert null != response; long networkCount = response.size(); assertTrue(networkCount >= 0); for (Network network : response) { Network newDetails = getOnlyElement(client.getNetworkClient().listNetworks(id(network.getId()))); assertEquals(network, newDetails); assertEquals(network, client.getNetworkClient().getNetwork(network.getId())); checkNetwork(network); } }