Skip to content

Commit

Permalink
Merge pull request #1993 from alphadev4/Azure/Databricks-Workspace-Ha…
Browse files Browse the repository at this point in the history
…s-Tags

Azure/Databricks-Workspace-Has-Tags
  • Loading branch information
alphadev4 authored May 20, 2024
2 parents 28a3c8b + 5899b31 commit 0268265
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,7 @@ module.exports = {
'workspaceSecureCluster' : require(__dirname + '/plugins/azure/databricks/workspaceSecureCluster.js'),
'workspaceDiagnosticLogs' : require(__dirname + '/plugins/azure/databricks/workspaceDiagnosticLogs.js'),
'workspaceManagedServicesCmk' : require(__dirname + '/plugins/azure/databricks/workspaceManagedServicesCmk.js'),
'workspaceHasTags' : require(__dirname + '/plugins/azure/databricks/workspaceHasTags.js'),

},
github: {
Expand Down
53 changes: 53 additions & 0 deletions plugins/azure/databricks/workspaceHasTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var async = require('async');
var helpers = require('../../../helpers/azure');

module.exports = {
title: 'Databricks Workspace Has Tags',
category: 'AI & ML',
domain: 'Machine Learning',
severity: 'Low',
description: 'Ensures that Azure Databricks Workspace has tags associated.',
more_info: 'Tags help you to group resources together that are related to or associated with each other. It is a best practice to tag cloud resources to better organize and gain visibility into their usage.',
link: 'https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources',
recommended_action: 'Modify databricks workspace and add tags.',
apis: ['databricks:listWorkspaces'],
realtime_triggers: ['microsoftdatabricks:workspaces:write','microsoftdatabricks:workspaces:delete','microsoftresources:tags:write'],

run: function(cache, settings, callback) {
const results = [];
const source = {};
const locations = helpers.locations(settings.govcloud);

async.each(locations.databricks, function(location, rcb) {
const databricks = helpers.addSource(cache, source,
['databricks', 'listWorkspaces', location]);

if (!databricks) return rcb();

if (databricks.err || !databricks.data) {
helpers.addResult(results, 3, 'Unable to query for Databricks Workspaces: ' + helpers.addError(databricks), location);
return rcb();
}

if (!databricks.data.length) {
helpers.addResult(results, 0, 'No existing Databricks Workspaces found', location);
return rcb();
}

for (let workspace of databricks.data) {
if (!workspace.id) continue;

if (workspace.tags && Object.entries(workspace.tags).length > 0) {
helpers.addResult(results, 0, 'Databricks workspace has tags associated', location, workspace.id);
} else {
helpers.addResult(results, 2, 'Databricks workspace does not have tags associated', location, workspace.id);
}
}

rcb();
}, function() {
// Global checking goes here
callback(null, results, source);
});
}
};
89 changes: 89 additions & 0 deletions plugins/azure/databricks/workspaceHasTags.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var expect = require('chai').expect;
var workspaceHasTags = require('./workspaceHasTags.js');

const workspaces = [
{
"id": "/subscriptions/1234/resourceGroups/test/providers/Microsoft.Databricks/workspaces/test-workspace",
"name": "test-workspace",
"type": "Microsoft.Databricks/workspaces",
"sku": {
"name": "trial"
},
"location": "eastus",
"tags": {}
},
{
"id": "/subscriptions/1234/resourceGroups/test/providers/Microsoft.Databricks/workspaces/test-workspace",
"name": "test-workspace",
"type": "Microsoft.Databricks/workspaces",
"sku": {
"name": "premium"
},
"location": "eastus",
"tags": {"key": "value"},
},
];


const createCache = (workspaces, err) => {

return {
databricks: {
listWorkspaces: {
'eastus': {
data: workspaces,
err: err
}
}
}
};
};

describe('workspaceHasTags', function () {
describe('run', function () {

it('should give a passing result if no Databricks workspaces are found', function (done) {
const cache = createCache([], null);
workspaceHasTags.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
expect(results[0].message).to.include('No existing Databricks Workspaces found');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give unknown result if unable to query for Databricks workspaces', function (done) {
const cache = createCache(null, ['error']);
workspaceHasTags.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(3);
expect(results[0].message).to.include('Unable to query for Databricks Workspaces');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give passing result if Databricks workspace has tags associated', function (done) {
const cache = createCache([workspaces[1]], null);
workspaceHasTags.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(0);
expect(results[0].message).to.include('Databricks workspace has tags associated');
expect(results[0].region).to.equal('eastus');
done();
});
});

it('should give failing result if Databricks workspace does not have tags associated', function (done) {
const cache = createCache([workspaces[0]], null);
workspaceHasTags.run(cache, {}, (err, results) => {
expect(results.length).to.equal(1);
expect(results[0].status).to.equal(2);
expect(results[0].message).to.include('Databricks workspace does not have tags associated');
expect(results[0].region).to.equal('eastus');
done();
});
});
});
});

0 comments on commit 0268265

Please sign in to comment.