Skip to content

Commit

Permalink
removes deployableVersions from db and s3 when the channel is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgraham committed Feb 15, 2021
1 parent 125fc7d commit 98d14ab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
36 changes: 27 additions & 9 deletions app/apollo/resolvers/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const S3ClientClass = require('../../s3/s3Client');
const { WritableStreamBuffer } = require('stream-buffers');
const streamToString = require('stream-to-string');
const stream = require('stream');
const pLimit = require('p-limit');
const { applyQueryFieldsToChannels } = require('../utils/applyQueryFields');

const yaml = require('js-yaml');
Expand All @@ -32,6 +33,18 @@ const { whoIs, validAuth, getAllowedChannels, filterChannelsToAllowed, NotFoundE

const { encryptOrgData, decryptOrgData} = require('../../utils/orgs');

const deleteDeployableVersionFromS3 = async(deployableVersionObj)=>{
const url = deployableVersionObj.content;
const urlObj = new URL(url);
const fullPath = urlObj.pathname;
var parts = _.filter(_.split(fullPath, '/'));
var bucketName = parts.shift();
var path = `${parts.join('/')}`;

const s3Client = new S3ClientClass(conf);
return await s3Client.deleteObject(bucketName, path);
};

const channelResolvers = {
Query: {
channels: async(parent, { orgId }, context, fullQuery) => {
Expand Down Expand Up @@ -394,6 +407,19 @@ const channelResolvers = {
throw new RazeeValidationError(`${subCount} subscriptions depend on this channel. Please update/remove them before removing this channel.`, context);
}

// deletes the linked deployableVersions in s3
var versionsToDeleteFromS3 = await models.DeployableVersion.find({ org_id, channel_id: channel.uuid, location: 's3', });
const limit = pLimit(5);
await Promise.all(_.map(versionsToDeleteFromS3, async(deployableVersionObj)=>{
return limit(async()=>{
return await deleteDeployableVersionFromS3(deployableVersionObj);
});
}));

// deletes the linked deployableVersions in db
await models.DeployableVersion.deleteMany({ org_id, channel_id: channel.uuid });

// deletes the channel
await models.Channel.deleteOne({ org_id, uuid });

return {
Expand Down Expand Up @@ -432,15 +458,7 @@ const channelResolvers = {
throw new NotFoundError(`versionObj "${uuid}" is not found for ${channel.name}:${channel.uuid}`, context);
}
if(versionObj.location === 's3'){
const url = deployableVersionObj.content;
const urlObj = new URL(url);
const fullPath = urlObj.pathname;
var parts = _.filter(_.split(fullPath, '/'));
var bucketName = parts.shift();
var path = `${parts.join('/')}`;

const s3Client = new S3ClientClass(conf);
await s3Client.deleteObject(bucketName, path);
await deleteDeployableVersionFromS3(deployableVersionObj);
}
await models.DeployableVersion.deleteOne({ org_id, uuid});

Expand Down
10 changes: 5 additions & 5 deletions app/apollo/resolvers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const _ = require('lodash');
const { ApolloError } = require('apollo-server');
const { TYPES, ACTIONS } = require('../models/const');

const whoIs = me => {
const whoIs = me => {
if (me === null || me === undefined) return 'null';
if (me.email) return me.email;
if (me.identifier) return me.identifier;
Expand Down Expand Up @@ -86,7 +86,7 @@ var filterSubscriptionsToAllowed = async(me, orgId, action, field, subscriptions
return subscriptions;
};

// return user permitted cluster groups in an array
// return user permitted cluster groups in an array
const getAllowedGroups = async (me, org_id, action, field, queryName, context) => {
const {req_id, models, logger} = context;

Expand Down Expand Up @@ -114,7 +114,7 @@ const getGroupConditions = async (me, org_id, action, field, queryName, context)
return {
groups: {$not: {$elemMatch: {uuid: {$nin: allowedGroups}}}},
};
}
}
return {
'groups': {$not: {$elemMatch: {$nin: allowedGroups}}},
};
Expand All @@ -130,7 +130,7 @@ const getGroupConditionsIncludingEmpty = async (me, org_id, action, field, query
{groups: {$not: {$elemMatch: {uuid: {$nin: allowedGroups}}}}}
]
};
}
}
return {
$or: [
{'groups': {$not: {$elemMatch: {$nin: allowedGroups}}}},
Expand All @@ -147,7 +147,7 @@ const validAuth = async (me, org_id, action, type, queryName, context, attrs = n
if (context.recoveryHintsMap) {
context['recoveryHints'] = context.recoveryHintsMap[queryName];
}

// razeedash users (x-api-key)
if(me && me.type == 'userToken'){
const result = await models.User.userTokenIsAuthorized(me, org_id, action, type, context);
Expand Down

0 comments on commit 98d14ab

Please sign in to comment.