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

Fixes #38116 - Handle version removal for multi-CV hosts #11282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pavanshekar
Copy link
Contributor

What are the changes introduced in this pull request?

Added checks for multi-CV host associations and helper methods to handle version removal from the environment, ensuring that relevant content facets and environment links are cleaned up. This ensures that only the targeted version is removed without affecting unrelated associations or environments.

Considerations taken when implementing this change?

Ensured selective removal of associations for multi-CV hosts to avoid reassigning hosts linked to single CVEs or impacting unrelated environments, while preserving existing associations and removing only the targeted version from the hosts' content facets. Existing methods were reused for consistency and to minimize duplication, and checks were added to handle edge cases, such as multiple hosts linked to the same environment, to prevent unintended side effects.

What are the testing steps for this pull request?

Create a multi-CV activation key

  1. Promote a CV Version to multiple environments
  2. Using Hammer CLI, update the activation key to use mutli CVE
    Example command: hammer activation-key update --organization="Default Organization" --id=1 --content-view-environments="ABC/cv1,XYZ/cv1"
  3. Confirm that the activation key uses multi CVEs

Assign and register multiple hosts using the multi-CV activation key

Remove the version from the environment

  1. Go to the CV associated with the activation key -> Versions
  2. Click on the 3 dots of the respective CV Version and select the option "Remove from environments"
  3. Follow the steps to remove the CV Version from the environment
  4. A task will be created which should successfully remove the version from the environment when the CVE is in use by a multi-CV host

Copy link
Member

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

Looks pretty good! some initial comments

const multiCVWarning = hostResponse?.results?.some?.(host =>
host.content_facet_attributes?.multi_content_view_environment);

const multiCVRemovalInfo = __('This environment is used in one or more hosts. The environment will simply be removed from the multi-environment hosts. The content view and lifecycle environment you select here will only apply to single-environment host. See hammer activation-key --help for more details.');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const multiCVRemovalInfo = __('This environment is used in one or more hosts. The environment will simply be removed from the multi-environment hosts. The content view and lifecycle environment you select here will only apply to single-environment host. See hammer activation-key --help for more details.');
const multiCVRemovalInfo = __('This content view version is used in one or more multi-environment hosts. The version will simply be removed from the multi-environment hosts. The content view and lifecycle environment you select here will only apply to single-environment hosts. See hammer activation-key --help for more details.');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack...

<FlexItem><ExclamationTriangleIcon /></FlexItem>
<FlexItem>
<p>
{__(`Content view environment will be removed from ${pluralize(multiCVHostsCount, 'multi-environment host')}.`)}
Copy link
Member

Choose a reason for hiding this comment

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

Can we also include the content view environment's label? "Content view environment envX/cvX will be removed.."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have included and pushed the updated code

plan_action(Host::Reassign, host, options[:system_content_view_id], options[:system_environment_id])
content_facet_attributes = host.content_facet
if content_facet_attributes.multi_content_view_environment?
content_facet_attributes.content_view_environments -= [content_view_environment]
Copy link
Member

Choose a reason for hiding this comment

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

note to self for testing: Need to make sure this calls #content_view_environments= so that the priority is properly updated.

