Skip to content

Commit

Permalink
feat: add support for device management intents (#119)
Browse files Browse the repository at this point in the history
* feat: add support for device management intents

* fix: fix endpoint for intents
  • Loading branch information
santese authored Aug 15, 2022
1 parent 4e3251b commit ad95e65
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/lib/deviceManagementIntents/deviceManagementIntents.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
38 changes: 38 additions & 0 deletions src/lib/deviceManagementIntents/deviceManagementIntents.ts
Original file line number Diff line number Diff line change
@@ -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<Group> {
return this.graphClient.api(`/deviceManagement/intents/${intentId}`).get()
}

async update(
intentId: string,
intent: DeviceManagementIntent,
): Promise<DeviceManagementIntent> {
return this.graphClient.api(`/deviceManagement/intents/${intentId}`).patch(intent)
}

async delete(intentId: string): Promise<void> {
return this.graphClient.api(`/deviceManagement/intents/${intentId}`).delete()
}

async create(intent: DeviceManagementIntent): Promise<DeviceManagementIntent> {
return this.graphClient.api('/deviceManagement/intents').post(intent)
}
}
4 changes: 4 additions & 0 deletions src/lib/intune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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)
}
}
4 changes: 4 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ad95e65

Please sign in to comment.