Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/745#discussion_r124042243
--- Diff:
locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/networking/creator/DefaultAzureArmNetworkCreator.java
---
@@ -64,57 +68,71 @@ public static void
createDefaultNetworkAndAddToTemplateOptionsIfRequired(Compute
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);
+
//Only create a default network if we haven't specified a network
name (in template options or config) or ip options
if (config.containsKey(NETWORK_NAME)) {
- LOG.debug("Network config specified when provisioning Azure
machine. Not creating default network");
+ LOG.debug("Network config [{}] specified when provisioning
Azure machine. Not creating default network", NETWORK_NAME.getName());
return;
}
- if (templateOptions != null &&
(templateOptions.containsKey(NETWORK_NAME.getName()) ||
templateOptions.containsKey("ipOptions"))) {
+ 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 location = config.get(CLOUD_REGION_ID);
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);
+
//Check if default already exists
- Subnet preexistingSubnet = api.getSubnetApi(resourceGroupName,
vnetName).get(subnetName);
+ 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);
- 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.SubnetProperties subnetProperties =
Subnet.SubnetProperties.builder().addressPrefix(DEFAULT_SUBNET_ADDRESS_PREFIX).build();
- createResourceGroupIfNeeded(api, resourceGroupName, location);
+ //Creating network + subnet if network doesn't exist
+ 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);
- //Setup properties for creating subnet/network
- Subnet subnet = Subnet.create(subnetName, null, null,
-
Subnet.SubnetProperties.builder().addressPrefix(DEFAULT_SUBNET_ADDRESS_PREFIX).build());
+ Subnet subnet = Subnet.create(subnetName, null, null,
subnetProperties);
- VirtualNetwork.VirtualNetworkProperties virtualNetworkProperties =
VirtualNetwork.VirtualNetworkProperties
-
.builder().addressSpace(VirtualNetwork.AddressSpace.create(Arrays.asList(DEFAULT_VNET_ADDRESS_PREFIX)))
- .subnets(Arrays.asList(subnet)).build();
+ VirtualNetwork.VirtualNetworkProperties
virtualNetworkProperties = VirtualNetwork.VirtualNetworkProperties
+
.builder().addressSpace(VirtualNetwork.AddressSpace.create(Arrays.asList(DEFAULT_VNET_ADDRESS_PREFIX)))
+ .subnets(Arrays.asList(subnet)).build();
+ virtualNetworkApi.createOrUpdate(vnetName, location,
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);
- //Create network
-
api.getVirtualNetworkApi(resourceGroupName).createOrUpdate(vnetName, location,
virtualNetworkProperties);
+ //Just creating the subnet
+ subnetApi.createOrUpdate(subnetName, subnetProperties);
+ }
+
+ //Get created subnet
Subnet createdSubnet = api.getSubnetApi(resourceGroupName,
vnetName).get(subnetName);
--- End diff --
You already have `subnetApi` available here, could reuse it.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---