Java Code Examples for com.microsoft.azure.management.network.PublicIPAddress#refresh()

The following examples show how to use com.microsoft.azure.management.network.PublicIPAddress#refresh() . 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: TestVirtualMachineSsh.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Override
public VirtualMachine createResource(VirtualMachines virtualMachines) throws Exception {
    final String vmName = "vm" + this.testId;
    final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD [email protected]";        
    final String publicIpDnsLabel = vmName;
    PublicIPAddress pip = pips.define(publicIpDnsLabel)
        .withRegion(Region.US_EAST)
        .withNewResourceGroup()
        .withLeafDomainLabel(publicIpDnsLabel)
        .create();

    VirtualMachine vm = virtualMachines.define(vmName)
            .withRegion(pip.regionName())
            .withExistingResourceGroup(pip.resourceGroupName())
            .withNewPrimaryNetwork("10.0.0.0/28")
            .withPrimaryPrivateIPAddressDynamic()
            .withExistingPrimaryPublicIPAddress(pip)
            .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_14_04_LTS)
            .withRootUsername("testuser")
            .withRootPassword("12NewPA$$w0rd!")
            .withSsh(sshKey)
            .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
            .create();

    pip.refresh();
    Assert.assertTrue(pip.hasAssignedNetworkInterface());

    JSch jsch= new JSch();
    Session session = null;
    if (TestBase.isRecordMode()) {
        try {
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            // jsch.addIdentity(sshFile, filePassword);
            session = jsch.getSession("testuser", publicIpDnsLabel + "." + "eastus.cloudapp.azure.com", 22);
            session.setPassword("12NewPA$$w0rd!");
            session.setConfig(config);
            session.connect();
        } catch (Exception e) {
            Assert.fail("SSH connection failed" + e.getMessage());
        } finally {
            if (session != null) {
                session.disconnect();
            }
        }

        Assert.assertNotNull(vm.inner().osProfile().linuxConfiguration().ssh());
        Assert.assertTrue(vm.inner().osProfile().linuxConfiguration().ssh().publicKeys().size() > 0);
    }
    return vm;
}