forked from googleapis/gcp-metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (39 loc) · 1.26 KB
/
index.js
File metadata and controls
49 lines (39 loc) · 1.26 KB
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
'use strict'
var extend = require('extend')
var request = require('retry-request')
var BASE_URL = 'http://metadata.google.internal/computeMetadata/v1'
var gcpMetadata = {
_buildMetadataAccessor: function (type) {
return function (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof options === 'string') {
options = {
property: options
}
}
var property = options.property ? '/' + options.property : ''
var reqOpts = extend(true, {
uri: BASE_URL + '/' + type + property,
headers: { 'Metadata-Flavor': 'Google' }
}, options)
delete reqOpts.property
return request(reqOpts, function (err, res, body) {
if (err) {
callback(err)
} else if (!res) {
callback(new Error('Invalid response from metadata service'))
} else if (res.statusCode !== 200) {
callback(new Error('Unsuccessful response status code'), res)
} else {
callback(null, res, body)
}
})
}
}
}
gcpMetadata.instance = gcpMetadata._buildMetadataAccessor('instance')
gcpMetadata.project = gcpMetadata._buildMetadataAccessor('project')
module.exports = gcpMetadata