gpt4 book ai didi

java - 创建自动化作业以使用 REST API 执行 azure runbook

转载 作者:搜寻专家 更新时间:2023-10-31 20:27:07 24 4
gpt4 key购买 nike

在此链接 ( 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);
}

最佳答案

@Joe 的猜测是正确的。

引用链接https://msdn.microsoft.com/en-us/library/azure/mt163849.aspx 。如文档所示,

In Windows PowerShell, you can use the this command to create the job ID:[GUID]::NewGuid().ToString().

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

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

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。

但是我在您的线程中发现了函数“createRunbook”的另一个问题 Create azure automation account using REST api from java ,create Runbook 的请求体中需要包含“properties/publishContentLink/uri”元素(参见 https://msdn.microsoft.com/en-us/library/azure/mt163812.aspx )。

enter image description here

因此,如果创建作业的响应正文包含信息 {"code":"NotFound","message":"Runbook not find."},我建议您检查代码并查看 Azure Portal 上的 Runbook 页面.

关于java - 创建自动化作业以使用 REST API 执行 azure runbook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31940947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com