Skip to content

Commit

Permalink
upcoming: [M3-9224] - Limits evolution: revise types, queries and moc…
Browse files Browse the repository at this point in the history
…k data (#11597)

* Initial commit: adjust type & responses

* better mock data

* Changesets
  • Loading branch information
abailly-akamai authored Feb 4, 2025
1 parent 2bfcd65 commit 8af77d7
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/api-v4": Upcoming Features
---

Add support for quotas usage endpoint ([#11597](https://github.com/linode/manager/pull/11597))
16 changes: 15 additions & 1 deletion packages/api-v4/src/quotas/quotas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Filter, Params, ResourcePage as Page } from 'src/types';
import { API_ROOT } from '../constants';
import Request, { setMethod, setParams, setURL, setXFilter } from '../request';
import { Quota, QuotaType } from './types';
import { Quota, QuotaType, QuotaUsage } from './types';

/**
* getQuota
Expand Down Expand Up @@ -34,3 +34,17 @@ export const getQuotas = (
setXFilter(filter),
setParams(params)
);

/**
* getQuotaUsage
*
* Returns the usage for a single quota within a particular service specified by `type`.
*
* @param type { QuotaType } retrieve a quota within this service type.
* @param id { number } the quota ID to look up.
*/
export const getQuotaUsage = (type: QuotaType, id: number) =>
Request<QuotaUsage>(
setURL(`${API_ROOT}/${type}/quotas/${id}/usage`),
setMethod('GET')
);
24 changes: 18 additions & 6 deletions packages/api-v4/src/quotas/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ export interface Quota {
*/
quota_limit: number;

/**
* Current account usage, measured in units specified by the
* `resource_metric` field.
*/
used: number;

/**
* The unit of measurement for this service limit.
*/
Expand Down Expand Up @@ -69,4 +63,22 @@ export interface Quota {
s3_endpoint?: string;
}

/**
* A usage limit for a given Quota based on service metrics such
* as vCPUs, instances or storage size.
*/
export interface QuotaUsage {
/**
* The account-wide limit for this service, measured in units
* specified by the `resource_metric` field.
*/
quota_limit: number;

/**
* The current account usage, measured in units specified by the
* `resource_metric` field.
*/
used: number;
}

export type QuotaType = 'linode' | 'lke' | 'object-storage';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Upcoming Features
---

Add support for quotas usage endpoint ([#11597](https://github.com/linode/manager/pull/11597))
6 changes: 5 additions & 1 deletion packages/manager/src/factories/quotas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Factory from 'src/factories/factoryProxy';

import type { Quota } from '@linode/api-v4/lib/quotas/types';
import type { Quota, QuotaUsage } from '@linode/api-v4/lib/quotas/types';

export const quotaFactory = Factory.Sync.makeFactory<Quota>({
description: 'Maximimum number of vCPUs allowed',
Expand All @@ -9,5 +9,9 @@ export const quotaFactory = Factory.Sync.makeFactory<Quota>({
quota_name: 'Linode Dedicated vCPUs',
region_applied: 'us-east',
resource_metric: 'CPU',
});

export const quotaUsageFactory = Factory.Sync.makeFactory<QuotaUsage>({
quota_limit: 50,
used: 25,
});
60 changes: 51 additions & 9 deletions packages/manager/src/mocks/presets/crud/handlers/quotas.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { http } from 'msw';

import { quotaFactory } from 'src/factories/quotas';
import { quotaFactory, quotaUsageFactory } from 'src/factories/quotas';
import {
makeNotFoundResponse,
makePaginatedResponse,
makeResponse,
} from 'src/mocks/utilities/response';
import { pickRandom } from 'src/utilities/random';

import type { Quota, QuotaType } from '@linode/api-v4';
import type { Quota, QuotaType, QuotaUsage } from '@linode/api-v4';
import type { StrictResponse } from 'msw';
import type {
APIErrorResponse,
Expand All @@ -23,31 +24,27 @@ const mockQuotas: Record<QuotaType, Quota[]> = {
quota_name: 'Dedicated CPU',
region_applied: 'us-east',
resource_metric: 'CPU',
used: 8,
}),
quotaFactory.build({
description: 'Max number of vCPUs assigned to Linodes with Shared plans',
quota_limit: 25,
quota_name: 'Shared CPU',
region_applied: 'us-east',
resource_metric: 'CPU',
used: 22,
}),
quotaFactory.build({
description: 'Max number of GPUs assigned to Linodes with GPU plans',
quota_limit: 10,
quota_name: 'GPU',
region_applied: 'us-east',
resource_metric: 'GPU',
used: 5,
}),
quotaFactory.build({
description: 'Max number of VPUs assigned to Linodes with VPU plans',
quota_limit: 100,
quota_name: 'VPU',
region_applied: 'us-east',
resource_metric: 'VPU',
used: 20,
}),
quotaFactory.build({
description:
Expand All @@ -56,7 +53,6 @@ const mockQuotas: Record<QuotaType, Quota[]> = {
quota_name: 'High Memory',
region_applied: 'us-east',
resource_metric: 'CPU',
used: 0,
}),
],
lke: [
Expand All @@ -65,7 +61,6 @@ const mockQuotas: Record<QuotaType, Quota[]> = {
quota_name: 'Total number of Clusters',
region_applied: 'us-east',
resource_metric: 'cluster',
used: 12,
}),
],
'object-storage': [
Expand All @@ -75,7 +70,6 @@ const mockQuotas: Record<QuotaType, Quota[]> = {
quota_name: 'Total Capacity',
resource_metric: 'byte',
s3_endpoint: 'us-east-1.linodeobjects.com',
used: 900_000_000_000_000,
}),
quotaFactory.build({
endpoint_type: 'E3',
Expand Down Expand Up @@ -124,4 +118,52 @@ export const getQuotas = () => [
return makeResponse(quota);
}
),

http.get(
'*/v4/:service/quotas/:id/usage',
async ({
params,
}): Promise<StrictResponse<APIErrorResponse | QuotaUsage>> => {
const service = params.service as QuotaType;
const quota = mockQuotas[service].find(
({ quota_id }) => quota_id === +params.id
);

if (!quota) {
return makeNotFoundResponse();
}

switch (service) {
case 'linode':
return makeResponse(
quotaUsageFactory.build({
quota_limit: quota.quota_limit,
used: pickRandom([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
})
);
case 'lke':
return makeResponse(
quotaUsageFactory.build({
quota_limit: quota.quota_limit,
used: pickRandom([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
})
);
case 'object-storage':
return makeResponse(
quotaUsageFactory.build({
quota_limit: quota.quota_limit,
used: pickRandom([
0,
100_000_000_000_000,
200_000_000_000_000,
300_000_000_000_000,
400_000_000_000_000,
]),
})
);
default:
return makeNotFoundResponse();
}
}
),
];
17 changes: 16 additions & 1 deletion packages/manager/src/queries/quotas/quotas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getQuota, getQuotas } from '@linode/api-v4';
import { getQuota, getQuotaUsage, getQuotas } from '@linode/api-v4';
import { createQueryKeys } from '@lukemorales/query-key-factory';
import { keepPreviousData, useQuery } from '@tanstack/react-query';

Expand All @@ -10,6 +10,7 @@ import type {
Params,
Quota,
QuotaType,
QuotaUsage,
ResourcePage,
} from '@linode/api-v4';

Expand All @@ -28,6 +29,10 @@ export const quotaQueries = createQueryKeys('quotas', {
queryFn: () => getQuota(type, id),
queryKey: [id],
}),
usage: (id: number) => ({
queryFn: () => getQuotaUsage(type, id),
queryKey: [id],
}),
},
queryKey: [type],
}),
Expand Down Expand Up @@ -61,3 +66,13 @@ export const useAllQuotasQuery = (
...quotaQueries.service(service)._ctx.all(params, filter),
enabled,
});

export const useQuotaUsageQuery = (
service: QuotaType,
id: number,
enabled = true
) =>
useQuery<QuotaUsage, APIError[]>({
...quotaQueries.service(service)._ctx.usage(id),
enabled,
});

0 comments on commit 8af77d7

Please sign in to comment.