All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.azure.management.network.implementation.NicIPConfigurationImpl Maven / Gradle / Ivy

There is a newer version: 1.41.4
Show newest version
/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 */

package com.microsoft.azure.management.network.implementation;

import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.network.ApplicationGateway;
import com.microsoft.azure.management.network.ApplicationGatewayBackendAddressPool;
import com.microsoft.azure.management.network.IPAllocationMethod;
import com.microsoft.azure.management.network.IPVersion;
import com.microsoft.azure.management.network.LoadBalancer;
import com.microsoft.azure.management.network.Network;
import com.microsoft.azure.management.network.NetworkInterface;
import com.microsoft.azure.management.network.NicIPConfiguration;
import com.microsoft.azure.management.network.PublicIPAddress;
import com.microsoft.azure.management.resources.fluentcore.model.Creatable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *  Implementation for NicIPConfiguration and its create and update interfaces.
 */
@LangDefinition
class NicIPConfigurationImpl
        extends
        NicIPConfigurationBaseImpl
        implements
            NicIPConfiguration,
            NicIPConfiguration.Definition,
            NicIPConfiguration.UpdateDefinition,
            NicIPConfiguration.Update {
    /**
     * the network client.
     */
    private final NetworkManager networkManager;
    /**
     * flag indicating whether IP configuration is in create or update mode.
     */
    private final boolean isInCreateMode;
    /**
     * unique key of a creatable virtual network to be associated with the ip configuration.
     */
    private String creatableVirtualNetworkKey;
    /**
     * unique key of a creatable public IP to be associated with the ip configuration.
     */
    private String creatablePublicIPKey;
    /**
     * reference to an existing virtual network to be associated with the ip configuration.
     */
    private Network existingVirtualNetworkToAssociate;
    /**
     * reference to an existing public IP to be associated with the ip configuration.
     */
    private String existingPublicIPAddressIdToAssociate;
    /**
     * name of an existing subnet to be associated with a new or existing IP configuration.
     */
    private String subnetToAssociate;
    /**
     * flag indicating to remove public IP association from the ip configuration during update.
     */
    private boolean removePrimaryPublicIPAssociation;

    protected NicIPConfigurationImpl(NetworkInterfaceIPConfigurationInner inner,
                                     NetworkInterfaceImpl parent,
                                     NetworkManager networkManager,
                                     final boolean isInCreateModel) {
        super(inner, parent, networkManager);
        this.isInCreateMode = isInCreateModel;
        this.networkManager = networkManager;
    }

    protected static NicIPConfigurationImpl prepareNicIPConfiguration(
            String name,
            NetworkInterfaceImpl parent,
            final NetworkManager networkManager) {
        NetworkInterfaceIPConfigurationInner ipConfigurationInner = new NetworkInterfaceIPConfigurationInner();
        ipConfigurationInner.withName(name);
        return new NicIPConfigurationImpl(ipConfigurationInner,
                parent,
                networkManager,
                true);
    }

    @Override
    public String publicIPAddressId() {
        if (this.inner().publicIPAddress() == null) {
            return null;
        }
        return this.inner().publicIPAddress().id();
    }

    @Override
    public PublicIPAddress getPublicIPAddress() {
        String id = publicIPAddressId();
        if (id == null) {
            return null;
        }

        return this.networkManager.publicIPAddresses().getById(id);
    }

    @Override
    public NetworkInterfaceImpl attach() {
        return parent().withIPConfiguration(this);
    }

    @Override
    public NicIPConfigurationImpl withNewNetwork(Creatable creatable) {
        this.creatableVirtualNetworkKey = creatable.key();
        this.parent().addToCreatableDependencies(creatable);
        return this;
    }

    @Override
    public NicIPConfigurationImpl withNewNetwork(String name, String addressSpaceCidr) {
        Network.DefinitionStages.WithGroup definitionWithGroup = this.networkManager.networks()
                .define(name)
                .withRegion(this.parent().regionName());

        Network.DefinitionStages.WithCreate definitionAfterGroup;
        if (this.parent().newGroup() != null) {
            definitionAfterGroup = definitionWithGroup.withNewResourceGroup(this.parent().newGroup());
        } else {
            definitionAfterGroup = definitionWithGroup.withExistingResourceGroup(this.parent().resourceGroupName());
        }
        return withNewNetwork(definitionAfterGroup.withAddressSpace(addressSpaceCidr));
    }

    @Override
    public NicIPConfigurationImpl withNewNetwork(String addressSpaceCidr) {
        return withNewNetwork(this.parent().namer.randomName("vnet", 20), addressSpaceCidr);
    }

    @Override
    public NicIPConfigurationImpl withExistingNetwork(Network network) {
        this.existingVirtualNetworkToAssociate = network;
        return this;
    }

    @Override
    public NicIPConfigurationImpl withPrivateIPAddressDynamic() {
        this.inner().withPrivateIPAllocationMethod(IPAllocationMethod.DYNAMIC);
        this.inner().withPrivateIPAddress(null);
        return this;
    }

    @Override
    public NicIPConfigurationImpl withPrivateIPAddressStatic(String staticPrivateIPAddress) {
        this.inner().withPrivateIPAllocationMethod(IPAllocationMethod.STATIC);
        this.inner().withPrivateIPAddress(staticPrivateIPAddress);
        return this;
    }

    @Override
    public NicIPConfigurationImpl withNewPublicIPAddress(Creatable creatable) {
        if (this.creatablePublicIPKey == null) {
            this.creatablePublicIPKey = creatable.key();
            this.parent().addToCreatableDependencies(creatable);
        }
        return this;
    }

    @Override
    public NicIPConfigurationImpl withNewPublicIPAddress() {
        String name = this.parent().namer.randomName("pip", 15);
        return withNewPublicIPAddress(prepareCreatablePublicIP(name, name));
    }

    @Override
    public NicIPConfigurationImpl withNewPublicIPAddress(String leafDnsLabel) {
        return withNewPublicIPAddress(prepareCreatablePublicIP(this.parent().namer.randomName("pip", 15), leafDnsLabel));
    }

    @Override
    public NicIPConfigurationImpl withExistingPublicIPAddress(PublicIPAddress publicIPAddress) {
        return this.withExistingPublicIPAddress(publicIPAddress.id());
    }

    @Override
    public NicIPConfigurationImpl withExistingPublicIPAddress(String resourceId) {
        this.existingPublicIPAddressIdToAssociate = resourceId;
        return this;
    }

    @Override
    public NicIPConfigurationImpl withoutPublicIPAddress() {
        this.removePrimaryPublicIPAssociation = true;
        return this;
    }

    @Override
    public NicIPConfigurationImpl withSubnet(String name) {
        this.subnetToAssociate = name;
        return this;
    }

    @Override
    public NicIPConfigurationImpl withExistingLoadBalancerBackend(LoadBalancer loadBalancer, String backendName) {
        if (loadBalancer != null) {
            for (BackendAddressPoolInner pool : loadBalancer.inner().backendAddressPools()) {
                if (pool.name().equalsIgnoreCase(backendName)) {
                    ensureLoadBalancerBackendAddressPools().add(pool);
                    return this;
                }
            }
        }

        return null;
    }

    @Override
    public NicIPConfigurationImpl withExistingApplicationGatewayBackend(ApplicationGateway appGateway, String backendName) {
        if (appGateway != null) {
            for (ApplicationGatewayBackendAddressPool pool : appGateway.inner().backendAddressPools()) {
                if (pool.name().equalsIgnoreCase(backendName)) {
                    ensureAppGatewayBackendAddressPools().add(pool);
                    return this;
                }
            }
        }

        return null;
    }

    @Override
    public NicIPConfigurationImpl withExistingLoadBalancerInboundNatRule(LoadBalancer loadBalancer, String inboundNatRuleName) {
        if (loadBalancer != null) {
            for (InboundNatRuleInner rule : loadBalancer.inner().inboundNatRules()) {
                if (rule.name().equalsIgnoreCase(inboundNatRuleName)) {
                    ensureInboundNatRules().add(rule);
                    return this;
                }
            }
        }

        return null;
    }

    private List ensureAppGatewayBackendAddressPools() {
        List poolRefs = this.inner().applicationGatewayBackendAddressPools();
        if (poolRefs == null) {
            poolRefs = new ArrayList<>();
            this.inner().withApplicationGatewayBackendAddressPools(poolRefs);
        }
        return poolRefs;
    }

    private List ensureLoadBalancerBackendAddressPools() {
        List poolRefs = this.inner().loadBalancerBackendAddressPools();
        if (poolRefs == null) {
            poolRefs = new ArrayList<>();
            this.inner().withLoadBalancerBackendAddressPools(poolRefs);
        }
        return poolRefs;
    }

    private List ensureInboundNatRules() {
        List natRefs = this.inner().loadBalancerInboundNatRules();
        if (natRefs == null) {
            natRefs = new ArrayList<>();
            this.inner().withLoadBalancerInboundNatRules(natRefs);
        }
        return natRefs;
    }

    protected static void ensureConfigurations(Collection nicIPConfigurations) {
        for (NicIPConfiguration nicIPConfiguration : nicIPConfigurations) {
            NicIPConfigurationImpl config = (NicIPConfigurationImpl) nicIPConfiguration;
            config.inner().withSubnet(config.subnetToAssociate());
            config.inner().withPublicIPAddress(config.publicIPToAssociate());
        }
    }

    // Creates a creatable public IP address definition with the given name and DNS label.
    private Creatable prepareCreatablePublicIP(String name, String leafDnsLabel) {
        PublicIPAddress.DefinitionStages.WithGroup definitionWithGroup = this.networkManager.publicIPAddresses()
                    .define(name)
                    .withRegion(this.parent().regionName());

        PublicIPAddress.DefinitionStages.WithCreate definitionAfterGroup;
        if (this.parent().newGroup() != null) {
            definitionAfterGroup = definitionWithGroup.withNewResourceGroup(this.parent().newGroup());
        } else {
            definitionAfterGroup = definitionWithGroup.withExistingResourceGroup(this.parent().resourceGroupName());
        }
        return definitionAfterGroup.withLeafDomainLabel(leafDnsLabel);
    }

    /**
     * Gets the subnet to associate with the IP configuration.
     * 

* This method will never return null as subnet is required for a IP configuration, in case of * update mode if user didn't choose to change the subnet then existing subnet will be returned. * Updating the nic subnet has a restriction, the new subnet must reside in the same virtual network * as the current one. * * @return the subnet resource */ private SubnetInner subnetToAssociate() { SubnetInner subnetInner = new SubnetInner(); if (this.isInCreateMode) { if (this.creatableVirtualNetworkKey != null) { Network network = (Network) parent().createdDependencyResource(this.creatableVirtualNetworkKey); subnetInner.withId(network.inner().subnets().get(0).id()); return subnetInner; } for (SubnetInner subnet : this.existingVirtualNetworkToAssociate.inner().subnets()) { if (subnet.name().equalsIgnoreCase(this.subnetToAssociate)) { subnetInner.withId(subnet.id()); return subnetInner; } } throw new RuntimeException("A subnet with name '" + subnetToAssociate + "' not found under the network '" + this.existingVirtualNetworkToAssociate.name() + "'"); } else { if (subnetToAssociate != null) { int idx = this.inner().subnet().id().lastIndexOf('/'); subnetInner.withId(this.inner().subnet().id().substring(0, idx + 1) + subnetToAssociate); } else { subnetInner.withId(this.inner().subnet().id()); } return subnetInner; } } /** * Get the SubResource instance representing a public IP that needs to be associated with the * IP configuration. *

* null will be returned if withoutPublicIP() is specified in the update fluent chain or user did't * opt for public IP in create fluent chain. In case of update chain, if withoutPublicIP(..) is * not specified then existing associated (if any) public IP will be returned. * @return public IP SubResource */ private PublicIPAddressInner publicIPToAssociate() { String pipId = null; if (this.removePrimaryPublicIPAssociation) { return null; } else if (this.creatablePublicIPKey != null) { pipId = ((PublicIPAddress) this.parent() .createdDependencyResource(this.creatablePublicIPKey)).id(); } else if (this.existingPublicIPAddressIdToAssociate != null) { pipId = this.existingPublicIPAddressIdToAssociate; } if (pipId != null) { return new PublicIPAddressInner().withId(pipId); } else if (!this.isInCreateMode) { return this.inner().publicIPAddress(); } else { return null; } } @Override public NicIPConfigurationImpl withPrivateIPVersion(IPVersion ipVersion) { this.inner().withPrivateIPAddressVersion(ipVersion); return this; } @Override public NicIPConfigurationImpl withoutApplicationGatewayBackends() { this.inner().withApplicationGatewayBackendAddressPools(null); return this; } @Override public NicIPConfigurationImpl withoutLoadBalancerBackends() { this.inner().withLoadBalancerBackendAddressPools(null); return this; } @Override public NicIPConfigurationImpl withoutLoadBalancerInboundNatRules() { this.inner().withLoadBalancerInboundNatRules(null); return this; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy