package com.microsoft.azure.management.network.implementation;
import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.network.IPAllocationMethod;
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.NetworkSecurityGroup;
import com.microsoft.azure.management.network.NicIPConfiguration;
import com.microsoft.azure.management.network.PublicIPAddress;
import com.microsoft.azure.management.network.model.GroupableParentResourceWithTagsImpl;
import com.microsoft.azure.management.resources.ResourceGroup;
import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils;
import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource;
import com.microsoft.azure.management.resources.fluentcore.model.Creatable;
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import com.microsoft.azure.management.resources.fluentcore.utils.Utils;
import rx.Observable;
import rx.functions.Func1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@LangDefinition
class NetworkInterfaceImpl
extends GroupableParentResourceWithTagsImpl<
NetworkInterface,
NetworkInterfaceInner,
NetworkInterfaceImpl,
NetworkManager>
implements
NetworkInterface,
NetworkInterface.Definition,
NetworkInterface.Update {
private final String nicName;
protected final ResourceNamer namer;
private Map<String, NicIPConfiguration> nicIPConfigurations;
private String creatableNetworkSecurityGroupKey;
private NetworkSecurityGroup existingNetworkSecurityGroupToAssociate;
private NetworkSecurityGroup networkSecurityGroup;
NetworkInterfaceImpl(String name,
NetworkInterfaceInner innerModel,
final NetworkManager networkManager) {
super(name, innerModel, networkManager);
this.nicName = name;
this.namer = SdkContext.getResourceNamerFactory().createResourceNamer(this.nicName);
initializeChildrenFromInner();
}
@Override
public Observable<NetworkInterface> refreshAsync() {
return super.refreshAsync().map(new Func1<NetworkInterface, NetworkInterface>() {
@Override
public NetworkInterface call(NetworkInterface networkInterface) {
NetworkInterfaceImpl impl = (NetworkInterfaceImpl) networkInterface;
impl.clearCachedRelatedResources();
impl.initializeChildrenFromInner();
return impl;
}
});
}
@Override
protected Observable<NetworkInterfaceInner> getInnerAsync() {
return this.manager().inner().networkInterfaces().getByResourceGroupAsync(this.resourceGroupName(), this.name());
}
@Override
protected Observable<NetworkInterfaceInner> applyTagsToInnerAsync() {
return this.manager().inner().networkInterfaces().updateTagsAsync(resourceGroupName(), name(), inner().getTags());
}
@Override
public NetworkInterfaceImpl withAcceleratedNetworking() {
this.inner().withEnableAcceleratedNetworking(true);
return this;
}
@Override
public NetworkInterfaceImpl withoutAcceleratedNetworking() {
this.inner().withEnableAcceleratedNetworking(false);
return this;
}
@Override
public NetworkInterfaceImpl withNewPrimaryNetwork(Creatable<Network> creatable) {
this.primaryIPConfiguration().withNewNetwork(creatable);
return this;
}
@Override
public NetworkInterfaceImpl withNewPrimaryNetwork(String name, String addressSpaceCidr) {
this.primaryIPConfiguration().withNewNetwork(name, addressSpaceCidr);
return this;
}
@Override
public NetworkInterfaceImpl withNewPrimaryNetwork(String addressSpaceCidr) {
this.primaryIPConfiguration().withNewNetwork(addressSpaceCidr);
return this;
}
@Override
public NetworkInterfaceImpl withExistingPrimaryNetwork(Network network) {
this.primaryIPConfiguration().withExistingNetwork(network);
return this;
}
@Override
public NetworkInterfaceImpl withNewPrimaryPublicIPAddress(Creatable<PublicIPAddress> creatable) {
this.primaryIPConfiguration().withNewPublicIPAddress(creatable);
return this;
}
@Override
public NetworkInterfaceImpl withNewPrimaryPublicIPAddress() {
this.primaryIPConfiguration().withNewPublicIPAddress();
return this;
}
@Override
public NetworkInterfaceImpl withNewPrimaryPublicIPAddress(String leafDnsLabel) {
this.primaryIPConfiguration().withNewPublicIPAddress(leafDnsLabel);
return this;
}
@Override
public NetworkInterfaceImpl withExistingLoadBalancerBackend(LoadBalancer loadBalancer, String backendName) {
this.primaryIPConfiguration().withExistingLoadBalancerBackend(loadBalancer, backendName);
return this;
}
@Override
public NetworkInterfaceImpl withExistingLoadBalancerInboundNatRule(LoadBalancer loadBalancer, String inboundNatRuleName) {
this.primaryIPConfiguration().withExistingLoadBalancerInboundNatRule(loadBalancer, inboundNatRuleName);
return this;
}
@Override
public Update withoutLoadBalancerBackends() {
for (NicIPConfiguration ipConfig : this.ipConfigurations().values()) {
this.updateIPConfiguration(ipConfig.name())
.withoutLoadBalancerBackends();
}
return this;
}
@Override
public Update withoutLoadBalancerInboundNatRules() {
for (NicIPConfiguration ipConfig : this.ipConfigurations().values()) {
this.updateIPConfiguration(ipConfig.name())
.withoutLoadBalancerInboundNatRules();
}
return this;
}
@Override
public NetworkInterfaceImpl withoutPrimaryPublicIPAddress() {
this.primaryIPConfiguration().withoutPublicIPAddress();
return this;
}
@Override
public NetworkInterfaceImpl withExistingPrimaryPublicIPAddress(PublicIPAddress publicIPAddress) {
this.primaryIPConfiguration().withExistingPublicIPAddress(publicIPAddress);
return this;
}
@Override
public NetworkInterfaceImpl withPrimaryPrivateIPAddressDynamic() {
this.primaryIPConfiguration().withPrivateIPAddressDynamic();
return this;
}
@Override
public NetworkInterfaceImpl withPrimaryPrivateIPAddressStatic(String staticPrivateIPAddress) {
this.primaryIPConfiguration().withPrivateIPAddressStatic(staticPrivateIPAddress);
return this;
}
@Override
public NetworkInterfaceImpl withNewNetworkSecurityGroup(Creatable<NetworkSecurityGroup> creatable) {
if (this.creatableNetworkSecurityGroupKey == null) {
this.creatableNetworkSecurityGroupKey = this.addDependency(creatable);
}
return this;
}
@Override
public NetworkInterfaceImpl withExistingNetworkSecurityGroup(NetworkSecurityGroup networkSecurityGroup) {
this.existingNetworkSecurityGroupToAssociate = networkSecurityGroup;
return this;
}
@Override
public NetworkInterfaceImpl withoutNetworkSecurityGroup() {
this.inner().withNetworkSecurityGroup(null);
return this;
}
@Override
public NicIPConfigurationImpl defineSecondaryIPConfiguration(String name) {
return prepareNewNicIPConfiguration(name);
}
@Override
public NicIPConfigurationImpl updateIPConfiguration(String name) {
return (NicIPConfigurationImpl) this.nicIPConfigurations.get(name);
}
@Override
public NetworkInterfaceImpl withIPForwarding() {
this.inner().withEnableIPForwarding(true);
return this;
}
@Override
public NetworkInterfaceImpl withoutIPConfiguration(String name) {
this.nicIPConfigurations.remove(name);
return this;
}
@Override
public NetworkInterfaceImpl withoutIPForwarding() {
this.inner().withEnableIPForwarding(false);
return this;
}
@Override
public NetworkInterfaceImpl withDnsServer(String ipAddress) {
this.dnsServerIPs().add(ipAddress);
return this;
}
@Override
public NetworkInterfaceImpl withoutDnsServer(String ipAddress) {
this.dnsServerIPs().remove(ipAddress);
return this;
}
@Override
public NetworkInterfaceImpl withAzureDnsServer() {
this.dnsServerIPs().clear();
return this;
}
@Override
public NetworkInterfaceImpl withSubnet(String name) {
this.primaryIPConfiguration().withSubnet(name);
return this;
}
@Override
public NetworkInterfaceImpl withInternalDnsNameLabel(String dnsNameLabel) {
this.inner().dnsSettings().withInternalDnsNameLabel(dnsNameLabel);
return this;
}
@Override
public boolean isAcceleratedNetworkingEnabled() {
return Utils.toPrimitiveBoolean(this.inner().enableAcceleratedNetworking());
}
@Override
public String virtualMachineId() {
if (this.inner().virtualMachine() != null) {
return this.inner().virtualMachine().id();
} else {
return null;
}
}
@Override
public boolean isIPForwardingEnabled() {
return Utils.toPrimitiveBoolean(this.inner().enableIPForwarding());
}
@Override
public String macAddress() {
return this.inner().macAddress();
}
@Override
public String internalDnsNameLabel() {
return (this.inner().dnsSettings() != null) ? this.inner().dnsSettings().internalDnsNameLabel() : null;
}
@Override
public String internalDomainNameSuffix() {
return (this.inner().dnsSettings() != null) ? this.inner().dnsSettings().internalDomainNameSuffix() : null;
}
@Override
public List<String> appliedDnsServers() {
List<String> dnsServers = new ArrayList<String>();
if (this.inner().dnsSettings() == null) {
return Collections.unmodifiableList(dnsServers);
} else if (this.inner().dnsSettings().appliedDnsServers() == null) {
return Collections.unmodifiableList(dnsServers);
} else {
return Collections.unmodifiableList(this.inner().dnsSettings().appliedDnsServers());
}
}
@Override
public String internalFqdn() {
return (this.inner().dnsSettings() != null) ? this.inner().dnsSettings().internalFqdn() : null;
}
@Override
public List<String> dnsServers() {
return this.dnsServerIPs();
}
@Override
public String primaryPrivateIP() {
return this.primaryIPConfiguration().privateIPAddress();
}
@Override
public IPAllocationMethod primaryPrivateIPAllocationMethod() {
return this.primaryIPConfiguration().privateIPAllocationMethod();
}
@Override
public Map<String, NicIPConfiguration> ipConfigurations() {
return Collections.unmodifiableMap(this.nicIPConfigurations);
}
@Override
public String networkSecurityGroupId() {
return (this.inner().networkSecurityGroup() != null) ? this.inner().networkSecurityGroup().id() : null;
}
@Override
public NetworkSecurityGroup getNetworkSecurityGroup() {
if (this.networkSecurityGroup == null && this.networkSecurityGroupId() != null) {
String id = this.networkSecurityGroupId();
this.networkSecurityGroup = super.myManager
.networkSecurityGroups()
.getByResourceGroup(ResourceUtils.groupFromResourceId(id),
ResourceUtils.nameFromResourceId(id));
}
return this.networkSecurityGroup;
}
@Override
public NicIPConfigurationImpl primaryIPConfiguration() {
NicIPConfigurationImpl primaryIPConfig = null;
if (this.nicIPConfigurations.size() == 0) {
primaryIPConfig = prepareNewNicIPConfiguration("primary");
primaryIPConfig.inner().withPrimary(true);
withIPConfiguration(primaryIPConfig);
} else if (this.nicIPConfigurations.size() == 1) {
primaryIPConfig = (NicIPConfigurationImpl) this.nicIPConfigurations.values().iterator().next();
} else {
for (NicIPConfiguration ipConfig : this.nicIPConfigurations.values()) {
if (ipConfig.isPrimary()) {
primaryIPConfig = (NicIPConfigurationImpl) ipConfig;
break;
}
}
}
return primaryIPConfig;
}
private List<String> dnsServerIPs() {
List<String> dnsServers = new ArrayList<String>();
if (this.inner().dnsSettings() == null) {
return dnsServers;
} else if (this.inner().dnsSettings().dnsServers() == null) {
return dnsServers;
} else {
return this.inner().dnsSettings().dnsServers();
}
}
@Override
protected void initializeChildrenFromInner() {
this.nicIPConfigurations = new TreeMap<>();
List<NetworkInterfaceIPConfigurationInner> inners = this.inner().ipConfigurations();
if (inners != null) {
for (NetworkInterfaceIPConfigurationInner inner : inners) {
NicIPConfigurationImpl nicIPConfiguration = new NicIPConfigurationImpl(inner, this, super.myManager, false);
this.nicIPConfigurations.put(nicIPConfiguration.name(), nicIPConfiguration);
}
}
}
private NicIPConfigurationImpl prepareNewNicIPConfiguration(String name) {
NicIPConfigurationImpl nicIPConfiguration = NicIPConfigurationImpl.prepareNicIPConfiguration(
name,
this,
super.myManager
);
return nicIPConfiguration;
}
private void clearCachedRelatedResources() {
this.networkSecurityGroup = null;
}
NetworkInterfaceImpl withIPConfiguration(NicIPConfigurationImpl nicIPConfiguration) {
this.nicIPConfigurations.put(nicIPConfiguration.name(), nicIPConfiguration);
return this;
}
void addToCreatableDependencies(Creatable<? extends Resource> creatableResource) {
this.addDependency(creatableResource);
}
Resource createdDependencyResource(String key) {
return this.<Resource>taskResult(key);
}
Creatable<ResourceGroup> newGroup() {
return this.creatableGroup;
}
@Override
protected Observable<NetworkInterfaceInner> createInner() {
return this.manager().inner().networkInterfaces().createOrUpdateAsync(this.resourceGroupName(), this.name(), this.inner());
}
@Override
protected void afterCreating() {
clearCachedRelatedResources();
}
@Override
protected void beforeCreating() {
NetworkSecurityGroup networkSecurityGroup = null;
if (creatableNetworkSecurityGroupKey != null) {
networkSecurityGroup = this.<NetworkSecurityGroup>taskResult(creatableNetworkSecurityGroupKey);
} else if (existingNetworkSecurityGroupToAssociate != null) {
networkSecurityGroup = existingNetworkSecurityGroupToAssociate;
}
if (networkSecurityGroup != null) {
this.inner().withNetworkSecurityGroup(new NetworkSecurityGroupInner().withId(networkSecurityGroup.id()));
}
NicIPConfigurationImpl.ensureConfigurations(this.nicIPConfigurations.values());
this.inner().withIpConfigurations(innersFromWrappers(this.nicIPConfigurations.values()));
}
}