Functions/Add-GraylogSidecarConfigurationAssignment.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<#
    .SYNOPSIS
        Assign a configuration for the graylog node and collector.
 
    .DESCRIPTION
        Invoke a web request on the Graylog API to add the specified collector
        and configuration to the node.
 
    .EXAMPLE
        PS C:\> Add-GraylogSidecarConfigurationAssignment -Uri 'https://graylog.contoso.com/api' -Credential $cred -NodeId '99b5a37c-a277-444e-b8fd-c261c18ac5bd' -CollectorId '5d06f16771c02a78f6ed644f' -ConfigurationId '5d06ed64702a71f168f6c74f
        Add the specified configuration.
#>

function Add-GraylogSidecarConfigurationAssignment
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Uri,

        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $Credential,

        [Parameter(Mandatory = $true)]
        [System.String]
        $NodeId,

        [Parameter(Mandatory = $true)]
        [System.String]
        $CollectorId,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ConfigurationId
    )

    # Cerate basic authoriazion as base64 encoded string
    $auth = 'Basic {0}' -f [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(('{0}:{1}' -f $Credential.Username, $Credential.GetNetworkCredential().Password)))

    try
    {
        # Get the current graylog sidecar configuration, including assignments
        $graylogSidecarSplat = @{
            Method  = 'Get'
            Uri     = '{0}/sidecars/{1}' -f $Uri.TrimEnd('/'), $NodeId
            Headers = @{
                Authorization = $auth
            }
        }
        $graylogSidecar = Invoke-RestMethod @graylogSidecarSplat -ErrorAction 'Stop'

        # Create a new assignment array without our collector id
        $assignments = @($graylogSidecar.assignments | Where-Object { $_.collector_id -ne $CollectorId })

        # Now add the desired collector assignment
        $assignments += @{
            collector_id     = $CollectorId
            configuration_id = $ConfigurationId
        }

        # Create a new request to push the desired assignments
        $graylogSidecarConfigurationAssignmentSplat = @{
            Method  = 'Put'
            Uri     = '{0}/sidecars/configurations' -f $Uri.TrimEnd('/')
            Body    = (@{
                nodes = @(
                    @{
                        node_id     = $NodeId
                        assignments = $assignments
                    }
                )
            } | ConvertTo-Json -Depth 4)
            ContentType = 'application/json'
            Headers = @{
                Authorization    = $auth
                'X-Requested-By' = 'XMLHttpRequest'
            }
        }
        Invoke-RestMethod @graylogSidecarConfigurationAssignmentSplat -ErrorAction 'Stop'
    }
    catch
    {
        throw $_
    }
}