Java Code Examples for org.apache.brooklyn.util.core.config.ConfigBag#containsKey()

The following examples show how to use org.apache.brooklyn.util.core.config.ConfigBag#containsKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
protected ConfigBag extractWinrmConfig(ConfigBag setup, ConfigBag alt) {
    ConfigBag winrmConfig = new ConfigBag();
    
    for (HasConfigKey<?> key : WinRmMachineLocation.ALL_WINRM_CONFIG_KEYS) {
        String keyName = key.getConfigKey().getName();
        if (setup.containsKey(keyName)) {
            winrmConfig.putStringKey(keyName, setup.getStringKey(keyName));
        } else if (alt.containsKey(keyName)) {
            winrmConfig.putStringKey(keyName, setup.getStringKey(keyName));
        }
    }
    
    Map<String, Object> winrmToolClassProperties = Maps.filterKeys(setup.getAllConfig(), StringPredicates.startsWith(WinRmMachineLocation.WINRM_TOOL_CLASS_PROPERTIES_PREFIX));
    winrmConfig.putAll(winrmToolClassProperties);
    
    return winrmConfig;
}
 
Example 2
Source File: DefaultAzureArmNetworkCreator.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
private static void updateTemplateOptions(ConfigBag config, Subnet createdSubnet){
    Map<String, Object> templateOptions;

    if(config.containsKey(TEMPLATE_OPTIONS)) {
        templateOptions = MutableMap.copyOf(config.get(TEMPLATE_OPTIONS));
    } else {
        templateOptions = new HashMap<>();
    }

    templateOptions.put("ipOptions", ImmutableList.of(ImmutableMap.of(
            "allocateNewPublicIp", true, //jclouds will not provide a public IP unless we set this
            "subnet", createdSubnet.id()
    )));

    config.put(TEMPLATE_OPTIONS, templateOptions);

}
 
Example 3
Source File: FlagUtils.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** returns the flag/config-key record for the given input */
private static FlagConfigKeyAndValueRecord getFlagConfigKeyRecord(Field f, ConfigKey<?> key, ConfigBag input) {
    FlagConfigKeyAndValueRecord result = new FlagConfigKeyAndValueRecord(); 
    result.configKey = key;
    if (key!=null && input.containsKey(key))
        result.configKeyValue = input.getObjKeyMaybe(key);
    if (f != null) {
        SetFromFlag flag = f.getAnnotation(SetFromFlag.class);
        if (flag!=null) {
            String flagName = elvis(flag.value(), f.getName());
            result.flagName = flagName;
            if (input.containsKey(flagName))
                result.flagValue = Maybe.of(input.getStringKey(flagName));
        }
    }
    return result;
}
 
Example 4
Source File: KubernetesLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up {@link ConfigKey configuration} with the entity value taking precedence over the
 * location, and returning a default value (normally {@literal null}) if neither is present.
 */
public <T> T lookup(final ConfigKey<T> config, Entity entity, ConfigBag setup, T defaultValue) {
    boolean entityConfigPresent = !entity.config().findKeysPresent(config::equals).isEmpty();

    boolean setupBagConfigPresent = setup.containsKey(config);

    if (entityConfigPresent) {
        return entity.config().get(config);
    } else if (setupBagConfigPresent) {
        return setup.get(config);
    }

    return defaultValue;
}
 
