Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Pre-fill existing security groups and flavors on edit
Browse files Browse the repository at this point in the history
  • Loading branch information
mturley committed Sep 28, 2018
1 parent 5b2ce43 commit 9980e9b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class PlanWizardInstancePropertiesStep extends Component {
instancePropertiesRowsAction,
bestFitFlavorAction,
bestFitFlavorUrl,
recalculateBestFitFlavorAndSecurityGroup
recalculateBestFitFlavorAndSecurityGroup,
editingPlan
} = this.props;

const targetTenants =
Expand All @@ -39,7 +40,7 @@ class PlanWizardInstancePropertiesStep extends Component {
});
});

bestFitFlavorAction(bestFitFlavorUrl, sourceAndDestinationHrefSlugsForBestFit);
bestFitFlavorAction(bestFitFlavorUrl, sourceAndDestinationHrefSlugsForBestFit, editingPlan);
}
);

Expand Down Expand Up @@ -106,7 +107,8 @@ PlanWizardInstancePropertiesStep.propTypes = {
instancePropertiesRows: PropTypes.array,
bestFitFlavorUrl: PropTypes.string,
recalculateBestFitFlavorAndSecurityGroup: PropTypes.bool,
bestFitFlavors: PropTypes.array
bestFitFlavors: PropTypes.array,
editingPlan: PropTypes.object
};

PlanWizardInstancePropertiesStep.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const queryTenantsWithAttributesAction = (url, tenantIds, attributes) =>
return _getTenantWithAttributesActionCreator(uri.toString(), tenantIds);
};

export const setFlavorsAndSecurityGroups = (response, mappings, dispatch) => {
export const setFlavorsAndSecurityGroups = (response, mappings, editingPlan, dispatch) => {
const vmsSlugPrefix = 'vms/';
const flavorsSlugPrefix = 'flavors/';
const cloudTenantsSlugPrefix = 'cloud_tenants/';
Expand All @@ -41,11 +41,11 @@ export const setFlavorsAndSecurityGroups = (response, mappings, dispatch) => {

dispatch({
type: SET_V2V_BEST_FIT_FLAVORS_AND_DEFAULT_SECURITY_GROUPS,
payload: vmBestFitFlavors
payload: { vmBestFitFlavors, editingPlan }
});
};

export const _bestFitFlavorActionCreator = (url, flavor_mappings) => dispatch => {
export const _bestFitFlavorActionCreator = (url, flavor_mappings, editingPlan) => dispatch => {
const postBody = {
action: 'vm_flavor_fit',
mappings: flavor_mappings
Expand All @@ -56,7 +56,7 @@ export const _bestFitFlavorActionCreator = (url, flavor_mappings) => dispatch =>
API.post(url, postBody)
.then(response => {
resolve(response);
setFlavorsAndSecurityGroups(response, flavor_mappings, dispatch);
setFlavorsAndSecurityGroups(response, flavor_mappings, editingPlan, dispatch);
})
.catch(e => {
reject(e);
Expand All @@ -65,9 +65,9 @@ export const _bestFitFlavorActionCreator = (url, flavor_mappings) => dispatch =>
});
};

export const bestFitFlavorAction = (url, mappings) => {
export const bestFitFlavorAction = (url, mappings, editingPlan) => {
const uri = new URI(url);
return _bestFitFlavorActionCreator(uri.toString(), mappings);
return _bestFitFlavorActionCreator(uri.toString(), mappings, editingPlan);
};

export const instancePropertiesRowsAction = rows => dispatch =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ export default (state = initialState, action) => {
.set('isFetchingBestFitFlavor', false)
.set('isRejectedBestFitFlavor', true);
case SET_V2V_BEST_FIT_FLAVORS_AND_DEFAULT_SECURITY_GROUPS: {
const vmBestFitFlavors = action.payload;
const { vmBestFitFlavors, editingPlan } = action.payload;
const instancePropertiesRowsUpdatedWithBestFlavor = [];
vmBestFitFlavors.forEach(vmFlavor => {
const existingInstancePropertiesRow = state.instancePropertiesRows.find(row => row.id === vmFlavor.vm_id);
const tenant = state.tenantsWithAttributes.find(
tenantsWithAttribute => tenantsWithAttribute.id === vmFlavor.tenant_id
);
const tenantFlavors = tenant && tenant.flavors;
const tenantSecurityGroups = tenant && tenant.security_groups;

let bestFitFlavor;
let bestFitFlavorId = vmFlavor.flavor_id;
Expand All @@ -85,10 +86,29 @@ export default (state = initialState, action) => {
const defaultSecurityGroupId = defaultSecurityGroup && defaultSecurityGroup.id;
const defaultSecurityGroupName = defaultSecurityGroup && defaultSecurityGroup.name;

const bestFlavor = { name: bestFitFlavorName, id: bestFitFlavorId };
const bestGroup = { name: defaultSecurityGroupName, id: defaultSecurityGroupId };

let preselectedFlavor;
let preselectedGroup;

if (editingPlan) {
const { actions } = editingPlan.options.config_info;
const existingVm = actions.find(action => action.vm_id === existingInstancePropertiesRow.id);
if (existingVm) {
const existingFlavor =
tenantFlavors && tenantFlavors.find(flavor => flavor.id === existingVm.osp_flavor);
const existingGroup =
tenantSecurityGroups && tenantSecurityGroups.find(group => group.id === existingVm.osp_security_group);
preselectedFlavor = existingFlavor && { name: existingFlavor.name, id: existingFlavor.id };
preselectedGroup = existingGroup && { name: existingGroup.name, id: existingGroup.id };
}
}

const rowUpdatedWithBestFlavor = {
...existingInstancePropertiesRow,
osp_flavor: { name: bestFitFlavorName, id: bestFitFlavorId },
osp_security_group: { name: defaultSecurityGroupName, id: defaultSecurityGroupId },
osp_flavor: preselectedFlavor || bestFlavor,
osp_security_group: preselectedGroup || bestGroup,
target_cluster_name: tenant.name
};
instancePropertiesRowsUpdatedWithBestFlavor.push(rowUpdatedWithBestFlavor);
Expand Down

0 comments on commit 9980e9b

Please sign in to comment.