Skip to content

Commit

Permalink
Filter by AP type from inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrho committed Aug 13, 2024
1 parent 049ac49 commit 5d367b6
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 78 deletions.
2 changes: 1 addition & 1 deletion jccm/src/Frontend/Common/CommonVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

export const AppTitle = 'Juniper Cloud Connection Manager';
export const HeaderSpaceHeight = 45;
export const LeftSideSpaceWidth = 500;
export const LeftSideSpaceWidth = 600;
export const RightSideSpaceWidth = 200;
export const FooterSpaceHeight = 35;
export const LoginCardWidth = 600;
Expand Down
4 changes: 3 additions & 1 deletion jccm/src/Frontend/Common/StateStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ const useStore = create((set, get) => ({

if (org.inventory) {
org.inventory.forEach((device) => {
cloudDevices[device.serial] = device;
device.is_vmac_enabled
? (cloudDevices[device.original_serial] = device)
: (cloudDevices[device.serial] = device);
});
}
});
Expand Down
6 changes: 3 additions & 3 deletions jccm/src/Frontend/Components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export const Login = ({ isOpen, onClose }) => {
const { showMessageBar } = useMessageBar();
const [cloudList, setCloudList] = useState([]);

const [cloud, setCloud] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [cloud, setCloud] = useState('mist');
const [email, setEmail] = useState('[email protected]');
const [password, setPassword] = useState('Jinal@030691');

const [emailMessage, setEmailMessage] = useState('Please enter your email address.');
const [passwordMessage, setPasswordMessage] = useState('Please enter your password.');
Expand Down
4 changes: 2 additions & 2 deletions jccm/src/Frontend/Layout/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export default () => {
).length;

const doesSiteNameExist = (orgName, siteName) => {
const org = cloudInventory.find((item) => item.name === orgName);
const org = cloudInventory.find((item) => item?.name === orgName);

// If the organization is not found, return false
if (!org) {
return false;
}

// Check if the site name exists within the organization's sites array
const siteExists = org.sites.some((site) => site.name === siteName);
const siteExists = org.sites?.some((site) => site?.name === siteName);

return siteExists;
};
Expand Down
138 changes: 93 additions & 45 deletions jccm/src/Frontend/Layout/InventoryTreeMenuLocal.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ import {
useRestoreFocusTarget,
Spinner,
ProgressBar,
Accordion,
AccordionHeader,
AccordionItem,
AccordionPanel,
tokens,
} from '@fluentui/react-components';
import {
Expand Down Expand Up @@ -168,6 +172,8 @@ import {
ErrorCircleRegular,
FlagFilled,
WeatherThunderstormRegular,
WeatherPartlyCloudyDayRegular,
CloudRegular,
bundleIcon,
} from '@fluentui/react-icons';
import _ from 'lodash';
Expand All @@ -179,6 +185,7 @@ import { useContextMenu } from '../Common/ContextMenuContext';
import { copyToClipboard, capitalizeFirstChar } from '../Common/CommonVariables';
import { adoptDevices, executeJunosCommand, getDeviceFacts, releaseDevices } from './Devices';
import { RotatingIcon, CircleIcon } from './ChangeIcon';
import eventBus from '../Common/eventBus';

const MapIcon = bundleIcon(MapFilled, MapRegular);
const Rename = bundleIcon(RenameFilled, RenameRegular);
Expand Down Expand Up @@ -398,7 +405,26 @@ const InventoryTreeMenuLocal = () => {

useEffect(() => {
const isFact = !!deviceFacts[device._path];
const adopted = isFact ? !!cloudDevices[deviceFacts[device._path]?.serialNumber] : false;

const deviceSerial = deviceFacts[device._path]?.serialNumber;
const deviceHostname = deviceFacts[device._path]?.hostname;

// First method using serialNumber
let adopted = isFact ? !!cloudDevices[deviceSerial] : false;

if (!adopted && isFact) {
// Second method using hostname - used if the first method fails
// Now includes check for is_vmac_enabled being true
const namesMatchingHostname = Object.values(cloudDevices).filter(
(d) => d.name === deviceHostname && d.is_vmac_enabled === true
);

if (namesMatchingHostname.length > 0) console.log('namesMatchingHostname', namesMatchingHostname);

// Set adopted to true only if there is a unique match
adopted = namesMatchingHostname.length === 1;
}

if (adopted) {
const cloudDevice = cloudDevices[deviceFacts[device._path]?.serialNumber];
const cloudOrgName = cloudDevice.org_name;
Expand Down Expand Up @@ -558,6 +584,11 @@ const InventoryTreeMenuLocal = () => {
const isSelected = path === selectedTabValue;
const deviceFact = deviceFacts[path] ? useStore((state) => state.deviceFacts?.[path]) : null;
const deviceName = device.port === 22 ? device.address : `${device.address}:${device.port}`;
const cloudDevice = cloudDevices[deviceFact?.serialNumber];

// if (cloudDevice?.is_vmac_enabled) {
// console.log('CloudDevice:', cloudDevice);
// }

let facts = [];
if (deviceFact) {
Expand Down Expand Up @@ -628,13 +659,34 @@ const InventoryTreeMenuLocal = () => {
>
{deviceFact.hardwareModel}
</Text>
<Text
size={100}
font='numeric'
weight='normal'
>
{deviceFact.serialNumber}
</Text>
{cloudDevice?.is_vmac_enabled ? (
// <Text
// size={100}
// font='numeric'
// weight='normal'
// >
// {`${deviceFact.serialNumber} ↔ `}
// <span style={{ fontWeight: '500', color: tokens.colorPaletteLightGreenForeground1 }}>
// {cloudDevice.serial}
// </span>
// </Text>
<Text
size={100}
font='numeric'
weight='normal'
style={{ color: tokens.colorPaletteLightGreenForeground1 }}
>
{cloudDevice.serial}
</Text>
) : (
<Text
size={100}
font='numeric'
weight='normal'
>
{deviceFact.serialNumber}
</Text>
)}
<Text
size={100}
font='numeric'
Expand Down Expand Up @@ -684,7 +736,8 @@ const InventoryTreeMenuLocal = () => {
font='numeric'
style={{ color: 'purple' }}
>
🚫 Failed to get facts
🐞 Failed to retrieve facts. Please verify your inventory or device settings and try
again.
</Text>
)}
</div>
Expand Down Expand Up @@ -714,7 +767,7 @@ const InventoryTreeMenuLocal = () => {
font='numeric'
style={{ color: 'red' }}
>
🚫 Failed to be adopted
🐞 Failed to adopt. Please verify your inventory or device settings and try again.
</Text>
)}
</div>
Expand Down Expand Up @@ -815,22 +868,6 @@ const InventoryTreeMenuLocal = () => {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
const result = await adoptDevices(device, jsiTerm, deleteOutboundSSHTerm);
if (result.status) {
setTimeout(async () => {
const fetchAndUpdateCloudInventory = async () => {
try {
const data = await electronAPI.saGetCloudInventory();
if (data.cloudInventory) {
setCloudInventory(data.inventory);
setCloudInventoryFilterApplied(data.isFilterApplied);
}
} catch (error) {
console.error('Error fetching cloud inventory:', error);
}
};

await fetchAndUpdateCloudInventory();
}, 3000);

resetIsAdopting(device.path, false);
return;
} else {
Expand Down Expand Up @@ -907,31 +944,39 @@ const InventoryTreeMenuLocal = () => {
};

await adoptDeviceFactsWithRateLimit();

setTimeout(async () => {
await eventBus.emit('cloud-inventory-refresh', { notification: false });
}, 3000);
};

const actionReleaseDevice = async (device) => {
setIsReleasing(device.path, true);

const serialNumber = deviceFacts[device.path]?.serialNumber;
const organization = cloudDevices[serialNumber]?.org_name;
const deviceFact = deviceFacts[device.path];
const cloudDevice = cloudDevices[deviceFact?.serialNumber];

const serialNumber = cloudDevice?.serial;
const organization = cloudDevice?.org_name;

const result = await releaseDevices({ organization, serialNumber });
if (result.status) {
setTimeout(async () => {
const fetchAndUpdateCloudInventory = async () => {
try {
const data = await electronAPI.saGetCloudInventory();
if (data.cloudInventory) {
setCloudInventory(data.inventory);
setCloudInventoryFilterApplied(data.isFilterApplied);
}
} catch (error) {
console.error('Error fetching cloud inventory:', error);
}
};

await fetchAndUpdateCloudInventory();
}, 3000); // Delay of 3 seconds (3000 milliseconds)
// setTimeout(async () => {
// const fetchAndUpdateCloudInventory = async () => {
// try {
// const data = await electronAPI.saGetCloudInventory();
// if (data.cloudInventory) {
// setCloudInventory(data.inventory);
// setCloudInventoryFilterApplied(data.isFilterApplied);
// }
// } catch (error) {
// console.error('Error fetching cloud inventory:', error);
// }
// };

// await fetchAndUpdateCloudInventory();
// }, 3000); // Delay of 3 seconds (3000 milliseconds)
console.log(`Device(${serialNumber}) released successfully`);
} else {
notify(
<Toast>
Expand Down Expand Up @@ -996,6 +1041,9 @@ const InventoryTreeMenuLocal = () => {
};

await releaseDeviceFactsWithRateLimit();
setTimeout(async () => {
await eventBus.emit('cloud-inventory-refresh', { notification: false });
}, 3000);
};

const contextMenuContent = (event, node) => {
Expand Down Expand Up @@ -1141,15 +1189,15 @@ const InventoryTreeMenuLocal = () => {
};

const doesSiteNameExist = (orgName, siteName) => {
const org = cloudInventory.find((item) => item.name === orgName);
const org = cloudInventory.find((item) => item?.name === orgName);

// If the organization is not found, return false
if (!org) {
return false;
}

// Check if the site name exists within the organization's sites array
const siteExists = org.sites.some((site) => site.name === siteName);
const siteExists = org.sites?.some((site) => site?.name === siteName);

return siteExists;
};
Expand Down
Loading

0 comments on commit 5d367b6

Please sign in to comment.