Skip to content

Commit

Permalink
Harden Azure Hybrid Benefit toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielweyer committed Jan 12, 2024
1 parent 94ee7d9 commit 1695096
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
28 changes: 22 additions & 6 deletions parser/src/azurePortalExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as puppeteer from 'puppeteer';
import { PartialVmPricing, VmPricing } from "./vmPricing";
import { isSelectedSelect, setSelect } from './puppeteerExtensions';
import { json } from 'stream/consumers';
import { isSelectedSelect, setSelect, isSelectedCheckbox } from './puppeteerExtensions';

export class AzurePortal {
private p: puppeteer.Page;
private azureHybridBenefitButtonSelector = 'button#isAhb';

constructor(page: puppeteer.Page) {
this.p = page;
Expand Down Expand Up @@ -73,8 +73,7 @@ export class AzurePortal {

if (hasHybridBenefit) {
savingsPlanWithHybridBenefits = savingsPlanPricing;
await this.p.click('button#isAhb');
await this.waitForPriceWithoutHybridBenefits();
await this.hideAzureHybridBenefit();
savingsPlansWithoutHybridBenefits = await this.p.evaluate(() => getPricing());

if (savingsPlanWithHybridBenefits.length !== savingsPlansWithoutHybridBenefits.length) {
Expand Down Expand Up @@ -110,8 +109,7 @@ export class AzurePortal {

if (hasHybridBenefit) {
reservedWithoutHybridBenefits = reservedPricing;
await this.p.click('button#isAhb');
await this.waitForPriceWithHybridBenefits();
await this.showAzureHybridBenefit();
reservedWithHybridBenefits = await this.p.evaluate(() => getPricing());

if (reservedWithoutHybridBenefits.length !== reservedWithHybridBenefits.length) {
Expand Down Expand Up @@ -229,6 +227,24 @@ export class AzurePortal {
);
}

private async hideAzureHybridBenefit(): Promise<void> {
if (!await isSelectedCheckbox(this.p, this.azureHybridBenefitButtonSelector)) {
return;
}

await this.p.click(this.azureHybridBenefitButtonSelector);
await this.waitForPriceWithoutHybridBenefits();
}

private async showAzureHybridBenefit(): Promise<void> {
if (await isSelectedCheckbox(this.p, this.azureHybridBenefitButtonSelector)) {
return;
}

await this.p.click(this.azureHybridBenefitButtonSelector);
await this.waitForPriceWithHybridBenefits();
}

private async waitForPriceWithoutHybridBenefits(): Promise<void> {
await this.p.waitForFunction(
() => {
Expand Down
5 changes: 5 additions & 0 deletions parser/src/puppeteerExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export async function isSelectedSelect(page: puppeteer.Page, selector: string, v
return selectedValue === value;
}

export async function isSelectedCheckbox(page: puppeteer.Page, selector: string): Promise<boolean> {
const value = await page.$eval(selector, node => (<HTMLButtonElement> node).value);
return value === 'true';
}

async function getSelectedValue(page: puppeteer.Page, selector: string): Promise<string> {
const fullSelector = getSelectFullSelector(selector);
await page.waitForSelector(fullSelector, { visible: true });
Expand Down

0 comments on commit 1695096

Please sign in to comment.