1 | 'use strict';
|
2 |
|
3 | const ForceArray = require('force-array');
|
4 | const Hasha = require('hasha');
|
5 | const FWRule = require('fwrule');
|
6 | const Uniq = require('lodash.uniq');
|
7 |
|
8 | const Handlers = require('./handlers');
|
9 | const Utils = require('./utils');
|
10 |
|
11 |
|
12 | module.exports = {
|
13 | User: {
|
14 | keys: (root, args, request) => {
|
15 | return Handlers.keys(request.plugins.cloudapi.fetch, args);
|
16 | }
|
17 | },
|
18 | Machine: {
|
19 | brand: ({ brand }) => { return (brand ? brand.toUpperCase() : brand); },
|
20 |
|
21 | state: ({ state }) => { return (state ? state.toUpperCase() : state); },
|
22 |
|
23 | image: (root, args, request) => {
|
24 | return Handlers.image(request.plugins.cloudapi.fetch, { id: root.image });
|
25 | },
|
26 |
|
27 |
|
28 | primary_ip: ({ primaryIp }) => { return primaryIp; },
|
29 |
|
30 | tags: (root, { name }, request) => {
|
31 | const { id: machine } = root;
|
32 |
|
33 | return Handlers.tags(request.plugins.cloudapi.fetch, { machine, name });
|
34 | },
|
35 |
|
36 | metadata: (root, { name }, request) => {
|
37 | const { id: machine } = root;
|
38 | return Handlers.metadata(request.plugins.cloudapi.fetch, { machine, name });
|
39 | },
|
40 |
|
41 | networks: async (root, args, request) => {
|
42 | const networks = [];
|
43 | for (const id of root.networks) {
|
44 | const network = await Handlers.network(request.plugins.cloudapi.fetch, { id });
|
45 | networks.push(network);
|
46 | }
|
47 |
|
48 | return networks;
|
49 | },
|
50 |
|
51 |
|
52 | package: (root, args, request) => {
|
53 | return Handlers.package(request.plugins.cloudapi.fetch, { name: root.package });
|
54 | },
|
55 |
|
56 | snapshots: (root, { name }, request) => {
|
57 | const { id: machine } = root;
|
58 | return Handlers.snapshots(request.plugins.cloudapi.fetch, { machine, name });
|
59 | },
|
60 |
|
61 |
|
62 | firewall_rules: (root, { id }, request) => {
|
63 | const { id: machine } = root;
|
64 | return Handlers.firewall_rules(request.plugins.cloudapi.fetch, { machine, id });
|
65 | },
|
66 |
|
67 | actions: (root, args, request) => {
|
68 | const { id: machine } = root;
|
69 | return Handlers.actions(request.plugins.cloudapi.fetch, { machine });
|
70 | }
|
71 | },
|
72 | Image: {
|
73 | os: ({ os }) => { return (os ? os.toUpperCase() : os); },
|
74 |
|
75 | state: ({ state }) => { return (state ? state.toUpperCase() : state); },
|
76 |
|
77 | type: ({ type }) => { return (type ? type.replace('-', '_').toUpperCase() : type); }
|
78 | },
|
79 | Action: {
|
80 | name: ({ action }) => { return action; },
|
81 |
|
82 | parameters: ({ parameters }) => { return Utils.toNameValues(parameters); }
|
83 | },
|
84 | Caller: {
|
85 | type: ({ type }) => { return (type ? type.toUpperCase() : type); },
|
86 |
|
87 |
|
88 | key_id: ({ keyId }) => { return keyId; }
|
89 | },
|
90 | FirewallRule: {
|
91 | machines: ({ id }, args, request) => {
|
92 | return request.plugins.cloudapi.fetch(`/fwrules/${id}/machines`);
|
93 | },
|
94 | rule_str: ({ rule }) => { return rule; },
|
95 | rule_obj: ({ rule }) => {
|
96 | const parsed = FWRule.parse(rule);
|
97 | const _from = ForceArray(parsed.from);
|
98 | const _to = ForceArray(parsed.to);
|
99 |
|
100 | const getTags = (partial) => {
|
101 | return partial
|
102 | .map((partial) => { return ForceArray(partial); })
|
103 | .filter((partial) => { return partial[0] === 'tag'; })
|
104 | .map((partial) => { return [partial[0], ForceArray(partial[1])]; })
|
105 | .filter((partial) => { return partial[1]; })
|
106 | .map((partial) => { return partial[1][0]; })
|
107 | .filter(Boolean);
|
108 | };
|
109 |
|
110 | const isWildcard = (
|
111 | _from.some((frm) => { return frm[0] === 'wildcard'; }) &&
|
112 | _to.some((to) => { return to[0] === 'wildcard'; })
|
113 | );
|
114 |
|
115 | const tags = Uniq(getTags(_from).concat(getTags(_to)));
|
116 |
|
117 | return {
|
118 | ...parsed,
|
119 | isWildcard,
|
120 | tags
|
121 | };
|
122 | }
|
123 | },
|
124 | Snapshot: {
|
125 | state: ({ state }) => { return (state ? state.toUpperCase() : state); },
|
126 | id: ({ name }) => { return Hasha(name); }
|
127 | },
|
128 | ImageError: {
|
129 | code: ({ code }) => { return (code ? code.toUpperCase() : code); }
|
130 | },
|
131 | ImageFile: {
|
132 | compression: ({ compression }) => {
|
133 | return (compression ? compression.toUpperCase() : compression);
|
134 | }
|
135 | },
|
136 | Network: {
|
137 | machines: async ({ id, fabric }, args, request) => {
|
138 | if (!fabric) {
|
139 | return [];
|
140 | }
|
141 |
|
142 | const machines = await Handlers.machines(request.plugins.cloudapi.fetch, {});
|
143 |
|
144 | return ForceArray(machines).filter(({ networks }) => {
|
145 | return ForceArray(networks).some((network) => { return network === id; });
|
146 | });
|
147 | }
|
148 | }
|
149 | };
|