Skip to content

Commit

Permalink
feat: [UIE-8128] - add new account api with (#11181)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaleksee-akamai authored Nov 14, 2024
1 parent 8ee5281 commit 1d0051e
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/api-v4/.changeset/pr-11181-added-1730207508835.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/api-v4": Added
---

New IAM endpoints and types ([#11181](https://github.com/linode/manager/pull/11181))
15 changes: 14 additions & 1 deletion packages/api-v4/src/iam/iam.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BETA_API_ROOT } from '../constants';
import Request, { setData, setMethod, setURL } from '../request';
import { IamUserPermissions } from './types';
import { IamUserPermissions, IamAccountPermissions } from './types';

/**
* getUserPermissions
Expand Down Expand Up @@ -42,3 +42,16 @@ export const updateUserPermissions = (
setMethod('PUT'),
setData(data)
);

/**
* getAccountPermissions
*
* Return all permissions for account.
*
*/
export const getAccountPermissions = () => {
return Request<IamAccountPermissions>(
setURL(`${BETA_API_ROOT}/iam/role-permissions`),
setMethod('GET')
);
};
23 changes: 23 additions & 0 deletions packages/api-v4/src/iam/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,26 @@ export interface ResourceAccess {
resource_type: ResourceType;
roles: RoleType[];
}

export interface IamAccountPermissions {
account_access: Access[];
resource_access: Access[];
}

type PermissionType =
| 'create_linode'
| 'update_linode'
| 'update_firewall'
| 'delete_linode'
| 'view_linode';

interface Access {
resource_type: ResourceType;
roles: Roles[];
}

export interface Roles {
name: string;
description: string;
permissions?: PermissionType[];
}
2 changes: 2 additions & 0 deletions packages/api-v4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export * from './betas';

export * from './iam';

export * from './resources';

export {
baseRequest,
setToken,
Expand Down
3 changes: 3 additions & 0 deletions packages/api-v4/src/resources/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './resources';

export * from './types';
16 changes: 16 additions & 0 deletions packages/api-v4/src/resources/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BETA_API_ROOT } from '../constants';
import Request, { setMethod, setURL } from '../request';
import { IamAccountResource } from './types';

/**
* getAccountResources
*
* Return all resources for account.
*
*/
export const getAccountResources = () => {
return Request<IamAccountResource>(
setURL(`${BETA_API_ROOT}/resources`),
setMethod('GET')
);
};
21 changes: 21 additions & 0 deletions packages/api-v4/src/resources/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type ResourceType =
| 'linode'
| 'firewall'
| 'nodebalancer'
| 'longview'
| 'domain'
| 'stackscript'
| 'image'
| 'volume'
| 'database'
| 'vpc';

export type IamAccountResource = {
resource_type: ResourceType;
resources: Resource[];
}[];

export interface Resource {
name: string;
id: number;
}
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-11181-added-1730207569813.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Added
---

mock data and query for new account api ([#11181](https://github.com/linode/manager/pull/11181))
66 changes: 66 additions & 0 deletions packages/manager/src/factories/accountPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { IamAccountPermissions } from '@linode/api-v4';
import Factory from 'src/factories/factoryProxy';

export const accountPermissionsFactory = Factory.Sync.makeFactory<IamAccountPermissions>(
{
account_access: [
{
resource_type: 'account',
roles: [
{
name: 'account_admin',
description:
'Access to perform any supported action on all resources in the account',
permissions: ['create_linode', 'update_linode', 'update_firewall'],
},
],
},
{
resource_type: 'linode',
roles: [
{
name: 'account_linode_admin',
description:
'Access to perform any supported action on all linode instances in the account',
permissions: ['create_linode', 'update_linode', 'delete_linode'],
},
],
},
{
resource_type: 'firewall',
roles: [
{
name: 'firewall_creator',
description: 'Access to create a firewall instance',
},
],
},
],
resource_access: [
{
resource_type: 'linode',
roles: [
{
name: 'linode_contributor',
description: 'Access to update a linode instance',
permissions: ['update_linode', 'view_linode'],
},
],
},
{
resource_type: 'firewall',
roles: [
{
name: 'firewall_viewer',
description: 'Access to view a firewall instance',
},
{
name: 'firewall_admin',
description:
'Access to perform any supported action on a firewall instance',
},
],
},
],
}
);
29 changes: 29 additions & 0 deletions packages/manager/src/factories/accountResources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { IamAccountResource } from '@linode/api-v4';
import Factory from 'src/factories/factoryProxy';

export const accountResourcesFactory = Factory.Sync.makeFactory<IamAccountResource>(
[
{
resource_type: 'linode',
resources: [
{
name: 'debian-us-123',
id: 12345678,
},
{
name: 'linode-uk-123',
id: 23456789,
},
],
},
{
resource_type: 'firewall',
resources: [
{
name: 'firewall-us-123',
id: 45678901,
},
],
},
]
);
19 changes: 18 additions & 1 deletion packages/manager/src/queries/iam/iam.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import { APIError, IamUserPermissions } from '@linode/api-v4';
import {
APIError,
IamUserPermissions,
IamAccountPermissions,
} from '@linode/api-v4';
import { iamQueries } from './queries';
import { useQuery } from '@tanstack/react-query';
import { useProfile } from 'src/queries/profile/profile';
import { queryPresets } from '../base';

export const useAccountUserPermissions = (username: string) => {
return useQuery<IamUserPermissions, APIError[]>(
iamQueries.user(username)._ctx.permissions
);
};

export const useAccountPermissions = () => {
const { data: profile } = useProfile();

return useQuery<IamAccountPermissions, APIError[]>({
...iamQueries.permissions,
...queryPresets.oneTimeFetch,
...queryPresets.noRetry,
enabled: !profile?.restricted,
});
};
6 changes: 5 additions & 1 deletion packages/manager/src/queries/iam/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getUserPermissions } from '@linode/api-v4';
import { getUserPermissions, getAccountPermissions } from '@linode/api-v4';
import { createQueryKeys } from '@lukemorales/query-key-factory';

export const iamQueries = createQueryKeys('iam', {
Expand All @@ -11,4 +11,8 @@ export const iamQueries = createQueryKeys('iam', {
},
queryKey: [username],
}),
permissions: {
queryFn: getAccountPermissions,
queryKey: null,
},
});
9 changes: 9 additions & 0 deletions packages/manager/src/queries/resources/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getAccountResources } from '@linode/api-v4';
import { createQueryKeys } from '@lukemorales/query-key-factory';

export const resourcesQueries = createQueryKeys('resources', {
resources: {
queryFn: getAccountResources,
queryKey: null,
},
});
16 changes: 16 additions & 0 deletions packages/manager/src/queries/resources/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IamAccountResource, APIError } from '@linode/api-v4';
import { useQuery } from '@tanstack/react-query';
import { useProfile } from 'src/queries/profile/profile';
import { queryPresets } from '../base';
import { resourcesQueries } from './queries';

export const useAccountResources = () => {
const { data: profile } = useProfile();

return useQuery<IamAccountResource, APIError[]>({
...resourcesQueries.resources,
...queryPresets.oneTimeFetch,
...queryPresets.noRetry,
enabled: !profile?.restricted,
});
};

0 comments on commit 1d0051e

Please sign in to comment.