Skip to content

Commit

Permalink
Merge pull request #113 from pliancy/feat/device-management-templates
Browse files Browse the repository at this point in the history
Feat/device management templates
  • Loading branch information
kyl3c authored Jul 5, 2022
2 parents cfd41a6 + 3262e74 commit 27bcca4
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { mockClient } from '../../../__fixtures__/@microsoft/microsoft-graph-client'
import { Client } from '@microsoft/microsoft-graph-client'
import { DeviceManagementTemplates } from './deviceManagementTemplates'
import { CreateTemplateInstance, DeviceManagementTemplate } from '../types'

describe('Devices Management Templates', () => {
let graphClient: Client
let deviceManagementTemplates: DeviceManagementTemplates

const deviceManagementTemplate = {
name: 'test',
'@odata.type': '#microsoft.graph.deviceManagementTemplate',
id: '1',
}

beforeEach(() => {
graphClient = mockClient() as never as Client
deviceManagementTemplates = new DeviceManagementTemplates(graphClient)
})

it('should list device management templates', async () => {
jest.spyOn(graphClient.api(''), 'get').mockResolvedValue({
value: [deviceManagementTemplate],
})
const res = await deviceManagementTemplates.list()
expect(res).toEqual([deviceManagementTemplate])
})

it('should get a device management template', async () => {
jest.spyOn(graphClient.api(''), 'get').mockResolvedValue(deviceManagementTemplate)
const res = await deviceManagementTemplates.get('')
expect(res).toEqual(deviceManagementTemplate)
})

it('should create a device management template', async () => {
jest.spyOn(graphClient.api(''), 'post').mockResolvedValue(deviceManagementTemplate)
const res = await deviceManagementTemplates.create({} as DeviceManagementTemplate)
expect(res).toEqual(deviceManagementTemplate)
})

it('should update a device management template', async () => {
jest.spyOn(graphClient.api(''), 'patch')
const res = await deviceManagementTemplates.update('id', {} as DeviceManagementTemplate)
expect(res).toBeUndefined()
})

it('should delete a device management template', async () => {
jest.spyOn(graphClient.api(''), 'delete')
const res = await deviceManagementTemplates.delete('id')
expect(res).toBeUndefined()
})

it('should create create an instance ', async () => {
jest.spyOn(graphClient.api(''), 'post').mockResolvedValue(deviceManagementTemplate)
const res = await deviceManagementTemplates.createInstance(
'1',
{} as CreateTemplateInstance,
)
expect(res).toEqual(deviceManagementTemplate)
})
})
43 changes: 43 additions & 0 deletions src/lib/deviceManagementTemplates/deviceManagementTemplates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Client } from '@microsoft/microsoft-graph-client'
import { CreateTemplateInstance, DeviceManagementIntent, DeviceManagementTemplate } from '../types'

export class DeviceManagementTemplates {
constructor(private readonly graphClient: Client) {}

async list() {
let res = await this.graphClient.api('/deviceManagement/templates').get()
const templates: DeviceManagementTemplate[] = 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 nextTemplates = res.value as DeviceManagementTemplate[]
templates.push(...nextTemplates)
}
return templates
}

async get(id: string): Promise<DeviceManagementTemplate> {
return await this.graphClient.api(`/deviceManagement/templates/${id}`).get()
}

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

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

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

async createInstance(
templateId: string,
instance: CreateTemplateInstance,
): Promise<DeviceManagementIntent> {
return this.graphClient
.api(`/deviceManagement/templates/${templateId}/createInstance`)
.post(instance)
}
}
4 changes: 4 additions & 0 deletions src/lib/intune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Users } from './users/users'
import { GroupPolicyConfigurations } from './groupPolicyConfigurations/groupPolicyConfigurations'
import { CustomRequest } from './customRequest/customRequest'
import { Autopilot } from './autopilot/autopilot'
import { DeviceManagementTemplates } from './deviceManagementTemplates/deviceManagementTemplates'
require('isomorphic-fetch')

export class Intune {
Expand All @@ -34,6 +35,8 @@ export class Intune {

readonly customRequest: CustomRequest

readonly deviceManagementTemplates: DeviceManagementTemplates

constructor(private readonly config: Config) {
const credential = new ClientSecretCredential(
this.config.tenantId,
Expand All @@ -58,5 +61,6 @@ export class Intune {
this.users = new Users(this.graphclient)
this.customRequest = new CustomRequest(this.graphclient)
this.autopilot = new Autopilot(this.graphclient)
this.deviceManagementTemplates = new DeviceManagementTemplates(this.graphclient)
}
}
19 changes: 19 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,22 @@ export type MobileAppRelationship = MobileAppDependency | MobileAppSupersedence
export interface MobileAppAssignment extends Graph.MobileAppAssignment {
'@odata.type': '#microsoft.graph.mobileAppAssignment'
}

export interface DeviceManagementTemplate extends Graph.DeviceManagementTemplate {
'@odata.type': '#microsoft.graph.deviceManagementTemplate'
}

export interface DeviceManagementSettingInstance extends Graph.DeviceManagementSettingInstance {
'@odata.type': '#microsoft.graph.deviceManagementSettingInstance'
}

export interface CreateTemplateInstance {
displayName: string
description: string
settingsDelta: DeviceManagementSettingInstance[]
roleScopeTagIds: string[]
}

export interface DeviceManagementIntent extends Graph.DeviceManagementIntent {
'@odata.type': '#microsoft.graph.deviceManagementIntent'
}

0 comments on commit 27bcca4

Please sign in to comment.