From fb3bf168c64e1d73bdb42bd2da719a02913a3f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Camith-skyflow=E2=80=9D?= <“amit@skyflow.com”> Date: Fri, 25 Oct 2024 15:31:10 +0530 Subject: [PATCH] SK-1621: fix if id exits --- src/error/codes/index.ts | 5 +++-- src/error/messages/index.ts | 2 ++ src/utils/validations/index.ts | 8 ++------ src/vault/client/index.ts | 4 ++-- src/vault/controller/vault/index.ts | 4 ++-- src/vault/model/options/insert/index.ts | 13 ++----------- src/vault/skyflow/index.ts | 12 ++++++++++++ test/vault/controller/vault.test.js | 18 +++++++++--------- test/vault/skyflow/skyflow.test.js | 23 +++++++++++++++++++++++ 9 files changed, 57 insertions(+), 32 deletions(-) diff --git a/src/error/codes/index.ts b/src/error/codes/index.ts index dd6db20..0b641df 100644 --- a/src/error/codes/index.ts +++ b/src/error/codes/index.ts @@ -35,8 +35,9 @@ const SKYFLOW_ERROR_CODE = { EMPTY_CONNECTION_ID_VALIDATION: { http_code: 400, message: errorMessages.EMPTY_CONNECTION_ID_VALIDATION }, EMPTY_CONNECTION_URL: { http_code: 400, message: errorMessages.EMPTY_CONNECTION_URL }, INVALID_CONNECTION_URL: { http_code: 400, message: errorMessages.INVALID_CONNECTION_URL }, - - + + VAULT_ID_EXITS_IN_CONFIG_LIST: { http_code: 400, message: errorMessages.VAULT_ID_EXITS_IN_CONFIG_LIST }, + CONNECTION_ID_EXITS_IN_CONFIG_LIST: { http_code: 400, message: errorMessages.CONNECTION_ID_EXITS_IN_CONFIG_LIST }, VAULT_ID_NOT_IN_CONFIG_LIST: { http_code: 400, message: errorMessages.VAULT_ID_NOT_IN_CONFIG_LIST }, CONNECTION_ID_NOT_IN_CONFIG_LIST: { http_code: 400, message: errorMessages.CONNECTION_ID_NOT_IN_CONFIG_LIST }, diff --git a/src/error/messages/index.ts b/src/error/messages/index.ts index 8ad9e30..38ee016 100644 --- a/src/error/messages/index.ts +++ b/src/error/messages/index.ts @@ -37,6 +37,8 @@ const errorMessages = { EMPTY_CONNECTION_URL: `${errorPrefix} Initialization failed. Invalid connection URL. Specify a valid connection Url.`, INVALID_CONNECTION_URL: `${errorPrefix} Initialization failed. Invalid connection URL. Specify connection Url as a valid url.`, + VAULT_ID_EXITS_IN_CONFIG_LIST: `${errorPrefix} Validation error. %s1 already exists in the config list. Specify a new vaultId.`, + CONNECTION_ID_EXITS_IN_CONFIG_LIST: `${errorPrefix} Validation error. %s1 already exists in the config list. Specify a new vaultId.`, VAULT_ID_NOT_IN_CONFIG_LIST: `${errorPrefix} Validation error. %s1 is missing from the config. Specify the vaultId's from config.`, CONNECTION_ID_NOT_IN_CONFIG_LIST: `${errorPrefix} Validation error. %s1 is missing from the config. Specify the connectionIds from config.`, diff --git a/src/utils/validations/index.ts b/src/utils/validations/index.ts index c49bb45..97cc150 100644 --- a/src/utils/validations/index.ts +++ b/src/utils/validations/index.ts @@ -344,18 +344,14 @@ export const validateInsertOptions = (insertOptions?: InsertOptions) => { throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_RETURN_TOKEN, [typeof insertOptions?.getReturnTokens()]); } - if (insertOptions?.getUpsert() && typeof insertOptions.getUpsert() !== 'string') { - throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_UPSERT, [typeof insertOptions?.getUpsert()]); + if (insertOptions?.getUpsertColumn() && typeof insertOptions.getUpsertColumn() !== 'string') { + throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_UPSERT, [typeof insertOptions?.getUpsertColumn()]); } if (insertOptions?.getContinueOnError() && typeof insertOptions.getContinueOnError() !== 'boolean') { throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_CONTINUE_ON_ERROR, [typeof insertOptions?.getContinueOnError()]); } - if (insertOptions?.getTokenStrict() && typeof insertOptions.getTokenStrict() !== 'boolean') { - throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_TOKEN_STRICT, [typeof insertOptions?.getTokenStrict()]); - } - if (insertOptions?.getHomogeneous() && typeof insertOptions.getHomogeneous() !== 'boolean') { throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_HOMOGENEOUS, [typeof insertOptions?.getHomogeneous()]); } diff --git a/src/vault/client/index.ts b/src/vault/client/index.ts index fb4fa7c..803ae41 100644 --- a/src/vault/client/index.ts +++ b/src/vault/client/index.ts @@ -43,7 +43,7 @@ class VaultClient { } updateClientConfig(clusterID: string, vaultId: string, individualCredentials?: Credentials, skyflowCredentials?: Credentials, logLevel?: LogLevel) { - this.updateTriggered = true + this.updateTriggered = true; this.initializeClient(clusterID, vaultId, individualCredentials, skyflowCredentials, logLevel); } @@ -104,6 +104,7 @@ class VaultClient { } updateSkyflowCredentials(credentials?: Credentials) { + this.updateTriggered = true; this.skyflowCredentials = credentials; } @@ -153,7 +154,6 @@ class VaultClient { grpcCode?: number, details?: any ) { - printLog(description, MessageType.ERROR, this.getLogLevel()); reject(new SkyflowError({ http_code: err?.response?.status || 400, message: description, diff --git a/src/vault/controller/vault/index.ts b/src/vault/controller/vault/index.ts index 19caf3f..5e1531d 100644 --- a/src/vault/controller/vault/index.ts +++ b/src/vault/controller/vault/index.ts @@ -174,7 +174,7 @@ class VaultController { tokenization: options?.getReturnTokens() || false, method: BatchRecordMethod.Post, tokens: this.getTokens(index, options?.getTokens()), - upsert: options?.getUpsert(), + upsert: options?.getUpsertColumn(), })); return { records, @@ -191,7 +191,7 @@ class VaultController { return { records, tokenization: options?.getReturnTokens(), - upsert: options?.getUpsert(), + upsert: options?.getUpsertColumn(), homogeneous: options?.getHomogeneous(), byot: options?.getTokenMode() }; diff --git a/src/vault/model/options/insert/index.ts b/src/vault/model/options/insert/index.ts index b9f18f4..14b5d2c 100644 --- a/src/vault/model/options/insert/index.ts +++ b/src/vault/model/options/insert/index.ts @@ -8,7 +8,6 @@ class InsertOptions { private tokens?: Array; private homogeneous?: boolean; private tokenMode?: BYOT; - private tokenStrict?: boolean; private continueOnError?: boolean; // Constructor @@ -19,7 +18,7 @@ class InsertOptions { this.returnTokens = returnTokens; } - setUpsert(upsert: string) { + setUpsertColumn(upsert: string) { this.upsert = upsert; } @@ -35,10 +34,6 @@ class InsertOptions { this.tokenMode = tokenMode; } - setTokenStrict(tokenStrict: boolean) { - this.tokenStrict = tokenStrict; - } - setContinueOnError(continueOnError: boolean) { this.continueOnError = continueOnError; } @@ -48,7 +43,7 @@ class InsertOptions { return this.returnTokens; } - getUpsert(): string | undefined { + getUpsertColumn(): string | undefined { return this.upsert; } @@ -64,10 +59,6 @@ class InsertOptions { return this.tokenMode; } - getTokenStrict(): boolean | undefined { - return this.tokenStrict; - } - getContinueOnError(): boolean | undefined { return this.continueOnError; } diff --git a/src/vault/skyflow/index.ts b/src/vault/skyflow/index.ts index af64b11..d50411c 100644 --- a/src/vault/skyflow/index.ts +++ b/src/vault/skyflow/index.ts @@ -58,11 +58,13 @@ class Skyflow { addVaultConfig(config: VaultConfig) { validateVaultConfig(config); + this.throwErrorIfIdExits(config?.vaultId, this.vaultClients, VAULT_ID); this.addVaultClient(config, this.vaultClients); } addConnectionConfig(config: ConnectionConfig) { validateConnectionConfig(config); + this.throwErrorIfIdExits(config?.connectionId, this.connectionClients, CONNECTION_ID); this.addConnectionClient(config, this.connectionClients); } @@ -122,6 +124,16 @@ class Skyflow { } } + private throwErrorIfIdExits(id: string, clients: ClientObj, idKey: string) { + const errorMapping = { + [VAULT_ID]: SKYFLOW_ERROR_CODE.VAULT_ID_EXITS_IN_CONFIG_LIST, + [CONNECTION_ID]: SKYFLOW_ERROR_CODE.CONNECTION_ID_EXITS_IN_CONFIG_LIST, + }; + if(Object.keys(clients).includes(id)){ + this.throwSkyflowError(idKey, errorMapping, [id]); + } + } + private throwErrorForUnknownId(id: string, idKey: string) { const errorMapping = { [VAULT_ID]: SKYFLOW_ERROR_CODE.VAULT_ID_NOT_IN_CONFIG_LIST, diff --git a/test/vault/controller/vault.test.js b/test/vault/controller/vault.test.js index c4d86f8..6fe2753 100644 --- a/test/vault/controller/vault.test.js +++ b/test/vault/controller/vault.test.js @@ -151,7 +151,7 @@ describe('VaultController insert method', () => { const mockOptions = { getContinueOnError: jest.fn().mockReturnValue(false), getReturnTokens: jest.fn().mockReturnValue(true), - getUpsert: jest.fn().mockReturnValue(''), + getUpsertColumn: jest.fn().mockReturnValue(''), getHomogeneous: jest.fn().mockReturnValue(false), getTokenMode: jest.fn().mockReturnValue(''), getTokens: jest.fn().mockReturnValue([{}]) @@ -180,7 +180,7 @@ describe('VaultController insert method', () => { const mockOptions = { getContinueOnError: jest.fn().mockReturnValue(false), getReturnTokens: jest.fn().mockReturnValue(true), - getUpsert: jest.fn().mockReturnValue(''), + getUpsertColumn: jest.fn().mockReturnValue(''), getHomogeneous: jest.fn().mockReturnValue(false), getTokenMode: jest.fn().mockReturnValue(''), getTokens: jest.fn().mockReturnValue([{}]) @@ -209,7 +209,7 @@ describe('VaultController insert method', () => { const mockOptions = { getContinueOnError: jest.fn().mockReturnValue(false), getReturnTokens: jest.fn().mockReturnValue(true), - getUpsert: jest.fn().mockReturnValue(''), + getUpsertColumn: jest.fn().mockReturnValue(''), getHomogeneous: jest.fn().mockReturnValue(false), getTokenMode: jest.fn().mockReturnValue(''), getTokens: jest.fn().mockReturnValue([{}]) @@ -238,7 +238,7 @@ describe('VaultController insert method', () => { const mockOptions = { getContinueOnError: jest.fn().mockReturnValue(true), getReturnTokens: jest.fn().mockReturnValue(false), - getUpsert: jest.fn().mockReturnValue(''), + getUpsertColumn: jest.fn().mockReturnValue(''), getHomogeneous: jest.fn().mockReturnValue(false), getTokenMode: jest.fn().mockReturnValue(''), getTokens: jest.fn().mockReturnValue([]) @@ -261,7 +261,7 @@ describe('VaultController insert method', () => { const mockOptions = { getContinueOnError: jest.fn().mockReturnValue(true), getReturnTokens: jest.fn().mockReturnValue(false), - getUpsert: jest.fn().mockReturnValue(''), + getUpsertColumn: jest.fn().mockReturnValue(''), getHomogeneous: jest.fn().mockReturnValue(false), getTokenMode: jest.fn().mockReturnValue(''), getTokens: jest.fn().mockReturnValue([]) @@ -284,7 +284,7 @@ describe('VaultController insert method', () => { const mockOptions = { getContinueOnError: jest.fn().mockReturnValue(true), getReturnTokens: jest.fn().mockReturnValue(false), - getUpsert: jest.fn().mockReturnValue(''), + getUpsertColumn: jest.fn().mockReturnValue(''), getHomogeneous: jest.fn().mockReturnValue(false), getTokenMode: jest.fn().mockReturnValue(''), getTokens: jest.fn().mockReturnValue([]) @@ -307,7 +307,7 @@ describe('VaultController insert method', () => { const mockOptions = { getContinueOnError: jest.fn().mockReturnValue(true), getReturnTokens: jest.fn().mockReturnValue(false), - getUpsert: jest.fn().mockReturnValue(''), + getUpsertColumn: jest.fn().mockReturnValue(''), getHomogeneous: jest.fn().mockReturnValue(false), getTokenMode: jest.fn().mockReturnValue(''), getTokens: jest.fn().mockReturnValue([]) @@ -330,7 +330,7 @@ describe('VaultController insert method', () => { const mockOptions = { getContinueOnError: jest.fn().mockReturnValue(false), getReturnTokens: jest.fn().mockReturnValue(true), - getUpsert: jest.fn().mockReturnValue(''), + getUpsertColumn: jest.fn().mockReturnValue(''), getHomogeneous: jest.fn().mockReturnValue(false), getTokenMode: jest.fn().mockReturnValue('') }; @@ -352,7 +352,7 @@ describe('VaultController insert method', () => { const mockOptions = { getContinueOnError: jest.fn().mockReturnValue(false), getReturnTokens: jest.fn().mockReturnValue(true), - getUpsert: jest.fn().mockReturnValue(''), + getUpsertColumn: jest.fn().mockReturnValue(''), getHomogeneous: jest.fn().mockReturnValue(false), getTokenMode: jest.fn().mockReturnValue('') }; diff --git a/test/vault/skyflow/skyflow.test.js b/test/vault/skyflow/skyflow.test.js index 2a37dd4..6677b95 100644 --- a/test/vault/skyflow/skyflow.test.js +++ b/test/vault/skyflow/skyflow.test.js @@ -196,6 +196,7 @@ describe('Skyflow initialization', () => { const missingVaultConfigError = "VAULT_ID is missing from the config."; const missingIdConfigError = "ID is missing from the config."; const noConfigFound = "No vault config found."; + const idExits = "already exists in the config list."; const validVaultConfig = { vaultId: "VAULT_ID", @@ -257,6 +258,28 @@ describe('Skyflow initialization', () => { expect(() => skyflow.removeVaultConfig("ID")).toThrowError(missingIdConfigError); }); + test('should throw error when calling a non-existing vault config', () => { + const skyflow = new Skyflow({ + vaultConfigs: [validVaultConfig], + }); + expect(() => skyflow.vault("ID")).toThrowError(missingIdConfigError); + }); + + test('should throw error when no vault configs exits', () => { + const skyflow = new Skyflow({ + vaultConfigs: [validVaultConfig], + }); + skyflow.removeVaultConfig("VAULT_ID"); + expect(() => skyflow.vault("ID")).toThrowError(noConfigFound); + }); + + test('should throw error when adding vault config', () => { + const skyflow = new Skyflow({ + vaultConfigs: [validVaultConfig], + }); + expect(() => skyflow.addVaultConfig(validVaultConfig)).toThrowError(idExits); + }); + test('should throw error when updating a non-existing vault config', () => { const skyflow = new Skyflow({ vaultConfigs: [validVaultConfig],