import { ProgressLocation , window } from "vscode" ; import { IActionContext } from "vscode-azureextensionui" ; import { INamedValuesTreeItemContext , NamedValuesTreeItem } from "../explorer/NamedValuesTreeItem" ; import { NamedValueTreeItem } from "../explorer/NamedValueTreeItem" ; import { ServiceTreeItem } from "../explorer/ServiceTreeItem" ; import { ext } from "../extensionVariables" ; import { localize } from "../localize" ; export async function createNamedValue ( context , node ? ) { if ( ! node ) { const serviceNode = < ServiceTreeItem > await ext . tree . showTreeItemPicker ( ServiceTreeItem . contextValue , context ) ; node = serviceNode . namedValuesTreeItem ; } context . key = await askId ( ) ; context . value = await askValue ( ) ; context . secret = await isSecret ( ) ; window . withProgress ( { location : ProgressLocation . Notification , title : localize ( "createNamedValue" , `Creating named value '${ context . key }' in API Management service ${ node . root . serviceName } ...` ) , cancellable : false } , async ( ) => { return node ! . createChild ( context ) ; } ) . then ( async ( ) => { await node ! . refresh ( context ) ; window . showInformationMessage ( localize ( "creatededNamedValue" , `Created named value '${ context . key }' succesfully.` ) ) ; } ) ; } export async function updateNamedValue ( context , node ? ) { if ( ! node ) { node = < NamedValueTreeItem > await ext . tree . showTreeItemPicker ( NamedValueTreeItem . contextValue , context ) ; } const displayName = node . propertyContract . displayName ; const initialValue = await node . getValue ( ) ; const value = await askValue ( initialValue ) ; const secret = await isSecret ( ) ; window . withProgress ( { location : ProgressLocation . Notification , title : localize ( "updateNamedValue" , `Updating value for '${ displayName }' ...` ) , cancellable : false } , async ( ) => { return node ! . updateValue ( context , value , secret ) ; } ) . then ( async ( ) => { await node ! . refresh ( context ) ; window . showInformationMessage ( localize ( "updatedNamedValue" , `Updated value for '${ displayName }' succesfully.` ) ) ; } ) ; } async function isSecret ( ) { const options = [ 'Yes' , 'No' ] ; const option = await ext . ui . showQuickPick ( options . map ( ( s ) => { return { label : s , description : '' , detail : '' } ; } ) , { placeHolder : 'Is this a secret?' , canPickMany : false } ) ; if ( option . label === options [ 0 ] ) { return true ; } return undefined ; } async function askId ( ) { const idPrompt = localize ( 'idPrompt' , 'Enter id' ) ; return ( await ext . ui . showInputBox ( { prompt : idPrompt , validateInput : async ( value ) : Promise < string | undefined > => { value = value ? value . trim ( ) : '' ; return validateId ( value ) ; } } ) ) . trim ( ) ; } async function askValue ( initialValue ? ) { const valuePrompt = localize ( 'valuePrompt' , 'Enter value' ) ; return ( await ext . ui . showInputBox ( { prompt : valuePrompt , value : initialValue , validateInput : async ( value ) : Promise < string | undefined > => { value = value ? value . trim ( ) : '' ; if ( value === '' ) { return localize ( "valueInvalid" , 'value cannot be empty.' ) ; } return undefined ; } } ) ) ; } function validateId ( id ) { const test = "^[\w]+$)|(^[\w][\w\-]+[\w]$" ; if ( id . match ( test ) === null ) { return localize ( "idInvalid" , 'Invalid API Name.' ) ; } return undefined ; }