8000 Adding Azure ARM provider (azure-v2) by morsh · Pull Request #550 · pkgcloud/pkgcloud · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bcbccbf
initial checkin azure v2
morsh Dec 12, 2016
d62014b
adding login to get server
morsh Dec 13, 2016
906472d
DestroyServer addition
jeniawhite Dec 13, 2016
725ef47
Merge branch 'master' of https://github.com/CatalystCode/pkgcloud
morsh Dec 13, 2016
043e151
Fixing removal of options in logintoazure after merge
jeniawhite Dec 13, 2016
49745cc
Fixing get server status response on errors
jeniawhite Dec 13, 2016
6244f34
adding storage for azure-v2
morsh Dec 30, 2016
d49cfcd
azure-v2 compute mgmt + moving to templates
morsh Jan 2, 2017
69ee436
not including .private. files
morsh Jan 2, 2017
61b7b3e
adjustments to code with self
morsh Jan 2, 2017
6e51331
minor fixes
morsh Jan 2, 2017
1f0301b
updating code according to travis-ci comments
morsh Jan 3, 2017
288c81e
travis-ci review #2
morsh Jan 3, 2017
1eb444f
adding first tests
morsh Jan 4, 2017
186f0d0
update test name
morsh Jan 4, 2017
c586d42
fix bind
morsh Jan 4, 2017
6ae7635
update test for multple calls
morsh Jan 4, 2017
ee05c2d
destroying dependencies when destroying a linux VM
morsh Jan 8, 2017
424d6b8
adding tests to containers for azure-v2
morsh Jan 8, 2017
2398625
travis review
morsh Jan 8, 2017
32f957e
removing launch.json
morsh Jan 8, 2017
b37ed6c
updating examples
morsh Jan 8, 2017
07cdf66
changing destroy server to enable different dependencies
morsh Jan 11, 2017
5fa39a7
adding remote extension for windows vms
morsh Feb 9, 2017
d965ec3
Merge remote-tracking branch 'pkgcloud/master'
jackyalbo Feb 13, 2017
5607270
adding remove file functionality
morsh Feb 14, 2017
227467c
updating relative pkgcloud path
morsh Feb 14, 2017
8de41e9
adding dynamic data disk creation
morsh Feb 15, 2017
a2ce4f0
reverting package to right name/version
morsh Feb 15, 2017
63f27a5
fixing jshint #1
morsh Feb 15, 2017
e030cd3
jshint fixes #2
morsh Feb 15, 2017
3113e82
jshint fix #3
morsh Feb 15, 2017
bd3867e
Merge pull request #1 from pkgcloud/master
Feb 15, 2017
1e44b67
removing all arrow functions
morsh Feb 15, 2017
6a38525
reverting package.json
morsh Feb 15, 2017
7058437
removing es6 singlequotes
morsh Feb 15, 2017
326a5f5
jshint #4
morsh Feb 15, 2017
cd551f1
removing extractors
morsh Feb 15, 2017
ac50dc8
adjusting some tests
morsh Feb 16, 2017
0e8b229
canceling filter of azure-v2
morsh Feb 16, 2017
13575ee
Merge remote-tracking branch 'origin/master'
jackyalbo Mar 12, 2017
b8f41ac
adding ssh support for both linux and windows
jackyalbo Mar 15, 2017
4dec2ec
Merge pull request #3 from CatalystCode/ssh_fix2
Mar 16, 2017
b02a55c
small urgent fix
jackyalbo Apr 25, 2017
98aa959
Merge pull request #4 from CatalystCode/ssh_fix2
jackyalbo Apr 25, 2017
718987c
miniature fix for running ssh on windows
jackyalbo Apr 26, 2017
a8fb5b4
Merge pull request #5 from CatalystCode/ssh_fix2
jackyalbo Apr 26, 2017
2caa019
bug fix
jackyalbo Apr 27, 2017
c1c749d
Merge pull request #6 from CatalystCode/ssh_fix2
jackyalbo Apr 27, 2017
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ azure_error
coverage.html
.coveralls.yml
pkgcloud.lcov*
node-jscoverage
node-jscoverage
*.private.*
*.vscode/launch.json
64 changes: 64 additions & 0 deletions examples/compute/azure-v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var pkgcloud = require('../../lib/pkgcloud');
var client;
var options;

