module armExplorer { enum ARMUrlParts { Protocol , Blank , DomainName , SubscriptionsKey , SubscriptionsValue , ResourceGroupsKey , ResourceGroupsValue , ProviderKey , ProviderValue , ResourceType1Name , ResourceType1Value } export class ARMUrlParser { private originalUrl : string = "" ; private url : string = "" ; private urlParts : Array < string > = [ ] ; constructor ( private value , private actions ) { this . url = value . url ; this . originalUrl = value . url ; if ( this . isSecureGet ( this . value . httpMethod , this . url ) ) { this . url = this . value . url . replace ( "/list" , "" ) ; } this . urlParts = this . url . split ( "/" ) ; } private isSecureGet ( httpMethod , url ) { return ( httpMethod . toLowerCase ( ) === HttpVerb [ HttpVerb . POST ] . toLowerCase ( ) ) && url . toLowerCase ( ) . endsWith ( "/list" ) ; } private hasResourceType ( ) { return this . urlParts . length > ARMUrlParts . ResourceType1Name ; } private hasResourceName ( ) { return this . urlParts . length > ARMUrlParts . ResourceType1Value ; } getSubscriptionId ( ) { return this . urlParts [ ARMUrlParts . SubscriptionsValue ] ; } getResourceGroup ( ) { return this . urlParts [ ARMUrlParts . ResourceGroupsValue ] ; } getAPIVersion ( ) { return this . value . resourceDefinition . apiVersion ; } getURL ( ) { return this . value . url ; } getResourceDefinitionChildren ( ) { return this . value . resourceDefinition . children ; } getOriginalURL ( ) { return this . originalUrl ; } getHttpMethod ( ) { return this . value . httpMethod ; } getActions ( ) { return this . actions ; } getResourceActions ( ) { return this . value . resourceDefinition . actions ; } hasResourceProvider ( ) { return this . urlParts . length > ARMUrlParts . ProviderKey ; } isResourceGroupURL ( ) { return this . urlParts . length === ( ARMUrlParts . ResourceGroupsKey + 1 ) ; } getResourceIdentifier ( ) { let resourceIdentifier = { } as ResourceIdentifier ; if ( ! this . hasResourceType ( ) ) { resourceIdentifier . resourceIdentifierType = ResourceIdentifierType . WithIDOnly ; resourceIdentifier . resourceId = this . url . replace ( "https://management.azure.com" , "" ) ; } else { resourceIdentifier . resourceGroup = this . urlParts [ ARMUrlParts . ResourceGroupsValue ] ; resourceIdentifier . resourceType = this . urlParts [ ARMUrlParts . ProviderValue ] + "/" + this . urlParts [ ARMUrlParts . ResourceType1Name ] ; resourceIdentifier . resourceIdentifierType = ResourceIdentifierType . WithGroupType ; if ( this . hasResourceName ( ) ) { resourceIdentifier . resourceName = this . urlParts [ ARMUrlParts . ResourceType1Value ] ; resourceIdentifier . resourceIdentifierType = ResourceIdentifierType . WithGroupTypeName ; } for ( let i = ARMUrlParts . ResourceType1Value + 1 ; i < this . urlParts . length ; i ++ ) { if ( i % 2 === 1 ) { resourceIdentifier . resourceType += ( "/" + this . urlParts [ i ] ) ; } else { resourceIdentifier . resourceName += ( "/" + this . urlParts [ i ] ) ; } } } return resourceIdentifier ; } } }