Skip to content

Commit

Permalink
Merge pull request #1259 from razee-io/kdai7
Browse files Browse the repository at this point in the history
add cluster details for subscriptions query, channel and channelByName can list serviceSubscriptions
  • Loading branch information
kdai7 committed Nov 30, 2022
2 parents 25bd4a6 + 0b2e3ab commit 43b8aa1
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/apollo/schema/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const channelSchema = gql`
updated: Date
versions: [ChannelVersion]
subscriptions: [ChannelSubscription]
serviceSubscriptions: [ServiceSubscription]
tags: [String!]!
custom: JSON
owner: BasicUser
Expand Down
1 change: 1 addition & 0 deletions app/apollo/schema/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const subscriptionSchema = gql`
name: String!
groups: [String!]
clusterId: String
cluster: BasicCluster
channelUuid: String!
channelName: String!
channel: Channel
Expand Down
21 changes: 21 additions & 0 deletions app/apollo/test/channel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions app/apollo/test/channelApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ const channelFunc = grahqlUrl => {
name
}
}
serviceSubscriptions {
name
}
custom
}
}
Expand Down
23 changes: 23 additions & 0 deletions app/apollo/test/subscriptions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ) => {
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions app/apollo/test/subscriptionsApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ const subscriptionsFunc = grahqlUrl => {
pendingCount
failedCount
}
cluster {
clusterId
orgId
registration
}
custom
}
}
Expand Down
16 changes: 16 additions & 0 deletions app/apollo/utils/applyQueryFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion local-dev/api/subList.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"'"}'
Expand Down

0 comments on commit 43b8aa1

Please sign in to comment.