Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/modules/rg_resource_pool.ps1

raw
1 #!powershell
2 # -*- coding: utf-8 -*-
3
4 # (c) 2022, John McCall (@lowlydba)
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 #AnsibleRequires -CSharpUtil Ansible.Basic
8 #AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils
9 #Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" }
10
11 $ErrorActionPreference = "Stop"
12
13 $spec = @{
14 supports_check_mode = $true
15 options = @{
16 resource_pool = @{type = 'str'; required = $true }
17 type = @{type = 'str'; required = $false; default = 'Internal'; choices = @('Internal', 'External') }
18 max_cpu_perc = @{type = 'int'; required = $false; }
19 min_cpu_perc = @{type = 'int'; required = $false; }
20 cap_cpu_perc = @{type = 'int'; required = $false; }
21 max_mem_perc = @{type = 'int'; required = $false; }
22 min_mem_perc = @{type = 'int'; required = $false; }
23 min_iops_per_vol = @{type = 'int'; required = $false; }
24 max_iops_per_vol = @{type = 'int'; required = $false; }
25 state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') }
26 }
27 }
28
29 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
30 $state = $module.Params.state
31 $sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
32 $options = @{
33 SqlInstance = $sqlInstance
34 SqlCredential = $sqlCredential
35 ResourcePool = $module.Params.resource_pool
36 Type = $module.Params.type
37 MaximumCpuPercentage = $module.Params.max_cpu_perc
38 MinimumCpuPercentage = $module.Params.min_cpu_perc
39 CapCpuPercentage = $module.Params.cap_cpu_perc
40 MaximumMemoryPercentage = $module.params.max_mem_perc
41 MinimumMemoryPercentage = $module.Params.min_mem_perc
42 MinimumIOPSPerVolume = $module.params.min_iops_per_vol
43 MaximumIOPSPerVolume = $module.params.max_iops_per_vol
44 }
45 $checkMode = $module.CheckMode
46 $module.Result.changed = $false
47
48 [System.Collections.ArrayList]$compareProperty = @(
49 'MaximumCpuPercentage',
50 'MinimumCpuPercentage',
51 'CapCpuPercentage',
52 'MinimumMemoryPercentage',
53 'MaximumMemoryPercentage',
54 'MinimumIOPSPerVolume',
55 'MaximumIOPSPerVolume'
56 )
57
58 $optionsToRemove = @()
59 foreach ($item in $options.GetEnumerator() ) {
60 if ($null -eq $item.Value) {
61 $optionsToRemove += $item.Name
62 }
63 }
64 foreach ($item in $optionsToRemove) {
65 $options.Remove($item)
66 $compareProperty.Remove($item)
67 }
68
69 try {
70 $getPoolParams = @{
71 SqlInstance = $options.SqlInstance
72 SqlCredential = $options.SqlCredential
73 Type = $options.Type
74 EnableException = $true
75 }
76 $existingResourcePool = Get-DbaRgResourcePool @getPoolParams | Where-Object Name -eq $options.ResourcePool
77
78 if ($state -eq "absent") {
79 if ($null -ne $existingResourcePool) {
80 $removePoolParams = @{
81 SqlInstance = $options.SqlInstance
82 SqlCredential = $options.SqlCredential
83 Type = $options.Type
84 ResourcePool = $options.ResourcePool
85 WhatIf = $checkMode
86 EnableException = $true
87 Confirm = $false
88 }
89 $output = Remove-DbaRgResourcePool @removePoolParams
90 $module.Result.changed = $true
91 }
92 }
93 elseif ($state -eq "present") {
94 $options.Add("WhatIf", $checkMode)
95 if ($null -ne $existingResourcePool) {
96 # Check for value parity
97 $diff = Compare-Object -ReferenceObject $options -DifferenceObject $existingResourcePool -Property $compareProperty
98 if ($null -ne $diff) {
99 # Set to new values
100 $output = Set-DbaRgResourcePool @options -EnableException
101 $module.Result.changed = $true
102 }
103 }
104 else {
105 # Create a resource pool
106 $output = New-DbaRgResourcePool @options -EnableException
107 $module.Result.changed = $true
108 }
109 }
110 if ($null -ne $output) {
111 $resultData = ConvertTo-SerializableObject -InputObject $output
112 $module.Result.data = $resultData
113 }
114 $module.ExitJson()
115 }
116 catch {
117 $module.FailJson("Configuring resource pool failed: $($_.Exception.Message)", $_)
118 }