Skip to content

Commit ad95e65

Browse files
authored
feat: add support for device management intents (#119)
* feat: add support for device management intents * fix: fix endpoint for intents
1 parent 4e3251b commit ad95e65

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Client } from '@microsoft/microsoft-graph-client'
2+
import { DeviceManagementIntent } from 'lib/types'
3+
import { mockClient } from '../../../__fixtures__/@microsoft/microsoft-graph-client'
4+
import { DeviceManagementIntents } from './deviceManagementIntents'
5+
6+
describe('DeviceManagementIntents', () => {
7+
let graphClient: Client
8+
let deviceManagementIntents: DeviceManagementIntents
9+
const intent = {
10+
displayName: 'test',
11+
id: 'test',
12+
} as DeviceManagementIntent
13+
14+
beforeEach(() => {
15+
graphClient = mockClient() as never as Client
16+
deviceManagementIntents = new DeviceManagementIntents(graphClient)
17+
})
18+
19+
it('should get a intent', async () => {
20+
jest.spyOn(graphClient.api(''), 'get').mockResolvedValue(intent)
21+
const result = await deviceManagementIntents.get('')
22+
expect(result).toEqual(intent)
23+
})
24+
25+
it('should list all intents', async () => {
26+
jest.spyOn(graphClient.api(''), 'get').mockResolvedValueOnce({
27+
value: [intent],
28+
'@odata.nextLink': 'next',
29+
})
30+
jest.spyOn(graphClient.api(''), 'get').mockResolvedValueOnce({
31+
value: [intent],
32+
})
33+
34+
const result = await deviceManagementIntents.list()
35+
expect(result).toEqual([intent, intent])
36+
})
37+
38+
it('should update a intent', async () => {
39+
const postSpy = jest.spyOn(graphClient.api(''), 'patch').mockResolvedValue(intent)
40+
const result = await deviceManagementIntents.update('id', intent)
41+
expect(result).toEqual(intent)
42+
expect(postSpy).toHaveBeenCalledWith(intent)
43+
})
44+
45+
it('should delete a intent', async () => {
46+
const spy = jest.spyOn(graphClient, 'api')
47+
jest.spyOn(graphClient.api(''), 'patch')
48+
const result = await deviceManagementIntents.delete('id')
49+
expect(result).toBeUndefined()
50+
expect(spy).toHaveBeenCalledWith('/deviceManagement/intents/id')
51+
})
52+
53+
it('should create a intent', async () => {
54+
const apiSpy = jest.spyOn(graphClient, 'api')
55+
const postSpy = jest.spyOn(graphClient.api(''), 'post').mockResolvedValue(intent)
56+
const result = await deviceManagementIntents.create(intent)
57+
expect(result).toEqual(intent)
58+
expect(apiSpy).toHaveBeenCalledWith('/deviceManagement/intents')
59+
expect(postSpy).toHaveBeenCalledWith(intent)
60+
})
61+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Client } from '@microsoft/microsoft-graph-client'
2+
import { Group } from '@microsoft/microsoft-graph-types-beta'
3+
import { DeviceManagementIntent } from 'lib/types'
4+
5+
export class DeviceManagementIntents {
6+
constructor(private readonly graphClient: Client) {}
7+
8+
async list() {
9+
let res = await this.graphClient.api('/deviceManagement/intents').get()
10+
const intents: Group[] = res.value
11+
while (res['@odata.nextLink']) {
12+
const nextLink = res['@odata.nextLink'].replace('https://graph.microsoft.com/beta', '')
13+
res = await this.graphClient.api(nextLink).get()
14+
const nextIntents = res.value as DeviceManagementIntent[]
15+
intents.push(...nextIntents)
16+
}
17+
return intents
18+
}
19+
20+
async get(intentId: string): Promise<Group> {
21+
return this.graphClient.api(`/deviceManagement/intents/${intentId}`).get()
22+
}
23+
24+
async update(
25+
intentId: string,
26+
intent: DeviceManagementIntent,
27+
): Promise<DeviceManagementIntent> {
28+
return this.graphClient.api(`/deviceManagement/intents/${intentId}`).patch(intent)
29+
}
30+
31+
async delete(intentId: string): Promise<void> {
32+
return this.graphClient.api(`/deviceManagement/intents/${intentId}`).delete()
33+
}
34+
35+
async create(intent: DeviceManagementIntent): Promise<DeviceManagementIntent> {
36+
return this.graphClient.api('/deviceManagement/intents').post(intent)
37+
}
38+
}

src/lib/intune.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { GroupPolicyConfigurations } from './groupPolicyConfigurations/groupPoli
1212
import { CustomRequest } from './customRequest/customRequest'
1313
import { Autopilot } from './autopilot/autopilot'
1414
import { DeviceManagementTemplates } from './deviceManagementTemplates/deviceManagementTemplates'
15+
import { DeviceManagementIntents } from './deviceManagementIntents/deviceManagementIntents'
1516
require('isomorphic-fetch')
1617

1718
export class Intune {
@@ -37,6 +38,8 @@ export class Intune {
3738

3839
readonly deviceManagementTemplates: DeviceManagementTemplates
3940

41+
readonly deviceManagementIntents: DeviceManagementIntents
42+
4043
constructor(private readonly config: Config) {
4144
const credential = new ClientSecretCredential(
4245
this.config.tenantId,
@@ -62,5 +65,6 @@ export class Intune {
6265
this.customRequest = new CustomRequest(this.graphclient)
6366
this.autopilot = new Autopilot(this.graphclient)
6467
this.deviceManagementTemplates = new DeviceManagementTemplates(this.graphclient)
68+
this.deviceManagementIntents = new DeviceManagementIntents(this.graphclient)
6569
}
6670
}

src/lib/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ export interface DeviceManagementSettingInstance extends Graph.DeviceManagementS
157157
'@odata.type': '#microsoft.graph.deviceManagementSettingInstance'
158158
}
159159

160+
export interface DeviceManagementIntent extends Graph.DeviceManagementIntent {
161+
'@odata.type': '#microsoft.graph.deviceManagementIntent'
162+
}
163+
160164
export interface CreateTemplateInstance {
161165
displayName: string
162166
description: string

0 commit comments

Comments
 (0)