Public/InfluxDB.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
function InfluxDB {
    [cmdletbinding()]
    param(
        [parameter(Mandatory, Position = 0)]
        [hashtable]$Options,

        [ValidateSet('Always', 'OnSuccess', 'OnFailure')]
        [string]$When = $script:Watchmen.Options.NotifierConditions.WatchmenTest
    )

    begin {
        Write-Debug -Message "Entering: $($PSCmdlet.MyInvocation.MyCommand.Name)"
        Assert-InWatchmen -Command $PSCmdlet.MyInvocation.MyCommand.Name
    }

    process {
        $i = [pscustomobject]@{
            PSTypeName = 'Watchmen.Notifier.InfluxDB'
            Type = 'InfluxDB'
            Url = $Options.Url
            Port = 8086
            Database = $Options.Database
            MeasurementName = 'watchmen_test'
            Tags = @{}
            RetentionPolicy = [string]::Empty
            WriteConsistency = [string]::Empty
            Timeout = 5
            Credential = $null
            UserAgent = 'Watchmen'
            Enabled = $true
            SkipSSLVerification = $false
            NotifierCondition = $When
        }

        $reservedTags = @('context', 'describe', 'filename', 'module', 'test')

        # Optional settings
        if ($Options.Port) { $i.Port = $Options.Port }
        if ($Options.MeasurementName) { $i.MeasurementName = $Options.MeasurementName }
        if ($Options.Tags) {
            foreach ($tag in $Options.Tags.Keys) {
                if ($reservedTags -contains $tag) {
                    Throw "Tag [$tag] has already been specified. Cannot overwrite reserved tags [context, describe, filename, module, test]"
                } else {
                    $i.Tags.$tag = $Options.Tags[$tag]
                }
            }
        }
        if ($Options.RetentionPolicy) { $i.RetentionPolicy = $Options.RetentionPolicy }
        if ($Options.WriteConsistency) { $i.WriteConsistency = $Options.WriteConsistency }
        if ($Options.Timeout) { $i.Timeout = $Options.Timeout }
        if ($Options.Credential) { $i.Credential = $Options.Credential }
        if ($Options.SkipSSLVerification) { $i.SkipSSLVerification = $Options.SkipSSLVerification }

        return $i
    }

    end {
        Write-Debug -Message "Exiting: $($PSCmdlet.MyInvocation.MyCommand.Name)"
    }
}