feat(cloudapi): add metadata list resolver

This commit is contained in:
Sérgio Ramos 2017-09-27 14:00:46 +01:00 committed by Sérgio Ramos
parent bdb364b63d
commit 83b11b198b
3 changed files with 50 additions and 11 deletions

View File

@ -13,6 +13,7 @@
"prepublish": "echo 0"
},
"dependencies": {
"apr-awaitify": "^1.0.4",
"bunyan": "^1.8.12",
"cors": "^2.8.4",
"dotenv": "^4.0.0",
@ -22,6 +23,7 @@
"graphql": "^0.11.2",
"graphql-tools": "^1.2.2",
"minimist": "^1.2.0",
"node-fetch": "^1.7.3",
"smartdc-auth": "^2.5.5",
"triton": "^5.3.1"
},

View File

@ -1,5 +1,19 @@
const awaitify = require('apr-awaitify');
const fetch = require('node-fetch');
const url = require('url');
const request = require('./request');
const {
_path,
_getAuthHeaders,
account,
url: host
} = request.client;
const client = request.client;
const getAuthHeaders = awaitify(_getAuthHeaders.bind(client));
const snapshots = {
list: ctx => {
return request('listMachineSnapshots', ctx);
@ -16,17 +30,21 @@ const snapshots = {
};
const metadata = {
list: ctx => {
return request('', ctx);
list: async ({ id }) => {
const pathname = _path.call(client, `/${account}/machines/${id}/metadata`);
const headers = await getAuthHeaders('GET', pathname);
const href = url.format({
protocol: 'https',
host: host.replace(/^https\:\/\//, ''),
pathname
});
return fetch(href, { method: 'GET', headers })
.then((response) => response.json());
},
get: ctx => {
return request('', ctx);
},
update: ctx => {
return request('', ctx);
},
destroy: ctx => {
return request('', ctx);
return request('getMachineMetadata', ctx);
}
};

View File

@ -1,6 +1,6 @@
const api = require('../../api');
const DynamicObjectType = require('./dynamic-object');
const SnapshotType = require('./snapshot');
const api = require('../../api');
const {
GraphQLBoolean,
@ -48,7 +48,26 @@ module.exports = new GraphQLObjectType({
},
metadata: {
type: DynamicObjectType,
description: 'Any additional metadata this instance has'
description: 'Any additional metadata this instance has',
args: {
name: {
type: GraphQLString,
description: 'Filter on the name of the metadata field'
}
},
resolve: (root, args) => {
const { metadata } = api.machines;
const { list, get } = metadata;
return args.name
? get({
id: root.id,
key: args.name
}).then(value => ({
[args.name]: value
}))
: list({ id: root.id });
}
},
tags: {
type: DynamicObjectType,