In this link ( Create azure automation account using REST api from java ) I'd asked about how to create an automation account in order to create runbook. Now, that I've created an automation account and also a runbook which is published, I want to execute(start) the runbook. In order to do so I am following this link ( https://msdn.microsoft.com/en-us/library/azure/mt163849.aspx ) but I am getting an error:
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
This is the java function:
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);
}
My guess is the error is due to the fact that you are not passing a proper GUID as the job id for the job to create. You are passing 8c3e715-9b27
but GUIDs are in the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
@Joe's guess is correct.
Refer to the link https://msdn.microsoft.com/en-us/library/azure/mt163849.aspx . As document shown,
In Windows PowerShell, you can use the this command to create the job ID:[GUID]::NewGuid().ToString().
The GUID in C#/.NET is generated by the functuion "System.Guid.NewGuid()". In Java, UUID is the same as GUID. Refer to the UUID Class link http://docs.oracle.com/javase/8/docs/api/java/util/UUID.html , it is generated by the function "java.util.UUID.randomUUID()".
So your code should be modified as following:
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);
}
If you have created the runbook correctly and set up the correct runbook name in the request body, the code will run as expection and response the StatusCode 201.
However I found the other issue of the function "createRunbook" in your thread Create azure automation account using REST api from java , the element " properties/publishContentLink/uri" is required in the request body of create runbook(refer to https://msdn.microsoft.com/en-us/library/azure/mt163812.aspx ).
So if the response body of create job include the information {"code":"NotFound","message":"Runbook not found."}, I suggest you to check your code and review the Runbook page on Azure Portal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.