Search in sources :

Example 1 with AzureDiagnosticSettings

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);
    }
}
Also used : AzureDiagnosticSettings(com.vmware.photon.controller.model.adapters.azure.model.diagnostics.AzureDiagnosticSettings) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) File(java.io.File) InvalidKeyException(java.security.InvalidKeyException) StorageException(com.microsoft.azure.storage.StorageException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudException(com.microsoft.azure.CloudException)

Aggregations

CloudException (com.microsoft.azure.CloudException)1 ApplicationTokenCredentials (com.microsoft.azure.credentials.ApplicationTokenCredentials)1 StorageException (com.microsoft.azure.storage.StorageException)1 AzureDiagnosticSettings (com.vmware.photon.controller.model.adapters.azure.model.diagnostics.AzureDiagnosticSettings)1 Operation (com.vmware.xenon.common.Operation)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 InvalidKeyException (java.security.InvalidKeyException)1