mirror of
https://github.com/harvester/harvester-ui-extension.git
synced 2025-12-14 05:31:44 +00:00
refactor: move logic to node model
Signed-off-by: Yi-Ya Chen <yiya.chen@suse.com> (cherry picked from commit 43b10b250ef1dc89982a54677c53de040b009f4d)
This commit is contained in:
parent
1cb16f0c74
commit
2604101eb2
@ -3,7 +3,7 @@ import ResourceTable from '@shell/components/ResourceTable';
|
|||||||
import Loading from '@shell/components/Loading';
|
import Loading from '@shell/components/Loading';
|
||||||
import { STATE, NAME, AGE } from '@shell/config/table-headers';
|
import { STATE, NAME, AGE } from '@shell/config/table-headers';
|
||||||
import {
|
import {
|
||||||
CAPI, METRIC, NODE, SCHEMA, LONGHORN, POD, MANAGEMENT, NORMAN
|
CAPI, METRIC, NODE, SCHEMA, LONGHORN, POD
|
||||||
} from '@shell/config/types';
|
} from '@shell/config/types';
|
||||||
import { allHash } from '@shell/utils/promise';
|
import { allHash } from '@shell/utils/promise';
|
||||||
import metricPoller from '@shell/mixins/metric-poller';
|
import metricPoller from '@shell/mixins/metric-poller';
|
||||||
@ -62,42 +62,8 @@ export default {
|
|||||||
_hash.machines = this.$store.dispatch(`${ inStore }/findAll`, { type: CAPI.MACHINE });
|
_hash.machines = this.$store.dispatch(`${ inStore }/findAll`, { type: CAPI.MACHINE });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
|
||||||
this.$store.getters['rancher/schemaFor'](NORMAN.PRINCIPAL) &&
|
|
||||||
this.$store.getters['rancher/schemaFor'](NORMAN.CLUSTER_ROLE_TEMPLATE_BINDING)
|
|
||||||
) {
|
|
||||||
_hash.normanPrincipal = this.$store.dispatch('rancher/findAll', { type: NORMAN.PRINCIPAL });
|
|
||||||
_hash.clusterRoleTemplateBinding = this.$store.dispatch(`management/findAll`, { type: MANAGEMENT.CLUSTER_ROLE_TEMPLATE_BINDING });
|
|
||||||
}
|
|
||||||
|
|
||||||
const hash = await allHash(_hash);
|
const hash = await allHash(_hash);
|
||||||
|
|
||||||
// Remove delete action if current user role is cluster member
|
|
||||||
if (hash.normanPrincipal && hash.clusterRoleTemplateBinding) {
|
|
||||||
const role = hash.clusterRoleTemplateBinding.find(
|
|
||||||
(template) => template.userPrincipalName === hash.normanPrincipal[0]?.id
|
|
||||||
);
|
|
||||||
const isClusterMember = role?.roleTemplateName === 'cluster-member';
|
|
||||||
|
|
||||||
if (isClusterMember) {
|
|
||||||
hash.nodes = hash.nodes.map((node) => {
|
|
||||||
const updatedActions = node.availableActions.map((action) => {
|
|
||||||
return action.action === 'promptRemove' ? { ...action, enabled: false } : action;
|
|
||||||
});
|
|
||||||
|
|
||||||
// keep availableActions non-enumerable
|
|
||||||
Object.defineProperty(node, 'availableActions', {
|
|
||||||
value: updatedActions,
|
|
||||||
writable: true,
|
|
||||||
enumerable: false,
|
|
||||||
configurable: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
return node;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.rows = hash.nodes;
|
this.rows = hash.nodes;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import pickBy from 'lodash/pickBy';
|
import pickBy from 'lodash/pickBy';
|
||||||
import { CAPI, LONGHORN, POD, NODE } from '@shell/config/types';
|
import { CAPI, LONGHORN, POD, NODE, NORMAN } from '@shell/config/types';
|
||||||
import { CAPI as CAPI_ANNOTATIONS } from '@shell/config/labels-annotations.js';
|
import { CAPI as CAPI_ANNOTATIONS } from '@shell/config/labels-annotations.js';
|
||||||
import { HCI as HCI_ANNOTATIONS } from '@pkg/harvester/config/labels-annotations';
|
import { HCI as HCI_ANNOTATIONS } from '@pkg/harvester/config/labels-annotations';
|
||||||
import { clone } from '@shell/utils/object';
|
import { clone } from '@shell/utils/object';
|
||||||
@ -25,7 +25,17 @@ const HEALTHY = 'healthy';
|
|||||||
const WARNING = 'warning';
|
const WARNING = 'warning';
|
||||||
|
|
||||||
export default class HciNode extends HarvesterResource {
|
export default class HciNode extends HarvesterResource {
|
||||||
|
constructor(...args) {
|
||||||
|
super(...args);
|
||||||
|
this._roleBasedActions = [];
|
||||||
|
this._initialized = false; // flag to prevent repeated initialization
|
||||||
|
}
|
||||||
|
|
||||||
get _availableActions() {
|
get _availableActions() {
|
||||||
|
if (!this._initialized) {
|
||||||
|
this.setupRoleBasedActions();
|
||||||
|
}
|
||||||
|
|
||||||
const cordon = {
|
const cordon = {
|
||||||
action: 'cordon',
|
action: 'cordon',
|
||||||
enabled: this.hasAction('cordon') && !this.isCordoned,
|
enabled: this.hasAction('cordon') && !this.isCordoned,
|
||||||
@ -108,10 +118,44 @@ export default class HciNode extends HarvesterResource {
|
|||||||
shutDown,
|
shutDown,
|
||||||
powerOn,
|
powerOn,
|
||||||
reboot,
|
reboot,
|
||||||
...super._availableActions
|
...this._roleBasedActions || []
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async setupRoleBasedActions() {
|
||||||
|
const baseActions = super._availableActions || [];
|
||||||
|
|
||||||
|
// access control is only available on the multiple cluster Harvester
|
||||||
|
if (this.$rootGetters['isStandaloneHarvester']) {
|
||||||
|
this._roleBasedActions = baseActions;
|
||||||
|
} else {
|
||||||
|
this._roleBasedActions = await this._updateRoleBasedActions(baseActions);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
async _updateRoleBasedActions(actions) {
|
||||||
|
const hasSchema = (type) => this.$rootGetters['rancher/schemaFor'](type);
|
||||||
|
|
||||||
|
if (!hasSchema(NORMAN.PRINCIPAL) || !hasSchema(NORMAN.CLUSTER_ROLE_TEMPLATE_BINDING)) return actions;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const templates = await this.$dispatch('rancher/findAll', { type: NORMAN.CLUSTER_ROLE_TEMPLATE_BINDING }, { root: true });
|
||||||
|
const [currentUser] = this.$rootGetters['rancher/all'](NORMAN.PRINCIPAL) || [];
|
||||||
|
const userRole = templates.find((template) => template.userPrincipalId === currentUser?.id);
|
||||||
|
|
||||||
|
if (userRole?.roleTemplateId === 'cluster-member') {
|
||||||
|
return actions.filter((action) => action.action !== 'promptRemove');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error('Error fetching role-based actions:', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return actions;
|
||||||
|
}
|
||||||
|
|
||||||
promptRemove(resources = this) {
|
promptRemove(resources = this) {
|
||||||
this.$dispatch('promptModal', {
|
this.$dispatch('promptModal', {
|
||||||
resources,
|
resources,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user