如何使用最新的SDK启动Azure VM异步

时间:2016-12-27 23:43:56

标签: java azure sdk virtual-machine

使用最新的azure java sdk -

<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.0.0-beta4</version>

  1. 如何同步创建虚拟机并监控进度?

  2. ComputeManagementClient不支持clientId,租户类型auth。它使用证书和所有。

  3. 互联网上没有任何示例。 github页面上只有同步示例:https://github.com/Azure-Samples/compute-java-manage-vm

  4. 请回复此帖。我发现,即使在微软论坛上,他们的反应也不是很快。

1 个答案:

答案 0 :(得分:1)

这是我的代码示例,供您参考。

import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.CloudException;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.credentials.AzureTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.PowerState;
import com.microsoft.azure.management.compute.VirtualMachine;
import okhttp3.logging.HttpLoggingInterceptor;

String clientId = "xxxxx";
String domain = "xxxxx";
String secret = "xxxxx";
AzureEnvironment environment = AzureEnvironment.AZURE; 
/* 
 * Or you can use the code `new AzureEnvironment(String authenticationEndpoint, String managementEndpoint, String resourceManagerEndpoint, String graphEndpoint)`, 
 * please see http://azure.github.io/azure-sdk-for-java/com/microsoft/azure/AzureEnvironment.html
 */
AzureTokenCredentials credentials = new ApplicationTokenCredentials(clientId, domain, secret, environment);
Azure azure = Azure.configure().withLogLevel(HttpLoggingInterceptor.Level.BASIC).authenticate(credentials).withDefaultSubscription();
// Get VM instance
String resourceGroup = "xxxx";
String vmName = "xxxx";
VirtualMachine vm = azure.virtualMachines().getByGroup(resourceGroup, vmName);
// Start the VM instance async
vm.start();
// Get the power status of the VM instance by polling
PowerState powerState = vm.powerState();
System.out.println(powerState);

对于上述代码中使用的API,请参阅javadocs http://azure.github.io/azure-sdk-for-java/

希望它有所帮助。