diff --git a/app/apollo/schema/channel.js b/app/apollo/schema/channel.js index 78448e437..856329921 100644 --- a/app/apollo/schema/channel.js +++ b/app/apollo/schema/channel.js @@ -77,6 +77,7 @@ const channelSchema = gql` updated: Date versions: [ChannelVersion] subscriptions: [ChannelSubscription] + serviceSubscriptions: [ServiceSubscription] tags: [String!]! custom: JSON owner: BasicUser diff --git a/app/apollo/schema/subscription.js b/app/apollo/schema/subscription.js index 8290d3bb9..fc9f88669 100644 --- a/app/apollo/schema/subscription.js +++ b/app/apollo/schema/subscription.js @@ -61,6 +61,7 @@ const subscriptionSchema = gql` name: String! groups: [String!] clusterId: String + cluster: BasicCluster channelUuid: String! channelName: String! channel: Channel diff --git a/app/apollo/test/channel.spec.js b/app/apollo/test/channel.spec.js index 01d165ba5..661ef8503 100644 --- a/app/apollo/test/channel.spec.js +++ b/app/apollo/test/channel.spec.js @@ -87,6 +87,9 @@ const channelVersion_01_uuid = 'fake_cv_01_uuid'; const subscription_01_name = 'fake_subscription_01'; const subscription_01_uuid = 'fake_sub_01_uuid'; +const serviceSubscription_01_name = 'fake_serviceSubscription_01'; +const serviceSubscription_01_uuid = 'fake_servsub_01_uuid'; + const createOrganizations = async () => { org01Data = JSON.parse( fs.readFileSync( @@ -237,6 +240,21 @@ const createSubscriptions = async () => { }); }; +const createServiceSubscriptions = async () => { + await models.ServiceSubscription.create({ + _id: 'fake_ss_id_1', + org_id: org01._id, + uuid: serviceSubscription_01_uuid, + name: serviceSubscription_01_name, + owner: 'abc', + groups: ['dev'], + channel_uuid: channel_04_uuid, + channelName: channel_04_name, + version: channelVersion_01_name, + version_uuid: channelVersion_01_uuid, + }); +}; + const createGroups = async () => { await models.Group.create({ _id: 'dummyuuid', @@ -263,6 +281,7 @@ describe('channel graphql test suite', () => { await createChannels(); await createVersions(); await createSubscriptions(); + await createServiceSubscriptions(); await createGroups(); // Can be uncommented if you want to see the test data that was added to the DB @@ -408,6 +427,8 @@ describe('channel graphql test suite', () => { expect(channelByName.subscriptions.length).to.equal(1); expect(channelByName.subscriptions[0].versionUuid).to.equal(channelByName.subscriptions[0].versionObj.uuid); + + expect(channelByName.serviceSubscriptions.length).to.equal(1); } catch (error) { if (error.response) { console.error('error encountered: ', error.response.data); diff --git a/app/apollo/test/channelApi.js b/app/apollo/test/channelApi.js index 9c18477f3..8e832f894 100644 --- a/app/apollo/test/channelApi.js +++ b/app/apollo/test/channelApi.js @@ -104,6 +104,9 @@ const channelFunc = grahqlUrl => { name } } + serviceSubscriptions { + name + } custom } } diff --git a/app/apollo/test/subscriptions.spec.js b/app/apollo/test/subscriptions.spec.js index 698777800..6d6375771 100644 --- a/app/apollo/test/subscriptions.spec.js +++ b/app/apollo/test/subscriptions.spec.js @@ -341,6 +341,7 @@ const createSubscriptions = async () => { name: subscription_04_name, owner: user77._id, groups: ['dev'], + clusterId: 'cluster_03', channel_uuid: channel_04_uuid, channel: channel_04_name, version: channelVersion_04_name, @@ -386,6 +387,24 @@ const createClusters = async () => { }, registration: { name: 'my-cluster2' } }); + await models.Cluster.create({ + org_id: org77._id, + cluster_id: 'cluster_03', + metadata: { + kube_version: { + major: '1', + minor: '16', + gitVersion: '1.99', + gitCommit: 'abc', + gitTreeState: 'def', + buildDate: 'a_date', + goVersion: '1.88', + compiler: 'some compiler', + platform: 'linux/amd64', + }, + }, + registration: { name: 'my-cluster3' } + }); }; const assignClusterGroups = async ( token, orgId, groupUUIDs, clusterUUID ) => { @@ -500,6 +519,10 @@ describe('subscription graphql test suite', () => { }); expect(result3.data.data.subscriptions).to.have.length(2); expect(Object.keys(result3.data.data.subscriptions[1].custom)).to.have.length(2); + + // subscription 2 should have cluster details + expect( result3.data.data.subscriptions[1].cluster ).to.exist; + } catch (error) { if (error.response) { console.error('error encountered: ', error.response.data); diff --git a/app/apollo/test/subscriptionsApi.js b/app/apollo/test/subscriptionsApi.js index 3cf883896..5ef4790e6 100644 --- a/app/apollo/test/subscriptionsApi.js +++ b/app/apollo/test/subscriptionsApi.js @@ -79,6 +79,11 @@ const subscriptionsFunc = grahqlUrl => { pendingCount failedCount } + cluster { + clusterId + orgId + registration + } custom } } diff --git a/app/apollo/utils/applyQueryFields.js b/app/apollo/utils/applyQueryFields.js index 0989ed354..7e01446be 100644 --- a/app/apollo/utils/applyQueryFields.js +++ b/app/apollo/utils/applyQueryFields.js @@ -281,6 +281,22 @@ const applyQueryFieldsToChannels = async(channels, queryFields={}, args, context channel.subscriptions = subscriptionsByChannelUuid[channel.uuid] || []; }); } + + if(queryFields.serviceSubscriptions){ + const conditions = await getGroupConditions(me, orgId, ACTIONS.READ, 'name', 'applyQueryFieldsToChannels queryFields.serviceSubscriptions', context); + const channelUuids = _.uniq(_.map(channels, 'uuid')); + let serviceSubs = await models.ServiceSubscription.find({ org_id: orgId, channel_uuid: { $in: channelUuids }, ...conditions }, {}).lean({ virtuals: true }); + serviceSubs = await filterSubscriptionsToAllowed(me, orgId, ACTIONS.READ, TYPES.SERVICESUBSCRIPTION, serviceSubs, context); + + serviceSubs.forEach(i => i.ssid = i.uuid); + + await applyQueryFieldsToSubscriptions(serviceSubs, queryFields.serviceSubscriptions, {orgId, servSub: true}, context); + + const servSubsByChannelUuid = _.groupBy(serviceSubs, 'channel_uuid'); + _.each(channels, (channel)=>{ + channel.serviceSubscriptions = servSubsByChannelUuid[channel.uuid] || []; + }); + } }; const applyQueryFieldsToSubscriptions = async(subs, queryFields={}, args, context)=>{ // eslint-disable-line diff --git a/local-dev/api/subList.sh b/local-dev/api/subList.sh index d9bc96e58..32a78c9db 100755 --- a/local-dev/api/subList.sh +++ b/local-dev/api/subList.sh @@ -4,7 +4,7 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" RAZEE_ORG_ID=${1:-${RAZEE_ORG_ID:-pOrgId}} -RAZEE_QUERY='query($orgId: String!) { subscriptions( orgId: $orgId ) { uuid, orgId, name, groupObjs { uuid, name }, channelUuid, channelName, version, versionUuid, versionObj { name, remote { parameters { key, value } } }, created, updated, rolloutStatus {errorCount, successCount}, owner { id, name }, identitySyncStatus { syncedCount, failedCount, unknownCount, pendingCount } } }' +RAZEE_QUERY='query($orgId: String!) { subscriptions( orgId: $orgId ) { uuid, orgId, name, groupObjs { uuid, name }, channelUuid, channelName, version, versionUuid, versionObj { name, remote { parameters { key, value } } }, created, updated, rolloutStatus {errorCount, successCount}, owner { id, name }, identitySyncStatus { syncedCount, failedCount, unknownCount, pendingCount }, cluster { clusterId orgId registration } } }' #RAZEE_QUERY='query($orgId: String!) { subscriptions( orgId: $orgId ) { name orgId groups clusterId uuid channelUuid version versionUuid owner { id, name } SubscriptionType: __typename rolloutStatus {successCount, errorCount} remoteResources { cluster{clusterId, name} } } }' #RAZEE_QUERY='query($orgId: String!) { subscriptions( orgId: $orgId ) { uuid name channelName channelUuid version kubeOwnerName owner { id name } rolloutStatus { errorCount successCount } } }' RAZEE_VARIABLES='{"orgId":"'"${RAZEE_ORG_ID}"'"}'