#Connect-AzAccount

[string] $currentSubs = ""

function ApplyTeamTag (
    [string] $resourceId, 
    [string] $subscriptionId,
    [string] $tagValue) {

    if ($global:currentSubs -ne $subscriptionId) 
    {
        Set-AzContext -SubscriptionId $subscriptionId
        $global:currentSubs = $subscriptionId
    }
    try 
    {
        $tags = (Get-AzResource -ResourceId $resourceId).Tags
        $tags.team = $tagValue
        Update-AzTag -ResourceId $resourceId -Tag $tags -Operation Merge
    }
    catch {
         Write-Warning "Could not update $resourceId"
    }
}

function ApplyProductTag (
    [string] $resourceId, 
    [string] $subscriptionId,
    [string] $tagValue) {

    if ($global:currentSubs -ne $subscriptionId) {
        Set-AzContext -SubscriptionId $subscriptionId
        $global:currentSubs = $subscriptionId
    }
    try {
    $tags = (Get-AzResource -ResourceId $resourceId).Tags
    $tags.product = $tagValue
 
    Update-AzTag -ResourceId $resourceId -Tag $tags -Operation Merge
    }
    catch {
        Write-Warning "Could not update $resourceId"
    }
}

function ApplyEnvironmentTag (
    [string] $resourceId, 
    [string] $subscriptionId,
    [string] $tagValue) {

    if ($global:currentSubs -ne $subscriptionId) {
        Set-AzContext -SubscriptionId $subscriptionId
        $global:currentSubs = $subscriptionId
    }
    try {
    $tags = (Get-AzResource -ResourceId $resourceId).Tags
    $tags.environment = $tagValue
    Update-AzTag -ResourceId $resourceId -Tag $tags -Operation Merge
    }
    catch {
        Write-Warning "Could not update $resourceId"
    }
}

function ApplyDataTag (
    [string] $resourceId, 
    [string] $subscriptionId,
    [string] $tagValue) {

    if ($global:currentSubs -ne $subscriptionId) {
        Set-AzContext -SubscriptionId $subscriptionId
        $global:currentSubs = $subscriptionId
    }
    try {
    $tags = (Get-AzResource -ResourceId $resourceId).Tags
    $tags.data = $tagValue
 
    Update-AzTag -ResourceId $resourceId -Tag $tags -Operation Merge
    }
    catch {
        Write-Warning "Could not update $resourceId"
    }
}