Skip to content

Commit

Permalink
feat: Add support for device health scripts (#121)
Browse files Browse the repository at this point in the history
* feat: Add support for device health scripts

* chore: fix typos
  • Loading branch information
santese authored Sep 13, 2022
1 parent 72d80c1 commit 8949201
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
80 changes: 80 additions & 0 deletions src/lib/deviceHealthScripts/deviceHealthScripts.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
70 changes: 70 additions & 0 deletions src/lib/deviceHealthScripts/deviceHealthScripts.ts
Original file line number Diff line number Diff line change
@@ -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<DeviceHealthScript> {
return await this.graphClient.api(`/deviceManagement/deviceHealthScripts/${id}`).get()
}

async create(script: DeviceHealthScript): Promise<DeviceHealthScript> {
return await this.graphClient.api('/deviceManagement/deviceHealthScripts').post(script)
}

async update(id: string, script: DeviceHealthScript): Promise<DeviceHealthScript> {
return await this.graphClient
.api(`/deviceManagement/deviceHealthScripts/${id}`)
.patch(script)
}

async delete(id: string): Promise<void> {
await this.graphClient.api(`/deviceManagement/deviceHealthScripts/${id}`).delete()
}

async createAssignment(
id: string,
assignment: DeviceHealthScriptAssignment,
): Promise<DeviceHealthScriptAssignment> {
return this.graphClient
.api(`/deviceManagement/deviceHealthScripts/${id}/assignments`)
.post(assignment)
}

async listAssignments(id: string): Promise<DeviceHealthScriptAssignment[]> {
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<void> {
await this.graphClient
.api(`/deviceManagement/deviceHealthScripts/${id}/assignments/${assignmentId}`)
.delete()
}

async getAssignment(id: string, assignmentId: string): Promise<DeviceHealthScriptAssignment> {
return this.graphClient
.api(`/deviceManagement/deviceHealthScripts/${id}/assignments/${assignmentId}`)
.get()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/intune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,6 +25,8 @@ export class Intune {

readonly deviceConfigurations: DeviceConfigurations

readonly deviceHealthScripts: DeviceHealthScripts

readonly deviceManagementScripts: DeviceManagementScripts

readonly mobileApps: MobileApps
Expand Down Expand Up @@ -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)
}
}
8 changes: 8 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

0 comments on commit 8949201

Please sign in to comment.