Github user Graeme-Miller commented on a diff in the pull request: https://github.com/apache/brooklyn-server/pull/739#discussion_r123230837 --- Diff: locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/networking/creator/DefaultAzureArmNetworkCreator.java --- @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.location.jclouds.networking.creator; + +import static org.apache.brooklyn.core.location.cloud.CloudLocationConfig.CLOUD_REGION_ID; +import static org.apache.brooklyn.location.jclouds.api.JcloudsLocationConfigPublic.NETWORK_NAME; +import static org.apache.brooklyn.location.jclouds.api.JcloudsLocationConfigPublic.TEMPLATE_OPTIONS; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.ImmutableMap; + +import org.jclouds.azurecompute.arm.AzureComputeApi; +import org.jclouds.azurecompute.arm.domain.ResourceGroup; +import org.jclouds.azurecompute.arm.domain.Subnet; +import org.jclouds.azurecompute.arm.domain.VirtualNetwork; +import org.jclouds.compute.ComputeService; + +import org.apache.brooklyn.util.core.config.ConfigBag; + +public class DefaultAzureArmNetworkCreator { + + public static final Logger LOG = LoggerFactory.getLogger(DefaultAzureArmNetworkCreator.class); + + private static final String DEFAULT_RESOURCE_GROUP = "brooklyn-default-resource-group"; + private static final String DEFAULT_NETWORK_NAME = "brooklyn-default-network"; + private static final String DEFAULT_SUBNET_NAME = "brooklyn-default-subnet"; + + private static final String defaultVnetAddressPrefix = "10.1.0.0/16"; + private static final String defaultSubnetAddressPrefix = "10.1.0.0/24"; + + public static void createDefaultNetworkAndAddToTemplateOptions(ComputeService computeService, ConfigBag config) { + 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.info("Network config specified when creating Azure location. Not creating default network"); + return; + } + if (templateOptions != null && (templateOptions.containsKey(NETWORK_NAME.getName()) || templateOptions.containsKey("ipOptions"))) { + LOG.info("Network config specified when creating Azure location. Not creating default network"); + return; + } + + LOG.info("Network config not specified when creating Azure location. Creating default network if doesn't exist"); + + AzureComputeApi api = computeService.getContext().unwrapApi(AzureComputeApi.class); + String location = config.get(CLOUD_REGION_ID); + + String resourceGroupName = DEFAULT_RESOURCE_GROUP + "-" + location; + String vnetName = DEFAULT_NETWORK_NAME + "-" + location; + String subnetName = DEFAULT_SUBNET_NAME + "-" + location; + + //Check if default already exists + Subnet preexistingSubnet = api.getSubnetApi(resourceGroupName, vnetName).get(subnetName); + if(preexistingSubnet != null){ + LOG.info("Default Azure network and subnet already created, "+vnetName); + updateTemplateOptions(config, preexistingSubnet); + return; + } + + + LOG.info("Network config not specified when creating Azure location and default network/subnet does not exists. Creating"); + + createResourceGroupIfNeeded(api, resourceGroupName, location); + + //Setup properties for creating subnet/network + Subnet subnet = Subnet.create(subnetName, null, null, + Subnet.SubnetProperties.builder().addressPrefix(defaultSubnetAddressPrefix).build()); + + VirtualNetwork.VirtualNetworkProperties virtualNetworkProperties = VirtualNetwork.VirtualNetworkProperties + .builder().addressSpace(VirtualNetwork.AddressSpace.create(Arrays.asList(defaultVnetAddressPrefix))) + .subnets(Arrays.asList(subnet)).build(); + + //Create network + api.getVirtualNetworkApi(resourceGroupName).createOrUpdate(vnetName, location, virtualNetworkProperties); + Subnet createdSubnet = api.getSubnetApi(resourceGroupName, vnetName).get(subnetName); + + //Add config + updateTemplateOptions(config, createdSubnet); + + } + + private static void updateTemplateOptions(ConfigBag config, Subnet createdSubnet){ + Map<String, Object> templateOptions = config.get(TEMPLATE_OPTIONS); + + if(templateOptions == null) { + templateOptions = new HashMap<>(); + config.put(TEMPLATE_OPTIONS, templateOptions); + } + + Map<String, Object> ipOptions = new HashMap<>(); + ipOptions.put("allocateNewPublicIp", true); //JClouds will not provide a public IP unless we set this --- End diff -- setting allocateNewPublicIp makes sense to me. When a user doesn't setup any network options in brooklyn, I think they can expect: a public IP associated with the VM a private IP on a default network. Setting this config gives them that. If we don't set this config, they won't be able to access their nodes (in fact, I am not sure brooklyn will be able to access the vm- i'd have to check). Currently, without these changes, it is already confusing for a user to specify there own network as they need to know to set this option. I don't think the answer is to not set this by default, but rather to make it clearer that this needs to be set.
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. ---