创建自动化作业以使用REST API执行azure Runbook

时间:2015-08-11 11:47:55

标签: java api rest azure azure-automation

在此链接(Create azure automation account using REST api from java)中,我询问了如何创建自动帐户以创建Runbook。现在,我已经创建了一个自动化帐户以及一个已发布的Runbook,我想执行(启动)Runbook。为了做到这一点,我正在关注此链接(https://msdn.microsoft.com/en-us/library/azure/mt163849.aspx)但我收到错误:

Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>404 - File or directory not found.</h2>
  <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable

这是java函数:

    private static int processPutRequest(URL url, byte[] data, String contentType, String keyStore, String keyStorePassword) 
    throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException {
       SSLSocketFactory sslFactory = getSSLSocketFactory(keyStore, keyStorePassword);
       HttpsURLConnection con = null;
       con = (HttpsURLConnection) url.openConnection();
       con.setSSLSocketFactory(sslFactory);
       con.setDoOutput(true);
       con.setRequestMethod("PUT");
       con.addRequestProperty("x-ms-version", "2013-08-01");
       con.setRequestProperty("Content-Length", String.valueOf(data.length));
       con.setRequestProperty("Content-Type", contentType);

       DataOutputStream requestStream = new DataOutputStream (con.getOutputStream());
       requestStream.write(data);
       requestStream.flush();
       requestStream.close();

       System.out.println(con.getResponseMessage());

       InputStream error = ((HttpURLConnection) con).getErrorStream();

       BufferedReader br = null;
       if (error == null) {
          InputStream inputstream = con.getInputStream();
          br = new BufferedReader(new InputStreamReader(inputstream));
       } else {
          br = new BufferedReader(new InputStreamReader(error));
       }
       String response = "";
       String nachricht;
       while ((nachricht = br.readLine()) != null){
          response += nachricht;
       }
       System.out.println(response);
       return con.getResponseCode();
   }

   public static void createJobId(String keyStorePath, String keyStorePassword, String subscriptionId)
throws  UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException
   {
       String url = String.format("https://management.core.windows.net/%s/cloudServices/OaaSCSI6EGAZU6F6QTCK5XRVT45FKJC6RC7IQIQW3OPR7SVLE4ZPD4IQQQ-East-US/resources/automation/~/automationAccounts/xdtauto/jobs/8c3e715-9b27?api-version=2014-12-08", subscriptionId);
       String requestBody = "{ \"properties\":{ \"runbook\":{ \"name\":\"createVM\" } } }";
       int createResponseCode = processPutRequest(new URL(url), requestBody.getBytes(), "application/json", keyStorePath, keyStorePassword);
       System.out.println("JOB created :: " + createResponseCode);
   }

2 个答案:

答案 0 :(得分:1)

我的猜测是错误是由于您没有传递正确的GUID作为要创建的作业的作业ID。您正在传递 public class Controller implements Initializable{ public TableView<Student> fxClassroom; @Override public void initialize(URL url, ResourceBundle rb) { .... fxClassroom.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); .... } public void deleteStudent(){ ObservableList<Student> studentSelected, allStudents; allStudents = fxClassroom.getItems(); studentSelected = fxClassroom.getSelectionModel().getSelectedItems(); allStudents.removeAll(studentSelected); } } ,但GUID的格式为8c3e715-9b27

答案 1 :(得分:1)

@ Joe的猜测是正确的。

请参阅链接https://msdn.microsoft.com/en-us/library/azure/mt163849.aspx。如图所示,

  

在Windows PowerShell中,您可以使用此命令创建作业   ID:[GUID] :: NewGuid()的ToString()

C#/ .NET中的GUID由功能&#34; System.Guid.NewGuid()&#34;生成。在Java中,UUID与GUID相同。请参阅UUID类链接http://docs.oracle.com/javase/8/docs/api/java/util/UUID.html,它由函数&#34; java.util.UUID.randomUUID()&#34;生成。

因此,您的代码应修改如下:

public static void createJobId(String keyStorePath, String keyStorePassword, String subscriptionId)
            throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException,
            IOException {
        String automationName = <auto_account_name>;
        String jobId=java.util.UUID.randomUUID().toString();
        String url = String.format(
                "https://management.core.windows.net/%s/cloudServices/ OaaSCSI6EGAZU6F6QTCK5XRVT45FKJC6RC7IQIQW3OPR7SVLE4ZPD4IQQQ-East-US/resources/automation/~/automationAccounts/%s/jobs/%s?api-version=2014-12-08",
                subscriptionId, automationName, jobId);
        System.out.println("URL: "+url);
        String requestBody = "{ \"properties\":{ \"runbook\":{ \"name\":\"<RUNBOOK_NAME>\" } } }";
        int createResponseCode = processPutRequest(new URL(url), requestBody.getBytes(), "application/json",
                keyStorePath, keyStorePassword);
        System.out.println("JOB created :: " + createResponseCode);
    }

如果您已正确创建Runbook并在请求正文中设置了正确的Runbook名称,则代码将作为期望和响应StatusCode 201运行。

但是我发现了函数的另一个问题&#34; createRunbook&#34;在你的帖子Create azure automation account using REST api from java中,元素&#34;属性/ publishContentLink / URI&#34;在create Runbook的请求主体中是必需的(请参阅https://msdn.microsoft.com/en-us/library/azure/mt163812.aspx)。

enter image description here

因此,如果创建作业的响应主体包含信息{&#34;代码&#34;:&#34; NotFound&#34;,&#34;消息&#34;:&#34;未找到Runbook。 &#34;},我建议您检查代码并查看Azure门户上的Runbook页面。