<p>
{__(`Content view environment
${selectedEnv
?.map(({ name }) => `${name}/${multiCVHosts[0]?.content_facet_attributes?.content_view?.name}`)
Copy link
Member

Choose a reason for hiding this comment

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

@sjha4 Is there a less awkward way this CVE label could be assembled?

Comment on lines 79 to 83
{__(`Content view environment
${selectedEnv
?.map(({ name }) => `${name}/${multiCVHosts[0]?.content_facet_attributes?.content_view?.name}`)
.join(', ')}
will be removed from ${pluralize(multiCVHostsCount, 'multi-environment host')}.`)}
Copy link
Member

Choose a reason for hiding this comment

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

When using ${...}, the __() won't properly translate the string. We should use something similar to this:

<FormattedMessage
defaultMessage={__('After configuring Foreman, configuration must also be updated on {hosts}. Choose one of the following options to update {hosts}:')}
values={{
hosts: (
<FormattedMessage
defaultMessage="{count, plural, one {{singular}} other {# {plural}}}"
values={{
count: hostCount,
singular: __('the host'),
plural: __('hosts'),
}}
id="ccs-options-i18n"
/>
),
}}
id="ccs-options-description-i18n"
/>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack...

{singleCVActivationKeysCount > 0 && (
<Flex>
<FlexItem><ExclamationTriangleIcon /></FlexItem>
<FlexItem><p>{__(`${pluralize(singleCVActivationKeysCount, 'activation key')} will be moved to content view ${selectedCVNameForAK} in `)}</p></FlexItem>
Copy link
Member

Choose a reason for hiding this comment

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

Same here; please use FormattedMessage

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack...

}) => (
<Tr ouiaId={id} key={id}>
<Td>
<a rel="noreferrer" target="_blank" href={urlBuilder(`new/hosts/${id}`, '')}>{name}</a>
</Td>
<Td><EnvironmentLabels environments={environment} /></Td>
<Td>{ multiContentViewEnvironment ? 'Yes' : 'No' }</Td>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<Td>{ multiContentViewEnvironment ? 'Yes' : 'No' }</Td>
<Td>{ multiContentViewEnvironment ? __('Yes') : __('No') }</Td>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have made the suggested changes and pushed the code

@pavanshekar pavanshekar force-pushed the issue-38116 branch 3 times, most recently from c9c6b5a to a019aca Compare January 23, 2025 14:29
@@ -192,7 +193,7 @@ test('Can open Remove wizard and remove version from environment with hosts', as
fireEvent.click(getByText('Next'));
await patientlyWaitFor(() => {
expect(getByText('Review details')).toBeInTheDocument();
expect(getByText('1 host will be moved to content view cv2 in')).toBeInTheDocument();
expect(getByTestId('single-cv-hosts-remove')).toBeInTheDocument();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated test case to use getTestById for reliable assertion of FormattedMessage, as its text spans multiple elements.

Copy link
Member

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

Tested and seems to work as expected, except for one comment below.

<FlexItem data-testid="single-cv-hosts-remove">
<FormattedMessage
id="single-cv-hosts-remove"
defaultMessage="{count, plural, one {{singular}} other {# {plural}}} will be moved to content view {cvName} in {envName}."
Copy link
Member

Choose a reason for hiding this comment

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

This is omitting the number:
image

<FlexItem data-testid="single-cv-activation-keys-remove">
<FormattedMessage
id="single-cv-activation-keys-remove"
defaultMessage="{count, plural, one {{singular}} other {# {plural}}} will be moved to content view {cvName} in {envName}."
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
defaultMessage="{count, plural, one {{singular}} other {# {plural}}} will be moved to content view {cvName} in {envName}."
defaultMessage="{count, plural, one {# {singular}} other {# {plural}}} will be moved to content view {cvName} in {envName}."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack...

Copy link
Member

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

Another edge case I found:

The correct content view environment label for "Library/Default Organization View" should just be "Library" (without the "Default Organization View").

image

I'm thinking it may be best to alter the rabl somehow to provide the correct content view environment labels, and then we wouldn't have to assemble them on the frontend..

Comment on lines 141 to 143
defaultMessage="Content view {envSingularOrPlural} {envCV} will be removed from {count, plural, one {# {keySingular}} other {# {keyPlural}}}."
values={{
envSingularOrPlural: selectedEnv?.length === 1 ? __('environment') : __('environments'),
Copy link
Member

Choose a reason for hiding this comment

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

These plurals will not be properly translated, since plural rules can be different in different languages.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have resolved the edge case and the plurals translation issue and pushed the updated code. The changes have been implemented in the frontend because other pages are using rabl for rendering. Please let me know if this approach is okay.

<FormattedMessage
id="environment.plural"
defaultMessage="{count, plural, one {environment} other {environments}}"
values={{ count: selectedEnv?.length }}
Copy link
Member

Choose a reason for hiding this comment

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

The values object must contain values for every placeholder in the defaultMessage string. Looks like this one is missing environment and environments

<FlexItem>
<FormattedMessage
id="multi-cv-hosts-remove"
defaultMessage="Content view {envSingularOrPlural} {envCV} will be removed from {count, plural, one {# {hostSingular}} other {# {hostPlural}}}."
Copy link
Member

Choose a reason for hiding this comment

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

plural isn't defined. I think maybe it needs this?

Suggested change
defaultMessage="Content view {envSingularOrPlural} {envCV} will be removed from {count, plural, one {# {hostSingular}} other {# {hostPlural}}}."
defaultMessage="Content view {envSingularOrPlural} {envCV} will be removed from {count, hostPlural, one {# {hostSingular}} other {# {hostPlural}}}."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the formatted message for correct translations. The plural keyword is used to handle singular and plural forms based on the hostCount value.

</p>
<FormattedMessage
id="multi-cv-activation-keys-remove"
defaultMessage="Content view {envSingularOrPlural} {envCV} will be removed from {count, plural, one {# {keySingular}} other {# {keyPlural}}}."
Copy link
Member

Choose a reason for hiding this comment

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

Same, missing definition for plural

),
envCV: selectedCVE
?.map(cve => cve.label)
.join(', '),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated and optimized the logic, making it more concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants