Skip to content

Commit f0fd9f2

Browse files
authored
Merge pull request hashicorp#174 from hashicorp/add-policy-with-http-call
add check-external-http-api.sentinel
2 parents d2983bf + 684006b commit f0fd9f2

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# This policy uses the Sentinel HTTP import to call an external API,
2+
# https://yesno.wtf/api that randomly returns "yes" or "no"
3+
# This simulates what a policy might do to check an external system
4+
# that has a JSON-based API in order to confirm that the run is
5+
# allowed to do an apply.
6+
7+
# For example, some customers require tickets to be opened and approved
8+
# before an apply can be done. The HTTP import could be used to check
9+
# those types of systems.
10+
11+
# It also uses the Sentinel case statement
12+
13+
# Note that the single associated test.json test case will pass sometimes and
14+
# fail the other times depending on the value returned by the API. To see the
15+
# answer that was returned, run `sentinel test -run=check -verbose`
16+
17+
18+
##### Imports #####
19+
import "http"
20+
import "json"
21+
22+
##### Functions #####
23+
24+
# Validate that the proposed monthly cost is less than the limit
25+
check_external_approval_system = func() {
26+
req = http.request("https://yesno.wtf/api")
27+
res = json.unmarshal(http.get(req).body)
28+
answer = res.answer
29+
print("answer:", answer)
30+
31+
case answer {
32+
# https://yesno.wtf/api returns "maybe" every 10,000th time
33+
when "yes", "maybe":
34+
return true
35+
when "no":
36+
return false
37+
else:
38+
return false
39+
}
40+
41+
}
42+
43+
##### Rules #####
44+
approved = check_external_approval_system()
45+
main = rule {
46+
approved
47+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"test": {
3+
"main": true
4+
}
5+
}

0 commit comments

Comments
 (0)