Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/storage diagnostic settings #55

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}