Skip to content

Instantly share code, notes, and snippets.

@Forest-Dewberry
Created June 14, 2022 19:22
Show Gist options
  • Save Forest-Dewberry/5b90704c4ac808a7defc91e5943ebe76 to your computer and use it in GitHub Desktop.
Save Forest-Dewberry/5b90704c4ac808a7defc91e5943ebe76 to your computer and use it in GitHub Desktop.
# Generic Chef Api function
function Get-ChefAutomateAPIcall
{
param (
[Parameter(Mandatory = $true)]
[String]$APITOKEN,
#[Parameter(Mandatory = $true)]
[String]$uriExtension,
#[Parameter(Mandatory = $true)]
[String]$jsonPayload,
#[Parameter(Mandatory = $true)]
[String]$runId
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
$uri = $baseUri+$uriExtension#+
# "?filter=name:mySO*&filter=platform:ubun*"+
""
# if ($filter){$uri +="?filter="
# $uri +=$filter}
#"?type=environment"+
$json = ''+$jsonPayload
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
if ($method){$methodWasPassed=$method}
$method = "Get"
if ($jsonPayload){$method="Post"}
if ($methodWasPassed){$method=$methodWasPassed}
if ($method -like "get"){
$return = (
Invoke-RestMethod -Method $method -Header $Header -ContentType $ct -uri $uri
)
}
if (($method -like "post") -or ($method -like "put")){
$return = (
Invoke-RestMethod -Method $method -Header $Header -ContentType $ct -uri $uri -json
)
}
Write-Output $return
}
# Get all infra Error info
function Get-NodeFailureInformationFull
{
#This may take a long time to run.
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN
)
# find all failed nodes.
# use id, latest_run_id, and name
# Use id and latest run id to get error with Get-InfraRunError
# find all failed chef infra nodes.
$failedNodes = Get-FailedChefInfraNodes -SAPW $SAPW -APITOKEN $APITOKEN
# Make a hash table with the key being each node and the value being important object(s) such as
# nodeReport, sysFriendlyName, and OwnedByTeam.
$return = [ordered]@{}
foreach ($node in $failedNodes){
# not tested
$nodeId = $node.id
$runId = $node.latest_run_id
$nodeName = $node.name
# find all failed nodes. ✅
# use id, latest_run_id, and name ✅
# Use id and latest run id to get error with Get-InfraRunError
# Getting whole report, not just error, because we might want to compare other metadata as well.
$currentNodeReport = Get-InfraRunReport -SAPW $SAPW -APITOKEN $APITOKEN -nodeId $nodeId -runId $runId
# Put information in the hash table
$return[$node.name] = @($node,$currentNodeReport)
}
return $return
}
function Get-InfraRunError
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN,
#[Parameter(Mandatory = $true)]
[String]$nodeId,
#[Parameter(Mandatory = $true)]
[String]$runId
)
$report = Get-InfraRunReport -SAPW $SAPW -APITOKEN $APITOKEN -nodeId $nodeId -runId $runId
$return = $report.error.message
Write-Output $return
}
# https://automate.chef.io/api/v0/cfgmgmt/nodes/{node_id}/runs/{run_id}
function Get-InfraRunReport
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN,
#[Parameter(Mandatory = $true)]
[String]$nodeId,
#[Parameter(Mandatory = $true)]
[String]$runId
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
$uri = $baseUri+"/cfgmgmt/nodes/"+$nodeId + "/runs/" + $runId +#{node_id}/runs/{run_id}"+
# "?filter=name:mySO*&filter=platform:ubun*"+
""
# if ($filter){$uri +="?filter="
# $uri +=$filter}
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method Get -Header $Header -ContentType $ct -uri $uri
)
Write-Output $return
}
function Get-FailedChefInfraNodes
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN
)
$allNodes = Get-CheckedInInfraNodes -SAPW $SAPW -APITOKEN $APITOKEN
$failedNodes = $allNodes | Where-Object {$_.status -eq "failure"}
Write-Output $failedNodes
}
# cfgmgmt/nodes?pagination.page=1&pagination.size=100&sorting.field=name&sorting.order=ASC&filter=name:mySO*&filter=platform:ubun*
# https://docs.chef.io/automate/api/#tag/ConfigMgmt/operation/ConfigMgmt_GetNodes
# cfgmgmt/nodes?pagination.page=1&pagination.size=100&sorting.field=name&sorting.order=ASC
function Get-CheckedInInfraNodes
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN,
#[Parameter(Mandatory = $true)]
[String]$filter
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
$uri = $baseUri+"/cfgmgmt/nodes"+
"?pagination.page=1&pagination.size=10000"+
"&sorting.field=name&sorting.order=ASC"+
# "?filter=status:failure"+ #didn't work for some reason
# "?filter=name:mySO*&filter=platform:ubun*"+
""
# if ($filter){$uri +="?filter="
# $uri +=$filter}
#"?type=environment"+
$json ='{
"output_type":"json",
"node_id":"123-456-7890",
"start":{"seconds":1585336095},
"end":{"seconds":1665337095},
"filters":[
{"status":"success"}
]
}'
# $json='
# {
# "filters": [
# {
# "type": "node_id",
# "values": [
# ' + '"'+$nodeId+'"'+'
# ]
# }
# ]
# }
# '
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method Get -Header $Header -ContentType $ct -uri $uri
)
Write-Output $return
}
# https://automate.chef.io/api/v0/cfgmgmt/reports/export
function Get-NodeRunReports
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN,
#[Parameter(Mandatory = $true)]
[String]$filter
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
$uri = $baseUri+"/cfgmgmt/reports/export"+
# "?filter=name:mySO*&filter=platform:ubun*"+
""
# if ($filter){$uri +="?filter="
# $uri +=$filter}
#"?type=environment"+
$json ='{
"output_type":"json",
"node_id":"'+$nodeId+'",'+
# '
# "start":{"seconds":999999999},
# "end":{"seconds":99999999},'+
'
"filters":[
{"status":"success"}
]
}
'
# $json='
# {
# "filters":
# [
# {
# "type": "node_id",
# "values":
# [
# ' + '"'+$nodeId+'"'+'
# ]
# }
# ]
# }
# '
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method Post -Header $Header -ContentType $ct -uri $uri -body $json
)
Write-Output $return
}
# https://automate.chef.io/api/v0/cfgmgmt/source_fqdns
function Get-AssociatedChefInfraServers
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN,
#[Parameter(Mandatory = $true)]
[String]$filter
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
$uri = $baseUri+"/cfgmgmt/source_fqdns"
$json =''
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method Get -Header $Header -ContentType $ct -uri $uri
)
Write-Output $return
}
# cfgmgmt/stats/node_counts?filter=name:mySO*&filter=platform:ubun*
function Get-NodeCounts
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN,
#[Parameter(Mandatory = $true)]
[String]$filter
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
$uri = $baseUri+"/cfgmgmt/stats/node_counts"+
# "?filter=name:mySO*&filter=platform:ubun*"+
""
if ($filter){$uri +="?filter="
$uri +=$filter}
#"?type=environment"+
$json =''
# $json='
# {
# "filters": [
# {
# "type": "node_id",
# "values": [
# ' + '"'+$nodeId+'"'+'
# ]
# }
# ]
# }
# '
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method Get -Header $Header -ContentType $ct -uri $uri
)
Write-Output $return
}
# cfgmgmt/suggestions?type=environment&text=_d
# https://docs.chef.io/automate/api/#tag/ConfigMgmt/operation/ConfigMgmt_GetSuggestions
function Get-FilterSuggestions
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN,
#[Parameter(Mandatory = $true)]
[String]$text
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
$uri = $baseUri+"/cfgmgmt/suggestions"+
"?type="+
$text +
#"?type=environment"+
""
$json =''
# $json='
# {
# "filters": [
# {
# "type": "node_id",
# "values": [
# ' + '"'+$nodeId+'"'+'
# ]
# }
# ]
# }
# '
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method Get -Header $Header -ContentType $ct -uri $uri
)
Write-Output $return
}
# https://docs.chef.io/automate/api/#operation/NodesService_List
function Get-NodesFromChefInfraList
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
## From https://docs.chef.io/automate/api/#operation/ReportingService_ListNodes
# Return ALL NODES THAT FAILED THEIR LAST CHEF CLIENT RUN
$uri = $baseUri+"/nodes/search"
$json=''+
'
{'+
'
"filters":['+
# '
# {"key": "last_scan_status", "values": ["FAILED"]},'+
# '
# {"key": "last_scan_penultimate_status", "values": ["PASSED"]},'+
# '
# {"key": "name", "values": ["MyNode*"]}'+
'
],
"page":1, "per_page":10000,
"sort":"status", "order":"ASC"
}
'+
''
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method POST -Header $Header -ContentType $ct -uri $uri -Body $json
)
Write-Output $return
}
function Get-ComplianceFailedNodes
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
## From https://docs.chef.io/automate/api/#operation/ReportingService_ListNodes
# Return ALL NODES THAT ARE FAILING ANY CONTROL
$uri = $baseUri+"/compliance/reporting/nodes/search"
$json='
{'+
'"filters":['+
'{"type":"status","values":["failed"]}'+
'],'+
# I tested without the above filter (with no filter) and it returned over 2000 nodes, for 16M of data.
# So the "4MB limit" from https://docs.chef.io/automate/api/#operation/ReportingService_ListNodes might not be true?
'"page":1,"per_page":10000,"sort":"latest_report.end_time","order":"DESC"}
'
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method POST -Header $Header -ContentType $ct -uri $uri -Body $json
)
Write-Output $return
}
function Get-ControlsNodeIsFailing
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN,
[Parameter(Mandatory = $true)]
[String]$nodeId
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
## From https://docs.chef.io/automate/api/#operation/ReportingService_ListControlItems
# Return ALL CONTROLS THAT ARE BEING FAILED BY GIVEN NODE
$uri = $baseUri+"/compliance/reporting/controls"
$json='
{
"filters": [
{
"type": "node_id",
"values": [
' + '"'+$nodeId+'"'+'
]
}
]
}
'
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method POST -Header $Header -ContentType $ct -uri $uri -Body $json
)
Write-Output $return
}
function Get-NodesFailingControl
{
param (
# [Parameter(Mandatory = $true)]
[String]$APITOKEN,
[Parameter(Mandatory = $true)]
[String]$controlId
)
$Header = @{"api-token" = $APITOKEN }
$baseUri='https://yourdns.here/api/v0'
## From https://docs.chef.io/automate/api/#operation/ReportingService_ListNodes
# Return ALL NODES THAT ARE FAILING GIVEN CONTROL
$uri = $baseUri+"/compliance/reporting/nodes/search"
$json='
{
"filters": [
{
"type": "control",
"values": ['+
'"'+ $controlId+'"'+
'
]
},
{
"type": "status",
"values": [
"failed"
]
}
],
"page": 1,
"per_page": 100,
"sort": "latest_report.end_time",
"order": "DESC"
}
'
## Content Type
$ct = "application/json"
## Remove all newlines and spaces
$bod = $json -replace "`n","" -replace "`r",""
$bod = ($bod).replace(" ","")
$json=$bod
##
$return = (
Invoke-RestMethod -Method POST -Header $Header -ContentType $ct -uri $uri -Body $json
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment