From 696d8c8cd519638f269659b1a0c3021db4c13b97 Mon Sep 17 00:00:00 2001 From: manushak Date: Thu, 7 Mar 2024 20:35:11 +0400 Subject: [PATCH] fix(lib): add condition to check region with 'cfe-region' as well - downgrade supporting gcp vendor - update test --- .../lib/cloud-instance-metadata/index.test.ts | 8 ++-- src/lib/cloud-instance-metadata/index.ts | 44 ++++++------------- 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/src/__tests__/unit/lib/cloud-instance-metadata/index.test.ts b/src/__tests__/unit/lib/cloud-instance-metadata/index.test.ts index 18e9e2b..2b94f15 100644 --- a/src/__tests__/unit/lib/cloud-instance-metadata/index.test.ts +++ b/src/__tests__/unit/lib/cloud-instance-metadata/index.test.ts @@ -75,7 +75,7 @@ describe('lib/cloud-instance-metadata:', () => { it('throws on `cloud/instance-type` when `cloud/vendor` is aws.', async () => { const errorMessage = - "CloudInstanceMetadata(cloud/instance-type): 't2.micro2' is not supported in 'aws'."; + "CloudInstanceMetadata(cloud/instance-type): 't2.micro2' instance type is not supported in 'aws' cloud vendor."; const inputs = [ { timestamp: '', @@ -97,7 +97,7 @@ describe('lib/cloud-instance-metadata:', () => { it('throws on `cloud/instance-type` when `cloud/vendor` is azure.', async () => { const errorMessage = - "CloudInstanceMetadata(cloud/instance-type): 't2.micro2' is not supported in 'azure'."; + "CloudInstanceMetadata(cloud/instance-type): 't2.micro2' instance type is not supported in 'azure' cloud vendor."; const inputs = [ { timestamp: '', @@ -119,7 +119,7 @@ describe('lib/cloud-instance-metadata:', () => { it('throws on unsupported `cloud/vendor`.', async () => { const errorMessage = - "\"cloud/vendor\" parameter is invalid enum value. expected 'aws' | 'azure' | 'gcp', received 'aws2'. Error code: invalid_union."; + "\"cloud/vendor\" parameter is invalid enum value. expected 'aws' | 'azure', received 'aws2'. Error code: invalid_enum_value."; const inputs = [ { timestamp: '', @@ -141,7 +141,7 @@ describe('lib/cloud-instance-metadata:', () => { it('throws on missed required parameters.', async () => { const errorMessage = - '"cloud/vendor" parameter is only aws,azure,gcp is currently supported. Error code: invalid_union.'; + '"cloud/vendor" parameter is only aws,azure is currently supported. Error code: invalid_type.,"cloud/instance-type" parameter is required. Error code: invalid_type.'; const inputs = [ { timestamp: '', diff --git a/src/lib/cloud-instance-metadata/index.ts b/src/lib/cloud-instance-metadata/index.ts index 8853a69..9ff6282 100644 --- a/src/lib/cloud-instance-metadata/index.ts +++ b/src/lib/cloud-instance-metadata/index.ts @@ -20,7 +20,7 @@ const GSF_DATA = path.resolve(__dirname, './GSF-data.csv'); const {UnsupportedValueError} = ERRORS; export const CloudInstanceMetadata = (): PluginInterface => { - const SUPPORTED_CLOUDS = ['aws', 'azure', 'gcp'] as const; + const SUPPORTED_CLOUDS = ['aws', 'azure'] as const; const errorBuilder = buildErrorMessage(CloudInstanceMetadata.name); const metadata = { kind: 'execute', @@ -35,18 +35,19 @@ export const CloudInstanceMetadata = (): PluginInterface => { for await (const input of inputs) { const safeInput = Object.assign({}, input, validateInput(input)); const outputParameters = (config && config['fields']) || []; + const region = input['cloud/region']; const draftInput: Record = {}; - // Process region metadata based on cloud vendor - if (safeInput['cloud/region']) { - Object.assign(draftInput, await processRegionData(safeInput)); - } - // Process instance metadata based on cloud vendor if (safeInput['cloud/instance-type']) { Object.assign(draftInput, await processInstanceTypeData(safeInput)); } + // Process region metadata based on cloud vendor + if (region) { + Object.assign(draftInput, await processRegionData(safeInput)); + } + const configuredParmeters = configureOutput(draftInput, outputParameters); Object.assign(safeInput, configuredParmeters); @@ -68,7 +69,7 @@ export const CloudInstanceMetadata = (): PluginInterface => { if (!regionInput) { throw new UnsupportedValueError( errorBuilder({ - message: `'${region}' is not supported in '${vendor}'`, + message: `'${region}' region is not supported in '${vendor}' cloud vendor`, }) ); } @@ -97,7 +98,7 @@ export const CloudInstanceMetadata = (): PluginInterface => { throw new UnsupportedValueError( errorBuilder({ scope: 'cloud/instance-type', - message: `'${instanceType}' is not supported in '${vendor}'`, + message: `'${instanceType}' instance type is not supported in '${vendor}' cloud vendor`, }) ); } @@ -141,7 +142,7 @@ export const CloudInstanceMetadata = (): PluginInterface => { const filteredResult = result.find( item => item['cloud-provider'] === cloudProvider[vendor] && - item['cloud-region'] === region + (item['cloud-region'] === region || item['cfe-region'] === region) ); return filteredResult; @@ -198,33 +199,14 @@ export const CloudInstanceMetadata = (): PluginInterface => { * Checks for required fields in input. */ const validateInput = (input: PluginParams) => { - const regionSchema = z.object({ + const schema = z.object({ 'cloud/vendor': z.enum(SUPPORTED_CLOUDS, { required_error: `Only ${SUPPORTED_CLOUDS} is currently supported`, }), - 'cloud/region': z.string(), + 'cloud/instance-type': z.string(), + 'cloud/region': z.string().optional(), }); - const instanceSchema = z - .object({ - 'cloud/vendor': z.enum(SUPPORTED_CLOUDS, { - required_error: `Only ${SUPPORTED_CLOUDS} is currently supported`, - }), - 'cloud/instance-type': z.string(), - }) - .refine( - param => { - if (param['cloud/vendor'] === 'gcp') return false; - return true; - }, - { - message: - '`cloud/vendor` is aceppted `aws` or `azure` if `cloud/instance-type` is provided.', - } - ); - - const schema = regionSchema.or(instanceSchema); - return validate>(schema, input); };