From bff7fe704e7ea9de86f67e63fcac4aee81c5a272 Mon Sep 17 00:00:00 2001 From: Sara Vieira Date: Wed, 25 Oct 2017 18:09:02 +0100 Subject: [PATCH] feat(my-joy-beta): make queries for dns fixes #725 --- packages/cloudapi-gql/package.json | 10 +-- packages/cloudapi-gql/src/schema/resolvers.js | 28 +++++++-- .../cloudapi-gql/src/schema/schema.graphql | 2 + packages/my-joy-beta/package.json | 4 +- .../src/containers/instances/dns.js | 63 +++++++++++++++++++ .../src/containers/instances/index.js | 1 + .../my-joy-beta/src/graphql/get-instance.gql | 13 +--- packages/my-joy-beta/src/graphql/list-dns.gql | 7 +++ packages/my-joy-beta/src/router.js | 2 + packages/my-joy-beta/src/state/state.js | 1 + 10 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 packages/my-joy-beta/src/containers/instances/dns.js create mode 100644 packages/my-joy-beta/src/graphql/list-dns.gql diff --git a/packages/cloudapi-gql/package.json b/packages/cloudapi-gql/package.json index d49a92b6..ee5c34c5 100644 --- a/packages/cloudapi-gql/package.json +++ b/packages/cloudapi-gql/package.json @@ -6,8 +6,7 @@ "main": "src/schema/index.js", "scripts": { "lint": "eslint . --fix --ext .js --ext .graphql", - "fmt": - "prettier --write --single-quote src/**/*.js src/*.js src/**/*.graphql", + "fmt": "prettier --write --single-quote src/**/*.js src/*.js src/**/*.graphql", "test": "echo 0", "test-ci": "echo 0", "start": "PORT=4000 node src/server.js", @@ -36,19 +35,22 @@ "hapi": "^16.6.2", "hasha": "^3.0.0", "inert": "^4.2.1", + "lodash.get": "^4.4.2", "node-fetch": "^1.7.3", "smartdc-auth": "^2.5.6", "triton": "^5.4.0" }, "devDependencies": { - "graphql-faker": "^1.4.0", "eslint": "^4.8.0", "eslint-config-joyent-portal": "3.1.0", "eslint-plugin-graphql": "^1.4.0-1", + "graphql-faker": "^1.4.0", "nodemon": "^1.12.1", "prettier": "^1.7.4" }, "nodemonConfig": { - "ignore": ["doc/*"] + "ignore": [ + "doc/*" + ] } } diff --git a/packages/cloudapi-gql/src/schema/resolvers.js b/packages/cloudapi-gql/src/schema/resolvers.js index 3b00cc93..c4f1a72c 100644 --- a/packages/cloudapi-gql/src/schema/resolvers.js +++ b/packages/cloudapi-gql/src/schema/resolvers.js @@ -1,5 +1,7 @@ const { toKeyValue, fromKeyValue } = require('../api/key-value'); const api = require('../api'); +const forceArray = require('force-array'); +const get = require('lodash.get'); const resolvers = { Query: { @@ -57,16 +59,34 @@ const resolvers = { package: (root, { id, name }) => api.packages.get({ id, name }), - machines: (root, { id, brand, state, tags, ...rest }) => - id - ? api.machines.get({ id }).then(machine => [machine]) + machines: (root, { id, brand, state, tags, ...rest }, _, ctx) => + id ? api.machines.get({ id }).then(machine => [machine]) : api.machines.list( Object.assign(rest, { brand: brand ? brand.toLowerCase() : brand, state: state ? state.toLowerCase() : state, tags: fromKeyValue(tags) }) - ), + ) + .then(machines => { + const field = forceArray(ctx.fieldNodes) + .filter(({ name }) => name.value === 'machines') + .shift(); + + if (!field) { + return machines; + } + + const prop = get(field, 'selectionSet.selections', []) + .filter(({ name }) => name.value === 'dns_names') + .shift(); + + if (!prop) { + return machines; + } + + return machines.map(({ id }) => api.machines.get({ id })); + }), machine: (root, { id }) => api.machines.get({ id }), diff --git a/packages/cloudapi-gql/src/schema/schema.graphql b/packages/cloudapi-gql/src/schema/schema.graphql index 60e7b18d..d3e8c867 100644 --- a/packages/cloudapi-gql/src/schema/schema.graphql +++ b/packages/cloudapi-gql/src/schema/schema.graphql @@ -199,6 +199,8 @@ type Machine { compute_node: ID # The id or name of the package used to create this instance package: Package + # DNS names of the instance (if the instance is using CNS) + dns_names: [String] # The snapshots based on this instance snapshots( # Snapshot name diff --git a/packages/my-joy-beta/package.json b/packages/my-joy-beta/package.json index 66741682..f5b03194 100644 --- a/packages/my-joy-beta/package.json +++ b/packages/my-joy-beta/package.json @@ -6,8 +6,7 @@ "repository": "github:yldio/joyent-portal", "main": "build/", "scripts": { - "dev": - "REACT_APP_GQL_PORT=4000 PORT=3069 REACT_APP_GQL_PROTOCOL=http joyent-react-scripts start", + "dev": "REACT_APP_GQL_PORT=4000 PORT=3069 REACT_APP_GQL_PROTOCOL=http joyent-react-scripts start", "start": "PORT=3069 joyent-react-scripts start", "build": "NODE_ENV=production joyent-react-scripts build", "lint:css": "stylelint './src/**/*.js'", @@ -20,6 +19,7 @@ "dependencies": { "@manaflair/redux-batch": "^0.1.0", "apollo": "^0.2.2", + "eslint-plugin-markdown": "^1.0.0-beta.6", "joyent-ui-toolkit": "^2.0.0", "lodash.find": "^4.6.0", "lodash.get": "^4.4.2", diff --git a/packages/my-joy-beta/src/containers/instances/dns.js b/packages/my-joy-beta/src/containers/instances/dns.js new file mode 100644 index 00000000..8862769b --- /dev/null +++ b/packages/my-joy-beta/src/containers/instances/dns.js @@ -0,0 +1,63 @@ +import React from 'react'; +import ReactJson from 'react-json-view'; +import PropTypes from 'prop-types'; +import { compose, graphql } from 'react-apollo'; +import find from 'lodash.find'; +import get from 'lodash.get'; + +import { + ViewContainer, + Title, + StatusLoader, + Message, + MessageDescription, + MessageTitle +} from 'joyent-ui-toolkit'; + +import ListDNS from '@graphql/list-dns.gql'; + +const DNS = ({ instance, loading, error }) => { + const { name, dns_names } = instance || {}; + const _title = DNS; + const _loading = loading && !name && !dns_names && ; + const _summary = !_loading && instance && ; + + const _error = error && + !_loading && + !instance && ( + + Ooops! + + An error occurred while loading your instance DNS + + + ); + + return ( + + {_title} + {_loading} + {_error} + {_summary} + + ); +}; + +DNS.propTypes = { + loading: PropTypes.bool +}; + +export default compose( + graphql(ListDNS, { + options: ({ match }) => ({ + variables: { + name: get(match, 'params.instance') + } + }), + props: ({ data: { loading, error, variables, ...rest } }) => ({ + instance: find(get(rest, 'machines', []), ['name', variables.name]), + loading, + error + }) + }) +)(DNS); diff --git a/packages/my-joy-beta/src/containers/instances/index.js b/packages/my-joy-beta/src/containers/instances/index.js index ac2c961d..3886352c 100644 --- a/packages/my-joy-beta/src/containers/instances/index.js +++ b/packages/my-joy-beta/src/containers/instances/index.js @@ -4,6 +4,7 @@ export { default as Tags } from './tags'; export { default as Metadata } from './metadata'; export { default as Networks } from './networks'; export { default as Firewall } from './firewall'; +export { default as Dns } from './dns'; export { default as Snapshots } from './snapshots'; export { default as Resize } from './resize'; export { default as CreateSnapshot } from './create-snapshot'; diff --git a/packages/my-joy-beta/src/graphql/get-instance.gql b/packages/my-joy-beta/src/graphql/get-instance.gql index dc2fd961..6494ba9a 100644 --- a/packages/my-joy-beta/src/graphql/get-instance.gql +++ b/packages/my-joy-beta/src/graphql/get-instance.gql @@ -1,15 +1,6 @@ query instance($name: String) { machines(name: $name) { - id - name - state - memory - disk - created - updated - firewall_enabled - package { - name - } + id, + dns_names } } diff --git a/packages/my-joy-beta/src/graphql/list-dns.gql b/packages/my-joy-beta/src/graphql/list-dns.gql new file mode 100644 index 00000000..7f18139b --- /dev/null +++ b/packages/my-joy-beta/src/graphql/list-dns.gql @@ -0,0 +1,7 @@ +query instance($name: String!) { + machines(name: $name) { + id, + name, + dns_names + } +} diff --git a/packages/my-joy-beta/src/router.js b/packages/my-joy-beta/src/router.js index af237633..4702a128 100644 --- a/packages/my-joy-beta/src/router.js +++ b/packages/my-joy-beta/src/router.js @@ -14,6 +14,7 @@ import { Metadata as InstanceMetadata, Networks as InstanceNetworks, Firewall as InstanceFirewall, + Dns as InstanceDns, Snapshots as InstanceSnapshots, Resize as InstanceResize, CreateSnapshot as InstanceCreateSnapshot @@ -78,6 +79,7 @@ export default () => ( exact component={InstanceFirewall} /> +