-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azure: Add wizard steps to list/create
UserAssignedIdentities
and e…
…xecute role definitions (#1757) * Add wizard steps to list/create UserAssignedIdentities and execute role definitions * Add output log to role assignment creation * Fix create message * PR feedback * Better comments * Slightly more clarification * More logical line breaks * Fix user identity assigned string * Probably dont need the location code considering most wizards have a location
- Loading branch information
Showing
9 changed files
with
307 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { type RoleDefinition } from '@azure/arm-authorization'; | ||
import { AzureWizardExecuteStep, nonNullValueAndProp } from '@microsoft/vscode-azext-utils'; | ||
import { randomUUID } from 'crypto'; | ||
import { l10n, Progress } from 'vscode'; | ||
import * as types from '../../index'; | ||
import { createAuthorizationManagementClient } from '../clients'; | ||
import { ext } from '../extensionVariables'; | ||
|
||
export class RoleAssignmentExecuteStep<T extends types.IResourceGroupWizardContext> extends AzureWizardExecuteStep<T> { | ||
public priority: number = 900; | ||
private getScopeId: () => string | undefined; | ||
private _roleDefinition: RoleDefinition; | ||
public constructor(getScopeId: () => string | undefined, roleDefinition: RoleDefinition) { | ||
super(); | ||
this.getScopeId = getScopeId; | ||
this._roleDefinition = roleDefinition; | ||
} | ||
|
||
public async execute(wizardContext: T, progress: Progress<{ message?: string; increment?: number }>): Promise<void> { | ||
const amClient = await createAuthorizationManagementClient(wizardContext) | ||
const scope = this.getScopeId(); | ||
if (!scope) { | ||
throw new Error(l10n.t('No scope was provided for the role assignment.')); | ||
} | ||
const scopeSplitArr = scope.split('/'); | ||
const resourceName = scopeSplitArr[scopeSplitArr.length - 1] ?? ''; | ||
const resourceType = scopeSplitArr[scopeSplitArr.length - 2] ?? ''; | ||
|
||
const guid = randomUUID(); | ||
const roleDefinitionId = this._roleDefinition.id as string; | ||
const principalId = nonNullValueAndProp(wizardContext.managedIdentity, 'principalId'); | ||
|
||
await amClient.roleAssignments.create(scope, guid, { roleDefinitionId, principalId }); | ||
const roleAssignmentCreated = l10n.t('Role assignment "{0}" created for the {2} resource "{1}".', this._roleDefinition.roleName ?? '', resourceName, resourceType); | ||
progress.report({ message: roleAssignmentCreated }); | ||
ext.outputChannel.appendLog(roleAssignmentCreated); | ||
} | ||
|
||
public shouldExecute(wizardContext: T): boolean { | ||
return !!wizardContext.managedIdentity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { ManagedServiceIdentityClient } from '@azure/arm-msi'; | ||
import { AzureWizardExecuteStep, nonNullValueAndProp } from '@microsoft/vscode-azext-utils'; | ||
import { l10n, Progress } from 'vscode'; | ||
import * as types from '../../index'; | ||
import { createManagedServiceIdentityClient } from '../clients'; | ||
import { storageProvider } from '../constants'; | ||
import { ext } from '../extensionVariables'; | ||
import { LocationListStep } from './LocationListStep'; | ||
|
||
/** | ||
* Naming constraints: | ||
* The resource name must start with a letter or number, | ||
* have a length between 3 and 128 characters and | ||
* can only contain a combination of alphanumeric characters, hyphens and underscores | ||
* But since we are appending "-identities" to the resource group name and that has the same constraints and a 90 character limit, | ||
* we don't need to do any verification | ||
**/ | ||
export class UserAssignedIdentityCreateStep<T extends types.IResourceGroupWizardContext> extends AzureWizardExecuteStep<T> { | ||
public priority: number = 140; | ||
|
||
public constructor() { | ||
super(); | ||
} | ||
|
||
public async execute(wizardContext: T, progress: Progress<{ message?: string; increment?: number }>): Promise<void> { | ||
const newLocation: string = (await LocationListStep.getLocation(wizardContext, storageProvider)).name; | ||
const rgName: string = nonNullValueAndProp(wizardContext.resourceGroup, 'name'); | ||
const newName: string = `${rgName}-identities`; | ||
const creatingUserAssignedIdentity: string = l10n.t('Creating user assigned identity "{0}" in location "{1}""...', newName, newLocation); | ||
ext.outputChannel.appendLog(creatingUserAssignedIdentity); | ||
progress.report({ message: creatingUserAssignedIdentity }); | ||
const msiClient: ManagedServiceIdentityClient = await createManagedServiceIdentityClient(wizardContext); | ||
wizardContext.managedIdentity = await msiClient.userAssignedIdentities.createOrUpdate( | ||
rgName, | ||
newName, | ||
{ | ||
location: newLocation | ||
} | ||
); | ||
const createdUserAssignedIdentity: string = l10n.t('Successfully created user assigned identity "{0}".', newName); | ||
ext.outputChannel.appendLog(createdUserAssignedIdentity); | ||
} | ||
|
||
public shouldExecute(wizardContext: T): boolean { | ||
return !wizardContext.managedIdentity; | ||
} | ||
} |
Oops, something went wrong.