Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Fix missing package-level vars in GCP policy editor #132068

Merged
merged 5 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ describe('Fleet - validatePackagePolicy()', () => {
],
},
],
vars: {},
};

const invalidPackagePolicy: NewPackagePolicy = {
Expand Down Expand Up @@ -332,6 +333,7 @@ describe('Fleet - validatePackagePolicy()', () => {
],
},
],
vars: {},
};

const noErrorsValidationResults = {
Expand Down Expand Up @@ -370,6 +372,7 @@ describe('Fleet - validatePackagePolicy()', () => {
vars: { 'var-name': null },
},
},
vars: {},
};

it('returns no errors for valid package policy', () => {
Expand Down Expand Up @@ -416,6 +419,7 @@ describe('Fleet - validatePackagePolicy()', () => {
streams: { 'with-no-stream-vars-bar': {} },
},
},
vars: {},
});
});

Expand Down Expand Up @@ -487,6 +491,7 @@ describe('Fleet - validatePackagePolicy()', () => {
streams: { 'with-no-stream-vars-bar': {} },
},
},
vars: {},
});
});

Expand All @@ -505,6 +510,7 @@ describe('Fleet - validatePackagePolicy()', () => {
description: null,
namespace: null,
inputs: null,
vars: {},
});
expect(
validatePackagePolicy(
Expand All @@ -520,6 +526,7 @@ describe('Fleet - validatePackagePolicy()', () => {
description: null,
namespace: null,
inputs: null,
vars: {},
});
});

Expand All @@ -538,6 +545,7 @@ describe('Fleet - validatePackagePolicy()', () => {
description: null,
namespace: null,
inputs: null,
vars: {},
});
expect(
validatePackagePolicy(
Expand All @@ -553,6 +561,7 @@ describe('Fleet - validatePackagePolicy()', () => {
description: null,
namespace: null,
inputs: null,
vars: {},
});
});

Expand Down Expand Up @@ -604,6 +613,7 @@ describe('Fleet - validatePackagePolicy()', () => {
},
},
},
vars: {},
});
});

Expand Down Expand Up @@ -729,6 +739,7 @@ describe('Fleet - validatePackagePolicy()', () => {
},
},
},
vars: {},
name: null,
namespace: null,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const validatePackagePolicy = (
description: null,
namespace: null,
inputs: {},
vars: {},
};
const namespaceValidation = isValidNamespace(packagePolicy.namespace);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,19 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{

{/* Required vars */}
{requiredVars.map((varDef) => {
const { name: varName, type: varType } = varDef;
if (!packagePolicy.vars || !packagePolicy.vars[varName]) return null;
const value = packagePolicy.vars[varName].value;
const { name: varName, type: varType, default: defaultValue } = varDef;

// Set up the `vars` object and assign the default value if it doesn't exist
if (!packagePolicy.vars) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems odd to me to reassign vars in a component render, can we do this in a useEffect calling onChange ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this logic to the existing useEffect and verified things still work in 2e2469e.

Also added tests.

packagePolicy.vars = {};
}

if (!packagePolicy.vars[varName]) {
packagePolicy.vars[varName] = { value: defaultValue, type: varType };
}

const value = packagePolicy.vars?.[varName]?.value;

return (
<EuiFlexItem key={varName}>
<PackagePolicyInputVarField
Expand Down