Skip to content

Commit

Permalink
version 0.7.10 review (#27)
Browse files Browse the repository at this point in the history
* common enum for registry response states

* homogenize single and multi tenant registry calls

* fix tests

* better splitting to avoid null parameter
  • Loading branch information
rlindner81 authored Mar 13, 2023
1 parent 5cb1227 commit 2b33505
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
58 changes: 25 additions & 33 deletions src/submodules/tenantRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const { request } = require("../shared/request");
const REGISTRY_PAGE_SIZE = 200;
const POLL_FREQUENCY = 10000;
const TENANT_UPDATABLE_STATES = ["SUBSCRIBED", "UPDATE_FAILED"];
const RESPONSE_STATE = Object.freeze({
SUCCEEDED: "SUCCEEDED",
FAILED: "FAILED",
});

const _registrySubscriptionsPaged = async (context, tenant) => {
const { subdomain: filterSubdomain, tenantId: filterTenantId } = resolveTenantArg(tenant);
Expand Down Expand Up @@ -112,7 +116,7 @@ const _registryJobPoll = async (context, location, { skipFirst = false } = {}) =
});
const responseBody = await response.json();
const { state } = responseBody;
if (!state || state === "SUCCEEDED" || state === "FAILED") {
if (!state || state === RESPONSE_STATE.SUCCEEDED || state === RESPONSE_STATE.FAILED) {
return JSON.stringify(responseBody, null, 2);
}
}
Expand All @@ -125,7 +129,7 @@ const registryJob = async (context, [jobId]) => {

// https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/4a8b63678cf24d5b8b36bd1957391ce3.html
// https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/9c4f927011db4bd0a53b23a1b33b36d0.html
const _registryCall = async (
const _registryCallForTenant = async (
context,
tenantId,
method,
Expand All @@ -150,7 +154,7 @@ const _registryCall = async (
});

if (!doJobPoll) {
const state = response.status === 200 ? "SUCCESS" : "FAILED";
const state = response.status === 200 ? RESPONSE_STATE.SUCCEEDED : RESPONSE_STATE.FAILED;
console.log("Subscription Operation with method %s for tenant %s finished with state %s", method, tenantId, state);
return JSON.stringify({ tenantId, state }, null, 2);
}
Expand All @@ -162,49 +166,37 @@ const _registryCall = async (
return _registryJobPoll(context, location);
};

const _registryUpdateAllDependencies = async (context) => {
const _registryCallForTenants = async (context, method, options = {}) => {
const { subscriptions } = await _registrySubscriptionsPaged(context);
const result = [];
// NOTE: we do this serially, so the logging output is understandable for users and the endpoint is not overloaded
for (const { consumerTenantId } of subscriptions.filter(({ state }) => TENANT_UPDATABLE_STATES.includes(state))) {
result.push(await _registryCall(context, consumerTenantId, "PATCH"));
result.push(await _registryCallForTenant(context, consumerTenantId, method, options));
}
return result;
};

const _registryUpdateApplicationURL = async (context, tenantId = null) => {
if (tenantId) {
return await _registryCall(context, tenantId, "PATCH", {
updateApplicationURL: true,
skipUpdatingDependencies: true,
doJobPoll: false,
});
} else {
const { subscriptions } = await _registrySubscriptionsPaged(context);
const result = [];
for (const { consumerTenantId } of subscriptions.filter(({ state }) => TENANT_UPDATABLE_STATES.includes(state))) {
result.push(
await _registryCall(context, consumerTenantId, "PATCH", {
updateApplicationURL: true,
skipUpdatingDependencies: true,
doJobPoll: false,
})
);
}
return result;
}
};

const registryUpdateDependencies = async (context, [tenantId]) => _registryCall(context, tenantId, "PATCH");
const registryUpdateDependencies = async (context, [tenantId]) => _registryCallForTenant(context, tenantId, "PATCH");

const registryUpdateAllDependencies = async (context) => _registryUpdateAllDependencies(context);
const registryUpdateAllDependencies = async (context) => _registryCallForTenants(context, "PATCH");

const registryUpdateApplicationURL = async (context, [tenantId]) => _registryUpdateApplicationURL(context, tenantId);
const registryUpdateApplicationURL = async (context, [tenantId]) =>
tenantId
? _registryCallForTenant(context, tenantId, "PATCH", {
updateApplicationURL: true,
skipUpdatingDependencies: true,
doJobPoll: false,
})
: _registryCallForTenants(context, "PATCH", {
updateApplicationURL: true,
skipUpdatingDependencies: true,
doJobPoll: false,
});

const registryOffboardSubscription = async (context, [tenantId]) => _registryCall(context, tenantId, "DELETE");
const registryOffboardSubscription = async (context, [tenantId]) => _registryCallForTenant(context, tenantId, "DELETE");

const registryOffboardSubscriptionSkip = async (context, [tenantId, skipApps]) =>
_registryCall(context, tenantId, "DELETE", { noCallbacksAppNames: skipApps });
_registryCallForTenant(context, tenantId, "DELETE", { noCallbacksAppNames: skipApps });

module.exports = {
registryListSubscriptions,
Expand Down
16 changes: 8 additions & 8 deletions test/tenantRegistry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,23 @@ describe("reg tests", () => {
[
"{
"tenantId": "5ecc7413-2b7e-414a-9496-ad4a61f6cccf",
"state": "SUCCESS"
"state": "SUCCEEDED"
}",
"{
"tenantId": "6917dfd6-7590-4033-af2a-140b75263b0d",
"state": "SUCCESS"
"state": "SUCCEEDED"
}",
"{
"tenantId": "cb9158ce-f8fd-441b-b443-17219e8f79fa",
"state": "SUCCESS"
"state": "SUCCEEDED"
}",
]
`);
expect(outputFromLoggerPartitionFetch(loggerSpy.info.mock.calls)).toMatchInlineSnapshot(`
"targeting cf api https://api.cf.sap.hana.ondemand.com / org "skyfin" / space "dev"
Subscription Operation with method PATCH for tenant 5ecc7413-2b7e-414a-9496-ad4a61f6cccf finished with state SUCCESS
Subscription Operation with method PATCH for tenant 6917dfd6-7590-4033-af2a-140b75263b0d finished with state SUCCESS
Subscription Operation with method PATCH for tenant cb9158ce-f8fd-441b-b443-17219e8f79fa finished with state SUCCESS
Subscription Operation with method PATCH for tenant 5ecc7413-2b7e-414a-9496-ad4a61f6cccf finished with state SUCCEEDED
Subscription Operation with method PATCH for tenant 6917dfd6-7590-4033-af2a-140b75263b0d finished with state SUCCEEDED
Subscription Operation with method PATCH for tenant cb9158ce-f8fd-441b-b443-17219e8f79fa finished with state SUCCEEDED
GET https://saas-manager.mesh.cf.sap.hana.ondemand.com/saas-manager/v1/application/subscriptions?appName=afc-dev&size=200&page=1 200 OK
PATCH https://saas-manager.mesh.cf.sap.hana.ondemand.com/saas-manager/v1/application/tenants/5ecc7413-2b7e-414a-9496-ad4a61f6cccf/subscriptions?updateApplicationURL=true&skipUpdatingDependencies=true 200 OK
Expand All @@ -265,12 +265,12 @@ describe("reg tests", () => {
expect(await reg.registryUpdateApplicationURL(await freshContext(), [testTenantId])).toMatchInlineSnapshot(`
"{
"tenantId": "5ecc7413-2b7e-414a-9496-ad4a61f6cccf",
"state": "SUCCESS"
"state": "SUCCEEDED"
}"
`);
expect(outputFromLoggerPartitionFetch(loggerSpy.info.mock.calls)).toMatchInlineSnapshot(`
"targeting cf api https://api.cf.sap.hana.ondemand.com / org "skyfin" / space "dev"
Subscription Operation with method PATCH for tenant 5ecc7413-2b7e-414a-9496-ad4a61f6cccf finished with state SUCCESS
Subscription Operation with method PATCH for tenant 5ecc7413-2b7e-414a-9496-ad4a61f6cccf finished with state SUCCEEDED
PATCH https://saas-manager.mesh.cf.sap.hana.ondemand.com/saas-manager/v1/application/tenants/5ecc7413-2b7e-414a-9496-ad4a61f6cccf/subscriptions?updateApplicationURL=true&skipUpdatingDependencies=true 200 OK"
`);
Expand Down

0 comments on commit 2b33505

Please sign in to comment.