private/Remove-AdfObject.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function Remove-AdfObject {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true)] [Adf] $adfSource,
        [parameter(Mandatory = $true)] $obj,
        [parameter(Mandatory = $true)] $adfInstance
    )

    $name = $obj.Name
    $err = $null
    $ErrorMessage = $null
    $simtype = Get-SimplifiedType -Type $obj.GetType().Name

    [AdfObjectName] $oname = [AdfObjectName]::new("$simType.$name")
    $IsExcluded = $oname.IsNameExcluded($adfSource.PublishOptions)
    if (-not $IsExcluded) {
        Write-Host "Removing object: [$simtype].[$name]"
        $action = $simtype
    } else {
        $action = "DoNothing"
        Write-Verbose "Object [$simtype].[$name] won't be deleted as publish option 'DoNotDeleteExcludedObjects' = true."
    }

    Try 
    {
        switch -Exact ($action)
        {
            "Dataset" {
                Remove-AzDataFactoryV2Dataset `
                    -ResourceGroupName $ResourceGroupName `
                    -DataFactoryName $DataFactoryName `
                    -Name $name `
                    -Force -ErrorVariable err | Out-Null
            }
            "DataFlow" {
                Remove-AzDataFactoryV2DataFlow `
                    -ResourceGroupName $ResourceGroupName `
                    -DataFactoryName $DataFactoryName `
                    -Name $name `
                    -Force -ErrorVariable err | Out-Null
            }
            "Pipeline" {
                Remove-AzDataFactoryV2Pipeline `
                    -ResourceGroupName $ResourceGroupName `
                    -DataFactoryName $DataFactoryName `
                    -Name $name `
                    -Force -ErrorVariable err | Out-Null
            }
            "LinkedService" {
                Remove-AzDataFactoryV2LinkedService `
                    -ResourceGroupName $ResourceGroupName `
                    -DataFactoryName $DataFactoryName `
                    -Name $name `
                    -Force -ErrorVariable err | Out-Null
            }
            "IntegrationRuntime" {
                Remove-AzDataFactoryV2IntegrationRuntime `
                    -ResourceGroupName $ResourceGroupName `
                    -DataFactoryName $DataFactoryName `
                    -Name $name `
                    -Force -ErrorVariable err | Out-Null
            }
            "Trigger" {
                Remove-AzDataFactoryV2Trigger `
                    -ResourceGroupName $ResourceGroupName `
                    -DataFactoryName $DataFactoryName `
                    -Name $name `
                    -Force -ErrorVariable err | Out-Null
            }
            "DoNothing" {

            }
            default
            {
                Write-Error "ADFT0018: Type $($obj.GetType().Name) is not supported."
            }
        }
    }
    Catch {
        Write-Debug "$err"
        $ErrorMessage = $_.Exception.Message
    }

    # if ($ErrorMessage -match 'Error Code: TriggerEnabledCannotUpdate')
    # {
    # Write-host "Disabling trigger: $name..."
    # Stop-AzDataFactoryV2Trigger `
    # -ResourceGroupName $ResourceGroupName `
    # -DataFactoryName $DataFactoryName `
    # -Name $name `
    # -Force | Out-Null
    # Remove-AdfObject -obj $obj -adfInstance $adfInstance
    # }

    if ($ErrorMessage -match 'deleted since it is referenced by (?<RefName>.+)\.')
    {
        Write-Verbose "The document cannot be deleted since it is referenced by $($Matches.RefName)."
        #$Matches.RefName
        $refobj = $adfInstance.AllObjects() | Where-Object { $_.Name -eq $Matches.RefName }
        $refobj | ForEach-Object {
            Remove-AdfObject -adfSource $adfSource -obj $_ -adfInstance $adfInstance
        }
        Remove-AdfObject -adfSource $adfSource -obj $obj -adfInstance $adfInstance
    } elseif ($null -ne $ErrorMessage) {
        #Rethrow exception
        throw $ErrorMessage
    }

}