Skip to content

Commit

Permalink
NIC simplification
Browse files Browse the repository at this point in the history
- no need for primarySubnetId() since it can be retrieved from primaryIpConfiguration()
- renaming primaryNetwork() as getPrimaryNetwork() to be consistent with the naming of methods that make a separate call to Azure, rather than directly retrieve a prooperty of the object.
- removing the caching logic for the primary network as it seems unnecessary.
- refactoring NicIpConfiguration to use the shared HasSubnet interface for better API consistency enforcement
  • Loading branch information
unknown authored and unknown committed Sep 19, 2016
1 parent 80eee05 commit 6c19eb0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,14 @@ public interface NetworkInterface extends
*/
PublicIpAddress primaryPublicIpAddress();

/**
* @return the resource id of the virtual network subnet associated with this network interface.
*/
String primarySubnetId();

/**
* Gets the virtual network associated this network interface's primary IP configuration.
* <p>
* This method makes a rest API call to fetch the virtual network.
*
* @return the virtual network associated with this network interface.
*/
Network primaryNetwork();
Network getPrimaryNetwork();

/**
* Gets the private IP address allocated to this network interface's primary IP configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.microsoft.azure.management.network.implementation.NetworkInterfaceIPConfigurationInner;
import com.microsoft.azure.management.network.model.HasPrivateIpAddress;
import com.microsoft.azure.management.resources.fluentcore.arm.models.ChildResource;
import com.microsoft.azure.management.resources.fluentcore.arm.models.HasSubnet;
import com.microsoft.azure.management.resources.fluentcore.model.Attachable;
import com.microsoft.azure.management.resources.fluentcore.model.Creatable;
import com.microsoft.azure.management.resources.fluentcore.model.Settable;
Expand All @@ -17,7 +18,8 @@
public interface NicIpConfiguration extends
Wrapper<NetworkInterfaceIPConfigurationInner>,
ChildResource<NetworkInterface>,
HasPrivateIpAddress {
HasPrivateIpAddress,
HasSubnet {
// Getters

/**
Expand All @@ -32,11 +34,6 @@ public interface NicIpConfiguration extends
*/
PublicIpAddress getPublicIpAddress();

/**
* @return the resource id of the virtual network subnet associated with this IP configuration.
*/
String subnetId();

/**
* @return the virtual network associated with this this IP configuration.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class NetworkInterfaceImpl
private NetworkSecurityGroup existingNetworkSecurityGroupToAssociate;
// Cached related resources.
private PublicIpAddress primaryPublicIp;
private Network primaryNetwork;
private NetworkSecurityGroup networkSecurityGroup;

NetworkInterfaceImpl(String name,
Expand Down Expand Up @@ -293,16 +292,8 @@ public PublicIpAddress primaryPublicIpAddress() {
}

@Override
public String primarySubnetId() {
return this.primaryIpConfiguration().subnetId();
}

@Override
public Network primaryNetwork() {
if (this.primaryNetwork == null) {
this.primaryNetwork = this.primaryIpConfiguration().getNetwork();
}
return this.primaryNetwork;
public Network getPrimaryNetwork() {
return this.primaryIpConfiguration().getNetwork();
}

@Override
Expand Down Expand Up @@ -402,7 +393,6 @@ private NicIpConfigurationImpl prepareNewNicIpConfiguration(String name) {

private void clearCachedRelatedResources() {
this.primaryPublicIp = null;
this.primaryNetwork = null;
this.networkSecurityGroup = null;
this.nicPrimaryIpConfiguration = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,29 @@ public PublicIpAddress getPublicIpAddress() {
}

@Override
public String subnetId() {
return this.inner().subnet().id();
public String subnetName() {
SubResource subnetRef = this.inner().subnet();
if (subnetRef != null) {
return ResourceUtils.nameFromResourceId(subnetRef.id());
} else {
return null;
}
}

@Override
public String networkId() {
SubResource subnetRef = this.inner().subnet();
if (subnetRef != null) {
return ResourceUtils.parentResourcePathFromResourceId(subnetRef.id());
} else {
return null;
}
}

@Override
public Network getNetwork() {
String id = subnetId();
return this.networkManager.networks().getByGroup(ResourceUtils.groupFromResourceId(id),
ResourceUtils.extractFromResourceId(id, "virtualNetworks"));
String id = this.networkId();
return this.networkManager.networks().getById(id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public static void main(String[] args) {
System.out.println("Powered OFF VM: " + windowsVM.id() + "; state = " + windowsVM.powerState());

// Get the network where Windows VM is hosted
Network network = windowsVM.primaryNetworkInterface().primaryNetwork();
Network network = windowsVM.primaryNetworkInterface().getPrimaryNetwork();


//=============================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ public static void print(NetworkInterface resource) {
.append("\n\tMAC Address:").append(resource.macAddress())
.append("\n\tPrivate IP:").append(resource.primaryPrivateIp())
.append("\n\tPrivate allocation method:").append(resource.primaryPrivateIpAllocationMethod())
.append("\n\tSubnet Id:").append(resource.primarySubnetId());
.append("\n\tPrimary virtual network ID: ").append(resource.primaryIpConfiguration().networkId())
.append("\n\tPrimary subnet name:").append(resource.primaryIpConfiguration().subnetName());

System.out.println(info.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public void print(NetworkInterface resource) {
.append("\n\tMAC Address:").append(resource.macAddress())
.append("\n\tPrivate IP:").append(resource.primaryPrivateIp())
.append("\n\tPrivate allocation method:").append(resource.primaryPrivateIpAllocationMethod())
.append("\n\tSubnet Id:").append(resource.primarySubnetId())
.append("\n\tPrimary virtual network ID: ").append(resource.primaryIpConfiguration().networkId())
.append("\n\tPrimary subnet name: ").append(resource.primaryIpConfiguration().subnetName())
.append("\n\tIP configurations: ");

// Output IP configs
Expand All @@ -76,7 +77,8 @@ public void print(NetworkInterface resource) {
.append("\n\t\tPrivate IP allocation method: ").append(ipConfig.privateIpAllocationMethod().toString())
.append("\n\t\tPrivate IP version: ").append(ipConfig.privateIpAddressVersion().toString())
.append("\n\t\tPIP id: ").append(ipConfig.publicIpAddressId())
.append("\n\t\tSubnet ID: ").append(ipConfig.subnetId());
.append("\n\t\tAssociated network ID: ").append(ipConfig.networkId())
.append("\n\t\tAssociated subnet name: ").append(ipConfig.subnetName());
}

System.out.println(info.toString());
Expand Down

0 comments on commit 6c19eb0

Please sign in to comment.