Skip to content

Commit

Permalink
Merge pull request #913 from selvasingh/sample-placeholders
Browse files Browse the repository at this point in the history
Editorial changes to manage ip address
  • Loading branch information
anuchandy authored Jun 28, 2016
2 parents 8bbab34 + b98198e commit a464ec1
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.microsoft.azure.management.network.NetworkInterface;
import com.microsoft.azure.management.network.PublicIpAddress;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.model.Creatable;
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
import com.microsoft.azure.management.samples.Utils;
import okhttp3.logging.HttpLoggingInterceptor;
Expand All @@ -37,12 +36,13 @@ public final class ManageIPAddress {
* @param args the parameters
*/
public static void main(String[] args) {

final String publicIpAddressName1 = ResourceNamer.randomResourceName("pip1", 20);
final String publicIpAddressName2 = ResourceNamer.randomResourceName("pip2", 20);
final String publicIpAddressLeafDNS1 = ResourceNamer.randomResourceName("pip1", 20);
final String publicIpAddressLeafDNS2 = ResourceNamer.randomResourceName("pip2", 20);
final String vmName = ResourceNamer.randomResourceName("vm", 8);
final String rgName = ResourceNamer.randomResourceName("rgNEMPIP", 24);
final String rgName = ResourceNamer.randomResourceName("rgNEMP", 24);
final String userName = "tirekicker";
final String password = "12NewPA$$w0rd!";
try {
Expand All @@ -61,6 +61,7 @@ public static void main(String[] args) {
// Print selected subscription
System.out.println("Selected subscription: " + azure.subscriptionId());
try {

//============================================================
// Assign a public IP address for a VM during its creation

Expand All @@ -74,6 +75,8 @@ public static void main(String[] args) {
.withNewResourceGroup(rgName)
.withLeafDomainLabel(publicIpAddressLeafDNS1)
.create();

System.out.println("Created a public IP address");
// Print public IP address details
Utils.print(publicIpAddress);

Expand Down Expand Up @@ -101,23 +104,26 @@ public static void main(String[] args) {
// Print virtual machine details
Utils.print(vm);


//============================================================
// Gets the public IP address associated with the VM's primary NIC

System.out.println("Reading public IP address associated with the VM's primary NIC [After create]");
System.out.println("Public IP address associated with the VM's primary NIC [After create]");
// Print the public IP address details
Utils.print(vm.primaryPublicIpAddress());


//============================================================
// Assign a new public IP address for the VM

// Define a new public IP address

Creatable<PublicIpAddress> publicIpAddress2Creatable = azure.publicIpAddresses()
PublicIpAddress publicIpAddress2 = azure.publicIpAddresses()
.define(publicIpAddressName2)
.withRegion(Region.US_EAST)
.withNewResourceGroup(rgName)
.withLeafDomainLabel(publicIpAddressLeafDNS2);
.withLeafDomainLabel(publicIpAddressLeafDNS2)
.create();

// Update VM's primary NIC to use the new public IP address

Expand All @@ -126,31 +132,38 @@ public static void main(String[] args) {
NetworkInterface primaryNetworkInterface = vm.primaryNetworkInterface();
primaryNetworkInterface
.update()
.withNewPrimaryPublicIpAddress(publicIpAddress2Creatable)
.withExistingPrimaryPublicIpAddress(publicIpAddress2)
.apply();


//============================================================
// Gets the updated public IP address associated with the VM

// Get the associated public IP address for a virtual machine
System.out.println("Reading public IP address associated with the VM's primary NIC [After Update]");
System.out.println("Public IP address associated with the VM's primary NIC [After Update]");
vm.refresh();
Utils.print(vm.primaryPublicIpAddress());


//============================================================
// Remove public IP associated with the VM

System.out.println("Removing public IP address associated with the VM");
vm.refresh();
primaryNetworkInterface = vm.primaryNetworkInterface();
publicIpAddress = primaryNetworkInterface.primaryPublicIpAddress();
primaryNetworkInterface.update()
.withoutPrimaryPublicIpAddress()
.apply();

System.out.println("Removed public IP address associated with the VM");


//============================================================
// Delete the public ip
System.out.println("Deleting the public IP address");
azure.publicIpAddresses().delete(publicIpAddress.id());
System.out.println("Deleted the public IP address");
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
Expand Down

0 comments on commit a464ec1

Please sign in to comment.