DefaultAzureArmNetworkCreator { public static void createDefaultNetworkAndAddToTemplateOptionsIfRequired(ComputeService computeService, ConfigBag config) { if (!config.get(AZURE_ARM_DEFAULT_NETWORK_ENABLED)) { LOG.debug("azure.arm.default.network.enabled is disabled, not creating default network"); return; } String location = config.get(CLOUD_REGION_ID); if(StringUtils.isEmpty(location)) { LOG.debug("No region information, so cannot create a default network"); return; } Map<String, Object> templateOptions = config.get(TEMPLATE_OPTIONS); if (config.containsKey(NETWORK_NAME)) { LOG.debug("Network config [{}] specified when provisioning Azure machine. Not creating default network", NETWORK_NAME.getName()); return; } if (templateOptions != null && (templateOptions.containsKey("networks") || templateOptions.containsKey("ipOptions"))) { LOG.debug("Network config specified when provisioning Azure machine. Not creating default network"); return; } AzureComputeApi api = computeService.getContext().unwrapApi(AzureComputeApi.class); String resourceGroupName = DEFAULT_RESOURCE_GROUP_PREFIX + "-" + location; String vnetName = DEFAULT_NETWORK_NAME_PREFIX + "-" + location; String subnetName = DEFAULT_SUBNET_NAME_PREFIX + "-" + location; SubnetApi subnetApi = api.getSubnetApi(resourceGroupName, vnetName); VirtualNetworkApi virtualNetworkApi = api.getVirtualNetworkApi(resourceGroupName); Subnet preexistingSubnet = subnetApi.get(subnetName); if(preexistingSubnet != null){ LOG.info("Using pre-existing default Azure network [{}] and subnet [{}] when provisioning machine", vnetName, subnetName); updateTemplateOptions(config, preexistingSubnet); return; } createResourceGroupIfNeeded(api, resourceGroupName, location); Subnet.SubnetProperties subnetProperties = Subnet.SubnetProperties.builder().addressPrefix(DEFAULT_SUBNET_ADDRESS_PREFIX).build(); if(virtualNetworkApi.get(vnetName) == null) { LOG.info("Network config not specified when provisioning Azure machine, and default network/subnet does not exists. " + "Creating network [{}] and subnet [{}], and updating template options", vnetName, subnetName); Subnet subnet = Subnet.create(subnetName, null, null, subnetProperties); VirtualNetwork.VirtualNetworkProperties virtualNetworkProperties = VirtualNetwork.VirtualNetworkProperties .builder().addressSpace(AddressSpace.create(Arrays.asList(DEFAULT_VNET_ADDRESS_PREFIX))) .subnets(Arrays.asList(subnet)).build(); virtualNetworkApi.createOrUpdate(vnetName, location, Maps.newHashMap(), virtualNetworkProperties); } else { LOG.info("Network config not specified when provisioning Azure machine, and default subnet does not exists. " + "Creating subnet [{}] on network [{}], and updating template options", subnetName, vnetName); subnetApi.createOrUpdate(subnetName, subnetProperties); } Subnet createdSubnet = api.getSubnetApi(resourceGroupName, vnetName).get(subnetName); CountdownTimer timeout = CountdownTimer.newInstanceStarted(Duration.minutes(new Integer(20))); while (createdSubnet == null || createdSubnet.properties() == null || PROVISIONING_STATE_UPDATING.equals(createdSubnet.properties().provisioningState())) { if (timeout.isExpired()) { throw new IllegalStateException("Creating subnet " + subnetName + " stuck in the updating state, aborting."); } LOG.debug("Created subnet {} is still in updating state, waiting for it to complete", createdSubnet); Duration.sleep(Duration.ONE_SECOND); createdSubnet = api.getSubnetApi(resourceGroupName, vnetName).get(subnetName); } String lastProvisioningState = createdSubnet.properties().provisioningState(); if (!lastProvisioningState.equals(PROVISIONING_STATE_SUCCEEDED)) { LOG.debug("Created subnet {} in wrong state, expected state {} but found {}", new Object[] {subnetName, PROVISIONING_STATE_SUCCEEDED, lastProvisioningState}); throw new IllegalStateException("Created subnet " + subnetName + " in wrong state, expected state " + PROVISIONING_STATE_SUCCEEDED + " but found " + lastProvisioningState ); } updateTemplateOptions(config, createdSubnet); }  static void createDefaultNetworkAndAddToTemplateOptionsIfRequired(ComputeService computeService, ConfigBag config); static final Logger LOG; static ConfigKey<Boolean> AZURE_ARM_DEFAULT_NETWORK_ENABLED; }
@Test public void testPreExisting() throws Exception { ConfigBag configBag = ConfigBag.newInstance(); configBag.put(CLOUD_REGION_ID, TEST_LOCATION); when(subnetApi.get(TEST_SUBNET_NAME)).thenReturn(subnet); DefaultAzureArmNetworkCreator.createDefaultNetworkAndAddToTemplateOptionsIfRequired(computeService, configBag); verify(subnetApi).get(TEST_SUBNET_NAME); verify(subnet).id(); Map<String, Object> templateOptions = configBag.get(TEMPLATE_OPTIONS); Map<String, Object> ipOptions = (Map<String, Object>) ((List)templateOptions.get("ipOptions")).iterator().next(); assertEquals(ipOptions.get("subnet"), TEST_SUBNET_ID); assertEquals(ipOptions.get("allocateNewPublicIp"), true); }
@Test public void testVanillaWhereExistingResourceGroup() throws Exception { ConfigBag configBag = ConfigBag.newInstance(); configBag.put(CLOUD_REGION_ID, TEST_LOCATION); when(subnetApi.get(TEST_SUBNET_NAME)).thenReturn(null).thenReturn(subnet); when(resourceGroupApi.get(TEST_RESOURCE_GROUP)).thenReturn(resourceGroup); when(subnet.properties().provisioningState()).thenReturn("Updating").thenReturn("Succeeded"); DefaultAzureArmNetworkCreator.createDefaultNetworkAndAddToTemplateOptionsIfRequired(computeService, configBag); verify(subnetApi, atLeast(2)).get(TEST_SUBNET_NAME); verify(subnet).id(); verify(resourceGroupApi).get(TEST_RESOURCE_GROUP); verify(resourceGroupApi, never()).create(any(), any(), any()); verify(virtualNetworkApi).createOrUpdate(eq(TEST_NETWORK_NAME), eq(TEST_LOCATION), any(), any()); Map<String, Object> templateOptions = configBag.get(TEMPLATE_OPTIONS); Map<String, Object> ipOptions = (Map<String, Object>) ((List)templateOptions.get("ipOptions")).iterator().next(); assertEquals(ipOptions.get("subnet"), TEST_SUBNET_ID); assertEquals(ipOptions.get("allocateNewPublicIp"), true); }
@Test public void testVanillaWhereExistingNetworkButNoSubnet() throws Exception { ConfigBag configBag = ConfigBag.newInstance(); configBag.put(CLOUD_REGION_ID, TEST_LOCATION); when(subnetApi.get(TEST_SUBNET_NAME)).thenReturn(null).thenReturn(subnet); when(virtualNetworkApi.get(TEST_NETWORK_NAME)).thenReturn(virtualNetwork); when(resourceGroupApi.get(TEST_RESOURCE_GROUP)).thenReturn(resourceGroup); when(subnet.properties().provisioningState()).thenReturn("Updating").thenReturn("Succeeded"); DefaultAzureArmNetworkCreator.createDefaultNetworkAndAddToTemplateOptionsIfRequired(computeService, configBag); verify(subnetApi).createOrUpdate(eq(TEST_SUBNET_NAME), any()); verify(subnetApi, atLeast(2)).get(TEST_SUBNET_NAME); verify(subnet).id(); verify(resourceGroupApi).get(TEST_RESOURCE_GROUP); verify(resourceGroupApi, never()).create(any(), any(), any()); verify(virtualNetworkApi, never()).createOrUpdate(any(), any(), any(), any()); Map<String, Object> templateOptions = configBag.get(TEMPLATE_OPTIONS); Map<String, Object> ipOptions = (Map<String, Object>) ((List)templateOptions.get("ipOptions")).iterator().next(); assertEquals(ipOptions.get("subnet"), TEST_SUBNET_ID); assertEquals(ipOptions.get("allocateNewPublicIp"), true); }