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

Fix tree item state issues by adding account and tenant to ids #1008

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/commands/createResourceGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { window } from 'vscode';
import { AzureSubscription } from '../../api/src/index';
import { ext } from '../extensionVariables';
import { SubscriptionItem } from '../tree/azure/SubscriptionItem';
import { getAccountAndTenantPrefix } from '../tree/azure/idPrefix';
import { createActivityContext } from '../utils/activityUtils';
import { localize } from '../utils/localize';

Expand Down Expand Up @@ -37,5 +38,5 @@ export async function createResourceGroup(context: IActionContext, node?: Subscr
if (!wizardContext.suppressNotification) {
void window.showInformationMessage(localize('createdRg', 'Created resource group "{0}".', newResourceGroupName));
}
ext.azureTreeState.notifyChildrenChanged(`/subscriptions/${subscription.subscriptionId}`);
ext.azureTreeState.notifyChildrenChanged(`${getAccountAndTenantPrefix(subscription)}/subscriptions/${subscription.subscriptionId}`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AzureResource, AzureSubscription } from '../../../../api/src/index';
import { createResourceGroup } from '../../../api/DefaultAzureResourceProvider';
import { ext } from '../../../extensionVariables';
import { isResourceGroupGroupingItem, ResourceGroupGroupingItem } from '../../../tree/azure/grouping/ResourceGroupGroupingItem';
import { getAccountAndTenantPrefix } from '../../../tree/azure/idPrefix';
import { createActivityContext } from '../../../utils/activityUtils';
import { createResourceClient } from '../../../utils/azureClients';
import { localize } from '../../../utils/localize';
Expand Down Expand Up @@ -111,7 +112,7 @@ async function deleteResourceGroups(context: IActionContext, subscription: Azure
}
}

