src/cloudSubscriptions.psm1

Set-StrictMode -Version Latest
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\common.ps1"

function _buildURL {
   if(-not $env:TEAM_ACCT) {
      throw 'You must call Add-TeamAccount before calling any other functions in this module.'
   }

   $resource = "/distributedtask/serviceendpointproxy/azurermsubscriptions"
   $instance = $env:TEAM_ACCT

   # Build the url to list the projects
   return $instance + '/_apis' + $resource
}

function _applyTypes {
   param($item)

   $item.PSObject.TypeNames.Insert(0, 'Team.AzureSubscription')
}

function Get-CloudSubscription {
   [CmdletBinding()]
   param()

   # Build the url
   $url = _buildURL

   # Call the REST API
   $resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Uri $url -Headers @{Authorization = "Basic $env:TEAM_PAT"}

   # Apply a Type Name so we can use custom format view and custom type extensions
   foreach($item in $resp.value) {
      _applyTypes -item $item
   }

   Write-Output $resp.value
}

Export-ModuleMember -Alias * -Function Get-CloudSubscription