-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_policy_log.ps1
73 lines (56 loc) · 2.48 KB
/
get_policy_log.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
param(
$RS_HOST = "us-3.rightscale.com", #or us-4.rightscale.com,
$ACCOUNT_ID = "",
$GRS_ACCOUNT = "",
$REFRESH_TOKEN = "",
$POLICY_NAME = ""
)
function Index-PolicyAggregates ($RS_HOST, $ACCESS_TOKEN, $GRS_ACCOUNT) {
try {
$contentType = "application/json"
$header = @{"Api-Version"="1.0";"Authorization"="Bearer $ACCESS_TOKEN"}
$shard = $RS_HOST.split("-")[1]
Write-Output "URI: https://governance-$shard/api/governance/orgs/$GRS_ACCOUNT/policy_aggregates"
$indexPoliciesResult = Invoke-RestMethod -UseBasicParsing -Uri "https://governance-$shard/api/governance/orgs/$GRS_ACCOUNT/policy_aggregates" -Method Get -Headers $header -ContentType $contentType
return $indexPoliciesResult
}
catch {
Write-Output "Error creating policy! $($_ | Out-String)"
}
}
function Get-PolicyAggregate ($RS_HOST, $ACCESS_TOKEN, $HREF){
try {
$contentType = "application/json"
$header = @{"Api-Version"="1.0";"Authorization"="Bearer $ACCESS_TOKEN"}
$shard = $RS_HOST.split("-")[1]
Write-Output "URI: https://governance-$shard$HREF"
$policyAggResult = Invoke-RestMethod -UseBasicParsing -Uri "https://governance-$shard$HREF" -Method Get -Headers $header -ContentType $contentType
return $policyAggResult
}
catch {
Write-Output "Error creating policy! $($_ | Out-String)"
}
}
function Get-PolicyLog ($ACCESS_TOKEN, $URL){
try {
$contentType = "application/json"
$header = @{"Api-Version"="1.0";"Authorization"="Bearer $ACCESS_TOKEN"}
Write-Output "URI: $URL/log"
$policyLogResult = Invoke-RestMethod -UseBasicParsing -Uri "$URL/log" -Method Get -Headers $header -ContentType $contentType
return $policyLogResult
}
catch {
Write-Output "Error creating policy! $($_ | Out-String)"
}
}
#Auth
$contentType = "application/json"
$oauthHeader = @{"X_API_VERSION"="1.5"}
$oauthBody = @{"grant_type"="refresh_token";"refresh_token"=$REFRESH_TOKEN} | ConvertTo-Json
$oauthResult = Invoke-RestMethod -Uri "https://$RS_HOST/api/oauth2" -Method Post -Headers $oauthHeader -ContentType $contentType -Body $oauthBody
$accessToken = $oauthResult.access_token
$policies = Index-PolicyAggregates -ACCESS_TOKEN $accessToken -RS_HOST $RS_HOST -GRS_ACCOUNT $GRS_ACCOUNT
$target_policy = $policies.items | where name -eq $POLICY_NAME
$policy_agg = Get-PolicyAggregate -ACCESS_TOKEN $accessToken -RS_HOST $RS_HOST -HREF $target_policy.href
$policy_log = Get-PolicyLog -ACCESS_TOKEN $accessToken -URL $policy_agg.items.url
return $policy_log