Skip to content

Commit

Permalink
fix(lib): add condition to check region with 'cfe-region' as well
Browse files Browse the repository at this point in the history
- downgrade supporting gcp vendor
- update test
  • Loading branch information
manushak committed Mar 7, 2024
1 parent eb047e0 commit 696d8c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/unit/lib/cloud-instance-metadata/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand All @@ -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: '',
Expand All @@ -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: '',
Expand All @@ -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: '',
Expand Down
44 changes: 13 additions & 31 deletions src/lib/cloud-instance-metadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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<string, any> = {};

// 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);

Expand All @@ -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`,
})
);
}
Expand Down Expand Up @@ -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`,
})
);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<z.infer<typeof schema>>(schema, input);
};

Expand Down

0 comments on commit 696d8c8

Please sign in to comment.