//
// Create a pkgcloud compute instance
//
options = {
provider: 'azure-v2',
subscriptionId: '{subscriptionId}',
resourceGroup: '{resourceGroup}',

servicePrincipal: {
clientId: '{spClientId}',
secret: '{spSecret}',
domain: '{spDomain}'
}
};
client = pkgcloud.compute.createClient(options);

//
// Create a server.
// This may take several minutes.
//
var createVMOfferOptions = {
name: 'ms-pkgc-vm-test',
flavor: 'Standard_D1',
username: 'pkgcloud',
password: 'Pkgclo EE5B ud!!',

storageOSDiskName: 'osdisk',
storageDataDiskNames: [ 'datadisk1' ],

imagePublisher: 'Canonical',
imageOffer: 'UbuntuServer',
imageSku: '16.04.0-LTS',
imageVersion: 'latest'
};

console.log('creating server...');

client.createServer(createVMOfferOptions, function (err, server) {

console.log('servre created successfully:');
console.dir(server);

if (err) {
console.log(err);
} else {
client.destroyServer(createVMOfferOptions, {
destroyNics: true,
destroyPublicIP: true,
destroyVnet: true,
destroyStorage: true
}, function (err, server) {
if (err) {
console.log(err);
} else {
console.log('deleted successfully');
console.dir(server);
}
});
}
});
76 changes: 76 additions & 0 deletions examples/storage/azure-v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var path = require('path');
var pkgcloud = require('../../lib/pkgcloud');

var client = pkgcloud.storage.createClient({
provider: 'azure-v2',
subscriptionId: '{subscriptionId}',
resourceGroup: '{resourceGroup}',

servicePrincipal: {
clientId: '{spClientId}',
secret: '{spSecret}',
domain: '{spDomain}'
}
});

// client.getFiles('storageacountname', null, function (err, files) (function (err) {

// var file = files[0];
// client.getFile('storageacountname', file, null, function (err, file) (function (err) {
// console.dir(file);
// })
// });

client.getFiles('storageacountname', { container: 'container-name' }, function (err, files) {

if (err) {
return console.error(err);
}

var file = files[0];
console.dir(file);

var download = client.download({
container: 'storageacountname',
storage: { container: 'container-name' },
remote: 'file.name.to.download.ext',
local: path.join(__dirname, 'file.name.to.download.ext')
}, function (err) {
return err ? console.dir(err) : null;
});

download.on('error', function(err) {
console.error(err);
});

download.on('end', function(file) {
console.log('file write has ended:');
console.dir(file);
});

download.on('data', function(data) {
console.log(data && data.length);
});
});

// client.createContainer('storageacountname', function (err, container) {
// console.log('created: ', container.toJSON());

// client.getContainer('storageacountname', function (err, container) {
// console.log('found: ', container.toJSON());
// });
// });

// client.getContainers(function (err, containers) {
// if (err) {
// console.error(err);
// }

// client.getContainer(containers[0], function (err, container) {
// console.log('found: ', container.toJSON());
// });

