brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #739: A default network is now created when dep...
Date Wed, 21 Jun 2017 11:34:51 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/739#discussion_r123221556
  
    --- 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 --
    
    Why set `allocateNewPublicIp` if the user hasn't asked for it? Is that definitely the
default that we want? I probably agree, as it's more like other clouds and this is just the
defaults in brooklyn (when a user isn't configuring the security options they really desire).
But I worry that injecting defaults like this will be confusing - e.g. if the user supplies
their own network or subnet name, then this value doesn't get set (I don't think that difference
is intuitive to the user).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message