void ext.azureTreeState.runWithTemporaryDescription(rg.id, localize('deleting', 'Deleting...'), async () => {
void ext.azureTreeState.runWithTemporaryDescription(`${getAccountAndTenantPrefix(rg.subscription)}${rg.id}`, localize('deleting', 'Deleting...'), async () => {
const wizard = new AzureWizard<DeleteResourceGroupContext>({
subscription: createSubscriptionContext(subscription),
resourceGroupToDelete: rg.name,
Expand All @@ -123,7 +124,7 @@ async function deleteResourceGroups(context: IActionContext, subscription: Azure
});

await wizard.execute();
ext.azureTreeState.notifyChildrenChanged(`/subscriptions/${rg.subscription.subscriptionId}`);
ext.azureTreeState.notifyChildrenChanged(`${getAccountAndTenantPrefix(rg.subscription)}/subscriptions/${rg.subscription.subscriptionId}`);
});
}
}
5 changes: 5 additions & 0 deletions src/tree/TreeItemState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class TreeItemStateStore implements vscode.Disposable {

/**
* Notify a resource that its children have changed.
*
* @param id id needs to include the account and tenant id
*/
notifyChildrenChanged(id: string): void {
this.onDidUpdateStateEmitter.fire(id);
Expand All @@ -51,6 +53,9 @@ export class TreeItemStateStore implements vscode.Disposable {
});
}

/**
* @param id id needs to include the account and tenant id
*/
async runWithTemporaryDescription<T = void>(id: string, description: string, callback: () => Promise<T>): Promise<T> {
let result: T;
this.update(id, { ...this.getState(id), temporaryDescription: description, spinner: true });
Expand Down
6 changes: 3 additions & 3 deletions src/tree/azure/AzureResourceItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createPortalUrl } from '../../utils/v2/createPortalUrl';
import { BranchDataItemCache } from '../BranchDataItemCache';
import { BranchDataItemOptions, BranchDataItemWrapper } from '../BranchDataItemWrapper';
import { ResourceGroupsItem } from '../ResourceGroupsItem';
import { createAzureIdPrefix } from './idPrefix';
import { getAccountAndTenantPrefix } from './idPrefix';

export class AzureResourceItem<T extends AzureResource> extends BranchDataItemWrapper {
constructor(
Expand All @@ -28,7 +28,7 @@ export class AzureResourceItem<T extends AzureResource> extends BranchDataItemWr
}

override readonly portalUrl: Uri;
readonly id = `${createAzureIdPrefix(this.resource.subscription)}${this.resource.id}`;
readonly id = `${getAccountAndTenantPrefix(this.resource.subscription)}${this.resource.id}`;
readonly tagsModel = new ResourceTags(this.resource);

override async getParent(): Promise<ResourceGroupsItem | undefined> {
Expand Down Expand Up @@ -59,6 +59,6 @@ export function createResourceItemFactory<T extends AzureResource>(itemCache: Br
itemCache.createOrGetItem(
branchItem,
() => new AzureResourceItem(resource, branchItem, branchDataProvider, itemCache, parent, options),
`${createAzureIdPrefix(resource.subscription)}${resource.id}`,
`${getAccountAndTenantPrefix(resource.subscription)}${resource.id}`,
);
}
4 changes: 2 additions & 2 deletions src/tree/azure/SubscriptionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createPortalUrl } from "../../utils/v2/createPortalUrl";
import { ResourceGroupsItem } from "../ResourceGroupsItem";
import { ResourceGroupsTreeContext } from "../ResourceGroupsTreeContext";
import { AzureResourceGroupingManager } from "./grouping/AzureResourceGroupingManager";
import { createAzureIdPrefix } from "./idPrefix";
import { getAccountAndTenantPrefix } from "./idPrefix";

export class SubscriptionItem implements ResourceGroupsItem {
constructor(
Expand All @@ -29,7 +29,7 @@ export class SubscriptionItem implements ResourceGroupsItem {
...subscription
};

this.id = `${createAzureIdPrefix(this.subscription)}/subscriptions/${subscription.subscriptionId}`;
this.id = `${getAccountAndTenantPrefix(this.subscription)}/subscriptions/${subscription.subscriptionId}`;
this.description = description ? description : '';

this.portalUrl = createPortalUrl(this.subscription, `/subscriptions/${this.subscription.subscriptionId}`);
Expand Down
6 changes: 3 additions & 3 deletions src/tree/azure/grouping/GroupingItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ResourceGroupsItem } from '../../ResourceGroupsItem';
import { ResourceGroupsTreeContext } from '../../ResourceGroupsTreeContext';
import { BranchDataProviderFactory } from '../AzureResourceBranchDataProviderManager';
import { ResourceItemFactory } from '../AzureResourceItem';
import { createAzureIdPrefix } from '../idPrefix';
import { getAccountAndTenantPrefix } from '../idPrefix';
import { GroupingItemFactoryOptions } from './GroupingItemFactory';

export class GroupingItem implements ResourceGroupsItem {
Expand Down Expand Up @@ -57,7 +57,7 @@ export class GroupingItem implements ResourceGroupsItem {
} : undefined;

if (this.context?.subscription) {
this.id = `${createAzureIdPrefix(this.context?.subscription)}/subscriptions/${this.context?.subscriptionContext.subscriptionId}/groupings/${this.label}`;
this.id = `${getAccountAndTenantPrefix(this.context?.subscription)}/subscriptions/${this.context?.subscriptionContext.subscriptionId}/groupings/${this.label}`;
} else {
// favorites groups don't always have a subscription
this.id = `/groupings/${this.label}`;
Expand Down Expand Up @@ -122,7 +122,7 @@ export class GroupingItem implements ResourceGroupsItem {
data: resource.raw
},
// prefix child items with the account and tenant id
idPrefix: createAzureIdPrefix(resource.subscription),
idPrefix: getAccountAndTenantPrefix(resource.subscription),
};

items.push(this.resourceItemFactory(resource, resourceItem, branchDataProvider, this, options));
Expand Down
4 changes: 2 additions & 2 deletions src/tree/azure/grouping/ResourceGroupGroupingItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ITagsModel, ResourceTags } from "../../../commands/tags/TagFileSystem";
import { canFocusContextValue, showHiddenTypesSettingKey } from "../../../constants";
import { settingUtils } from "../../../utils/settingUtils";
import { createPortalUrl } from "../../../utils/v2/createPortalUrl";
import { createAzureIdPrefix } from "../idPrefix";
import { getAccountAndTenantPrefix } from "../idPrefix";
import { GroupingItem, GroupingItemOptions } from "./GroupingItem";
import { GroupingItemFactoryOptions } from "./GroupingItemFactory";

Expand All @@ -36,7 +36,7 @@ export class ResourceGroupGroupingItem extends GroupingItem {
...this.resourceGroup.subscription,
...createSubscriptionContext(resourceGroup.subscription),
};
this.id = `${createAzureIdPrefix(this.subscription)}${resourceGroup.id}`;
this.id = `${getAccountAndTenantPrefix(this.subscription)}${resourceGroup.id}`;
this.tagsModel = new ResourceTags(resourceGroup);
this.contextValues.push('hasPortalUrl', 'azureResourceGroup', canFocusContextValue);
}
Expand Down
5 changes: 4 additions & 1 deletion src/tree/azure/idPrefix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { AzureSubscription } from "api/src/resources/azure";

export function createAzureIdPrefix(subscription: AzureSubscription): string {
/**
* @returns `/accounts/<account id>/tenants/<tenant id>`
*/
export function getAccountAndTenantPrefix(subscription: AzureSubscription): string {
return `/accounts/${subscription.account?.id}/tenants/${subscription.tenantId}`;
}
Loading