Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: use grapql for channels/subscriptions #304

Merged
merged 24 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e2c2aec
update subscriptions meteor methods use graphql
dalehille May 1, 2020
eef592d
add a token to razeedash-api graphql calls
dalehille May 4, 2020
1997871
updating channels methods to use graphql
dalehille May 5, 2020
5424c81
store the token in mongo
dalehille May 12, 2020
8f188be
use existing apiKey instead of jwt
dalehille May 12, 2020
efc0caa
create a token if it doesn't exist
dalehille May 12, 2020
7887be2
sanitize RAZEEDASH_API_URL. remove pubsub code
dalehille May 15, 2020
5fb0ef7
remove REDIS_CONN_JSON
dalehille May 18, 2020
ae35d29
better error handling from meteror methods. fix toastr popup css
dalehille May 18, 2020
85cbb5f
use x-api-key to match razeedash-api
dalehille May 18, 2020
942d4a6
debugging meteor mongo
dalehille May 19, 2020
3dd3bcf
WIP debugging meteor methods
dalehille May 20, 2020
42187a3
WIP meteor method debugging
dalehille May 20, 2020
0e11f50
fix polling interval
dalehille May 20, 2020
ab6d217
remove overflow
dalehille May 20, 2020
6b9430c
set razeedash_api_url as a required env variable
dalehille May 22, 2020
bc92adc
package updates
dalehille Jun 2, 2020
56ff534
markdownlint revert
dalehille Jun 2, 2020
896e5d3
Merge branch 'master' of https://github.com/razee-io/razeedash into g…
dalehille Jun 4, 2020
0b91967
add nvmrc to .gitignore
dalehille Jun 4, 2020
e96b69b
updated package-lock after merge with master
dalehille Jun 4, 2020
8ff228c
updating ClusterSubscription help modals with graphql example
dalehille Jun 4, 2020
612b9f3
bump version
dalehille Jun 4, 2020
96ca040
add channel uuid to the channels table
dalehille Jun 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ npm-debug.log
.vscode/settings.json
dev-envvars.sh
.vscode
.nvmrc
86 changes: 55 additions & 31 deletions imports/api/deployables/channels/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

import { check, Match } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import { Channels } from './channels';
import { DeployableVersions } from './deployableVersions';
import { requireOrgAccess } from '/imports/api/org/utils.js';
import { updateDeployablesCountStat } from '../../stat/utils.js';
import { logUserAction } from '../../userLog/utils.js';

import { v4 as uuid } from 'uuid';
const { getQueryClient } = require('/imports/api/lib/graphql.js');
const gql = require('graphql-tag');

