private/steps/Update-ConfluenceSpaces.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
function Update-ConfluenceSpaces {
    [CmdletBinding()]
    param (
        # Keys of specific spaces to pull (if null/empty, will get all spaces)
        [Parameter(Position=0)]
        [string[]]
        $SpaceKeys = @(),

        # Synchronization options for spaces
        [Parameter(Position=1)]
        [AllowNull()]
        [hashtable]
        $SyncOptions,

        # The ID value of the refresh underway
        [Parameter(Mandatory,Position=2)]
        [int]
        $RefreshId,

        # The sql instance to update data in
        [Parameter(Mandatory,Position=3)]
        [string]
        $SqlInstance,

        # The sql database to update data in
        [Parameter(Mandatory,Position=4)]
        [string]
        $SqlDatabase,

        # The schema to use when updating data
        [Parameter(Position=5)]
        [string]
        $SchemaName="dbo"
    )
    
    begin {
        Write-Verbose "Updating Spaces"
        $spaceTableName = "tbl_stg_Confluence_Space"
        $spaces = @()

        $permissionTableName = "tbl_stg_Confluence_Space_Permission"
        $permissions = @()
        $getPermissions = $null -ne $SyncOptions -and $SyncOptions.ContainsKey("Permissions") -and $SyncOptions.Permissions -ne $false
        if (!$getPermissions) { Write-Verbose "Skipping Space Permissions" }

        $sqlConnSplat = @{
            DatabaseServer = $SqlInstance
            DatabaseName = $SqlDatabase
            SchemaName = $SchemaName
        }
    }
    
    process {
        #set up to loop through results (there is a limit of 25 per call, so we have to loop to make sure we got them all)
        $limit = 25
        $currentPage = 1
        $returnCount = 0
        do {
            Write-Verbose "Fetching Page $currentPage of spaces"
            #get a response
            $restParams = @{
                MaxResults = $limit
                StartAt = $limit * ($currentPage - 1)
                Expand = @("icon")
            }
            if ($null -ne $SpaceKeys -and $SpaceKeys.Count -gt 0) { $restParams.Add("SpaceKeys", $SpaceKeys) }
            if ($getPermissions) { $restParams.Expand += "permissions" }
            $results = Invoke-ConfluenceGetSpaces @restParams

            #map spaces
            $mappedSpaces = $results.results | Read-ConfluenceSpace -RefreshId $RefreshId -ReadPermissions:$getPermissions

            #look for and remove duplicates
            $existingKeys = @()
            $existingKeys += $spaces | ForEach-Object { $_.Space_Key }
            $newSpaces = $mappedSpaces | Where-Object { $existingKeys -notcontains $_.Space_Key }

            #handle permissions storage, if configured
            if ($getPermissions) {
                #get the permissions objects, then remove the property from the space objects so it's not written to the db
                $permissions += $newSpaces | ForEach-Object { $_.Permissions }
                $newSpaces = $newSpaces | Select-Object -ExcludeProperty Permissions -Property '*'
            }

            #add space results to master list, checking for duplicates
            $spaces += $newSpaces

            #check how many results came back
            $returnCount = $results.size

            #move the start marker forward
            $currentPage += 1

            #loop again if we got the max number of results (implying that we haven't go them all yet)
        } while ($returnCount -eq $limit)
    }
    
    end {
        Write-AtlassianData @sqlConnSplat -Data $spaces -TableName $spaceTableName
        if ($getPermissions) { Write-AtlassianData @sqlConnSplat -Data $permissions -TableName $permissionTableName }
    }
}