summaryrefslogtreecommitdiffstats
path: root/ansible_collections/lowlydba/sqlserver/plugins/modules/rg_resource_pool.ps1
blob: 22838e177643357253b4f91ee9d800964c1fa751 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
#!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 = @{
        resource_pool = @{type = 'str'; required = $true }
        type = @{type = 'str'; required = $false; default = 'Internal'; choices = @('Internal', 'External') }
        max_cpu_perc = @{type = 'int'; required = $false; }
        min_cpu_perc = @{type = 'int'; required = $false; }
        cap_cpu_perc = @{type = 'int'; required = $false; }
        max_mem_perc = @{type = 'int'; required = $false; }
        min_mem_perc = @{type = 'int'; required = $false; }
        min_iops_per_vol = @{type = 'int'; required = $false; }
        max_iops_per_vol = @{type = 'int'; required = $false; }
        state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') }
    }
}

$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
$state = $module.Params.state
$sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
$options = @{
    SqlInstance = $sqlInstance
    SqlCredential = $sqlCredential
    ResourcePool = $module.Params.resource_pool
    Type = $module.Params.type
    MaximumCpuPercentage = $module.Params.max_cpu_perc
    MinimumCpuPercentage = $module.Params.min_cpu_perc
    CapCpuPercentage = $module.Params.cap_cpu_perc
    MaximumMemoryPercentage = $module.params.max_mem_perc
    MinimumMemoryPercentage = $module.Params.min_mem_perc
    MinimumIOPSPerVolume = $module.params.min_iops_per_vol
    MaximumIOPSPerVolume = $module.params.max_iops_per_vol
}
$checkMode = $module.CheckMode
$module.Result.changed = $false

[System.Collections.ArrayList]$compareProperty = @(
    'MaximumCpuPercentage',
    'MinimumCpuPercentage',
    'CapCpuPercentage',
    'MinimumMemoryPercentage',
    'MaximumMemoryPercentage',
    'MinimumIOPSPerVolume',
    'MaximumIOPSPerVolume'
)

$optionsToRemove = @()
foreach ($item in $options.GetEnumerator() ) {
    if ($null -eq $item.Value) {
        $optionsToRemove += $item.Name
    }
}
foreach ($item in $optionsToRemove) {
    $options.Remove($item)
    $compareProperty.Remove($item)
}

try {
    $getPoolParams = @{
        SqlInstance = $options.SqlInstance
        SqlCredential = $options.SqlCredential
        Type = $options.Type
        EnableException = $true
    }
    $existingResourcePool = Get-DbaRgResourcePool @getPoolParams | Where-Object Name -eq $options.ResourcePool

    if ($state -eq "absent") {
        if ($null -ne $existingResourcePool) {
            $removePoolParams = @{
                SqlInstance = $options.SqlInstance
                SqlCredential = $options.SqlCredential
                Type = $options.Type
                ResourcePool = $options.ResourcePool
                WhatIf = $checkMode
                EnableException = $true
                Confirm = $false
            }
            $output = Remove-DbaRgResourcePool @removePoolParams
            $module.Result.changed = $true
        }
    }
    elseif ($state -eq "present") {
        $options.Add("WhatIf", $checkMode)
        if ($null -ne $existingResourcePool) {
            # Check for value parity
            $diff = Compare-Object -ReferenceObject $options -DifferenceObject $existingResourcePool -Property $compareProperty
            if ($null -ne $diff) {
                # Set to new values
                $output = Set-DbaRgResourcePool @options -EnableException
                $module.Result.changed = $true
            }
        }
        else {
            # Create a resource pool
            $output = New-DbaRgResourcePool @options -EnableException
            $module.Result.changed = $true
        }
    }
    if ($null -ne $output) {
        $resultData = ConvertTo-SerializableObject -InputObject $output
        $module.Result.data = $resultData
    }
    $module.ExitJson()
}
catch {
    $module.FailJson("Configuring resource pool failed: $($_.Exception.Message)", $_)
}