Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fb/better service plan resolving and object-store option #102

Merged
merged 6 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/cliOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const FLAG_ARG = Object.freeze({
SKIP_UNCHANGED: "--skip-unchanged",
ONLY_STALE: "--only-stale",
ONLY_FAILED: "--only-failed",
OBJECT_STORE: "--object-store",
});

const USAGE = `usage: ${NAME} [command]
Expand Down Expand Up @@ -304,15 +305,15 @@ const APP_CLI_OPTIONS = Object.freeze({
HDI_LIST: {
commandVariants: ["hdil", "--hdi-list"],
optionalPassArgs: [PASS_ARG.TENANT_ID],
optionalFlagArgs: [FLAG_ARG.TIMESTAMPS, FLAG_ARG.JSON_OUTPUT],
optionalFlagArgs: [FLAG_ARG.OBJECT_STORE, FLAG_ARG.JSON_OUTPUT, FLAG_ARG.TIMESTAMPS],
callback: hdi.hdiList,
useCache: false,
readonly: true,
},
HDI_LONG_LIST: {
commandVariants: ["hdill", "--hdi-long-list"],
optionalPassArgs: [PASS_ARG.TENANT_ID],
optionalFlagArgs: [FLAG_ARG.JSON_OUTPUT, FLAG_ARG.REVEAL],
optionalFlagArgs: [FLAG_ARG.OBJECT_STORE, FLAG_ARG.JSON_OUTPUT, FLAG_ARG.REVEAL],
callback: hdi.hdiLongList,
useCache: false,
readonly: true,
Expand Down
70 changes: 48 additions & 22 deletions src/submodules/hanaManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const HIDDEN_PASSWORD_TEXT = "*** show with --reveal ***";
const SERVICE_MANAGER_REQUEST_CONCURRENCY_FALLBACK = 10;
const SERVICE_MANAGER_IDEAL_BINDING_COUNT = 1;
const SENSITIVE_CREDENTIAL_FIELDS = ["password", "hdi_password"];
const HDI_SHARED_SERVICE_PLAN_NAME = "hdi-shared";
const HDI_SHARED_SERVICE_OFFERING = "hana";
const HDI_SHARED_SERVICE_PLAN = "hdi-shared";
const OBJECT_STORE_SERVICE_OFFERING = "objectstore";
const OBJECT_STORE_SERVICE_PLAN = "standard";

const logger = Logger.getInstance();

Expand Down Expand Up @@ -126,36 +129,55 @@ const _getQuery = (filters) =>
}, [])
.join(" and ");

const _getServicePlanId = async (sm_url, token, servicePlanName) => {
const response = await request({
const _getServicePlanId = async (sm_url, token, serviceOfferingName, servicePlanName) => {
const responseOfferings = await request({
url: sm_url,
pathname: "/v1/service_offerings",
query: { fieldQuery: _getQuery({ name: serviceOfferingName }) },
auth: { token },
});
const responseOfferingsData = (await responseOfferings.json()) || {};
const serviceOfferingId = responseOfferingsData.items?.[0]?.id;
assert(serviceOfferingId, `could not find service offering with name ${serviceOfferingName}`);
const responsePlans = await request({
url: sm_url,
pathname: "/v1/service_plans",
query: { fieldQuery: _getQuery({ name: servicePlanName }) },
query: { fieldQuery: _getQuery({ service_offering_id: serviceOfferingId, name: servicePlanName }) },
auth: { token },
});
const responseData = (await response.json()) || {};
const plans = responseData.items || [];
const servicePlanId = plans[0]?.id;
const responsePlansData = (await responsePlans.json()) || {};
const servicePlanId = responsePlansData.items?.[0]?.id;
assert(servicePlanId, `could not find service plan with name ${servicePlanName}`);
return servicePlanId;
};

const _getHdiSharedPlanId = makeOneTime(
async (sm_url, token) => await _getServicePlanId(sm_url, token, HDI_SHARED_SERVICE_PLAN_NAME)
async (sm_url, token) => await _getServicePlanId(sm_url, token, HDI_SHARED_SERVICE_OFFERING, HDI_SHARED_SERVICE_PLAN)
);

const _hdiInstancesServiceManager = async (context, { filterTenantId, doEnsureTenantLabel = true } = {}) => {
const _getObjectStorePlanId = makeOneTime(
async (sm_url, token) =>
await _getServicePlanId(sm_url, token, OBJECT_STORE_SERVICE_OFFERING, OBJECT_STORE_SERVICE_PLAN)
);

const _hdiInstancesServiceManager = async (
context,
{ filterTenantId, doObjectStore, doEnsureTenantLabel = true } = {}
) => {
const {
cfService: { credentials },
} = await context.getHdiInfo();
const { sm_url } = credentials;
const token = await context.getCachedUaaTokenFromCredentials(credentials);
const servicePlanId = await (doObjectStore
? _getObjectStorePlanId(sm_url, token)
: _getHdiSharedPlanId(sm_url, token));

const response = await request({
url: sm_url,
pathname: "/v1/service_instances",
query: {
fieldQuery: _getQuery({ service_plan_id: await _getHdiSharedPlanId(sm_url, token) }),
fieldQuery: _getQuery({ service_plan_id: servicePlanId }),
...(filterTenantId && { labelQuery: _getQuery({ tenant_id: filterTenantId }) }),
},
auth: { token },
Expand All @@ -170,20 +192,23 @@ const _hdiInstancesServiceManager = async (context, { filterTenantId, doEnsureTe

const _hdiBindingsServiceManager = async (
context,
{ filterTenantId, doReveal = false, doAssertFoundSome = false, doEnsureTenantLabel = true } = {}
{ filterTenantId, doObjectStore, doReveal = false, doAssertFoundSome = false, doEnsureTenantLabel = true } = {}
) => {
const {
cfService: { credentials },
} = await context.getHdiInfo();
const { sm_url } = credentials;
const token = await context.getCachedUaaTokenFromCredentials(credentials);
const servicePlanId = await (doObjectStore
? _getObjectStorePlanId(sm_url, token)
: _getHdiSharedPlanId(sm_url, token));

const getBindingsResponse = await request({
url: sm_url,
pathname: "/v1/service_bindings",
query: {
labelQuery: _getQuery({
service_plan_id: await _getHdiSharedPlanId(sm_url, token),
service_plan_id: servicePlanId,
...(filterTenantId && { tenant_id: filterTenantId }),
}),
},
Expand Down Expand Up @@ -422,10 +447,10 @@ const _getBindingsByInstance = (bindings) => {
}, {});
};

const hdiListServiceManager = async (context, { filterTenantId, doTimestamps, doJsonOutput } = {}) => {
const _hdiListServiceManager = async (context, { filterTenantId, doObjectStore, doJsonOutput, doTimestamps } = {}) => {
const [instances, bindings] = await Promise.all([
_hdiInstancesServiceManager(context, { filterTenantId }),
_hdiBindingsServiceManager(context, { filterTenantId }),
_hdiInstancesServiceManager(context, { filterTenantId, doObjectStore }),
_hdiBindingsServiceManager(context, { filterTenantId, doObjectStore }),
]);

if (doJsonOutput) {
Expand Down Expand Up @@ -456,13 +481,13 @@ const hdiListServiceManager = async (context, { filterTenantId, doTimestamps, do
return tableList(table, { withRowNumber: !filterTenantId });
};

const hdiList = async (context, [filterTenantId], [doTimestamps, doJsonOutput]) =>
await hdiListServiceManager(context, { filterTenantId, doTimestamps, doJsonOutput });
const hdiList = async (context, [filterTenantId], [doObjectStore, doJsonOutput, doTimestamps]) =>
await _hdiListServiceManager(context, { filterTenantId, doObjectStore, doJsonOutput, doTimestamps });

const _hdiLongListServiceManager = async (context, { filterTenantId, doJsonOutput, doReveal } = {}) => {
const _hdiLongListServiceManager = async (context, { filterTenantId, doObjectStore, doJsonOutput, doReveal } = {}) => {
const [instances, bindings] = await Promise.all([
_hdiInstancesServiceManager(context, { filterTenantId, doEnsureTenantLabel: false }),
_hdiBindingsServiceManager(context, { filterTenantId, doReveal, doEnsureTenantLabel: false }),
_hdiInstancesServiceManager(context, { filterTenantId, doObjectStore, doEnsureTenantLabel: false }),
_hdiBindingsServiceManager(context, { filterTenantId, doObjectStore, doReveal, doEnsureTenantLabel: false }),
]);

if (doJsonOutput) {
Expand All @@ -479,8 +504,8 @@ ${_formatOutput(bindings)}
`;
};

const hdiLongList = async (context, [filterTenantId], [doJsonOutput, doReveal]) =>
await _hdiLongListServiceManager(context, { filterTenantId, doJsonOutput, doReveal });
const hdiLongList = async (context, [filterTenantId], [doObjectStore, doJsonOutput, doReveal]) =>
await _hdiLongListServiceManager(context, { filterTenantId, doObjectStore, doJsonOutput, doReveal });

const _hdiListRelationsServiceManager = async (context, { filterTenantId, doTimestamps, doJsonOutput }) => {
const [instances, bindings] = await Promise.all([
Expand Down Expand Up @@ -665,6 +690,7 @@ module.exports = {
_: {
_reset() {
resetOneTime(_getHdiSharedPlanId);
resetOneTime(_getObjectStorePlanId);
},
},
};
Loading