Functions/Public/New-InfluxDbDatabase.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
function New-InfluxDbDatabase {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute(
        "PSAvoidUsingPlainTextForPassword", 
        "ReaderPassword", 
        Justification = "Convenience"
    )]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute(
        "PSAvoidUsingPlainTextForPassword", 
        "WriterPassword", 
        Justification = "Convenience"
    )]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute(
        "PSAvoidUsingUserNameAndPassWordParams", 
        "", 
        Justification = "Convenience"
    )]
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Database
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $ReaderUser
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $ReaderPassword
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $WriterUser
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $WriterPassword
    )
    
    begin {
        if (-not $PSBoundParameters.ContainsKey('Confirm')) {
            $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
        }
        if (-not $PSBoundParameters.ContainsKey('WhatIf')) {
            $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
        }
    }
    
    process {
        if ($Force -or $PSCmdlet.ShouldProcess("Add new database '$Database' (potentially with new reader and writer)?")) {
            Invoke-InfluxDbApi -Query "CREATE DATABASE $Database" -Method Post

            if ($PSBoundParameters.ContainsKey('ReaderUser') -and $PSBoundParameters.ContainsKey('ReaderPassword')) {
                New-InfluxDbUser -User $ReaderUser -Password $ReaderPassword
                Add-InfluxDbPrivilege -Database $Database -User $ReaderUser -Privilege Read
            }

            if ($PSBoundParameters.ContainsKey('WriterUser') -and $PSBoundParameters.ContainsKey('WriterPassword')) {
                New-InfluxDbUser -User $WriterUser -Password $WriterPassword
                Add-InfluxDbPrivilege -Database $Database -User $WriterUser -Privilege All
            }
        }
    }
}