Skip to content

Commit

Permalink
allow subscription groups to be specified by name or uuid (razee-io#1327
Browse files Browse the repository at this point in the history
)

* allow subscription groups to be specified by name or uuid, do not automatically create groups

* linting

* linting
  • Loading branch information
carrolp authored May 25, 2023
1 parent 714d1e3 commit ea02ef9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
20 changes: 10 additions & 10 deletions app/apollo/resolvers/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const pubSub = GraphqlPubSub.getInstance();

const { validateString, validateName } = require('../utils/directives');

const { validateGroups, validateSubscriptionLimit } = require('../utils/subscriptionUtils.js');
const { getGroupNames, validateSubscriptionLimit } = require('../utils/subscriptionUtils.js');
const { validateNewVersions, ingestVersionContent } = require('../utils/versionUtils');
const storageFactory = require('./../../storage/storageFactory');

Expand Down Expand Up @@ -345,8 +345,8 @@ const subscriptionResolvers = {
throw new NotFoundError(context.req.t('Could not find the organization with ID {{org_id}}.', {'org_id':org_id}), context);
}

// validate groups all exist
await validateGroups(org_id, groups, context);
// validate groups all exist and get names (while names or uuids could be passed, only the names are used on the database record at this time, and it is assumed/asserted that they are unique)
const groupNames = await getGroupNames( org_id, groups, context );