// containers.forEach(function (container) {
// console.log(container.toJSON());
// });
// });
1 change: 1 addition & 0 deletions lib/pkgcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var components = [
var providers = [
'amazon',
'azure',
'azure-v2',
'digitalocean',
'google',
'iriscouch',
Expand Down
112 changes: 112 additions & 0 deletions lib/pkgcloud/azure-v2/azure-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
var errs = require('errs');

var msRestAzure = require('ms-rest-azure');
var resourceManagement = require('azure-arm-resource');

var templates = require('./templates');
var constants = require('./constants');

/**
* This callback type is called `requestCallback` and is displayed as a global symbol.
*
* @callback requestCallback
* @param {object} error
* @param {object} result
*/

/**
* Request and save credentials for accessing azure ARM resources.
* @param {object} client object containing configuration.
* @param {boolean} setupLocation Should setup a location from the resource group.
* @param {requestCallback} callback to respond to when complete.
*/
function login(setupLocation, callback) {

var self = this;
if (typeof setupLocation == 'function' && typeof callback === 'undefined') {
callback = setupLocation;
setupLocation = null;
}

// Make sure credentials are refreshed by intervals
if (self.azure && self.azure.credentials && self.azure.lastRefresh) {
var now = new Date();
if (now - self.azure.lastRefresh < constants.CREDENTIALS_LIFESPAN) {
return callback(null, self.azure.credentials);
}
}

var config = self.config;
var servicePrincipal = config.servicePrincipal;
msRestAzure.loginWithServicePrincipalSecret(
servicePrincipal.clientId,
servicePrincipal.secret,
servicePrincipal.domain,
function (err, credentials) {

if (err) {
errs.handle(
errs.create({
message: 'There was a problem connecting to azure: ' + err
}),
callback
);
}

self.azure = self.azure || {};
self.azure.credentials = credentials;
self.azure.lastRefresh = new Date();

if (setupLocation) {
return self.setupLocation(credentials, function (err) {
return callback(err, credentials);
});
} else {
return callback(null, credentials);
}
});
}

/**
* Setting up an azure session including querying the default location from the resource group
* used by the current configuration
* @param {object} credentials object containing azure credentials
* @param {requestCallback} callback to respond to when complete.
*/
function setupLocation(credentials, callback) {
var self = this;

self.azure = self.azure || {};
self.azure.location = self.azure.location || self.config.location;
if (self.azure.location) {
return callback();
}

if (self.config.resourceGroup) {
var resourceClient = new resourceManagement.ResourceManagementClient(credentials, self.config.subscriptionId);
resourceClient.resourceGroups.get(self.config.resourceGroup, function (err, result) {

if (err) {
return callback(err);
}

self.azure.location = result.location;
return callback();
});
}
}

/**
* Binding common methods to be available to all clients
* @param {object} credentials object containing azure credentials
* @param {requestCallback} callback to respond to when complete.
*/
function bind(client) {
client.login = login.bind(client);
client.setupLocation = setupLocation.bind(client);
client.deploy = templates.deploy.bind(client);
}

module.exports = {
bind: bind
};
56 changes: 56 additions & 0 deletions lib/pkgcloud/azure-v2/client.js
4861
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* client.js: Base client from which all Azure clients inherit from
*
* (C) Microsoft Open Technologies, Inc. All rights reserved.
*
*/

var util = require('util');

var base = require('../core/base');
var azureApi = require('./azure-api');

var Client = exports.Client = function (options) {
base.Client.call(this, options);

options = options || {};

// Allow overriding serversUrl in child classes
this.provider = 'azure-v2';
azureApi.bind(this);

if (!this.before) {
this.before = [];
}
};

util.inherits(Client, base.Client);

Client.prototype._toArray = function toArray(obj) {
if (typeof obj === 'undefined') {
return [];
}

return Array.isArray(obj) ? obj : [obj];
};

Client.prototype.failCodes = {
400: 'Bad Request',
401: 'Unauthorized',
403: 'Forbidden',
404: 'Item not found',
409: 'Already exists or in progress',
412: 'Lease error',
413: 'Request Entity Too Large',
415: 'Bad Media Type',
500: 'Fault',
503: 'Service Unavailable'
};

Client.prototype.successCodes = {
200: 'OK',
201: 'Created',
202: 'Accepted',
203: 'Non-authoritative information',
204: 'No content'
};
60 changes: 60 additions & 0 deletions lib/pkgcloud/azure-v2/compute/client/flavors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* flavors.js: Implementation of Azure Flavors Client.
*
* (C) Microsoft Open Technologies, Inc.
*
*/

var _ = require('lodash');
var ComputeManagementClient = require('azure-arm-compute');

/**
* Lists all flavors available to your account.
* @param {function} callback - cb(err, flavors). `flavors` is an array that
* represents the flavors that are available to your account
*/
function getFlavors(callback) {
var self = this;

self.login(true, function (err) {

if (err) {
return callback(err);
}

var client = new ComputeManagementClient(self.azure.credentials, self.config.subscriptionId);
client.virtualMachineSizes.list(self.azure.location, function (err, results) {
return err
? callback(err)
: callback(null, results.map(function (res) {
return new self.models.Flavor(self, res);
}));
});
});
}

/**
* Gets a specified flavor of AWS DataSets using the provided details object.
* @param {Flavor|String} image - Flavor ID or an Flavor
* @param {function} callback cb(err, flavor). `flavor` is an object that
* represents the flavor that was retrieved.
*/
function getFlavor(flavor, callback) {
var self = this;
var flavorId = flavor instanceof self.models.Flavor ? flavor.id : flavor;

if (flavor instanceof self.models.Flavor) {
return callback(null, flavor);
}

self.getFlavors(function (err, flavors) {
return err ?
callback(err) :
callback(null, _.find(flavors, { id: flavorId }));
});
}

module.exports = {
getFlavors: getFlavors,
getFlavor: getFlavor
};
Loading
0