Skip to content

Commit

Permalink
Merge pull request #25 from sailpoint-oss/fning/PLTCONN-2322
Browse files Browse the repository at this point in the history
PLTCONN-2322: Supports stateful command
  • Loading branch information
fangming-ning-sp committed Mar 21, 2023
2 parents 580636e + 970f237 commit 461e3f3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
7 changes: 7 additions & 0 deletions lib/commands/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,10 @@ export type ObjectInput = {
}

export type ObjectOutput = { identity: string, uuid?: string } | { key: Key }

/**
* State input for stateful command
*/
export type CommandState = {
[key: string]: any
}
11 changes: 10 additions & 1 deletion lib/commands/std-account-list.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
/* Copyright (c) 2021. SailPoint Technologies, Inc. All rights reserved. */

import { Attributes, ObjectOutput } from './command'
import { Attributes, CommandState, ObjectOutput } from './command'

/**
* Input object of `std:account:list` command
*/
export type StdAccountListInput = {
stateful?: boolean
state?: CommandState
}

/**
* Output object of `std:account:list` command
*/
export type StdAccountListOutput = ObjectOutput & {
disabled?: boolean
locked?: boolean
deleted?: boolean
attributes: Attributes
}
5 changes: 4 additions & 1 deletion lib/commands/std-entitlement-list.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/* Copyright (c) 2021. SailPoint Technologies, Inc. All rights reserved. */

import { Attributes, ObjectOutput } from './command'
import { Attributes, CommandState, ObjectOutput } from './command'

/**
* Input object of `std:entitlement:list` command
*/
export type StdEntitlementListInput = {
type: string
stateful?: boolean
state?: CommandState
}

/**
* Output object of `std:entitlement:list` command
*/
export type StdEntitlementListOutput = ObjectOutput & {
type: string
deleted?: boolean
attributes: Attributes
}
10 changes: 8 additions & 2 deletions lib/connector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ describe('exec handlers', () => {
StandardCommand.StdTestConnection,
MOCK_CONTEXT,
undefined,
new PassThrough({ objectMode: true }).on('data', (chunk) => expect(chunk).toStrictEqual({}))
new PassThrough({ objectMode: true }).on('data', (chunk) => expect(chunk).toEqual({
"type": "output",
"data": {},
}))
)
})

Expand All @@ -207,7 +210,10 @@ describe('exec handlers', () => {
mockFS.restore()

const out = res.read(1)
expect(out).toStrictEqual({"specification": spec})
expect(out).toEqual({
"type": "output",
"data": {"specification": spec},
})
})

it('should execute custom handler', async () => {
Expand Down
3 changes: 2 additions & 1 deletion lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
StdTestConnectionOutput,
StdChangePasswordInput,
StdChangePasswordOutput,
StdAccountListInput
} from './commands'
import { Response } from './response'

Expand Down Expand Up @@ -67,7 +68,7 @@ export type StdAccountEnableHandler = (
) => Promise<void>
export type StdAccountListHandler = (
context: Context,
input: undefined,
input: StdAccountListInput,
res: Response<StdAccountListOutput>
) => Promise<void>
export type StdAccountReadHandler = (
Expand Down
32 changes: 31 additions & 1 deletion lib/response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Writable } from 'stream'
*/
export interface Response<T> {
send(output: T): void
saveState(state: any): void
}

/**
Expand All @@ -24,6 +25,35 @@ export class ResponseStream<T> implements Response<T> {
* @param chunk output chunk
*/
send(chunk: T): void {
this._writable.write(chunk)
this._writable.write(new RawResponse(chunk))
}

/**
* Save state for stateful command
* @param state the end state of running the stateful command
*/
saveState(state: any): void {
this._writable.write(new RawResponse(state, ResponseType.State))
}
}

/**
* Enum representing different types of responses
*/
enum ResponseType {
Output = 'output',
State = 'state'
}

/**
* RawResponse is the response that sdk sends out as command output
*/
class RawResponse {
type: ResponseType
data: string

constructor(data: any, type = ResponseType.Output) {
this.data = data
this.type = type
}
}

0 comments on commit 461e3f3

Please sign in to comment.