module armExplorer { export class ScriptParametersResolver { private supportedCommands : RMCommandInfo [ ] = [ ] ; constructor ( private urlParser ) { this . fillSupportedCommands ( ) ; } private fillSupportedCommands ( ) { let resourceActions = this . urlParser . getResourceActions ( ) ; if ( this . urlParser . getHttpMethod ( ) . contains ( HttpVerb [ HttpVerb . GET ] , true ) ) { this . supportedCommands . push ( { cmd : CmdType . Get , isAction : false , isSetAction : false } ) ; } else if ( this . urlParser . getHttpMethod ( ) . contains ( HttpVerb [ HttpVerb . POST ] , true ) && this . urlParser . getOriginalURL ( ) . contains ( "list" , true ) ) { this . supportedCommands . push ( { cmd : CmdType . Invoke , isAction : false , isSetAction : false } ) ; } if ( resourceActions . some ( a => ( a . toUpperCase ( ) === ResourceActions [ ResourceActions . PATCH ] || a . toUpperCase ( ) === ResourceActions [ ResourceActions . PUT ] ) ) ) { if ( resourceActions . includes ( ResourceActions [ ResourceActions . GET ] ) ) { this . supportedCommands . push ( { cmd : CmdType . Set , isAction : false , isSetAction : true } ) ; } else { this . supportedCommands . push ( { cmd : CmdType . New , isAction : false , isSetAction : true } ) ; } } if ( resourceActions . includes ( ResourceActions [ ResourceActions . CREATE ] ) ) { if ( this . urlParser . isResourceGroupURL ( ) ) { this . supportedCommands . push ( { cmd : CmdType . NewResourceGroup , isAction : false , isSetAction : false } ) ; } else { this . supportedCommands . push ( { cmd : CmdType . New , isAction : false , isSetAction : false } ) ; } } if ( this . urlParser . getActions ( ) . length > 0 ) { this . urlParser . getActions ( ) . forEach ( action => { if ( action . httpMethod . toUpperCase ( ) === HttpVerb [ HttpVerb . DELETE ] ) { this . supportedCommands . push ( { cmd : CmdType . RemoveAction , isAction : true , isSetAction : false } ) ; } else if ( action . httpMethod . toUpperCase ( ) === HttpVerb [ HttpVerb . POST ] ) { this . supportedCommands . push ( { cmd : CmdType . InvokeAction , isAction : true , isSetAction : false } ) ; } } ) ; } } getResourceGroup ( ) { return this . urlParser . getResourceGroup ( ) ; } getSubscriptionId ( ) { return this . urlParser . getSubscriptionId ( ) ; } getCompleteResourceId ( ) { return this . urlParser . getOriginalURL ( ) . substring ( this . urlParser . getOriginalURL ( ) . indexOf ( "/subscriptions" ) ) ; } getSupportedCommands ( ) { return this . supportedCommands ; } private doGetActionName ( url ) { return url . substr ( url . lastIndexOf ( "/" ) + 1 , url . length - url . lastIndexOf ( "/" ) - 1 ) ; } getActionName ( ) { return this . doGetActionName ( this . urlParser . getURL ( ) ) ; } getActionParameters ( actionIndex ) { return this . urlParser . getActions ( ) [ actionIndex ] ; } getActionNameFromAction ( actionIndex ) { return this . doGetActionName ( this . getActionParameters ( actionIndex ) . url ) ; } getActionNameFromList ( ) { return this . doGetActionName ( this . urlParser . getURL ( ) . replace ( "/list" , "" ) ) ; } getResourceName ( ) { return this . urlParser . getURL ( ) . substr ( this . urlParser . getURL ( ) . lastIndexOf ( "/" ) + 1 , this . urlParser . getURL ( ) . length - this . urlParser . getURL ( ) . lastIndexOf ( "/" ) - 2 ) ; } private supportsCollection ( resourceName ) { return ! ! ( ! resourceName && ( typeof this . urlParser . getResourceDefinitionChildren ( ) === "string" ) ) && this . urlParser . hasResourceProvider ( ) ; } getParameters ( ) { let cmd = { } as CmdletParameters ; cmd . apiVersion = this . urlParser . getAPIVersion ( ) ; cmd . resourceIdentifier = this . urlParser . getResourceIdentifier ( ) ; cmd . isCollection = this . supportsCollection ( cmd . resourceIdentifier . resourceName ) ; return cmd ; } } }