Skip to content

Commit

Permalink
Update appengine/metadata/standard sample (#624)
Browse files Browse the repository at this point in the history
* Update appengine/metadata/standard sample

* Fix the test
  • Loading branch information
chenyumic authored May 23, 2018
1 parent 5091a97 commit bce81f4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions appengine/metadata/standard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"lint": "samples lint",
"pretest": "npm run lint",
"system-test": "samples test app",
"system-test": "samples test app -- ./server.js",
"test": "npm run system-test"
},
"dependencies": {
Expand All @@ -28,7 +28,7 @@
"cloud-repo-tools": {
"test": {
"app": {
"msg": "External IP:",
"msg": "Project ID:",
"args": [
"server.js"
]
Expand Down
19 changes: 9 additions & 10 deletions appengine/metadata/standard/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,30 @@ const request = require('got');
const app = express();
app.enable('trust proxy');

const METADATA_NETWORK_INTERFACE_URL = 'http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip';
const METADATA_PROJECT_ID_URL = 'http://metadata.google.internal/computeMetadata/v1/project/project-id';

function getExternalIp () {
function getProjectId () {
const options = {
headers: {
'Metadata-Flavor': 'Google'
},
json: true
}
};

return request(METADATA_NETWORK_INTERFACE_URL, options)
return request(METADATA_PROJECT_ID_URL, options)
.then((response) => response.body)
.catch((err) => {
if (err && err.statusCode !== 200) {
console.log('Error while talking to metadata server, assuming localhost');
return 'localhost';
console.log('Error while talking to metadata server.');
return 'Unknown_Project_ID';
}
return Promise.reject(err);
});
}

app.get('/', (req, res, next) => {
getExternalIp()
.then((externalIp) => {
res.status(200).send(`External IP: ${externalIp}`).end();
getProjectId()
.then((projectId) => {
res.status(200).send(`Project ID: ${projectId}`).end();
})
.catch(next);
});
Expand Down
3 changes: 2 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ deployment:
- node scripts/build "appengine/errorreporting"
- node scripts/build "appengine/hello-world"
- node scripts/build "appengine/mailjet"
- node scripts/build "appengine/metadata"
- node scripts/build "appengine/metadata/flexible"
- node scripts/build "appengine/metadata/standard"
- node scripts/build "appengine/static-files"
- GCLOUD_STORAGE_BUCKET=docs-samples-gae-test-$(uuid); node scripts/build "appengine/storage"
- node scripts/build "auth"
Expand Down

0 comments on commit bce81f4

Please sign in to comment.