Skip to content

Commit

Permalink
migrate cluster facing api
Browse files Browse the repository at this point in the history
  • Loading branch information
yingwang-us committed Jul 1, 2020
1 parent 445e4db commit ddee5fa
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 22 deletions.
22 changes: 14 additions & 8 deletions app/apollo/resolvers/subscription.js
Expand Up @@ -45,11 +45,16 @@ async function validateGroups(org_id, groups, context) {

const subscriptionResolvers = {
Query: {
// Cluster-facing API
subscriptionsByCluster: async(parent, { clusterId: cluster_id, /* may add some unique data from the cluster later for verification. */ }, context) => {
// Cluster-facing API, deprecated,
subscriptionsByCluster: async(parent, { cluster_id }, context) => {
return await subscriptionResolvers.Query.subscriptionsByClusterId(parent, {clusterId: cluster_id, deprecated: true}, context);
},

// Cluster-facing API,
subscriptionsByClusterId: async(parent, { clusterId: cluster_id, deprecated /* may add some unique data from the cluster later for verification. */ }, context) => {
const { req_id, me, models, logger } = context;
const query = 'subscriptionsByCluster';
logger.debug({req_id, user: whoIs(me), cluster_id}, `${query} enter`);
const query = 'subscriptionsByClusterId';
logger.debug({req_id, user: whoIs(me), cluster_id, deprecated}, `${query} enter`);
await validClusterAuth(me, query, context);

const org = await models.User.getOrg(models, me);
Expand Down Expand Up @@ -79,7 +84,7 @@ const subscriptionResolvers = {
// subscription groups: ['dev', 'prod'] , clusterGroupNames: ['stage'] ==> false
const foundSubscriptions = await models.Subscription.find({ 'org_id': org_id, groups: { $in: clusterGroupNames }}).lean();
if(foundSubscriptions && foundSubscriptions.length > 0 ) {
urls = await getSubscriptionUrls(org_id, foundSubscriptions);
urls = await getSubscriptionUrls(org_id, foundSubscriptions, deprecated);
}
} catch (error) {
logger.error(error, `There was an error getting ${query} from mongo`);
Expand Down Expand Up @@ -307,7 +312,7 @@ const subscriptionResolvers = {
// Sends a message back to a subscribed client
// 'parent' is the object representing the subscription that was updated
//
return { 'hasUpdates': true };
return { has_updates: true, hasUpdates: true };
},

subscribe: withFilter(
Expand Down Expand Up @@ -358,8 +363,9 @@ const subscriptionResolvers = {
return Boolean(false);
}

if(subscriptionUpdated.data.orgId !== orgId) {
logger.error('wrong org id for this subscription. returning false');
const orgIdInUpdated = subscriptionUpdated.data.org_id || subscriptionUpdated.data.orgId;
if(orgIdInUpdated !== orgId) {
logger.error('wrong org id for this subscription. returning false');
found = false;
}

Expand Down
21 changes: 15 additions & 6 deletions app/apollo/schema/subscription.js
Expand Up @@ -49,18 +49,23 @@ const subscriptionSchema = gql`
type AddChannelSubscriptionReply {
uuid: String!
}
type UpdatedSubscriptionDeprecated {
subscription_name: String!,
subscription_channel: String!,
subscription_version: String!,
subscription_uuid: String!,
url: String!
}
type UpdatedSubscription {
subscriptionName: String!,
subscriptionChannel: String!,
subscriptionVersion: String!,
subscriptionUuid: String!,
url: String!
}
type ChannelsWithLinks {
channel: ChannelSubscription,
links: UpdatedSubscription
}
type SubscriptionUpdated {
"**has_updates**: deprecated, use hasUpdates"
has_updates: Boolean
hasUpdates: Boolean
}
Expand All @@ -74,9 +79,13 @@ const subscriptionSchema = gql`
"""
subscription(orgId: String!, uuid: String!): ChannelSubscription
"""
Gets all subscriptions for a cluster
Gets all subscriptions for a cluster, invoked from cluster-subscription agent, deprecated, please use subscriptionsByClusterId
"""
subscriptionsByCluster(cluster_id: String): [UpdatedSubscriptionDeprecated]
"""
Gets all subscriptions for a cluster, invoked from cluster-subscription agent
"""
subscriptionsByCluster(clusterId: String): [UpdatedSubscription]
subscriptionsByClusterId(clusterId: String!): [UpdatedSubscription]
}
extend type Mutation {
"""
Expand Down
51 changes: 47 additions & 4 deletions app/apollo/test/subscriptions.default.spec.js
Expand Up @@ -194,17 +194,18 @@ describe('subscriptions graphql test suite', () => {
await mongoServer.stop();
}); // after

it('get should return a subscription for a cluster', async () => {
it('get should return a subscription for a cluster by calling deprecated subscriptionsByCluster', async () => {
try {
const {
data: {
data: { subscriptionsByCluster },
},
} = await subscriptionsApi.subscriptionsByCluster(token, {
clusterId: cluster_id
cluster_id
}, orgKey);

expect(subscriptionsByCluster).to.have.length(1);
expect(subscriptionsByCluster[0].subscription_name).to.equal('fake_sub_01');
} catch (error) {
if (error.response) {
console.error('error encountered: ', error.response.data);
Expand All @@ -215,14 +216,36 @@ describe('subscriptions graphql test suite', () => {
}
});

it('get should return an empty array when there are no matching groups', async () => {
it('get should return a subscription for a cluster', async () => {
try {
const {
data: {
data: { subscriptionsByClusterId },
},
} = await subscriptionsApi.subscriptionsByClusterId(token, {
clusterId: cluster_id
}, orgKey);

expect(subscriptionsByClusterId).to.have.length(1);
expect(subscriptionsByClusterId[0].subscriptionName).to.equal('fake_sub_01');
} catch (error) {
if (error.response) {
console.error('error encountered: ', error.response.data);
} else {
console.error('error encountered: ', error);
}
throw error;
}
});

it('get should return an empty array when there are no matching groups by calling deprecated subscriptionsByCluster', async () => {
try {
const {
data: {
data: { subscriptionsByCluster },
},
} = await subscriptionsApi.subscriptionsByCluster(token, {
clusterId: cluster_id_2
cluster_id: cluster_id_2
}, orgKey);
expect(subscriptionsByCluster).to.have.length(0);
} catch (error) {
Expand All @@ -235,4 +258,24 @@ describe('subscriptions graphql test suite', () => {
}
});

it('get should return an empty array when there are no matching groups', async () => {
try {
const {
data: {
data: { subscriptionsByClusterId },
},
} = await subscriptionsApi.subscriptionsByClusterId(token, {
clusterId: cluster_id_2
}, orgKey);
expect(subscriptionsByClusterId).to.have.length(0);
} catch (error) {
if (error.response) {
console.error('error encountered: ', error.response.data);
} else {
console.error('error encountered: ', error);
}
throw error;
}
});

});
31 changes: 28 additions & 3 deletions app/apollo/test/subscriptionsApi.js
Expand Up @@ -22,8 +22,32 @@ const subscriptionsFunc = grahqlUrl => {
grahqlUrl,
{
query: `
query($clusterId: String) {
subscriptionsByCluster(clusterId: $clusterId) {
query($cluster_id: String) {
subscriptionsByCluster( cluster_id: $cluster_id) {
subscription_name
subscription_channel
subscription_uuid
subscription_version
url
}
}
`,
variables,
},
{
headers: {
Authorization: `Bearer ${token}`,
'razee-org-key': orgKey
},
},
);
const subscriptionsByClusterId = async (token, variables, orgKey) =>
axios.post(
grahqlUrl,
{
query: `
query($clusterId: String!) {
subscriptionsByClusterId(clusterId: $clusterId) {
subscriptionName
subscriptionChannel
subscriptionUuid
Expand Down Expand Up @@ -193,7 +217,8 @@ const subscriptionsFunc = grahqlUrl => {
);

return {
subscriptionsByCluster,
subscriptionsByCluster, /* deprecated */
subscriptionsByClusterId,
subscriptions,
subscription,
addSubscription,
Expand Down
11 changes: 10 additions & 1 deletion app/utils/subscriptions.js
Expand Up @@ -3,7 +3,7 @@ const { models } = require('../apollo/models');

const _ = require('lodash');

const getSubscriptionUrls = async(orgId, matchingSubscriptions) => {
const getSubscriptionUrls = async(orgId, matchingSubscriptions, deprecated) => {

const matchingChannels = await models.Channel.find({
org_id: orgId,
Expand All @@ -22,6 +22,15 @@ const getSubscriptionUrls = async(orgId, matchingSubscriptions) => {
if(foundVersion.length > 0) {
url = `api/v1/channels/${subscription.channel}/${foundVersion[0].uuid}`;
}
if (deprecated) {
return {
subscription_name: subscription.name,
subscription_channel: subscription.channel,
subscription_version: subscription.version,
subscription_uuid: subscription.uuid,
url: url
};
}
return {
subscriptionName: subscription.name,
subscriptionChannel: subscription.channel,
Expand Down

0 comments on commit ddee5fa

Please sign in to comment.