-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAC API.js
155 lines (116 loc) · 4.01 KB
/
OAC API.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var fs = require('fs');
var https = require('https');
var os = require('os');
var httpSignature = require('http-signature');
var jsSHA = require("jssha");
// TODO: update these values to your own
var tenancyId = "<YOUR TENANCY OCI ID>";
var authUserId = "<YOUR USER OCI ID>";
var keyFingerprint = "<YOUR FINGERPRONT>";
var privateKeyPath = "<PATH EXAMPLE>/oci_api_key.pem";
var OAChost = "analytics.eu-frankfurt-1.ocp.oraclecloud.com"; // My instance is in Frankfurt
if(privateKeyPath.indexOf("~/") === 0) {
privateKeyPath = privateKeyPath.replace("~", os.homedir());
}
var privateKey = fs.readFileSync(privateKeyPath, 'ascii');
// signing function as described at https://docs.cloud.oracle.com/Content/API/Concepts/signingrequests.htm
function sign(request, options) {
var apiKeyId = options.tenancyId + "/" + options.userId + "/" + options.keyFingerprint;
var headersToSign = [
"host",
"date",
"(request-target)"
];
var methodsThatRequireExtraHeaders = ["POST", "PUT"];
if(methodsThatRequireExtraHeaders.indexOf(request.method.toUpperCase()) !== -1) {
options.body = options.body || "";
var shaObj = new jsSHA("SHA-256", "TEXT");
shaObj.update(options.body);
request.setHeader("Content-Length", options.body.length);
request.setHeader("x-content-sha256", shaObj.getHash('B64'));
headersToSign = headersToSign.concat([
"content-type",
"content-length",
"x-content-sha256"
]);
}
httpSignature.sign(request, {
key: options.privateKey,
keyId: apiKeyId,
headers: headersToSign
});
var newAuthHeaderValue = request.getHeader("Authorization").replace("Signature ", "Signature version=\"1\",");
request.setHeader("Authorization", newAuthHeaderValue);
}
// generates a function to handle the https.request response object
function handleRequest(callback) {
return function(response) {
var responseBody = "";
response.on('data', function(chunk) {
responseBody += chunk;
});
response.on('end', function() {
callback(JSON.parse(responseBody));
});
};
}
function getOACInstance(userId, callback) {
const options = {
host: OAChost,
port: 443,
path: '/20190331/analyticsInstances/<OAC INSTANCE ID>',
method: 'GET'
};
var request = https.request(options,handleRequest(callback));
sign(request, {
privateKey: privateKey,
keyFingerprint: keyFingerprint,
tenancyId: tenancyId,
userId: authUserId
});
request.end();
};
function StopOACInstance(userId, callback) {
const options = {
host: OAChost,
port: 443,
path: '/20190331/analyticsInstances/<OAC INSTANCE ID>/actions/stop',
method: 'POST',
headers: {
"Content-Type": "application/json",
}
};
var request = https.request(options, handleRequest(callback));
sign(request, {
privateKey: privateKey,
keyFingerprint: keyFingerprint,
tenancyId: tenancyId,
userId: authUserId
});
console.log("headers ");
request.end();
};
function StartOACInstance(userId, callback) {
const options = {
host: OAChost,
port: 443,
path: '/20190331/analyticsInstances/<OAC INSTANCE ID>/start',
method: 'POST',
headers: {
"Content-Type": "application/json",
}
};
var request = https.request(options, handleRequest(callback));
sign(request, {
privateKey: privateKey,
keyFingerprint: keyFingerprint,
tenancyId: tenancyId,
userId: authUserId
});
console.log("headers ");
request.end();
};
// call funtions, for example StartOACInstance
StartOACInstance(authUserId, function(data) {
console.log(data);
});