Example 5
Source File: AbstractCloudMachineNamer.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** Returns the max length of a VM name for the cloud specified in setup;
 * this value is typically decremented by 9 to make room for jclouds labels;
 * delegates to {@link #getCustomMaxNameLength()} when 
 * {@link CloudLocationConfig#VM_NAME_MAX_LENGTH} is not set */
public int getMaxNameLength(ConfigBag setup) {
    if (setup.containsKey(CloudLocationConfig.VM_NAME_MAX_LENGTH)) {
        // if a length is set explicitly, use that (but intercept default behaviour)
        return setup.get(CloudLocationConfig.VM_NAME_MAX_LENGTH);
    }
    
    Integer custom = getCustomMaxNameLength(setup);
    if (custom!=null) return custom;
    
    // return the default
    return defaultMachineNameMaxLength;  
}
 
Example 6
Source File: AbstractCloudMachineProvisioningLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected ConfigBag extractSshConfig(ConfigBag setup, ConfigBag alt) {
    ConfigBag sshConfig = new ConfigBag();
    
    for (HasConfigKey<?> key : SshMachineLocation.ALL_SSH_CONFIG_KEYS) {
        String keyName = key.getConfigKey().getName();
        if (setup.containsKey(keyName)) {
            sshConfig.putStringKey(keyName, setup.getStringKey(keyName));
        } else if (alt.containsKey(keyName)) {
            sshConfig.putStringKey(keyName, setup.getStringKey(keyName));
        }
    }
    
    Map<String, Object> sshToolClassProperties = Maps.filterKeys(setup.getAllConfig(), StringPredicates.startsWith(SshMachineLocation.SSH_TOOL_CLASS_PROPERTIES_PREFIX));
    sshConfig.putAll(sshToolClassProperties);

    // Special cases (preserving old code!)
    if (setup.containsKey(PASSWORD)) {
        sshConfig.copyKeyAs(setup, PASSWORD, SshTool.PROP_PASSWORD);
    } else if (alt.containsKey(PASSWORD)) {
        sshConfig.copyKeyAs(alt, PASSWORD, SshTool.PROP_PASSWORD);
    }
    
    if (setup.containsKey(PRIVATE_KEY_DATA)) {
        sshConfig.copyKeyAs(setup, PRIVATE_KEY_DATA, SshTool.PROP_PRIVATE_KEY_DATA);
    } else if (setup.containsKey(PRIVATE_KEY_FILE)) {
        sshConfig.copyKeyAs(setup, PRIVATE_KEY_FILE, SshTool.PROP_PRIVATE_KEY_FILE);
    } else if (alt.containsKey(PRIVATE_KEY_DATA)) {
        sshConfig.copyKeyAs(setup, PRIVATE_KEY_DATA, SshTool.PROP_PRIVATE_KEY_DATA);
    }
    
    if (setup.containsKey(PRIVATE_KEY_PASSPHRASE)) {
        // NB: not supported in jclouds (but it is by our ssh tool)
        sshConfig.copyKeyAs(setup, PRIVATE_KEY_PASSPHRASE, SshTool.PROP_PRIVATE_KEY_PASSPHRASE);
    }

    return sshConfig;
}
 
Example 7
Source File: EntityInitializers.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the value for key from configBag.
 * <p>
 * If key is an instance of {@link ConfigKeySelfExtracting} and executionContext is
 * not null then its value will be retrieved per the key's implementation of
 * {@link ConfigKeySelfExtracting#extractValue extractValue}. Otherwise, the value
 * will be retrieved from configBag directly.
 */
public static <T> T resolve(ConfigBag configBag, ConfigKey<T> key, ExecutionContext executionContext) {
    if (key instanceof ConfigKeySelfExtracting && executionContext != null) {
        ConfigKeySelfExtracting<T> ckse = ((ConfigKeySelfExtracting<T>) key);
        T result = ckse.extractValue(configBag.getAllConfigAsConfigKeyMap(), executionContext);
        return (result != null || configBag.containsKey(key.getName())) ? result : key.getDefaultValue();
    }
    return configBag.get(key);
}
 
Example 8
Source File: ProcessTaskWrapper.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** for overriding */
protected ConfigBag getConfigForRunning() {
    ConfigBag config = ConfigBag.newInstanceCopying(ProcessTaskWrapper.this.config);
    if (stdout!=null) config.put(ShellTool.PROP_OUT_STREAM, stdout);
    if (stderr!=null) config.put(ShellTool.PROP_ERR_STREAM, stderr);
    
    if (!config.containsKey(ShellTool.PROP_NO_EXTRA_OUTPUT))
        // by default no extra output (so things like cat, etc work as expected)
        config.put(ShellTool.PROP_NO_EXTRA_OUTPUT, true);

    if (runAsRoot)
        config.put(ShellTool.PROP_RUN_AS_ROOT, true);
    return config;
}
 
Example 9
Source File: FlagUtils.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private static void setFieldFromConfig(Object o, Field f, ConfigBag bag, SetFromFlag optionalAnnotation, boolean setDefaultVals) {
    String flagName = optionalAnnotation==null ? null : elvis(optionalAnnotation.value(), f.getName());
    // prefer flag name, if present
    if (groovyTruth(flagName) && bag.containsKey(flagName)) {
        setField(o, f, bag.getStringKey(flagName), optionalAnnotation);
        return;
    }
    // first check whether it is a key
    ConfigKey<?> key = getFieldAsConfigKey(o, f);
    if (key!=null && bag.containsKey(key)) {
        Object uncoercedValue = bag.getObjKeyMaybe(key).get();
        setField(o, f, uncoercedValue, optionalAnnotation);
        return;
    }
    if (setDefaultVals && optionalAnnotation!=null && groovyTruth(optionalAnnotation.defaultVal())) {
        Object oldValue;
        try {
            f.setAccessible(true);
            oldValue = f.get(o);
            if (oldValue==null || oldValue.equals(getDefaultValueForType(f.getType()))) {
                setField(o, f, optionalAnnotation.defaultVal(), optionalAnnotation);
            }
        } catch (Exception e) {
            Exceptions.propagate(e);
        }
        return;
    }
}
 
Example 10
Source File: AbstractCloudMachineNamer.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public int getLengthForMachineUniqueNameSalt(ConfigBag setup, boolean includeSeparator) {
    int saltLen;
    if (setup.containsKey(CloudLocationConfig.VM_NAME_SALT_LENGTH)) {
        saltLen = setup.get(CloudLocationConfig.VM_NAME_SALT_LENGTH);
    } else {
        // default value comes from key, but custom default can be set
        saltLen = defaultMachineNameSaltLength;
    }
    
    if (saltLen>0 && includeSeparator)
        saltLen += separator.length();
    
    return saltLen;
}
 
Example 11
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** can generate a string describing where something is being created
 * (provider, region/location and/or endpoint, callerContext);
 * previously set on the config bag, but not any longer (Sept 2016) as config is treated like entities */
protected String getCreationString(ConfigBag config) {
    return elvis(config.get(CLOUD_PROVIDER), "unknown")+
            (config.containsKey(CLOUD_REGION_ID) ? ":"+config.get(CLOUD_REGION_ID) : "")+
            (config.containsKey(CLOUD_ENDPOINT) ? ":"+config.get(CLOUD_ENDPOINT) : "")+
            (config.containsKey(CALLER_CONTEXT) ? "@"+config.get(CALLER_CONTEXT) : "");
}
 
Example 12
Source File: KubernetesLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected Iterable<Integer> findInboundPorts(Entity entity, ConfigBag setup) {
    Iterable<String> inboundTcpPorts = entity.config().get(DockerContainer.INBOUND_TCP_PORTS);
    if(inboundTcpPorts == null)
        return setup.containsKey(INBOUND_PORTS) ? toIntPortList(setup.get(INBOUND_PORTS)) : ImmutableList.of(22);

    List<Integer> inboundPorts = Lists.newArrayList();
    List<String> portRanges = MutableList.copyOf(entity.config().get(DockerContainer.INBOUND_TCP_PORTS));
    for (String portRange : portRanges) {
        for (Integer port : PortRanges.fromString(portRange)) {
            inboundPorts.add(port);
        }
    }
    return inboundPorts;
}
 
Example 13
Source File: JCloudsPropertiesBuilder.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private String getDeprecatedProperty(ConfigBag conf, String key) {
    if (conf.containsKey(key)) {
        LOG.warn("Jclouds using deprecated brooklyn-jclouds property " + key + ": " + Sanitizer.sanitize(conf.getAllConfig()));
        return (String) conf.getStringKey(key);
    } else {
        return null;
    }
}
 
Example 14
Source File: AbstractComputeServiceRegistry.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * returns the jclouds modules user has specified using {@link JcloudsLocationConfig#COMPUTE_SERVICE_MODULES}
 */
protected Iterable<? extends Module> userDefinedModules(ConfigBag conf) {
    Iterable<Module> optionalModules = Collections.EMPTY_SET;
    if (conf.containsKey(JcloudsLocationConfig.COMPUTE_SERVICE_MODULES)) {
        optionalModules = Iterables.concat(optionalModules, conf.get(JcloudsLocationConfig.COMPUTE_SERVICE_MODULES));
    }
    return optionalModules;
}
 
Example 15
Source File: AddEffector.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** returns a ConfigBag containing the merger of the supplied parameters with default values on the effector-defined parameters */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ConfigBag getMergedParams(Effector<?> eff, ConfigBag params) {
    ConfigBag result = ConfigBag.newInstanceCopying(params);
    for (ParameterType<?> param: eff.getParameters()) {
        ConfigKey key = Effectors.asConfigKey(param);
        if (!result.containsKey(key))
            result.configure(key, params.get(key));
    }
    return result;
}
 
Example 16
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected void logAvailableTemplates(ConfigBag config) {
    LOG.info("Loading available images at "+this+" for reference...");
    ConfigBag m1 = ConfigBag.newInstanceCopying(config);
    if (m1.containsKey(IMAGE_ID)) {
        // if caller specified an image ID, remove that, but don't apply default filters
        m1.remove(IMAGE_ID);
        // TODO use key
        m1.putStringKey("anyOwner", true);
    }
    ComputeService computeServiceLessRestrictive = getComputeService(m1);
    Set<? extends Image> imgs = computeServiceLessRestrictive.listImages();
    LOG.info(""+imgs.size()+" available images at "+this);
    for (Image img: imgs) {
        LOG.info(" Image: "+img);
    }

    Set<? extends Hardware> profiles = computeServiceLessRestrictive.listHardwareProfiles();
    LOG.info(""+profiles.size()+" available profiles at "+this);
    for (Hardware profile: profiles) {
        LOG.info(" Profile: "+profile);
    }

    Set<? extends org.jclouds.domain.Location> assignableLocations = computeServiceLessRestrictive.listAssignableLocations();
    LOG.info(""+assignableLocations.size()+" available locations at "+this);
    for (org.jclouds.domain.Location assignableLocation: assignableLocations) {
        LOG.info(" Location: "+assignableLocation);
    }
}
 
Example 17
Source File: BrooklynNodeUpgradeEffectorBody.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Override
public Void call(ConfigBag parametersO) {
    if (!isPersistenceModeEnabled(entity())) {
        // would could try a `forcePersistNow`, but that's sloppy; 
        // for now, require HA/persistence for upgrading
        DynamicTasks.queue( Tasks.warning("Check persistence", 
            new IllegalStateException("Persistence does not appear to be enabled at this cluster. "
            + "In-place node upgrade will not succeed unless a custom launch script enables it.")) );
    }

    final ConfigBag parameters = ConfigBag.newInstanceCopying(parametersO);

    /*
     * all parameters are passed to children, apart from EXTRA_CONFIG
     * whose value (as a map) is so passed; it provides an easy way to set extra config in the gui.
     * (IOW a key-value mapping can be passed either inside EXTRA_CONFIG or as a sibling to EXTRA_CONFIG)  
     */
    if (parameters.containsKey(EXTRA_CONFIG)) {
        Map<String, Object> extra = parameters.get(EXTRA_CONFIG);
        parameters.remove(EXTRA_CONFIG);
        parameters.putAll(extra);
    }
    log.debug(this+" upgrading, using "+parameters);
    
    final String bkName;
    boolean doDryRunFirst = parameters.get(DO_DRY_RUN_FIRST);
    if(doDryRunFirst) {
        bkName = dryRunUpdate(parameters);
    } else {
        bkName = "direct-"+Identifiers.makeRandomId(4);
    }
    
    // Stop running instance
    DynamicTasks.queue(Tasks.builder().displayName("shutdown node")
            .add(Effectors.invocation(entity(), BrooklynNode.STOP_NODE_BUT_LEAVE_APPS, ImmutableMap.of(StopSoftwareParameters.STOP_MACHINE_MODE, StopMode.NEVER)))
            .build());

    // backup old files
    DynamicTasks.queue(Tasks.builder().displayName("backup old version").body(new Runnable() {
        @Override
        public void run() {
            String runDir = entity().getAttribute(SoftwareProcess.RUN_DIR);
            String bkDir = Urls.mergePaths(runDir, "..", Urls.getBasename(runDir)+"-backups", bkName);
            log.debug(this+" storing backup of previous version in "+bkDir);
            DynamicTasks.queue(SshEffectorTasks.ssh(
                "cd "+runDir,
                "mkdir -p "+bkDir,
                "mv * "+bkDir
                // By removing the run dir of the entity we force it to go through
                // the customize step again on start and re-generate local-brooklyn.properties.
                ).summary("move files"));
        }
    }).build());
    
    // Reconfigure entity
    DynamicTasks.queue(Tasks.builder().displayName("reconfigure").body(new Runnable() {
        @Override
        public void run() {
            DynamicTasks.waitForLast();
            entity().sensors().set(SoftwareProcess.INSTALL_DIR, (String)null);
            entity().config().set(SoftwareProcess.INSTALL_UNIQUE_LABEL, (String)null);
            entity().config().putAll(parameters.getAllConfig());
            entity().sensors().set(BrooklynNode.DOWNLOAD_URL, entity().getConfig(DOWNLOAD_URL));

            // Setting SUGGESTED_VERSION will result in an new empty INSTALL_FOLDER, but clear it
            // just in case the user specified already installed version.
            ((BrooklynNodeDriver)((DriverDependentEntity<?>)entity()).getDriver()).clearInstallDir();
        }
    }).build());
    
    // Start this entity, running the new version.
    // This will download and install the new dist (if not already done by the dry run node).
    DynamicTasks.queue(Effectors.invocation(entity(), BrooklynNode.START, ConfigBag.EMPTY));

    return null;
}
 
Example 18
Source File: LocationTransformer.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private static LocationSummary newInstance(ManagementContext mgmt, 
        LocationSpec<? extends Location> spec, ConfigBag explicitConfig, 
        String optionalExplicitId, String name, String specString, 
        LocationDetailLevel level, UriBuilder ub) {
    // mgmt not actually needed
    Map<String, Object> config = MutableMap.copyOf(explicitConfig==null ? null : explicitConfig.getAllConfig());
    if (spec!=null && (level==LocationDetailLevel.FULL_EXCLUDING_SECRET || level==LocationDetailLevel.FULL_INCLUDING_SECRET)) {
        // full takes from any resolved spec ie inherited, AND explicit config and flags;
        // would be nice to distinguish flags from config, but more important to make sure flags are supplied
        // (ideally would return yaml, using yoml)
        config = ConfigBag.newInstance(spec.getFlags()).putAll(spec.getConfig()).putAll(config).getAllConfig();
    } else if (level==LocationDetailLevel.LOCAL_EXCLUDING_SECRET) {
        // in local mode, make sure any inherited display name is set, but otherwise _no_ inherited
        // NB this may not include provider, region, and endpoint -- use 'full' for that
        if (spec!=null && !explicitConfig.containsKey(LocationConfigKeys.DISPLAY_NAME) ) {
            if (Strings.isNonBlank((String) spec.getFlags().get(LocationConfigKeys.DISPLAY_NAME.getName()))){
                config.put(LocationConfigKeys.DISPLAY_NAME.getName(), spec.getFlags().get(LocationConfigKeys.DISPLAY_NAME.getName()));
            } else if ( Strings.isNonBlank(spec.getDisplayName()) ) {
                config.put(LocationConfigKeys.DISPLAY_NAME.getName(), spec.getDisplayName());
            }
        }
    }

    String id = Strings.isNonBlank(optionalExplicitId) ? optionalExplicitId : spec!=null && Strings.isNonBlank(spec.getCatalogItemId()) ? spec.getCatalogItemId() : null;
    URI selfUri = serviceUriBuilder(ub, LocationApi.class, "get").build(id);
    
    CatalogLocationSummary catalogSummary = null;
    if (CatalogLocationResolver.isLegacyWrappedReference(specString)) {
        RegisteredType ci = mgmt.getTypeRegistry().get(CatalogLocationResolver.unwrapLegacyWrappedReference(specString));
        if (ci!=null) {
            BrooklynRestResourceUtils br = new BrooklynRestResourceUtils(mgmt);
            catalogSummary = CatalogTransformer.catalogLocationSummary(br, ci, ub);
        }
    }
    return new LocationSummary(
            id,
            Strings.isNonBlank(name) ? name : spec!=null ? spec.getDisplayName() : null,
            Strings.isNonBlank(specString) ? specString : spec!=null ? spec.getCatalogItemId() : null,
            null,
            copyConfig(config, level),
            catalogSummary,
            ImmutableMap.of("self", selfUri));
}
 
Example 19
Source File: AbstractComputeServiceRegistry.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
protected Iterable<? extends Module> linkingContext(ConfigBag conf) {
    if (!conf.containsKey(JcloudsLocationConfig.LINK_CONTEXT)) return Collections.EMPTY_SET;
    Context context = new LinkContextSupplier(conf).get();
    return Iterables.filter(ImmutableSet.of(ContextLinking.linkContext(context)), Predicates.notNull());
}
 
Example 20
Source File: DefaultAzureArmNetworkCreator.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
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);


    //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", 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);

    //Check if default already exists
    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();

    //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);

        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);

        //Just creating the subnet
        subnetApi.createOrUpdate(subnetName, subnetProperties);
    }

    //Get created subnet
    Subnet createdSubnet = api.getSubnetApi(resourceGroupName, vnetName).get(subnetName);

    //Wait until subnet is created
    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 );
    }

    //Add config
    updateTemplateOptions(config, createdSubnet);
}