cloudapi-gql
Advanced tools
Comparing version 4.4.0 to 4.5.0
@@ -5,3 +5,3 @@ 'use strict'; | ||
// SDC_URL, SDC_KEY_ID, SDC_KEY_PATH | ||
require('./.env.js'); | ||
require('./env.js'); | ||
@@ -89,48 +89,48 @@ const { renderVoyagerPage } = require('graphql-voyager/middleware'); | ||
server.route([ | ||
{ | ||
method: 'GET', | ||
path: '/doc/{param*}', | ||
config: { | ||
handler: { | ||
directory: { | ||
path: './doc', | ||
redirectToSlash: true, | ||
index: true | ||
} | ||
server.route([ | ||
{ | ||
method: 'GET', | ||
path: '/doc/{param*}', | ||
config: { | ||
handler: { | ||
directory: { | ||
path: './doc', | ||
redirectToSlash: true, | ||
index: true | ||
} | ||
} | ||
}, | ||
{ | ||
method: 'GET', | ||
path: '/voyager', | ||
handler: (request, h) => { | ||
const rendered = renderVoyagerPage({ path: '/voyager', endpointUrl: '/graphql'}); | ||
return h.response(rendered).type('text/html'); | ||
} | ||
}, | ||
{ | ||
method: 'GET', | ||
path: '/playground', | ||
handler: async (request, h) => { | ||
const rendered = await renderPlaygroundPage({ | ||
path: '/playground', | ||
endpoint: '/graphql', | ||
version: '1.3.20', | ||
env: 'development', | ||
htmlTitle: 'CloudAPI GQL' | ||
}); | ||
} | ||
}, | ||
{ | ||
method: 'GET', | ||
path: '/voyager', | ||
handler: (request, h) => { | ||
const rendered = renderVoyagerPage({ path: '/voyager', endpointUrl: '/graphql'}); | ||
return h.response(rendered).type('text/html'); | ||
} | ||
}, | ||
{ | ||
method: 'GET', | ||
path: '/playground', | ||
handler: async (request, h) => { | ||
const rendered = await renderPlaygroundPage({ | ||
path: '/playground', | ||
endpoint: '/graphql', | ||
version: '1.3.20', | ||
env: 'development', | ||
htmlTitle: 'CloudAPI GQL' | ||
}); | ||
return h.response(rendered).type('text/html'); | ||
} | ||
return h.response(rendered).type('text/html'); | ||
} | ||
]); | ||
} | ||
]); | ||
server.auth.default('sso'); | ||
server.auth.default('sso'); | ||
await server.start(); | ||
// eslint-disable-next-line no-console | ||
console.log(`server started at http://0.0.0.0:${server.info.port}`); | ||
await server.start(); | ||
// eslint-disable-next-line no-console | ||
console.log(`server started at http://0.0.0.0:${server.info.port}`); | ||
}; | ||
start(); |
@@ -18,3 +18,2 @@ 'use strict'; | ||
const cache = {}; | ||
@@ -26,2 +25,3 @@ class CloudApi { | ||
this._token = token; | ||
this._cache = {}; | ||
this._wreck = Wreck.defaults({ | ||
@@ -53,3 +53,3 @@ headers: this._authHeaders(), | ||
_getCache(method = '', path, options) { | ||
_getCache (method = '', path, options) { | ||
if (method.toLowerCase() !== 'get') { | ||
@@ -60,3 +60,3 @@ return; | ||
const ref = Hasha(JSON.stringify({ method, path, options })); | ||
const { val, when } = cache[ref] || {}; | ||
const { val, when } = this._cache[ref] || {}; | ||
const now = new Date().getTime(); | ||
@@ -70,3 +70,3 @@ | ||
if (now - when > 9000) { | ||
delete cache[ref]; | ||
delete this._cache[ref]; | ||
return val; | ||
@@ -78,3 +78,3 @@ } | ||
_setCache(method = '', path, options, payload) { | ||
_setCache (method = '', path, options, payload) { | ||
if (method.toLowerCase() !== 'get') { | ||
@@ -86,3 +86,3 @@ return; | ||
cache[ref] = { | ||
this._cache[ref] = { | ||
when: new Date().getTime(), | ||
@@ -93,3 +93,3 @@ val: payload | ||
async fetch(path = '/', options = {}, request) { | ||
async fetch (path = '/', options = {}, request) { | ||
const wreckOptions = { | ||
@@ -96,0 +96,0 @@ json: true, |
'use strict'; | ||
const Fs = require('fs'); | ||
const Boom = require('boom'); | ||
const Reach = require('reach'); | ||
@@ -17,3 +16,3 @@ const Path = require('path'); | ||
const setupCloudApi = async (request, h) => { | ||
const setupCloudApi = (request, h) => { | ||
if (request.route.settings.auth === false) { | ||
@@ -20,0 +19,0 @@ return h.continue; |
@@ -10,18 +10,23 @@ 'use strict'; | ||
const Sentiment = require('sentiment'); | ||
const isUndefined = require('lodash.isundefined'); | ||
const Uniq = require('lodash.uniq'); | ||
const CloudApi = require('./cloudapi'); | ||
const Products = require('./products'); | ||
const Adjectives = require('./adjectives.json'); | ||
const Names = require('./names.json'); | ||
const internals = {}; | ||
module.exports = internals.resolvers = { | ||
Query: { | ||
rndName: async (root, args, request) => { | ||
const name = Names[RandomInt(0, Names.length -1)]; | ||
const adjective = Adjectives[RandomInt(0, Adjectives.length -1)]; | ||
const name = Names[RandomInt(0, Names.length - 1)]; | ||
const adjective = Adjectives[RandomInt(0, Adjectives.length - 1)]; | ||
const str = `${adjective}-${name}`; | ||
if (Sentiment(str.split(/\-/).join(' ')).score < 0) { | ||
if (Sentiment(str.split(/-/).join(' ')).score < 0) { | ||
return internals.resolvers.Query.rndName(root, args, request); | ||
@@ -61,3 +66,3 @@ } | ||
return CloudApi(`/users`, {}, request); | ||
return CloudApi('/users', {}, request); | ||
}, | ||
@@ -75,3 +80,3 @@ | ||
return CloudApi(`/roles`, {}, request); | ||
return CloudApi('/roles', {}, request); | ||
}, | ||
@@ -89,3 +94,3 @@ | ||
return CloudApi(`/policies`, {}, request); | ||
return CloudApi('/policies', {}, request); | ||
}, | ||
@@ -108,2 +113,3 @@ | ||
name, | ||
place: internals.dataCenterPlace(name), | ||
url: datacenters[name] | ||
@@ -114,2 +120,6 @@ }; | ||
products: (root, args = {}, request) => { | ||
return Products(request.app.cloudapiUrl); | ||
}, | ||
services: async (root, args, request) => { | ||
@@ -206,3 +216,3 @@ const services = await CloudApi('/services', {}, request); | ||
return Map(machinesList, ({ id }) => internals.resolvers.Query.machine(root, { id }, request)); | ||
return Map(machinesList, ({ id }) => { return internals.resolvers.Query.machine(root, { id }, request); }); | ||
}, | ||
@@ -283,3 +293,3 @@ | ||
firewall_rules_create_machine: async (root, args, request) => { | ||
const tags = internals.fromNameValues(args.tags); | ||
const machineTags = internals.fromNameValues(args.tags); | ||
const res = await internals.resolvers.Query.firewall_rules(root, {}, request); | ||
@@ -290,48 +300,13 @@ | ||
rule_str: rule, | ||
rule_obj: FWRule.parse(rule) | ||
rule_obj: internals.resolvers.FirewallRule.rule_obj({ | ||
...rest, | ||
rule | ||
}, {}, request) | ||
}); | ||
}); | ||
const defaultRules = rules.filter(({ enabled, rule_obj = {} }) => { | ||
return ( | ||
ForceArray(rule_obj.from).some(frm => frm[0] === 'wildcard') && | ||
ForceArray(rule_obj.to).some(to => to[0] === 'wildcard') | ||
); | ||
return rules.filter(({ rule_obj }) => { | ||
const { isWildcard, tags } = rule_obj; | ||
return isWildcard || tags.some((tag) => { return !isUndefined(machineTags[tag]); }); | ||
}); | ||
const filterTagRulePartial = partial => { | ||
return partial | ||
.map(partial => ForceArray(partial)) | ||
.filter(partial => partial[0] === 'tag') | ||
.filter(partial => { | ||
const tag = ForceArray(partial[1]); | ||
const foundTagValue = tags[tag[0]]; | ||
if (!foundTagValue) { | ||
return false; | ||
} | ||
if (tag.length === 1) { | ||
return true; | ||
} | ||
return foundTagValue === tag[1]; | ||
}); | ||
}; | ||
const tagRules = rules | ||
.filter(({ enabled, rule_obj = {} }) => { | ||
const _from = ForceArray(rule_obj.from); | ||
const _to = ForceArray(rule_obj.to); | ||
const fromHas = filterTagRulePartial(_from).length; | ||
const toHas = filterTagRulePartial(_to).length; | ||
return Boolean(fromHas) || Boolean(toHas); | ||
}) | ||
.map(rule => { | ||
return Object.assign(rule, { tag: true }); | ||
}); | ||
return defaultRules.concat(tagRules); | ||
}, | ||
@@ -390,3 +365,2 @@ | ||
const path = user ? `/users/${user}/keys` : '/keys'; | ||
const payload = { name, key }; | ||
@@ -403,3 +377,2 @@ return CloudApi(path, { method: 'post', payload: { name, key } }, request); | ||
stopMachine: async (root, args, request) => { | ||
@@ -472,3 +445,3 @@ const { id } = args; | ||
createMachineSnapshot: async (root, { id, name }, request) => { | ||
createMachineSnapshot: (root, { id, name }, request) => { | ||
const payload = { | ||
@@ -491,3 +464,3 @@ name | ||
createMachine: async (root, { name, image, networks, affinity, metadata, tags, firewall_enabled, ...args }, request) => { | ||
createMachine: async (root, { name, image, networks, affinity = [], metadata, tags, firewall_enabled, ...args }, request) => { | ||
const AffinityRuleTypes = { | ||
@@ -505,3 +478,3 @@ MUST_EQUAL: '==', | ||
networks, | ||
affinity: affinity.map(({ key, value, type }) => `${key}${AffinityRuleTypes[type]}${value}`), | ||
affinity: affinity.map(({ key, value, type }) => { return `${key}${AffinityRuleTypes[type]}${value}`; }), | ||
...internals.fromNameValues(tags, 'tag.'), | ||
@@ -512,3 +485,3 @@ ...internals.fromNameValues(metadata, 'tag.'), | ||
const { id } = await CloudApi(`/machines`, { method: 'post', payload }, request); | ||
const { id } = await CloudApi('/machines', { method: 'post', payload }, request); | ||
return internals.resolvers.Query.machine(root, { id }, request); | ||
@@ -518,3 +491,3 @@ }, | ||
updateMachineMetadata: async (root, { id, metadata }, request) => { | ||
const payload = internals.fromNameValues(metadata) | ||
const payload = internals.fromNameValues(metadata); | ||
@@ -531,3 +504,3 @@ await CloudApi(`/machines/${id}/metadata`, { method: 'post', payload }, request); | ||
addMachineTags: async (root, { id, tags }, request) => { | ||
const payload = internals.fromNameValues(tags) | ||
const payload = internals.fromNameValues(tags); | ||
@@ -539,3 +512,3 @@ await CloudApi(`/machines/${id}/tags`, { method: 'post', payload }, request); | ||
replaceMachineTags: async (root, { id, tags }, request) => { | ||
const payload = internals.fromNameValues(tags) | ||
const payload = internals.fromNameValues(tags); | ||
@@ -546,4 +519,4 @@ await CloudApi(`/machines/${id}/tags`, { method: 'put', payload }, request); | ||
deleteMachineTag: async (root, { id, name: tag }, request) => { | ||
await CloudApi(`/machines/${id}/tags/${encodeURIComponent(name)}`, { method: 'delete' }, request); | ||
deleteMachineTag: async (root, { id, tag }, request) => { | ||
await CloudApi(`/machines/${id}/tags/${encodeURIComponent(tag)}`, { method: 'delete' }, request); | ||
return internals.resolvers.Query.machine(root, { id }, request); | ||
@@ -642,8 +615,35 @@ }, | ||
}, | ||
rule_str: ({ rule }, args, request) => rule, | ||
rule_obj: ({ rule }, args, request) => FWRule.parse(rule) | ||
rule_str: ({ rule }, args, request) => { return rule; }, | ||
rule_obj: ({ rule }, args, request) => { | ||
const parsed = FWRule.parse(rule); | ||
const _from = ForceArray(parsed.from); | ||
const _to = ForceArray(parsed.to); | ||
const getTags = (partial) => { | ||
return partial | ||
.map((partial) => { return ForceArray(partial); }) | ||
.filter((partial) => { return partial[0] === 'tag'; }) | ||
.map((partial) => { return [partial[0], ForceArray(partial[1])]; }) | ||
.filter((partial) => { return partial[1]; }) | ||
.map((partial) => { return partial[1][0]; }) | ||
.filter(Boolean); | ||
}; | ||
const isWildcard = ( | ||
_from.some((frm) => { return frm[0] === 'wildcard'; }) && | ||
_to.some((to) => { return to[0] === 'wildcard'; }) | ||
); | ||
const tags = Uniq(getTags(_from).concat(getTags(_to))); | ||
return { | ||
...parsed, | ||
isWildcard, | ||
tags | ||
}; | ||
} | ||
}, | ||
Snapshot: { | ||
state: ({ state }) => { return (state ? state.toUpperCase() : state); }, | ||
id: ({ name }) => { return Hasha(name) } | ||
id: ({ name }) => { return Hasha(name); } | ||
}, | ||
@@ -667,3 +667,3 @@ ImageError: { | ||
return ForceArray(machines).filter(({ networks }) => { | ||
return ForceArray(networks).some(network => network === id); | ||
return ForceArray(networks).some((network) => { return network === id; }); | ||
}); | ||
@@ -696,1 +696,14 @@ } | ||
}; | ||
internals.dataCenterPlace = (name) => { | ||
const nameParts = name.split('-'); | ||
const firstPart = nameParts && nameParts[0]; | ||
const placeMap = { | ||
ap: 'Asia', | ||
us: 'Americas', | ||
eu: 'Europe' | ||
}; | ||
return placeMap[firstPart] || 'Unknown'; | ||
}; |
{ | ||
"name": "cloudapi-gql", | ||
"version": "4.4.0", | ||
"version": "4.5.0", | ||
"license": "MPL-2.0", | ||
@@ -25,2 +25,4 @@ "repository": "github:yldio/joyent-portal", | ||
"hasha": "^3.0.0", | ||
"lodash.isundefined": "^3.0.1", | ||
"lodash.uniq": "^4.5.0", | ||
"random-int": "^1.0.0", | ||
@@ -27,0 +29,0 @@ "reach": "^1.0.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
220144
16
2404
15
8
+ Addedlodash.isundefined@^3.0.1
+ Addedlodash.uniq@^4.5.0
+ Addedlodash.isundefined@3.0.1(transitive)
+ Addedlodash.uniq@4.5.0(transitive)