Skip to content

Commit

Permalink
fix: Feature/storage diagnostic settings (#55)
Browse files Browse the repository at this point in the history
* set blob storage diagnostic settings via Azure Javascript SDK

* minor refactor
  • Loading branch information
bdschaap authored and whilke committed May 16, 2019
1 parent 7ce78e9 commit 09d2083
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
32 changes: 31 additions & 1 deletion ingredient/ingredient-storage/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion ingredient/ingredient-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"dependencies": {
"@azbake/arm-helper": "^0.1.47",
"@azure/arm-storage": "^8.0.0"
"@azure/arm-storage": "^8.0.0",
"@azure/storage-blob": "^10.3.0"
},
"gitHead": "dfddb0fc587d47c1d9aad2ff6ef877ef73260ef4"
}
106 changes: 104 additions & 2 deletions ingredient/ingredient-storage/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,124 @@
import { BaseIngredient, IngredientManager } from "@azbake/core"
import { ARMHelper } from "@azbake/arm-helper"
import ARMTemplate from "./storage.json"
import { StorageUtils } from "./functions.js";
import { StorageManagementClient } from "@azure/arm-storage"
import { ServiceURL, StorageURL, SharedKeyCredential, Aborter } from "@azure/storage-blob"

export class StoragePlugIn extends BaseIngredient {
public async Execute(): Promise<void> {
try {
let util = IngredientManager.getIngredientFunction("coreutils", this._ctx)
this._logger.log('Custom Plugin Logging: ' + this._ingredient.properties.source)
this._logger.log("Storage ingredient logging");

const helper = new ARMHelper(this._ctx);

let params = await helper.BakeParamsToARMParamsAsync(this._name, this._ingredient.properties.parameters)

await helper.DeployTemplate(this._name, ARMTemplate, params, await util.resource_group())


await this.ConfigureDiagnosticSettings(params, util);

} catch(error){
this._logger.error('deployment failed: ' + error)
throw error
}
}

private async ConfigureDiagnosticSettings(params: any, util: any) {
let accountName: string;
let accountKey: string;

//Get blob storage properties
accountName = params["storageAccountName"].value;
const storageUtils = new StorageUtils(this._ctx);
accountKey = await storageUtils.get_primary_key(accountName, await util.resource_group())
const credentials = new SharedKeyCredential(accountName, accountKey);
const pipeline = StorageURL.newPipeline(credentials, {
// Enable logger when debugging
// logger: new ConsoleHttpPipelineLogger(HttpPipelineLogLevel.INFO)
});
const blobPrimaryURL = `https://${accountName}.blob.core.windows.net/`;
var serviceURL = new ServiceURL(blobPrimaryURL, pipeline)
const serviceProperties = await serviceURL.getProperties(Aborter.none);

//Get Bake variables for diagnostic settings. Default to "true" (enabled) and 10 days data retention.
let blobDiagnosticHourlyMetricsEnabled: string = await util.variable("blobDiagnosticHourlyMetricsEnabled") || "true"
let blobDiagnosticHourlyMetricsRetentionDays = await util.variable("blobDiagnosticHourlyMetricsRetentionDays") || 10
let blobDiagnosticMinuteMetricsEnabled: string = await util.variable("blobDiagnosticMinuteMetricsEnabled") || "true"
let blobDiagnosticMinuteMetricsRetentionDays = await util.variable("blobDiagnosticMinuteMetricsRetentionDays") || 10
let blobDiagnosticLoggingEnabled: string = await util.variable("blobDiagnosticLoggingEnabled") || "true"
let blobDiagnosticLoggingRetentionDays = await util.variable("blobDiagnosticLoggingRetentionDays") || 10

//Workaround due to issues using boolean data type for Bake variables
var boolBlobDiagnosticHourlyMetricsEnabled: boolean = (blobDiagnosticHourlyMetricsEnabled == "true") ? true : false;
var boolBlobDiagnosticMinuteMetricsEnabled: boolean = (blobDiagnosticMinuteMetricsEnabled == "true") ? true : false;
var boolBlobDiagnosticLoggingEnabled: boolean = (blobDiagnosticLoggingEnabled == "true") ? true : false;

//Debug logging of Bake variables
this._logger.debug("blobDiagnosticHourlyMetricsEnabled:" + boolBlobDiagnosticHourlyMetricsEnabled)
this._logger.debug("blobDiagnosticHourlyMetricsRetentionDays:" + blobDiagnosticHourlyMetricsRetentionDays)
this._logger.debug("blobDiagnosticMinuteMetricsEnabled:" + boolBlobDiagnosticMinuteMetricsEnabled)
this._logger.debug("blobDiagnosticMinuteMetricsRetentionDays:" + blobDiagnosticMinuteMetricsRetentionDays)
this._logger.debug("blobDiagnosticLoggingEnabled:" + boolBlobDiagnosticLoggingEnabled)
this._logger.debug("blobDiagnosticLoggingRetentionDays:" + blobDiagnosticLoggingRetentionDays)

//Configure hourly metric settings
serviceProperties.hourMetrics = {
enabled: boolBlobDiagnosticHourlyMetricsEnabled,
retentionPolicy: {
days: blobDiagnosticHourlyMetricsRetentionDays,
enabled: true
},
version: "1.0"
};

//Azure will error if includeAPIs is set when the metrics diagnostics setting is not enabled
if (boolBlobDiagnosticHourlyMetricsEnabled) {
serviceProperties.hourMetrics.includeAPIs = true;
}

//Configure minute metric settings
serviceProperties.minuteMetrics = {
enabled: boolBlobDiagnosticMinuteMetricsEnabled,
retentionPolicy: {
days: blobDiagnosticMinuteMetricsRetentionDays,
enabled: true
},
version: "1.0"
};

//Azure will error if includeAPIs is set when the metrics diagnostics setting is not enabled
if (boolBlobDiagnosticMinuteMetricsEnabled) {
serviceProperties.minuteMetrics.includeAPIs = true;
}

//Configure logging settings
serviceProperties.logging = {
deleteProperty: boolBlobDiagnosticLoggingEnabled,
read: boolBlobDiagnosticLoggingEnabled,
retentionPolicy: {
days: blobDiagnosticLoggingRetentionDays,
enabled: true
},
version: "2.0",
write: boolBlobDiagnosticLoggingEnabled
};

//The Azure SDK will error with "storageServiceProperties.Cors must be of type Array." if no CORS value is specified. Set to a restrictive setting.
//See: https://github.com/Azure/azure-sdk-for-js/issues/2909
const requiredCORS = {
allowedHeaders: "*",
allowedMethods: "GET",
allowedOrigins: "localhost",
exposedHeaders: "*",
maxAgeInSeconds: 8888
};
if (!serviceProperties.cors) {
serviceProperties.cors = [requiredCORS];
}

//Post blob service properties back to Azure
await serviceURL.setProperties(Aborter.none, serviceProperties);
}
}

0 comments on commit 09d2083

Please sign in to comment.