-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharm_compute_ops.js
125 lines (110 loc) · 4.66 KB
/
arm_compute_ops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict"
// arm_compute_ops.js
//
// Operations to perform on compute resources. Only works with ARM resources.
var ComputeManagementClient = require('azure-arm-compute');
var AZURE_CONSTANTS = require('./azure_constants');
var exports = module.exports = {};
function performVmssOperation(operation, client, resourceGroup, resource, callback) {
if (client && resourceGroup && resource) {
// This means we can specify the target VM instance inside a scale set -> e.g. in the test file -> "resource": "scaleset:0" otherwise it will select a random instance.
let hasInstanceAppended = resource.split(':').length > 1;
let instanceId = -1;
if (hasInstanceAppended) {
instanceId = resource.split(':')[1];
resource = resource.split(':')[0];
}
client.virtualMachineScaleSetVMs.list(resourceGroup, resource, (err, result) => {
if (err) {
console.error(err);
throw err;
}
if (!hasInstanceAppended) {
let indexToKill = Math.floor(Math.random() * result.length);
instanceId = result[indexToKill].instanceId;
}
if (operation === 'restart') {
client.virtualMachineScaleSetVMs.restart(resourceGroup, resource, instanceId, callback);
} else if (operation === 'start') {
client.virtualMachineScaleSetVMs.start(resourceGroup, resource, instanceId, callback);
} else if (operation === 'stop') {
client.virtualMachineScaleSetVMs.powerOff(resourceGroup, resource, instanceId, callback);
} else {
var err = new Error('Unsupported operation: ' + operation);
if (callback) {
callback(err);
} else {
throw err;
}
}
});
} else {
var err = new Error('One or more required parameters null. Operation: ' + operation);
if (callback) {
callback(err);
} else {
throw err;
}
}
}
function performVmOperation(operation, client, resourceGroup, resource, callback) {
if (client && resourceGroup && resource) {
if (operation === 'restart') {
client.virtualMachines.restart(resourceGroup, resource, callback);
} else if (operation === 'start') {
client.virtualMachines.start(resourceGroup, resource, callback);
} else if (operation === 'stop') {
client.virtualMachines.powerOff(resourceGroup, resource, callback);
} else {
var err = new Error('Unsupported operation: ' + operation);
if (callback) {
callback(err);
} else {
throw err;
}
}
} else {
var err = new Error('One or more required parameters null. Operation: ' + operation);
if (callback) {
callback(err);
} else {
throw err;
}
}
}
exports.vmOperations = {};
exports.vmOperations.restart_vm = function (client, resourceGroup, resource, callback) {
performVmOperation('restart', client, resourceGroup, resource, callback);
};
exports.vmOperations.start_vm = function (client, resourceGroup, resource, callback) {
performVmOperation('start', client, resourceGroup, resource, callback);
};
exports.vmOperations.stop_vm = function (client, resourceGroup, resource, callback) {
performVmOperation('stop', client, resourceGroup, resource, callback);
};
exports.vmInfoOperations = {};
exports.vmInfoOperations.list_vms = function (client, resourceGroup, callback) {
if (client && resourceGroup) {
client.virtualMachines.list(resourceGroup, callback)
} else {
callback(new Error('list_vms: One or more required inputs missing.'));
}
}
exports.vmssOperations = {};
exports.vmssOperations.restart_vm = function (client, resourceGroup, resource, callback) {
performVmssOperation('restart', client, resourceGroup, resource, callback);
};
exports.vmssOperations.start_vm = function (client, resourceGroup, resource, callback) {
performVmssOperation('start', client, resourceGroup, resource, callback);
};
exports.vmssOperations.stop_vm = function (client, resourceGroup, resource, callback) {
performVmssOperation('stop', client, resourceGroup, resource, callback);
};
exports.vmssInfoOperations = {};
exports.vmssInfoOperations.list_vmss = function (client, resourceGroup, callback) {
if (client && resourceGroup) {
client.virtualMachineScaleSets.list(resourceGroup, callback);
} else {
callback(new Error('list_vmss: One or more required inputs missing.'));
}
}