summaryrefslogtreecommitdiffstats
path: root/ansible_collections/lowlydba/sqlserver/plugins/modules/maintenance_solution.ps1
blob: 6f94fb400fd56820cc29478fef0b29519b370af1 (plain)
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
#!powershell
# -*- coding: utf-8 -*-

# (c) 2022, John McCall (@lowlydba)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

#AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils
#Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" }

$ErrorActionPreference = "Stop"

$spec = @{
    supports_check_mode = $true
    options = @{
        backup_location = @{type = 'str'; required = $false }
        cleanup_time = @{type = 'int'; required = $false; default = 0 }
        output_file_dir = @{type = 'str'; required = $false }
        replace_existing = @{type = 'bool'; required = $false; }
        log_to_table = @{type = 'bool'; required = $false; default = $false }
        solution = @{type = 'str'; required = $false; choices = @('All', 'Backup', 'IntegrityCheck', 'IndexOptimize'); default = 'All' }
        install_jobs = @{type = 'bool'; required = $false; default = $false }
        local_file = @{type = 'str'; required = $false }
        database = @{type = 'str'; required = $true }
        force = @{type = 'bool'; required = $false; default = $false }
        install_parallel = @{type = 'bool'; required = $false; default = $false }
    }
}

$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
$sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
$database = $module.Params.database
$backupLocation = $module.Params.backup_location
$outputFileDirectory = $module.Params.output_file_dir
$cleanupTime = $module.Params.cleanup_time
$replaceExisting = $module.Params.replace_existing
$solution = $module.Params.solution
$installJobs = $module.Params.install_jobs
$installParallel = $module.Params.install_parallel
$logToTable = $module.Params.log_to_table
$localFile = $module.Params.local_file
$force = $module.Params.force
$checkMode = $module.CheckMode
$module.Result.changed = $false

try {
    $maintenanceSolutionSplat = @{
        SqlInstance = $sqlInstance
        SqlCredential = $sqlCredential
        Database = $database
        LogToTable = $logToTable
        Solution = $solution
        InstallJobs = $installJobs
        InstallParallel = $installParallel
        WhatIf = $checkMode
        Force = $force
        Confirm = $false
        EnableException = $true
    }
    if ($null -ne $localFile) {
        $maintenanceSolutionSplat.LocalFile = $localFile
    }
    if ($null -ne $backupLocation) {
        $maintenanceSolutionSplat.BackupLocation = $backupLocation
    }
    if ($null -ne $outputFileDirectory) {
        $maintenanceSolutionSplat.OutputFileDirectory = $outputFileDirectory
    }
    if ($installJobs -eq $true -and $null -ne $cleanupTime) {
        $maintenanceSolutionSplat.CleanupTime = $cleanupTime
    }
    # Only pass if true, otherwise removes warning that is used to track changed=$false
    if ($replaceExisting -eq $true) {
        $maintenanceSolutionSplat.ReplaceExisting = $replaceExisting
    }

    try {
        $output = Install-DbaMaintenanceSolution @maintenanceSolutionSplat
        $module.Result.changed = $true
    }
    catch {
        $errMessage = $_.Exception.Message
        if ($errMessage -like "*Maintenance Solution already exists*") {
            $server = Connect-DbaInstance -SqlInstance $sqlInstance -SqlCredential $sqlCredential
            $output = [PSCustomObject]@{
                ComputerName = $server.ComputerName
                InstanceName = $server.ServiceName
                SqlInstance = $server.DomainInstanceName
                Results = "Success"
            }
        }
        else {
            Write-Error -Message $errMessage
        }
    }

    if ($null -ne $output) {
        $resultData = ConvertTo-SerializableObject -InputObject $output
        $module.Result.data = $resultData
    }
    $module.ExitJson()
}
catch {
    $module.FailJson("Installing Maintenance Solution failed.", $_)
}