diff --git a/src/lib/deviceHealthScripts/deviceHealthScripts.spec.ts b/src/lib/deviceHealthScripts/deviceHealthScripts.spec.ts new file mode 100644 index 0000000..c4880f2 --- /dev/null +++ b/src/lib/deviceHealthScripts/deviceHealthScripts.spec.ts @@ -0,0 +1,80 @@ +import { Client } from '@microsoft/microsoft-graph-client' +import { DeviceHealthScript, DeviceHealthScriptAssignment } from 'lib/types' +import { mockClient } from '../../../__fixtures__/@microsoft/microsoft-graph-client' +import { DeviceHealthScripts } from './deviceHealthScripts' + +describe('Device Health Scripts', () => { + let graphClient: Client + let deviceHealthScripts: DeviceHealthScripts + const deviceHealthScript = { + name: 'test', + '@odata.type': '#microsoft.graph.deviceHealthScript', + id: '1', + } as DeviceHealthScript + + const assignment = { + '@odata.type': '#microsoft.graph.deviceHealthScriptAssignment', + targetGroupId: '1', + } as DeviceHealthScriptAssignment + + beforeEach(() => { + graphClient = mockClient() as never as Client + deviceHealthScripts = new DeviceHealthScripts(graphClient) + }) + + it('should get a device health script', async () => { + jest.spyOn(graphClient.api(''), 'get').mockResolvedValue(deviceHealthScript) + const result = await deviceHealthScripts.get('') + expect(result).toEqual(deviceHealthScript) + }) + + it('should list all device health scripts', async () => { + jest.spyOn(graphClient.api(''), 'get').mockResolvedValue({ + value: [deviceHealthScript], + }) + const result = await deviceHealthScripts.list() + expect(result).toEqual([deviceHealthScript]) + }) + + it('should create a device health script', async () => { + jest.spyOn(graphClient.api(''), 'post').mockResolvedValue(deviceHealthScript) + const result = await deviceHealthScripts.create(deviceHealthScript) + expect(result).toEqual(deviceHealthScript) + }) + + it('should update a device health script', async () => { + jest.spyOn(graphClient.api(''), 'patch') + const result = await deviceHealthScripts.update('id', deviceHealthScript) + expect(result).toBeUndefined() + }) + + it('should delete a device health script', async () => { + jest.spyOn(graphClient.api(''), 'delete') + const result = await deviceHealthScripts.delete('id') + expect(result).toBeUndefined() + }) + + it('should create an assignment', async () => { + jest.spyOn(graphClient.api(''), 'post').mockResolvedValue(assignment) + const result = await deviceHealthScripts.createAssignment('id', assignment) + expect(result).toEqual(assignment) + }) + + it('should list assignments', async () => { + jest.spyOn(graphClient.api(''), 'get').mockResolvedValue({ value: [assignment] }) + const result = await deviceHealthScripts.listAssignments('id') + expect(result).toEqual([assignment]) + }) + + it('should get an assignment', async () => { + jest.spyOn(graphClient.api(''), 'get').mockResolvedValue(assignment) + const result = await deviceHealthScripts.getAssignment('id', 'groupAssignmentId') + expect(result).toEqual(assignment) + }) + + it('should delete an assignment', async () => { + jest.spyOn(graphClient.api(''), 'delete') + const result = await deviceHealthScripts.deleteAssignment('id', 'groupId') + expect(result).toBeUndefined() + }) +}) diff --git a/src/lib/deviceHealthScripts/deviceHealthScripts.ts b/src/lib/deviceHealthScripts/deviceHealthScripts.ts new file mode 100644 index 0000000..fc02276 --- /dev/null +++ b/src/lib/deviceHealthScripts/deviceHealthScripts.ts @@ -0,0 +1,70 @@ +import { Client } from '@microsoft/microsoft-graph-client' +import { DeviceHealthScript, DeviceHealthScriptAssignment } from '../types' +export class DeviceHealthScripts { + constructor(private readonly graphClient: Client) {} + + async list() { + let res = await this.graphClient.api('/deviceManagement/deviceHealthScripts').get() + const scripts: DeviceHealthScript[] = 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 nextScripts = res.value as DeviceHealthScript[] + scripts.push(...nextScripts) + } + return scripts + } + + async get(id: string): Promise { + return await this.graphClient.api(`/deviceManagement/deviceHealthScripts/${id}`).get() + } + + async create(script: DeviceHealthScript): Promise { + return await this.graphClient.api('/deviceManagement/deviceHealthScripts').post(script) + } + + async update(id: string, script: DeviceHealthScript): Promise { + return await this.graphClient + .api(`/deviceManagement/deviceHealthScripts/${id}`) + .patch(script) + } + + async delete(id: string): Promise { + await this.graphClient.api(`/deviceManagement/deviceHealthScripts/${id}`).delete() + } + + async createAssignment( + id: string, + assignment: DeviceHealthScriptAssignment, + ): Promise { + return this.graphClient + .api(`/deviceManagement/deviceHealthScripts/${id}/assignments`) + .post(assignment) + } + + async listAssignments(id: string): Promise { + let res = await this.graphClient + .api(`/deviceManagement/deviceHealthScripts/${id}/assignments`) + .get() + const groupAssignments: DeviceHealthScriptAssignment[] = 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 nextGroupAssignments = res.value as DeviceHealthScriptAssignment[] + groupAssignments.push(...nextGroupAssignments) + } + return groupAssignments + } + + async deleteAssignment(id: string, assignmentId: string): Promise { + await this.graphClient + .api(`/deviceManagement/deviceHealthScripts/${id}/assignments/${assignmentId}`) + .delete() + } + + async getAssignment(id: string, assignmentId: string): Promise { + return this.graphClient + .api(`/deviceManagement/deviceHealthScripts/${id}/assignments/${assignmentId}`) + .get() + } +} diff --git a/src/lib/deviceManagementScripts/deviceManagementScripts.spec.ts b/src/lib/deviceManagementScripts/deviceManagementScripts.spec.ts index 0e7749c..f189560 100644 --- a/src/lib/deviceManagementScripts/deviceManagementScripts.spec.ts +++ b/src/lib/deviceManagementScripts/deviceManagementScripts.spec.ts @@ -2,7 +2,7 @@ import { Client } from '@microsoft/microsoft-graph-client' import { DeviceManagementScripts } from './deviceManagementScripts' import { mockClient } from '../../../__fixtures__/@microsoft/microsoft-graph-client' -describe('Devices Managament Scripts', () => { +describe('Device Management Scripts', () => { let graphClient: Client let deviceManagementScripts: DeviceManagementScripts const deviceManagementScript = { diff --git a/src/lib/intune.ts b/src/lib/intune.ts index aa85977..91578a8 100644 --- a/src/lib/intune.ts +++ b/src/lib/intune.ts @@ -13,6 +13,7 @@ import { CustomRequest } from './customRequest/customRequest' import { Autopilot } from './autopilot/autopilot' import { DeviceManagementTemplates } from './deviceManagementTemplates/deviceManagementTemplates' import { DeviceManagementIntents } from './deviceManagementIntents/deviceManagementIntents' +import { DeviceHealthScripts } from './deviceHealthScripts/deviceHealthScripts' require('isomorphic-fetch') export class Intune { @@ -24,6 +25,8 @@ export class Intune { readonly deviceConfigurations: DeviceConfigurations + readonly deviceHealthScripts: DeviceHealthScripts + readonly deviceManagementScripts: DeviceManagementScripts readonly mobileApps: MobileApps @@ -66,5 +69,6 @@ export class Intune { this.autopilot = new Autopilot(this.graphclient) this.deviceManagementTemplates = new DeviceManagementTemplates(this.graphclient) this.deviceManagementIntents = new DeviceManagementIntents(this.graphclient) + this.deviceHealthScripts = new DeviceHealthScripts(this.graphclient) } } diff --git a/src/lib/types.ts b/src/lib/types.ts index f296065..67c6804 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -171,3 +171,11 @@ export interface CreateTemplateInstance { export interface DeviceManagementIntent extends Graph.DeviceManagementIntent { '@odata.type': '#microsoft.graph.deviceManagementIntent' } + +export interface DeviceHealthScript extends Graph.DeviceHealthScript { + '@odata.type': '#microsoft.graph.deviceHealthScript' +} + +export interface DeviceHealthScriptAssignment extends Graph.DeviceHealthScriptAssignment { + '@odata.type': '#microsoft.graph.deviceHealthScriptAssignment' +}