-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Netsuite-adapter: Initial fetch implementation (#762)
- Loading branch information
Showing
21 changed files
with
1,051 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2020 Salto Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import _ from 'lodash' | ||
import { Element, InstanceElement, ObjectType, Change } from '@salto-io/adapter-api' | ||
import NetsuiteClient from './client/client' | ||
import { createInstanceElement, Types } from './transformer' | ||
import { METADATA_TYPE } from './constants' | ||
|
||
|
||
export interface NetsuiteAdapterParams { | ||
client: NetsuiteClient | ||
} | ||
|
||
export default class NetsuiteAdapter { | ||
private readonly client: NetsuiteClient | ||
|
||
public constructor({ client }: NetsuiteAdapterParams) { | ||
this.client = client | ||
} | ||
|
||
/** | ||
* Fetch configuration elements: objects, types and instances for the given Netsuite account. | ||
* Account credentials were given in the constructor. | ||
*/ | ||
public async fetch(): Promise<Element[]> { | ||
const objects = Types.customizationObjects | ||
const instances = await this.fetchInstances(Object.values(objects)) | ||
return _.flatten([objects, instances] as Element[][]) | ||
} | ||
|
||
private async fetchInstances(types: ObjectType[]): Promise<InstanceElement[]> { | ||
return _.flatten(await Promise.all(types.map(async type => { | ||
const customRecords = await this.client.listCustomizations(type.annotations[METADATA_TYPE]) | ||
return customRecords.map(record => createInstanceElement(record, type)) | ||
}))) | ||
} | ||
|
||
public async add(element: Element): Promise<Element> { // todo: implement | ||
// eslint-disable-next-line no-console | ||
console.log(this.client) | ||
return Promise.resolve(element) | ||
} | ||
|
||
public async remove(_element: Element): Promise<void> { // todo: implement | ||
// eslint-disable-next-line no-console | ||
console.log(this.client) | ||
} | ||
|
||
public async update(_before: Element, after: Element, _changes: Iterable<Change>): | ||
Promise<Element> { // todo: implement | ||
// eslint-disable-next-line no-console | ||
console.log(this.client) | ||
return Promise.resolve(after) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2020 Salto Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { | ||
AdapterCreator, BuiltinTypes, ElemID, Field, InstanceElement, ObjectType, | ||
} from '@salto-io/adapter-api' | ||
import NetsuiteClient, { Credentials } from './client/client' | ||
import NetsuiteAdapter from './adapter' | ||
import { NETSUITE } from './constants' | ||
|
||
const configID = new ElemID(NETSUITE) | ||
|
||
const credentialsType = new ObjectType({ | ||
elemID: configID, | ||
fields: { | ||
account: new Field(configID, 'account', BuiltinTypes.STRING), | ||
consumerKey: new Field(configID, 'consumerKey', BuiltinTypes.STRING), | ||
consumerSecret: new Field(configID, 'consumerSecret', BuiltinTypes.STRING), | ||
tokenId: new Field(configID, 'tokenId', BuiltinTypes.STRING), | ||
tokenSecret: new Field(configID, 'tokenSecret', BuiltinTypes.STRING), | ||
}, | ||
annotationTypes: {}, | ||
annotations: {}, | ||
}) | ||
|
||
const netsuiteCredentialsFromCredentials = (credentials: Readonly<InstanceElement>): Credentials => | ||
credentials.value as Credentials | ||
|
||
const clientFromCredentials = (credentials: InstanceElement): NetsuiteClient => | ||
new NetsuiteClient({ | ||
credentials: netsuiteCredentialsFromCredentials(credentials), | ||
}) | ||
|
||
export const creator: AdapterCreator = { | ||
create: opts => new NetsuiteAdapter({ | ||
client: clientFromCredentials(opts.credentials), | ||
}), | ||
validateConfig: config => { | ||
const credentials = netsuiteCredentialsFromCredentials(config) | ||
return NetsuiteClient.validateCredentials(credentials) | ||
}, | ||
credentialsType, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2020 Salto Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Configuration, Record, Service as Connection } from 'node-suitetalk' | ||
import { decorators } from '@salto-io/lowerdash' | ||
import { ATTRIBUTES } from '../constants' | ||
|
||
export type NetsuiteRecord = Record.Types.Record | ||
|
||
const API_VERSION = '2019_2' | ||
|
||
export type Credentials = { | ||
account: string | ||
consumerKey: string | ||
consumerSecret: string | ||
tokenId: string | ||
tokenSecret: string | ||
} | ||
|
||
export type NetsuiteClientOpts = { | ||
credentials: Credentials | ||
connection?: Connection | ||
} | ||
|
||
export const realConnection = (credentials: Credentials): Connection => { | ||
const config = new Configuration({ | ||
account: credentials.account, | ||
apiVersion: API_VERSION, | ||
accountSpecificUrl: true, | ||
token: { | ||
// eslint-disable-next-line @typescript-eslint/camelcase | ||
consumer_key: credentials.consumerKey, | ||
// eslint-disable-next-line @typescript-eslint/camelcase | ||
consumer_secret: credentials.consumerSecret, | ||
// eslint-disable-next-line @typescript-eslint/camelcase | ||
token_key: credentials.tokenId, | ||
// eslint-disable-next-line @typescript-eslint/camelcase | ||
token_secret: credentials.tokenSecret, | ||
}, | ||
wsdlPath: `https://webservices.netsuite.com/wsdl/v${API_VERSION}_0/netsuite.wsdl`, | ||
}) | ||
return new Connection(config) | ||
} | ||
|
||
export default class NetsuiteClient { | ||
private isLoggedIn = false | ||
private readonly conn: Connection | ||
|
||
static validateCredentials(credentials: Credentials): Promise<void> { | ||
return realConnection(credentials).init() | ||
} | ||
|
||
constructor({ credentials, connection }: NetsuiteClientOpts) { | ||
this.conn = connection ?? realConnection(credentials) | ||
} | ||
|
||
private async ensureLoggedIn(): Promise<void> { | ||
if (!this.isLoggedIn) { | ||
await this.conn.init() | ||
// this.isLoggedIn = true // todo uncomment -> currently each API call requires a new init() | ||
} | ||
} | ||
|
||
private static requiresLogin = decorators.wrapMethodWith( | ||
async function withLogin( | ||
this: NetsuiteClient, | ||
originalMethod: decorators.OriginalCall | ||
): Promise<unknown> { | ||
await this.ensureLoggedIn() | ||
return originalMethod.call() | ||
} | ||
) | ||
|
||
@NetsuiteClient.requiresLogin | ||
async list(recordReferences: { type: string; internalId: number }[]): | ||
Promise<NetsuiteRecord[]> { | ||
const recordRefs = recordReferences | ||
.map(recordReference => { | ||
const recordRef = new Record.Types.RecordRef() | ||
recordRef.internalId = recordReference.internalId | ||
recordRef.type = recordReference.type | ||
return recordRef | ||
}) | ||
const getListResponse = await this.conn.getList(recordRefs) | ||
return getListResponse.readResponseList.readResponse.map(item => item.record) | ||
} | ||
|
||
@NetsuiteClient.requiresLogin | ||
private async getCustomizationIds(type: string, includeInactives = true): Promise<number[]> { | ||
const getCustomizationIdResponse = await this.conn.getCustomizationId(type, includeInactives) | ||
return getCustomizationIdResponse.getCustomizationIdResult.customizationRefList | ||
.customizationRef.map(customization => customization[ATTRIBUTES].internalId) | ||
} | ||
|
||
@NetsuiteClient.requiresLogin | ||
async listCustomizations(type: string, includeInactives = true): Promise<NetsuiteRecord[]> { | ||
const customizationInternalIds = await this.getCustomizationIds(type, includeInactives) | ||
const customRecordRefs = customizationInternalIds.map(internalId => ({ type, internalId })) | ||
return this.list(customRecordRefs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.