频道栏目
首页 > 系统 > 虚拟机 > 正文

使用Java调用Restapi设置经典Linux虚拟机启停实例

2017-09-29 10:10:00      个评论    来源:zangdalei的专栏  
收藏   我要投稿

现象描述

用户可以通过 Rest API 设置经典 Linux 虚拟机实例的启停。在调用该 API 时需要通过 Azure Active Directory(下文简称 AAD) 获取 Token,但是因为中国 Azure 中通过 AAD 的 Application 获取到的 Token 无法操作经典 API,所以需要通过 Client ID 和管理员的用户名密码来获取 Token。

前提条件

创建一台 Linux 经典虚拟机。

示例代码

Java

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.*;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.net.ssl.*;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import org.codehaus.jackson.map.ObjectMapper;

//get Access token for Rest API
public void GetToken() { 
    ExecutorService service = Executors.newFixedThreadPool(1);
    AuthenticationContext ac = new AuthenticationContext("https://login.chinacloudapi.cn/tenantID", true, service); 
    Future future = ac.acquireToken("https://management.core.chinacloudapi.cn/",  "1950a258-227b-4e31-a9cf-717495945fc2", "username", "password", null);
    AuthenticationResult result = future.get();
    String token = result.getAccessToken();
    rest(token);
}

public static void rest(String accessToken) throws IOException{
    URL url = new URL(String.format("https://management.core.chinacloudapi.cn/subID/services/hostedservices/{hostedservices}/deployments/{deployments}/roleinstances/{roleinstancesName}/Operations"));
    HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
    conn.setRequestProperty("x-ms-version", "2013-06-01");
    conn.setRequestProperty("Authorization", "Bearer " + accessToken);
    conn.setRequestProperty("Content-Type", "application/xml");

    //StartRole
    String roleInstance = new String("\n" + "StartRoleOperation\n" +                               "");
    //ShutdownRole             
    String roleins = new String( "" +"ShutdownRoleOperation" +                                           "StoppedDeallocated" +                                               "");
    byte[] data = roleInstance.getBytes();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    if (data != null)
    {
        DataOutputStream requestStream = new DataOutputStream(conn.getOutputStream());
        requestStream.write(data);
        requestStream.flush();
        requestStream.close();
    }
    String mess =  conn.getResponseMessage();
    int code = conn.getResponseCode(); 
    InputStream input = conn.getErrorStream();
    if (input == null)
    input = conn.getInputStream();
    String response = null;
    try (Scanner scanner = new Scanner(input)) {
        scanner.useDelimiter("\\Z");
        response = scanner.next();
        scanner.close();
        input.close();
    }
}
上一篇:如何将CentOS虚拟机迁移到Azure
下一篇:Linux虚拟机磁盘设备名不一致的问题解决办法
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站