// Get or create the version
let version;
Expand Down Expand Up @@ -402,7 +402,7 @@ const subscriptionResolvers = {
uuid,
org_id,
name,
groups,
groups: groupNames,
owner: me._id,
channelName: channel.name,
channel_uuid,
Expand All @@ -425,7 +425,7 @@ const subscriptionResolvers = {
subscriptionsRbacSync( [subscription], { resync: false }, context ).catch(function(){/*ignore*/});

// Allow graphQL plugins to retrieve more information. addSubscription can get/create a version and create a subscription. Include details of each created and validated resource in pluginContext.
context.pluginContext = {channel: {name: channel.name, uuid: channel.uuid, tags: channel.tags}, version: {name: version.name, uuid: version.uuid, description: version.description}, subscription: {name: name, uuid: uuid, groups: groups}};
context.pluginContext = {channel: {name: channel.name, uuid: channel.uuid, tags: channel.tags}, version: {name: version.name, uuid: version.uuid, description: version.description}, subscription: {name: name, uuid: uuid, groups: groupNames}};

logger.info( {req_id, user, org_id, name, channel_uuid, version_uuid }, `${queryName} returning` );
return {
Expand Down Expand Up @@ -487,8 +487,8 @@ const subscriptionResolvers = {
throw new NotFoundError(context.req.t('Channel uuid "{{channel_uuid}}" not found.', {'channel_uuid':channel_uuid}), context);
}

// validate groups all exist
await validateGroups(org_id, groups, context);
// validate groups all exist and get names (while names or uuids could be passed, only the names are used on the database record at this time, and it is assumed/asserted that they are unique)
const groupNames = await getGroupNames( org_id, groups, context );

// Retreive version for graphQL plugins
const oldVersionObj = await models.DeployableVersion.findOne( { org_id, uuid: oldVersionUuid } );
Expand Down Expand Up @@ -543,7 +543,7 @@ const subscriptionResolvers = {

let sets = {
name,
groups,
groups: groupNames,
channelName: channel.name,
channel_uuid,
version: version.name,
Expand Down Expand Up @@ -583,7 +583,7 @@ const subscriptionResolvers = {
resyncNeeded = false;
} else {
// If adding any new group(s), trigger RBAC Sync of any un-synced clusters
for( const group of groups ) {
for( const group of groupNames ) {
if( !subscription.groups.includes(group) ) {
// At least one new group, trigger sync and stop checking for new groups
syncNeeded = true;
Expand All @@ -595,7 +595,7 @@ const subscriptionResolvers = {
// If sync needed, do it
if( syncNeeded ) {
// Set the new owner and groups on the subscription object before using it to do RBAC Sync
subscription.groups = groups;
subscription.groups = groupNames;
subscription.owner = me._id;
subscriptionsRbacSync( [subscription], { resync: resyncNeeded }, context ).catch(function(){/*ignore*/});
}
Expand Down
8 changes: 4 additions & 4 deletions app/apollo/test/subscriptions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,11 @@ describe('subscription graphql test suite', () => {
});
expect(addSubscription2.data.errors[0].message).to.equal(`Too many subscriptions are registered under ${org01._id}.`); // limit is set in definition of test job in package.json

// add subscription with custom attribute
// add subscription with custom attribute, using group uuid
const result = await subscriptionApi.addSubscription(token77, {
orgId: org77._id,
name: 'a_random_name3',
groups:['dev'],
groups:[org77_group_dev_uuid],
channelUuid: channel_04_uuid,
versionUuid: channelVersion_04_uuid,
custom: {
Expand All @@ -761,7 +761,7 @@ describe('subscription graphql test suite', () => {
orgId: org01._id,
uuid: subscription_01_uuid,
name: 'new-name',
groups:['new-tag'],
groups:['dev'],
channelUuid: channel_02_uuid,
versionUuid: channelVersion_03_uuid,
});
Expand Down Expand Up @@ -791,7 +791,7 @@ describe('subscription graphql test suite', () => {
orgId: org77._id,
uuid: subscription_04_uuid,
name: 'new-name',
groups:['new-tag'],
groups:['dev'],
channelUuid: channel_04_uuid,
versionUuid: channelVersion_04_uuid,
custom: {
Expand Down
38 changes: 23 additions & 15 deletions app/apollo/utils/subscriptionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,32 @@
* limitations under the License.
*/

const { whoIs, RazeeValidationError } = require ('../resolvers/common');
const { RazeeValidationError } = require ('../resolvers/common');
const { SUBSCRIPTION_LIMITS } = require('../models/const');
const { validateString } = require('./directives');

const validateGroups = async ( org_id, groups, context ) => {
const { req_id, me, models, logger } = context;
// validate cluster groups exists in the groups db
let groupCount = await models.Group.count({org_id: org_id, name: {$in: groups} });
if (groupCount < groups.length) {
if (process.env.LABEL_VALIDATION_REQUIRED) {
throw new RazeeValidationError(context.req.t('Could not find all the cluster groups {{groups}} in the groups database, please create them first.', {'groups':groups}), context);
} else {
// in migration period, we automatically populate groups into label db
logger.info({req_id, user: whoIs(me), org_id}, `could not find all the cluster groups ${groups}, migrate them into label database.`);
await models.Group.findOrCreateList(models, org_id, groups, context);
groupCount = await models.Group.count({org_id: org_id, name: {$in: groups} });
// Validate that specified groups (by name or uuid) exist, return array of group names
const getGroupNames = async ( org_id, groupNamesOrUuids, context ) => {
const { models } = context;
// Get all groups
const allGroups = await models.Group.find({ org_id: org_id });

const groupNames = [];

for( const groupNameOrUuid of groupNamesOrUuids ) {
const matchingGroup = allGroups.find( g => {
return( g.name == groupNameOrUuid || g.uuid == groupNameOrUuid );
} );

if( matchingGroup ) {
groupNames.push( matchingGroup.name );
}
else {
throw new RazeeValidationError(context.req.t('Could not find all the cluster groups {{groups}} in the groups database, please create them first.', {'groups':groupNamesOrUuids}), context);
}
}

return( groupNames );
};

// validate the number of total subscriptions are under the limit
Expand All @@ -56,7 +64,7 @@ const validateNewSubscriptions = async ( org_id, { versions, newSubscriptions },
s.groups.forEach( value => { validateString( 'groups', value ); } );

// validate groups all exist
await validateGroups(org_id, s.groups, context);
await getGroupNames(org_id, s.groups, context);

// validate the subscription references the version(s)
const badVersionRef = versions.find( v => v.name === s.versionName ).length == 0;
Expand All @@ -68,7 +76,7 @@ const validateNewSubscriptions = async ( org_id, { versions, newSubscriptions },


module.exports = {
validateGroups,
getGroupNames,
validateSubscriptionLimit,
validateNewSubscriptions,
};

0 comments on commit ea02ef9

Please sign in to comment.