From 83b11b198ba64b0c0f2d617b650fc05bbb23c8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81rgio=20Ramos?= Date: Wed, 27 Sep 2017 14:00:46 +0100 Subject: [PATCH] feat(cloudapi): add metadata list resolver --- packages/cloudapi-gql/package.json | 2 ++ packages/cloudapi-gql/src/api/machines.js | 36 ++++++++++++++----- .../cloudapi-gql/src/schema/types/machine.js | 23 ++++++++++-- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/packages/cloudapi-gql/package.json b/packages/cloudapi-gql/package.json index 1c19e831..d73f672c 100644 --- a/packages/cloudapi-gql/package.json +++ b/packages/cloudapi-gql/package.json @@ -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" }, diff --git a/packages/cloudapi-gql/src/api/machines.js b/packages/cloudapi-gql/src/api/machines.js index 60d99cff..74d5efdd 100644 --- a/packages/cloudapi-gql/src/api/machines.js +++ b/packages/cloudapi-gql/src/api/machines.js @@ -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); } }; diff --git a/packages/cloudapi-gql/src/schema/types/machine.js b/packages/cloudapi-gql/src/schema/types/machine.js index 4e9366a7..9a2e872a 100644 --- a/packages/cloudapi-gql/src/schema/types/machine.js +++ b/packages/cloudapi-gql/src/schema/types/machine.js @@ -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,