Uh-Oh.psm1
Function Assert-StaytusSubscriber { <# .SYNOPSIS Set a staytus subscriber as verified .DESCRIPTION Set a Staytus subscriber as verified .PARAMETER EmailAddress The email address to mark as verified .EXAMPLE Assert-StaytusSubscriber -EmailAddress joe@widgetstore.com #> [CmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Assert-StaytusSubscriber.md")] Param( [Parameter(Mandatory)] [String] $EmailAddress ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { $endpoint = 'subscribers/verify' $irmParams = @{ Uri = "$($StaytusConnection.Hostname)/$($endpoint)" Header = $StaytusConnection.Header ContentType = 'application/json' Method = 'POST' } $Body = @{ email_address = $EmailAddress } $irmParams.Add('Body',($Body | ConvertTo-Json)) Invoke-RestMethod @irmParams } } Function Connect-StaytusServer { <# .SYNOPSIS Establishes session connection details for Staytus Server .DESCRIPTION Establishes session connection details for Staytus Server .PARAMETER StaytusServer Hostname of Staytus server. If non-standard port use hostname:port format .PARAMETER UseSSL Toggle using HTTPS as protocol. If non-standard SSL port is in use, ensure hostname:port is used in StaytusServer parameter .PARAMETER Credential X-Auth-Token (username) and X-Auth-Secret (password) for Staytus api user .EXAMPLE Connect-StaytusServer -StaytusServer staytus.contoso.com:8787 -Credential (Get-Credential) .EXAMPLE Connect-StaytusServer -StaytusServer staytus.contoso.com -Credential $credential .EXAMPLE Connect-StaytusServer -StaytusServer staytus.contoso.com -Credential $credential -UseSSL .NOTES General notes #> [cmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Connect-StaytusServer.md")] Param( [Parameter(Mandatory)] [Alias('Hostname')] [String] $StaytusServer, [Parameter()] [Switch] $UseSSL, [Parameter(Mandatory)] [PSCredential] $Credential ) process { switch ($UseSSL) { $true { $protocol = 'https' } default { $protocol = 'http' } } $StaytusConnection = $Script:StaytusConnection = @{ Hostname = "$($protocol)://$StaytusServer/api/v1" Header = @{ 'X-Auth-Token' = "$($Credential.Username)" 'X-Auth-Secret' = "$($Credential.GetNetworkCredential().Password)" } } } } function Get-StaytusIncident { <# .SYNOPSIS Returns incident data for Staytus events .DESCRIPTION Returns detailed status information for incidents raised in your Staytus system .PARAMETER Title The Title of the incident to look for .PARAMETER Id The Id of the incident to look for .PARAMETER State The State of the incident to look for .EXAMPLE Get-StaytusIncident .EXAMPLE Get-StaytusIncident -Id 1,36,4 .EXAMPLE Get-STaytusIncident -Title 'Website experiencing slow response','SQL Database degraded' .EXAMPLE Get-StaytusIncident -State Identified,Investigating #> [CmdletBinding(DefaultParameterSetName='Credential',HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Get-StaytusIncident.md")] Param( [Parameter(ParameterSetName='Title')] [String[]] $Title, [Parameter(ParameterSetName='Id')] [Int[]] $Id, [Parameter(ParameterSetName='State')] [ValidateSet('Investigating','Identified','Monitoring','Resolved')] [string[]] $State ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { $endpoint = 'issues/all' $irmParams = @{ Uri = "$($StaytusConnection.Hostname)/$($endpoint)" Header = $StaytusConnection.Header Method = 'GET' } $result = Invoke-RestMethod @irmParams switch($PSCmdlet.ParameterSetName) { 'Title' { Foreach($t in $title){ $result.data | Where-Object { $_.Title -eq $t} } } 'Id' { Foreach($i in $Id){ $result.data | Where-Object { $_.id -eq $i} } } 'State' { Foreach($s in $State){ $result.data | Where-Object { $_.state -eq $s} } } default { $result.data} } } } Function Get-StaytusService { <# .SYNOPSIS Return Service information for services defined in Staytus .DESCRIPTION Return Service information for services defined in Staytus .PARAMETER Service The Service to interrogate .EXAMPLE Return all service details Get-StaytusService .EXAMPLE Return service details for given service(s) Get-StaytusService -Service Storefront .EXAMPLE Get-StaytusService -Service Storefront,Sql #> [CmdletBinding(HelpUri = "https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Get-StaytusService.md")] Param( [Parameter()] [String[]] $Service ) begin { if (-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { if ($Service) { foreach($s in $Service) { $endpoint = 'services/info' $irmParams = @{ Header = $StaytusConnection.Header Method = 'GET' } $Body = @{ service = $s } $irmParams.Add('Uri', "$($StaytusConnection.Hostname)/$($endpoint)") $irmParams.Add('ContentType', 'application/json') $irmParams.Add('Body', ($Body | ConvertTo-Json)) (Invoke-RestMethod @irmParams).data } } else { $endpoint = 'services/all' $irmParams = @{ Header = $StaytusConnection.Header Method = 'GET' } $irmParams.Add('Uri', "$($StaytusConnection.Hostname)/$($endpoint)") (Invoke-RestMethod @irmParams).data } } } Function Get-StaytusSubscriber { <# .SYNOPSIS Get Staytus Subscriber information .DESCRIPTION Get Staytus Subscriber information .PARAMETER EmailAddress The email address to look-up .PARAMETER VerificationToken The verification token to look-up .EXAMPLE Get-StaytusSubscriber -EmailAddress joe@widgetstore.com .EXAMPLE Get-StaytusSubscriber -VerificationToken 70b89737-e8de-437d-8fd0-4f043789474b .NOTES General notes #> [cmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Get-StaytusSubscriber.md")] Param( [Parameter()] [String] $EmailAddress, [Parameter()] [String] $VerificationToken ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { $endpoint = 'subscribers/info' $irmParams = @{ Uri = "$($StaytusConnection.Hostname)/$($endpoint)" Header = $StaytusConnection.Header Method = 'GET' } if($PSBoundParameters){ if($EmailAddress){ $Body = @{ email_address = $EmailAddress } $irmParams.Add('ContentType','application/json') $irmParams.Add('Body',($Body | ConvertTo-Json)) } if($VerificationToken){ $Body = @{ verification_token = $VerificationToken } $irmParams.Add('ContentType','application/json') $irmParams.Add('Body',($Body | ConvertTo-Json)) } (Invoke-RestMethod @irmParams).data } else { (Invoke-RestMethod @irmParams).data } } } Function New-StaytusIncident { <# .SYNOPSIS Post a new incident to Staytus .DESCRIPTION Post a new service incident to your Staytus instance .PARAMETER Title The incident Title .PARAMETER Service The affected Service .PARAMETER Status Current Status .PARAMETER State Current State .PARAMETER NotifySubscribers Send email to subscribers .PARAMETER UseSSL Use SSL in URI. If using non-standard HTTPS port, ensure 'hostname:port' format of StaytusServer is correct .EXAMPLE New-StaytusIncident -Title 'PowerShell Test' -Service Junk -State Identified -Status major-outage .EXAMPLE $incidentParams = @{ Title = 'Well, that's not good. SQL is down!' Service = 'Sql Cluster' State = 'Identified' Status = 'major-outage' } New-StaytusIncident @incidentParams #> [CmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/New-StaytusIncident.md")] Param( [Parameter(Mandatory)] [String] $Title, [Parameter(Mandatory)] [String[]] $Service, [Parameter(Mandatory)] [ValidateSet('major-outage', 'operational', 'degraded-performance', 'partial-outage', 'maintenance')] [String] $Status, [Parameter(Mandatory)] [ValidateSet('Investigating', 'Identified', 'Monitoring', 'Resolved')] [String] $State, [Parameter()] [Switch] $NotifySubscribers, [Parameter()] [Switch] $UseSSL ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { $endpoint = 'issues/create' $irmParams = @{ ContentType = 'application/json' Uri = "$($StaytusConnection.Hostname)/$($endpoint)" Header = $StaytusConnection.Header Method = 'POST' } $Body = @{ title = $Title services = $Service status = $Status.ToLower() state = $State.ToLower() } if($NotifySubscribers){ $Body.Add('Notify',$true) } $irmParams.Add('Body',($Body | ConvertTo-Json)) Invoke-RestMethod @irmParams } } Function New-StaytusSubscriber{ <# .SYNOPSIS Add a subscriber to Staytus .DESCRIPTION Add a subscriber to Staytus .PARAMETER EmailAddress The email address of the subscriber .PARAMETER Verified Automatically mark the email address as verified .EXAMPLE New-StaytusSubscriber -EmailAddress joe@widgetstore.com .EXAMPLE New-StaytusSubscriber -EmailAddress joe@widgetstore.com -Verified #> [CmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/New-StaytusSubscriber.md")] Param( [Parameter()] [String] $EmailAddress, [Parameter()] [String] $Verified ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { $endpoint = 'subscribers/add' $irmParams = @{ Uri = "$($StaytusConnection.Hostname)/$($endpoint)" Header = $StaytusConnection.Header ContentType = 'application/json' Method = 'POST' } $Body = @{ email_address = $EmailAddress } if($Verified){ $Body.Add('verifictation',1) } $irmParams.Add('Body',($Body | ConvertTo-Json)) (Invoke-RestMethod @irmParams).data } } Function Remove-StaytusSubscriber { <# .SYNOPSIS Remove a Staytus subscriber .DESCRIPTION Remove a Staytus subscriber by email address .PARAMETER EmailAddress Email address of subscriber .EXAMPLE REmove-StaytusSubscriber -EmailAddress joe@widgetstore.com #> [CmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Remove-StaytusSubscriber.md",ConfirmImpact="High",SupportsShouldProcess)] Param( [Parameter(Mandatory)] [String] $EmailAddress ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { $endpoint = 'subscribers/delete' $irmParams = @{ Uri = "$($StaytusConnection.Hostname)/$($endpoint)" Header = $StaytusConnection.Header ContentType = 'application/json' Method = 'DELETE' } $Body = @{ email_address = $EmailAddress } $irmParams.Add('Body',($Body | ConvertTo-Json)) if($PSCmdlet.ShouldProcess($EmailAddress,"Remove Staytus subscriber")){ Invoke-RestMethod @irmParams } } } Function Send-StaytusVerificationEmail { <# .SYNOPSIS Send a verification email to a Staytus subscriber .DESCRIPTION Send a verification email to a Staytus subscriber .PARAMETER EmailAddress The email address to send verification too .EXAMPLE Send-StaytusVerificationEmail #> [CmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Send-StaytusVerificationEmail.md")] Param( [Parameter(Mandatory)] [String] $EmailAddress ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { $endpoint = 'subscribers/send_verification_email' $irmParams = @{ Uri = "$($StaytusConnection.Hostname)/$($endpoint)" Header = $StaytusConnection.Header ContentType = 'application/json' Method = 'POST' } $Body = @{ email_address = $EmailAddress } $irmParams.Add('Body',($Body | ConvertTo-Json)) Invoke-RestMethod @irmParams } } Function Set-StaytusService { <# .SYNOPSIS Set the status of a Staytus service .DESCRIPTION Set the status of a Staytus service .PARAMETER Service The service to adjust .PARAMETER Status The status to set the service too .EXAMPLE Set-StaytusService -Service 'Storefront' -Status operational #> [cmdletBinding(HelpUri = "https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Get-StaytusService.md")] Param( [Parameter(Mandatory)] [ArgumentCompleter( { param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams) $results = (Get-StaytusService).permalink if ($WordToComplete) { $results.Where{ $_ -match "^$WordToComplete" } } else { $results } } )] [String] $Service, [Parameter(Mandatory)] [ValidateSet('major-outage', 'operational', 'degraded-performance', 'partial-outage', 'maintenance')] [String] $Status ) begin { if (-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { $endpoint = 'services/set_status' $irmParams = @{ ContentType = 'application/json' Uri = "$($StaytusConnection.Hostname)/$($endpoint)" Header = $StaytusConnection.Header Method = 'POST' Body = (@{ service = $Service.ToLower() status = $Status.ToLower() } | ConvertTo-Json) } Invoke-RestMethod @irmParams } } Function Update-StaytusIncident { <# .SYNOPSIS Updates an Incident in Staytus .DESCRIPTION Updates an Incident in Staytus .PARAMETER Incident The Incident to update .PARAMETER Update Provide a brief update about the incident .PARAMETER Status Update status if needed .PARAMETER State Update state if needed .PARAMETER NotifySubscribers Send an email to subscribers .EXAMPLE Update-StaytusIncident -Incident 'Website loading times degraded' -Update 'Still investigating' #> [CmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Update-StaytusIncident.md")] Param( [Parameter(Mandatory)] [Argumentcompleter( { param($Command,$Parameter,$WordToComplete,$CommandAst,$FakeBoundParams) $results = @((Get-StaytusIncident).title) if($WordToComplete){ $results.Where{$_.Title -match "^$WordToComplete"} } else { $results} } )] [String] $Incident, [Parameter(Mandatory)] [String] $Update, [Parameter()] [ValidateSet('major-outage', 'operational', 'degraded-performance', 'partial-outage', 'maintenance')] [String] $Status, [Parameter()] [ValidateSet('Investigating', 'Identified', 'Monitoring', 'Resolved')] [String] $State, [Parameter()] [Switch] $NotifySubscribers ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } $endpoint = 'issues/update' } process { [int]$id = (Get-StaytusIncident -Title $Incident).id $Body = @{ id = $id text = "$Update" } if($Status){ $Body.Add("status","$($Status.ToLower())") } if($State){ $Body.Add("state","$($State.tolower())") } if($NotifySubscribers){ $Body.Add("notify",$true) } $irmParams = @{ ContentType = 'application/json' Method = 'POST' Uri = "$($StaytusConnection.Hostname)/$($endpoint)" Header = $StaytusConnection.Header Body = ($Body | ConvertTo-Json) } Invoke-RestMethod @irmParams } } Function Connect-PoshBotStaytusServer { <# .SYNOPSIS Establishes session connection details for Staytus Server in your PoshBot instance .DESCRIPTION Establishes session connection details for Staytus Server n your PoshBot instance .PARAMETER StaytusServer Hostname of Staytus server. If non-standard port use hostname:port format .PARAMETER UseSSL Toggle using HTTPS as protocol. If non-standard SSL port is in use, ensure hostname:port is used in StaytusServer parameter .PARAMETER Credential X-Auth-Token (username) and X-Auth-Secret (password) for Staytus api user .EXAMPLE Connect-StaytusServer -StaytusServer staytus.contoso.com:8787 -Credential (Get-Credential) .EXAMPLE Connect-StaytusServer -StaytusServer staytus.contoso.com -Credential $credential .EXAMPLE Connect-StaytusServer -StaytusServer staytus.contoso.com -Credential $credential -UseSSL .NOTES PoshBot must be running as a Service for this to function properly with the rest of the commands for PoshBot in this Module #> [PoshBot.BotCommand(CommandName = 'ConnectStaytus')] [cmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Connect-PoshBotStaytusServer.md")] Param( [Parameter(Mandatory)] [Alias('Hostname')] [String] $StaytusServer, [Parameter()] [Switch] $UseSSL, [Parameter(Mandatory)] [PSCredential] $Credential ) process { switch ($UseSSL) { $true { $protocol = 'https' } default { $protocol = 'http' } } $StaytusConnection = $Script:StaytusConnection = @{ Hostname = "$($protocol)://$StaytusServer/api/v1" Header = @{ 'X-Auth-Token' = "$($Credential.Username)" 'X-Auth-Secret' = "$($Credential.GetNetworkCredential().Password)" } } } } function Get-PoshBotStaytusIncident { <# .SYNOPSIS Report Staytus Incident info in Slack/Teams/Discord .DESCRIPTION Report Staytus Incident info in Slack/Teams/Discord .PARAMETER Title Title(s) of Incidents to report .PARAMETER Id Id(s) of Incidents to report .PARAMETER State State(s) of Incidents to report .EXAMPLE Get-PoshBotStaytusIncident .EXAMPLE Get-PoshBotStaytusIncident -Title 'Storefront Web Slow Response' .EXAMPLE Get-PoshBotStaytusIncident -State 'Investigating' #> [PoshBot.BotCommand(CommandName = 'StaytusInfo')] [CmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/Get-PoshBotStaytusIncident.md")] Param( [Parameter(ParameterSetName = 'Title')] [String[]] $Title, [Parameter(ParameterSetName = 'Id')] [Int[]] $Id, [Parameter(ParameterSetName = 'State')] [ValidateSet('Investigating', 'Identified', 'Monitoring', 'Resolved')] [string[]] $State ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { if ($PSBoundParameters) { if ($Title) { $incidents = Get-StaytusIncident -Title $Title Foreach ($incident in $incidents) { New-PoshBotCardResponse -Title 'Current Staytus Incidents' -Text $incident } } if ($Id) { $incidents = Get-StaytusIncident -Id $Id Foreach ($incident in $incidents) { New-PoshBotCardResponse -Title 'Current Staytus Incidents' -Text $incident } } if ($State) { $incidents = Get-StaytusIncident -State $State Foreach ($incident in $incidents) { New-PoshBotCardResponse -Title 'Current Staytus Incidents' -Text $incident } } } else { $incidents = Get-StaytusIncident Foreach ($incident in $incidents) { New-PoshBotCardResponse -Title 'Current Staytus Incidents' -Text $incident } } } } Function New-PoshBotStaytusIncident { <# .SYNOPSIS Raises a new Staytus Incident card alert .DESCRIPTION Raises a new Staytus Incident card alert .PARAMETER Title Title of the Incident .PARAMETER Service The affected Service .PARAMETER Status The current status .PARAMETER State Current affected service State .PARAMETER NotifySubscribers Email Service subscribers .EXAMPLE $IncidentParams = @{ Title = 'Website response times degraded' Service = 'Storefront' Status = 'Investigating' State = 'degraded-performance' } New-PoshBotStaytusIncident @IncidentParams #> [PoshBot.BotCommand(CommandName = 'NewStaytusIncident')] [CmdletBinding(HelpUri="https://github.com/steviecoaster/Uh-Oh/blob/main/docs/New-PoshBotStaytusIncident.md")] Param( [Parameter(Mandatory)] [String] $Title, [Parameter(Mandatory)] [String[]] $Service, [Parameter(Mandatory)] [ValidateSet('major-outage', 'operational', 'degraded-performance', 'partial-outage', 'maintenance')] [String] $Status, [Parameter(Mandatory)] [ValidateSet('Investigating', 'Identified', 'Monitoring', 'Resolved')] [String] $State, [Parameter()] [Switch] $NotifySubscribers ) begin { if(-not $StaytusConnection) { throw "Not connected to Staytus server. Did you run Connect-StaytusServer?" } } process { $incidentParams = @{ Title = $Title Service = $Service Status = $Status State = $State } if($NotifySubscribers){ $incidentParams.Add('NotifySubscribers',$true) } $incident = New-StaytusIncident @incidentParams $response = Get-StaytusIncident -Id $incident.data.id New-PoshBotCardResponse -Title 'New Staytus Incident!' -Text $response } } |