// https://docs.meteor.com/api/check.html
const NonEmptyString = Match.Where((x) => {
Expand All @@ -31,57 +29,83 @@ const NonEmptyString = Match.Where((x) => {
});

Meteor.methods({
updateChannel(orgId, appId, channelName){
async updateChannel(orgId, appId, channelName){
requireOrgAccess(orgId);
check( orgId, String );
check( appId, String );
check( channelName, String );

logUserAction(Meteor.userId(), 'updateChannel', `Update channel ${orgId}:${appId}:${channelName}`);

Channels.update(
{
let client = await getQueryClient();
return await client.mutate({
mutation: gql`
mutation EditChannel($org_id: String!, $uuid: String!, $name: String!) {
editChannel(org_id: $org_id, uuid: $uuid, name: $name) {
uuid
name
success
}
}
`,
variables: {
'org_id': orgId,
uuid: appId
},
{
$set:
{
'name': channelName,
'updated': new Date()
}
});
return true;
'uuid': appId,
'name': channelName
}
}).catch( (err) => {
throw new Meteor.Error(err.message);
rmgraham marked this conversation as resolved.
Show resolved Hide resolved
});
},
addChannel(orgId, channelName ){
async addChannel(orgId, channelName ){
requireOrgAccess(orgId);
check( orgId, String );
check( channelName, NonEmptyString);

logUserAction(Meteor.userId(), 'addChannel', `Add channel ${orgId}:${channelName}`);

Channels.insert({
'org_id': orgId,
'name': channelName,
'uuid': uuid(),
'created': new Date(),
'versions': [],
let client = await getQueryClient();
return client.mutate({
mutation: gql`
mutation AddChannel($org_id: String!, $name: String!) {
addChannel(org_id: $org_id, name: $name) {
uuid
}
}
`,
variables: {
'org_id': orgId,
'name': channelName
}
}).catch( (err) => {
throw new Meteor.Error(err.message);
});
updateDeployablesCountStat(orgId);
return true;
},
removeChannel(orgId, channelName, resourceId ){
async removeChannel(orgId, channelName, resourceId ){
requireOrgAccess(orgId);
check( orgId, String );
check( channelName, String );
check( resourceId, String );

logUserAction(Meteor.userId(), 'removeChannel', `Remove channel ${orgId}:${channelName}:${resourceId}`);

Channels.remove({ 'org_id': orgId, 'name': channelName });
DeployableVersions.remove({ 'org_id': orgId, 'channel_id': resourceId});
updateDeployablesCountStat(orgId);
return true;
let client = await getQueryClient();
return client.mutate({
mutation: gql`
mutation RemoveChannel($org_id: String!, $uuid: String!) {
removeChannel(org_id: $org_id, uuid: $uuid) {
uuid
success
}
}
`,
variables: {
'org_id': orgId,
'uuid': resourceId
}
}).catch( (err) => {
throw new Meteor.Error(err.message);
});
},

});
4 changes: 2 additions & 2 deletions imports/api/deployables/channels/server/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import { DeployableVersions } from '../deployableVersions';
Meteor.publish('channels', function(orgId) {
check( orgId, String );
requireOrgAccess(orgId);
return Channels.find({ org_id: orgId });
return Channels.find({ org_id: orgId }, {pollingIntervalMs: 1000});
});

Meteor.publish('deployableVersions', function(orgId) {
check( orgId, String );
requireOrgAccess(orgId);
return DeployableVersions.find({ org_id: orgId });
return DeployableVersions.find({ org_id: orgId }, {pollingIntervalMs: 1000});
});
115 changes: 57 additions & 58 deletions imports/api/deployables/subscriptions/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

import { check, Match } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import { Subscriptions } from './subscriptions.js';
import { requireOrgAccess } from '/imports/api/org/utils.js';
import { v4 as uuid } from 'uuid';
import { pub } from '/imports/api/lib/pubsub';
import { logUserAction } from '../../userLog/utils.js';
const { getQueryClient } = require('/imports/api/lib/graphql.js');
const gql = require('graphql-tag');

// https://docs.meteor.com/api/check.html
const NonEmptyString = Match.Where((x) => {
Expand All @@ -42,33 +41,26 @@ Meteor.methods({

logUserAction(Meteor.userId(), 'updateSubscription', `Update subscription ${orgId}:${groupId}:${groupName}:${tags}:${resourceId}:${resourceName}:${version}:${versionName}`);

Subscriptions.update(
{
let client = await getQueryClient();
return client.mutate({
mutation: gql`
mutation EditSubscription($org_id: String!, $uuid: String!, $name: String!, $tags: [String!]!, $channel_uuid: String!, $version_uuid: String!) {
editSubscription(org_id: $org_id, uuid: $uuid, name: $name, tags: $tags, channel_uuid: $channel_uuid, version_uuid: $version_uuid) {
uuid
}
}
`,
variables: {
'org_id': orgId,
uuid: groupId
},
{
$set:
{
'name': groupName,
'tags': tags,
'channel_uuid': resourceId,
'channel': resourceName,
'version': versionName,
'version_uuid': version,
'updated': new Date()
}
'uuid': groupId,
'name': groupName,
'tags': tags,
'channel_uuid': resourceId,
'version_uuid': version,
}
);

var msg = {
orgId,
groupName,
subscription: await Subscriptions.findOne({ 'org_id': orgId, uuid: groupId }),
};
pub('updateSubscription', msg);

return true;
}).catch( (err) => {
throw new Meteor.Error(err.message);
});
},
async addSubscription(orgId, groupName, tags=[], resourceId='', resourceName='', version='', versionName='' ){
requireOrgAccess(orgId);
Expand All @@ -82,43 +74,50 @@ Meteor.methods({

logUserAction(Meteor.userId(), 'addSubscription', `Add subscription ${orgId}:${groupName}:${tags}:${resourceId}:${resourceName}:${version}:${versionName}`);

Subscriptions.insert({
'org_id': orgId,
'name': groupName,
'uuid': uuid(),
'tags': tags,
'channel_uuid': resourceId,
'channel': resourceName,
'version': versionName,
'version_uuid': version,
'owner': Meteor.userId(),
'created': new Date()
let client = await getQueryClient();
return client.mutate({
mutation: gql`
mutation AddSubscription($org_id: String!, $name: String!, $tags: [String!]!, $channel_uuid: String!, $version_uuid: String!) {
addSubscription(org_id: $org_id, name: $name, tags: $tags, channel_uuid: $channel_uuid, version_uuid: $version_uuid) {
uuid
}
}
`,
variables: {
'org_id': orgId,
'name': groupName,
'tags': tags,
'channel_uuid': resourceId,
'version_uuid': version,
}
}).catch( (err) => {
throw new Meteor.Error(err.message);
});

var msg = {
orgId,
groupName,
};
pub('addSubscription', msg);

return true;
},
async removeSubscription(orgId, groupName){
async removeSubscription(orgId, groupName, uuid){
requireOrgAccess(orgId);
check( orgId, String );
check( groupName, String );
check( uuid, String );

logUserAction(Meteor.userId(), 'removeSubscription', `Remove subscription ${orgId}:${groupName}`);
logUserAction(Meteor.userId(), 'removeSubscription', `Remove subscription ${orgId}:${groupName}:${uuid}`);

Subscriptions.remove({ 'org_id': orgId, 'name': groupName });

var msg = {
orgId,
groupName,
};
pub('removeSubscription', msg);

return true;
let client = await getQueryClient();
return client.mutate({
mutation: gql`
mutation RemoveSubscription($org_id: String!, $uuid: String!) {
removeSubscription(org_id: $org_id, uuid: $uuid) {
uuid
}
}
`,
variables: {
'org_id': orgId,
'uuid': uuid
}
}).catch( (err) => {
throw new Meteor.Error(err.message);
});
},

});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { requireOrgAccess } from '/imports/api/org/utils.js';
Meteor.publish('subscriptions', function(orgId) {
check( orgId, String );
requireOrgAccess(orgId);
return Subscriptions.find({ org_id: orgId });
return Subscriptions.find({ org_id: orgId }, {pollingIntervalMs: 1000});
});

Meteor.publish('users.byIds', function(userIds) {
Expand Down
65 changes: 65 additions & 0 deletions imports/api/lib/graphql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2019 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Meteor } from 'meteor/meteor';

const { ApolloLink } = require('apollo-link');
const ApolloClient = require('apollo-boost').ApolloClient;
const fetch = require('cross-fetch/polyfill').fetch;
const createHttpLink = require('apollo-link-http').createHttpLink;
const { onError } = require('apollo-link-error');
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache;

const getQueryClient = async () => {

const token = Meteor.user().apiKey ? Meteor.user().apiKey : Meteor.call('generateApikey');
dalehille marked this conversation as resolved.
Show resolved Hide resolved

// strip any trailing / from RAZEEDASH_API_URL
const regex = /\/*$/gi;
const API_HOST = Meteor.settings.public.RAZEEDASH_API_URL.replace(regex, '');

const httpLink = createHttpLink({
uri: `${API_HOST}/graphql`,
fetch: fetch,
headers: {
'x-api-key': token
}
});

const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, extensions }) => {
console.error(`[GraphQL error]: Message: ${message}, Type: ${extensions.code}`);
});
if (networkError) {
console.error(`[Network error]: ${networkError}`);
}
});

const links = ApolloLink.from ([
errorLink,
httpLink,
]);

const client = new ApolloClient({
link: links,
cache: new InMemoryCache(),
});

return client;
};

exports.getQueryClient = getQueryClient;
Loading