Skip to content

Commit

Permalink
impl editChannelVersion to allow changing remote params (#1219)
Browse files Browse the repository at this point in the history
* impl editChannelVersion to allow changing remote params

* Update 'updated' field when changing resources
  • Loading branch information
carrolp authored Sep 30, 2022
1 parent a2c0aaa commit 8efabe0
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 29 deletions.
4 changes: 4 additions & 0 deletions app/apollo/models/deployableVersion.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const DeployableVersionSchema = new mongoose.Schema({
type: Date,
default: Date.now,
},
updated: {
type: Date,
default: Date.now,
},
}, {
collection: 'deployableVersions',
strict: 'throw',
Expand Down
4 changes: 4 additions & 0 deletions app/apollo/models/group.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const GroupSchema = new mongoose.Schema({
type: Date,
default: Date.now,
},
updated: {
type: Date,
default: Date.now,
},
owner: {
type: String,
},
Expand Down
73 changes: 67 additions & 6 deletions app/apollo/resolvers/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ const channelResolvers = {
}

// Save the change
await models.Channel.updateOne({ org_id, uuid }, { $set: { name, tags, custom, remote } }, {});
await models.Channel.updateOne({ org_id, uuid }, { $set: { name, tags, custom, remote, updated: Date.now() } }, {});

// Attempt to update channelName in all versions and subscriptions under this channel (the duplication is unfortunate and should be eliminated in the future)
try {
Expand Down Expand Up @@ -676,20 +676,81 @@ const channelResolvers = {
};
},
editChannelVersion: async(parent, { orgId: org_id, uuid, description, remote }, context)=>{
const { /*models,*/ me, req_id, logger } = context;
const { models, me, req_id, logger } = context;
const queryName = 'editChannelVersion';

logger.debug({req_id, user: whoIs(me), org_id, uuid, description, remote }, `${queryName} enter`);

validateString( 'org_id', org_id );
validateString( 'uuid', uuid );

logger.debug({req_id, user: whoIs(me), org_id, uuid, description, remote }, `${queryName} enter`);

/*
Edit Channel Version not yet implemented.
- Allow changing description
- Allow altering `remote.parameters`
*/
throw new RazeeValidationError( context.req.t( 'Unsupported query: {{api}}', { api: queryName } ), context );
if( !description && !remote ){
throw new RazeeValidationError( context.req.t( 'No changes specified.' ), context );
}

try{
// Experimental
if( !process.env.EXPERIMENTAL_GITOPS ) {
// Block experimental features
if( remote ) {
throw new RazeeValidationError( context.req.t( 'Unsupported arguments: [{{args}}]', { args: 'remote' } ), context );
}
}

const version = await models.DeployableVersion.findOne( { uuid, org_id } );
if( !version ){
throw new NotFoundError( context.req.t( 'Version uuid "{{version_uuid}}" not found.', { 'version_uuid': uuid } ), context );
}

const channel = await models.Channel.findOne( { uuid: version.channel_id, org_id } );
if( !channel ){
throw new NotFoundError( context.req.t( 'Channel uuid "{{channel_uuid}}" not found.', { 'channel_uuid': version.channel_id } ), context );
}

// Verify authorization on the Channel
await validAuth(me, org_id, ACTIONS.MANAGEVERSION, TYPES.CHANNEL, queryName, context, [channel.uuid, channel.name]);

const set = {
updated: Date.now(),
};

// Validate REMOTE-specific values
if( channel.contentType === CHANNEL_CONSTANTS.CONTENTTYPES.REMOTE ) {
if( remote ) {
// Validate remote.parameters (length)
if( remote.parameters && JSON.stringify(remote.parameters).length > MAX_REMOTE_PARAMETERS_LENGTH ) {
throw new RazeeValidationError( context.req.t( 'The remote version parameters are too large. The string representation must be less than {{MAX_REMOTE_PARAMETERS_LENGTH}} characters long', { MAX_REMOTE_PARAMETERS_LENGTH } ), context );
}

set.content = {
metadata: {
type: 'remote',
},
remote: { parameters: remote.parameters },
};
}
}

if( description ) set.description = description;

// Save the change
await models.DeployableVersion.updateOne({ org_id, uuid }, { $set: set }, {});

return {
success: true,
};
}
catch( err ){
if( err instanceof BasicRazeeError ) {
throw err;
}
logger.error( err, `${queryName} encountered an error when serving ${req_id}.` );
throw new RazeeQueryError( context.req.t( 'Query {{queryName}} error. MessageID: {{req_id}}.', { 'queryName': queryName, 'req_id': req_id } ), context );
}
},
removeChannel: async (parent, { orgId: org_id, uuid }, context)=>{
const { models, me, req_id, logger } = context;
Expand Down
1 change: 1 addition & 0 deletions app/apollo/resolvers/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ const groupResolvers = {
});
const sets = {
groups: groupObjsToAdd,
updated: Date.now(),
};
const res = await models.Cluster.updateOne({ org_id: orgId, cluster_id: clusterId }, { $set: sets });

Expand Down
2 changes: 1 addition & 1 deletion app/apollo/resolvers/serviceSubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const serviceResolvers = {
throw new NotFoundError(context.req.t('Version with uuid "{{versionUuid}}" not found', { 'versionUuid': versionUuid }), context);
}

const sets = { name, channelName: channel.name, channel_uuid: channelUuid, version: version.name, version_uuid: versionUuid };
const sets = { name, channelName: channel.name, channel_uuid: channelUuid, version: version.name, version_uuid: versionUuid, updated: Date.now() };
await models.ServiceSubscription.updateOne({ _id: ssid }, { $set: sets });

pubSub.channelSubChangedFunc({ org_id: serviceSubscription.clusterOrgId }, context); // notify cluster should re-fetch its subscriptions
Expand Down
16 changes: 12 additions & 4 deletions app/apollo/resolvers/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,15 @@ const subscriptionResolvers = {
}

let sets = {
name, groups,
channelName: channel.name, channel_uuid, version: version.name, version_uuid,
clusterId, custom,
name,
groups,
channelName: channel.name,
channel_uuid,
version: version.name,
version_uuid,
clusterId,
custom,
updated: Date.now(),
};

// RBAC Sync
Expand Down Expand Up @@ -513,7 +519,9 @@ const subscriptionResolvers = {
}

var sets = {
version: version.name, version_uuid,
version: version.name,
version_uuid,
updated: Date.now(),
};
await models.Subscription.updateOne({ uuid, org_id }, { $set: sets });

Expand Down
6 changes: 3 additions & 3 deletions app/apollo/schema/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const channelSchema = gql`
remote: ChannelRemoteSource
data_location: String
created: Date!
updated: Date!
updated: Date
versions: [ChannelVersion]
subscriptions: [ChannelSubscription]
tags: [String!]!
Expand Down Expand Up @@ -120,7 +120,7 @@ const channelSchema = gql`
owner: BasicUser
kubeOwnerName: String
created: Date!
updated: Date!
updated: Date
}
extend type Query {
Expand Down Expand Up @@ -175,7 +175,7 @@ const channelSchema = gql`
"""
Edits a version
"""
editChannelVersion(orgId: String! @sv, uuid: String! @sv, name: String! @sv, description: String @sv, remote: VersionRemoteInput ): EditChannelVersionReply!
editChannelVersion(orgId: String! @sv, uuid: String! @sv, description: String @sv, remote: VersionRemoteInput ): EditChannelVersionReply!
"""
Removes a channel
Expand Down
19 changes: 6 additions & 13 deletions app/apollo/test/channel.remote.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,7 @@ describe('channel remote graphql test suite', () => {
try {
const result = await channelRemoteApi.editRemoteChannelVersion(userRootToken, {
orgId: org01._id,
uuid: channel01Uuid,
name: 'newvername',
uuid: ver01Uuid,
description: 'newverdesc',
remote: {
parameters: [
Expand All @@ -367,15 +366,9 @@ describe('channel remote graphql test suite', () => {
});
console.log( `editRemoteChannelVersion result: ${JSON.stringify( result.data, null, 2 )}` );

/*
const editChannelVersion = result.data.data.editChannelVersion;

expect(editChannel.success).to.equal(true);
*/

const errors = result.data.errors;

expect(errors[0].message).to.equal('Unsupported query: editChannelVersion');
expect(editChannelVersion.success).to.equal(true);
} catch (error) {
if (error.response) {
console.error('error encountered: ', error.response.data);
Expand All @@ -396,11 +389,11 @@ describe('channel remote graphql test suite', () => {
console.log( `getRemoteChannelVersionByUuid result: ${JSON.stringify( result.data, null, 2 )}` );
const retrievedVersion = result.data.data.channelVersion;

// Expect orig values currently as editChannelVersion is not yet implemented
// Expect orig name but changed description and remote parameters
expect(retrievedVersion.name).to.equal('origvername');
expect(retrievedVersion.description).to.equal('origverdesc');
expect(retrievedVersion.remote.parameters[0].key).to.equal('origverkey1');
expect(retrievedVersion.remote.parameters[0].value).to.equal('origverval1');
expect(retrievedVersion.description).to.equal('newverdesc');
expect(retrievedVersion.remote.parameters[0].key).to.equal('newverkey1');
expect(retrievedVersion.remote.parameters[0].value).to.equal('newverval1');
} catch (error) {
if (error.response) {
console.error('error encountered: ', error.response.data);
Expand Down
5 changes: 3 additions & 2 deletions app/apollo/test/channelRemoteApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const channelRemoteFunc = grahqlUrl => {
description
remote { parameters { key, value } }
created
updated
}
}
`,
Expand All @@ -143,8 +144,8 @@ const channelRemoteFunc = grahqlUrl => {
grahqlUrl,
{
query: `
mutation( $orgId: String!, $uuid: String!, $name: String!, $description: String!, $remote: VersionRemoteInput ) {
editChannelVersion(orgId: $orgId, uuid: $uuid, name: $name, description: $description, remote: $remote) {
mutation( $orgId: String!, $uuid: String!, $description: String!, $remote: VersionRemoteInput ) {
editChannelVersion(orgId: $orgId, uuid: $uuid, description: $description, remote: $remote) {
success
}
}
Expand Down

0 comments on commit 8efabe0

Please sign in to comment.