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