use of com.vmware.photon.controller.model.adapters.azure.model.diagnostics.AzureDiagnosticSettings in project photon-model by vmware.
the class AzureInstanceService method enableMonitoring.
private void enableMonitoring(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
Operation readFile = Operation.createGet(null).setCompletion((o, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
AzureDiagnosticSettings azureDiagnosticSettings = o.getBody(AzureDiagnosticSettings.class);
String vmName = ctx.vmName;
String azureInstanceId = ctx.vmId;
String storageAccountName = ctx.storageAccountName;
// Replace the resourceId and storageAccount keys with correct values
azureDiagnosticSettings.getProperties().getPublicConfiguration().getDiagnosticMonitorConfiguration().getMetrics().setResourceId(azureInstanceId);
azureDiagnosticSettings.getProperties().getPublicConfiguration().setStorageAccount(storageAccountName);
ApplicationTokenCredentials credentials = ctx.azureSdkClients.credentials;
URI uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(UriUtils.buildUri(AzureUtils.getAzureBaseUri()), azureInstanceId, AzureConstants.DIAGNOSTIC_SETTING_ENDPOINT, AzureConstants.DIAGNOSTIC_SETTING_AGENT), AzureConstants.QUERY_PARAM_API_VERSION, AzureConstants.DIAGNOSTIC_SETTING_API_VERSION);
Operation operation = Operation.createPut(uri);
operation.setBody(azureDiagnosticSettings);
operation.addRequestHeader(Operation.ACCEPT_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
operation.addRequestHeader(Operation.CONTENT_TYPE_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
try {
operation.addRequestHeader(Operation.AUTHORIZATION_HEADER, AzureConstants.AUTH_HEADER_BEARER_PREFIX + credentials.getToken(AzureUtils.getAzureBaseUri()));
} catch (Exception ex) {
handleError(ctx, ex);
return;
}
logFine(() -> String.format("Enabling monitoring on the VM [%s]", vmName));
operation.setCompletion((op, er) -> {
if (er != null) {
handleError(ctx, er);
return;
}
logFine(() -> String.format("Successfully enabled monitoring on the VM [%s]", vmName));
handleAllocation(ctx, nextStage);
});
sendRequest(operation);
});
String fileUri = getClass().getResource(AzureConstants.DIAGNOSTIC_SETTINGS_JSON_FILE_NAME).getFile();
File jsonPayloadFile = new File(fileUri);
try {
FileUtils.readFileAndComplete(readFile, jsonPayloadFile);
} catch (Exception e) {
handleError(ctx, e);
}
}
Aggregations