Skip to content

Commit 6c09f77

Browse files
committed
Release 1.3.0
1 parent 8b608d4 commit 6c09f77

File tree

8 files changed

+262
-122
lines changed

8 files changed

+262
-122
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ __pycache__/
1010

1111
# C extensions
1212
*.so
13-
13+
testopa.py
1414
# Distribution / packaging
1515
.Python
1616
build/

OpaExceptions/__init__.py

Whitespace-only changes.

README.md

+83-9
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ del client
8989
```python
9090
from opa_client.opa import OpaClient
9191

92-
client = OpaClient() # default host='localhost', port=8181, version='v1'
92+
client = OpaClient()
9393

9494
client.update_opa_policy_fromfile("/your/path/filename.rego", endpoint="fromfile") # response is True
9595

@@ -105,7 +105,7 @@ del client
105105

106106
from opa_client.opa import OpaClient
107107

108-
client = OpaClient() # default host='localhost', port=8181, version='v1'
108+
client = OpaClient()
109109

110110

111111
client.update_opa_policy_fromurl("http://opapolicyurlexample.test/example.rego", endpoint="fromurl") # response is True
@@ -124,11 +124,11 @@ del client
124124

125125
from opa_client.opa import OpaClient
126126

127-
client = OpaClient() # default host='localhost', port=8181, version='v1'
127+
client = OpaClient()
128128

129129
client.delete_opa_policy("fromfile") # response is True
130130

131-
client.get_policies_list() # response is [fromurl"]
131+
client.get_policies_list() # response is []
132132

133133
del client
134134
```
@@ -141,7 +141,7 @@ del client
141141

142142
from opa_client.opa import OpaClient
143143

144-
client = OpaClient() # default host='localhost', port=8181, version='v1'
144+
client = OpaClient()
145145

146146
print(client.get_opa_raw_data("testapi/testdata")) # response is {'result': ['world', 'hello']}
147147

@@ -156,7 +156,7 @@ del client
156156

157157
from opa_client.opa import OpaClient
158158

159-
client = OpaClient() # default host='localhost', port=8181, version='v1'
159+
client = OpaClient()
160160

161161
client.opa_policy_to_file(policy_name="fromurl",path="/your/path",filename="example.rego") # response is True
162162

@@ -171,7 +171,7 @@ del client
171171

172172
from opa_client.opa import OpaClient
173173

174-
client = OpaClient() # default host='localhost', port=8181, version='v1'
174+
client = OpaClient()
175175

176176
client.delete_opa_data("testapi") # response is True
177177

@@ -187,7 +187,7 @@ del client
187187

188188
from opa_client.opa import OpaClient
189189

190-
client = OpaClient() # default host='localhost', port=8181, version='v1'
190+
client = OpaClient()
191191

192192
client.get_policies_info()
193193

@@ -205,7 +205,7 @@ del client
205205

206206
from opa_client.opa import OpaClient
207207

208-
client = OpaClient() # default host='localhost', port=8181, version='v1'
208+
client = OpaClient()
209209

210210
permission_you_want_check = {"input": {"message": "hello"}}
211211
client.check_permission(input_data=permission_you_want_check, policy_name="testpolicy", rule_name="hello")
@@ -215,6 +215,80 @@ client.check_permission(input_data=permission_you_want_check, policy_name="testp
215215
del client
216216
```
217217

218+
### Queries a package rule with the given input data
219+
220+
```python
221+
from opa_client.opa import OpaClient
222+
223+
client = OpaClient()
224+
225+
rego = """
226+
package play
227+
228+
default hello = false
229+
230+
hello {
231+
m := input.message
232+
m == "world"
233+
}
234+
"""
235+
236+
check_data = {"message": "world"}
237+
client.check_policy_rule(input_data=check_data, package_path="play", rule_name="hello") # response {'result': True}
238+
239+
```
240+
241+
### Execute an Ad-hoc Query
242+
243+
```python
244+
from opa_client.opa import OpaClient
245+
246+
client = OpaClient()
247+
248+
print(client.ad_hoc_query(query_params={"q": "data.userinfo.user_roles[name]"})) # response is {}
249+
250+
data = {
251+
"user_roles": {
252+
"alice": [
253+
"admin"
254+
],
255+
"bob": [
256+
"employee",
257+
"billing"
258+
],
259+
"eve": [
260+
"customer"
261+
]
262+
}
263+
}
264+
265+
print(client.update_or_create_opa_data(data, "userinfo")) # response is True
266+
267+
# execute query
268+
print(client.ad_hoc_query(query_params={"q": "data.userinfo.user_roles[name]"}))
269+
# response is {'result': [{'name': 'eve'}, {'name': 'alice'}, {'name': 'bob'}]}
270+
271+
#you can send body request
272+
print(client.ad_hoc_query(body={"query": "data.userinfo.user_roles[name] "}))
273+
# response is {'result': [{'name': 'eve'}, {'name': 'alice'}, {'name': 'bob'}]}
274+
275+
```
276+
277+
### Check OPA healthy. If you want check bundels or plugins, add query params for this.
278+
279+
```python
280+
from opa_client.opa import OpaClient
281+
282+
client = OpaClient()
283+
284+
print(client.check_health()) # response is True or False
285+
print(client.check_health({"bundle": True})) # response is True or False
286+
# If your diagnostic url different than default url, you can provide it.
287+
print(client.check_health(diagnostic_url="http://localhost:8282/health")) # response is True or False
288+
print(client.check_health(query={"bundle": True}, diagnostic_url="http://localhost:8282/health")) # response is True or False
289+
290+
```
291+
218292

219293
# Contributing #
220294

OpaExceptions/OpaExceptions.py opa_client/OpaExceptions/OpaExceptions.py

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ def __init__(self, expression, message):
99
self.message = message
1010

1111

12+
class QueryExecuteError(Exception):
13+
"""
14+
expression -- input expression in which the error occurred
15+
message -- explanation of the error
16+
"""
17+
18+
def __init__(self, expression, message):
19+
self.expression = expression
20+
self.message = message
21+
22+
1223
class PolicyNotFoundError(Exception):
1324
"""
1425
expression -- input expression in which the error occurred
@@ -85,6 +96,7 @@ def __init__(self, expression, message):
8596
self.expression = expression
8697
self.message = message
8798

99+
88100
class FileError(ValueError):
89101
"""
90102
expression -- input expression in which the error occurred
@@ -94,6 +106,7 @@ def __init__(self, expression, message):
94106
self.expression = expression
95107
self.message = message
96108

109+
97110
class TypeExecption(TypeError):
98111
"""
99112
expression -- input expression in which the error occurred

opa_client/OpaExceptions/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .OpaExceptions import (CheckPermissionError,
2+
ConnectionsError, DeleteDataError,
3+
DeletePolicyError, PathNotFoundError,
4+
PolicyNotFoundError, RegoParseError,
5+
SSLError, FileError, TypeExecption,
6+
QueryExecuteError)

opa_client/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Initialize the OpaClient package."""
22

33
from .opa import OpaClient
4+
from .OpaExceptions import *
45

56
__all__ = [
67
"OpaClient",

0 commit comments

Comments
 (0)