From ad95e65adec6f8e35a854bc121f1badfc30d7cb8 Mon Sep 17 00:00:00 2001 From: Santese Smith <43831817+santese@users.noreply.github.com> Date: Mon, 15 Aug 2022 13:44:34 -0700 Subject: [PATCH] feat: add support for device management intents (#119) * feat: add support for device management intents * fix: fix endpoint for intents --- .../deviceManagementIntents.spec.ts | 61 +++++++++++++++++++ .../deviceManagementIntents.ts | 38 ++++++++++++ src/lib/intune.ts | 4 ++ src/lib/types.ts | 4 ++ 4 files changed, 107 insertions(+) create mode 100644 src/lib/deviceManagementIntents/deviceManagementIntents.spec.ts create mode 100644 src/lib/deviceManagementIntents/deviceManagementIntents.ts diff --git a/src/lib/deviceManagementIntents/deviceManagementIntents.spec.ts b/src/lib/deviceManagementIntents/deviceManagementIntents.spec.ts new file mode 100644 index 0000000..de1cf34 --- /dev/null +++ b/src/lib/deviceManagementIntents/deviceManagementIntents.spec.ts @@ -0,0 +1,61 @@ +import { Client } from '@microsoft/microsoft-graph-client' +import { DeviceManagementIntent } from 'lib/types' +import { mockClient } from '../../../__fixtures__/@microsoft/microsoft-graph-client' +import { DeviceManagementIntents } from './deviceManagementIntents' + +describe('DeviceManagementIntents', () => { + let graphClient: Client + let deviceManagementIntents: DeviceManagementIntents + const intent = { + displayName: 'test', + id: 'test', + } as DeviceManagementIntent + + beforeEach(() => { + graphClient = mockClient() as never as Client + deviceManagementIntents = new DeviceManagementIntents(graphClient) + }) + + it('should get a intent', async () => { + jest.spyOn(graphClient.api(''), 'get').mockResolvedValue(intent) + const result = await deviceManagementIntents.get('') + expect(result).toEqual(intent) + }) + + it('should list all intents', async () => { + jest.spyOn(graphClient.api(''), 'get').mockResolvedValueOnce({ + value: [intent], + '@odata.nextLink': 'next', + }) + jest.spyOn(graphClient.api(''), 'get').mockResolvedValueOnce({ + value: [intent], + }) + + const result = await deviceManagementIntents.list() + expect(result).toEqual([intent, intent]) + }) + + it('should update a intent', async () => { + const postSpy = jest.spyOn(graphClient.api(''), 'patch').mockResolvedValue(intent) + const result = await deviceManagementIntents.update('id', intent) + expect(result).toEqual(intent) + expect(postSpy).toHaveBeenCalledWith(intent) + }) + + it('should delete a intent', async () => { + const spy = jest.spyOn(graphClient, 'api') + jest.spyOn(graphClient.api(''), 'patch') + const result = await deviceManagementIntents.delete('id') + expect(result).toBeUndefined() + expect(spy).toHaveBeenCalledWith('/deviceManagement/intents/id') + }) + + it('should create a intent', async () => { + const apiSpy = jest.spyOn(graphClient, 'api') + const postSpy = jest.spyOn(graphClient.api(''), 'post').mockResolvedValue(intent) + const result = await deviceManagementIntents.create(intent) + expect(result).toEqual(intent) + expect(apiSpy).toHaveBeenCalledWith('/deviceManagement/intents') + expect(postSpy).toHaveBeenCalledWith(intent) + }) +}) diff --git a/src/lib/deviceManagementIntents/deviceManagementIntents.ts b/src/lib/deviceManagementIntents/deviceManagementIntents.ts new file mode 100644 index 0000000..063019e --- /dev/null +++ b/src/lib/deviceManagementIntents/deviceManagementIntents.ts @@ -0,0 +1,38 @@ +import { Client } from '@microsoft/microsoft-graph-client' +import { Group } from '@microsoft/microsoft-graph-types-beta' +import { DeviceManagementIntent } from 'lib/types' + +export class DeviceManagementIntents { + constructor(private readonly graphClient: Client) {} + + async list() { + let res = await this.graphClient.api('/deviceManagement/intents').get() + const intents: Group[] = res.value + while (res['@odata.nextLink']) { + const nextLink = res['@odata.nextLink'].replace('https://graph.microsoft.com/beta', '') + res = await this.graphClient.api(nextLink).get() + const nextIntents = res.value as DeviceManagementIntent[] + intents.push(...nextIntents) + } + return intents + } + + async get(intentId: string): Promise { + return this.graphClient.api(`/deviceManagement/intents/${intentId}`).get() + } + + async update( + intentId: string, + intent: DeviceManagementIntent, + ): Promise { + return this.graphClient.api(`/deviceManagement/intents/${intentId}`).patch(intent) + } + + async delete(intentId: string): Promise { + return this.graphClient.api(`/deviceManagement/intents/${intentId}`).delete() + } + + async create(intent: DeviceManagementIntent): Promise { + return this.graphClient.api('/deviceManagement/intents').post(intent) + } +} diff --git a/src/lib/intune.ts b/src/lib/intune.ts index e976a00..aa85977 100644 --- a/src/lib/intune.ts +++ b/src/lib/intune.ts @@ -12,6 +12,7 @@ import { GroupPolicyConfigurations } from './groupPolicyConfigurations/groupPoli import { CustomRequest } from './customRequest/customRequest' import { Autopilot } from './autopilot/autopilot' import { DeviceManagementTemplates } from './deviceManagementTemplates/deviceManagementTemplates' +import { DeviceManagementIntents } from './deviceManagementIntents/deviceManagementIntents' require('isomorphic-fetch') export class Intune { @@ -37,6 +38,8 @@ export class Intune { readonly deviceManagementTemplates: DeviceManagementTemplates + readonly deviceManagementIntents: DeviceManagementIntents + constructor(private readonly config: Config) { const credential = new ClientSecretCredential( this.config.tenantId, @@ -62,5 +65,6 @@ export class Intune { this.customRequest = new CustomRequest(this.graphclient) this.autopilot = new Autopilot(this.graphclient) this.deviceManagementTemplates = new DeviceManagementTemplates(this.graphclient) + this.deviceManagementIntents = new DeviceManagementIntents(this.graphclient) } } diff --git a/src/lib/types.ts b/src/lib/types.ts index fec37b2..f296065 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -157,6 +157,10 @@ export interface DeviceManagementSettingInstance extends Graph.DeviceManagementS '@odata.type': '#microsoft.graph.deviceManagementSettingInstance' } +export interface DeviceManagementIntent extends Graph.DeviceManagementIntent { + '@odata.type': '#microsoft.graph.deviceManagementIntent' +} + export interface CreateTemplateInstance { displayName: string description: string