import { StorageManagementClient , StorageManagementModels } from '@azure/arm-storage' ; import { isArray } from 'util' ; import { IActionContext , IAzureQuickPickItem , IStorageAccountWizardContext } from 'vscode-azureextensionui' ; import { localize } from '../localize' ; import { createStorageClient } from './azureClients' ; import { nonNullProp , nonNullValue } from './nonNull' ; function parseResourceId ( id ) { const matches = id . match ( /\/subscriptions\/(.*)\/resourceGroups\/(.*)\/providers\/(.*)\/(.*)/ ) ; if ( matches === null || matches . length < 3 ) { throw new Error ( localize ( 'InvalidResourceId' , 'Invalid Azure Resource Id' ) ) ; } return matches ; } export function getResourceGroupFromId ( id ) { return parseResourceId ( id ) [ 2 ] ; } export function getSubscriptionFromId ( id ) { return parseResourceId ( id ) [ 1 ] ; } export function getNameFromId ( id ) { return parseResourceId ( id ) [ 4 ] ; } export interface IBaseResourceWithName { name ? : string ; _description ? : string ; } export async function promptForResource < T extends IBaseResourceWithName > ( context , placeHolder , resourcesTask ) { const picksTask = resourcesTask . then ( ( resources ) => { const picks = ! isArray ( resources ) ? [ ] : < IAzureQuickPickItem < T > [ ] > ( resources . map ( ( r ) => r . name ? { data : r , label : r . name , description : r . _description } : undefined ) . filter ( ( p ) => p ) ) ; picks . push ( { label : localize ( 'skipForNow' , '$(clock) Skip for now' ) , data : undefined , suppressPersistence : true } ) ; return picks ; } ) ; const data = ( await context . ui . showQuickPick ( picksTask , { placeHolder } ) ) . data ; if ( data ?. name ) { context . valuesToMask . push ( data . name ) ; } return data ; } export interface IResourceResult { name : string ; connectionString : string ; } export async function getStorageConnectionString ( context ) { const client = await createStorageClient ( context ) ; const storageAccount = nonNullProp ( context , 'storageAccount' ) ; const name = nonNullProp ( storageAccount , 'name' ) ; const resourceGroup = getResourceGroupFromId ( nonNullProp ( storageAccount , 'id' ) ) ; const result = await client . storageAccounts . listKeys ( resourceGroup , name ) ; const key = nonNullProp ( nonNullValue ( nonNullProp ( result , 'keys' ) [ 0 ] , 'keys[0]' ) , 'value' ) ; let endpointSuffix = nonNullProp ( context . environment , 'storageEndpointSuffix' ) ; if ( endpointSuffix . startsWith ( '.' ) ) { endpointSuffix = endpointSuffix . substr ( 1 ) ; } return { name , connectionString : `DefaultEndpointsProtocol=https;AccountName=${ name };AccountKey=${ key };EndpointSuffix=${ endpointSuffix }` } ; }