@@ -89,7 +89,7 @@ del client
89
89
``` python
90
90
from opa_client.opa import OpaClient
91
91
92
- client = OpaClient() # default host='localhost', port=8181, version='v1'
92
+ client = OpaClient()
93
93
94
94
client.update_opa_policy_fromfile(" /your/path/filename.rego" , endpoint = " fromfile" ) # response is True
95
95
@@ -105,7 +105,7 @@ del client
105
105
106
106
from opa_client.opa import OpaClient
107
107
108
- client = OpaClient() # default host='localhost', port=8181, version='v1'
108
+ client = OpaClient()
109
109
110
110
111
111
client.update_opa_policy_fromurl(" http://opapolicyurlexample.test/example.rego" , endpoint = " fromurl" ) # response is True
@@ -124,11 +124,11 @@ del client
124
124
125
125
from opa_client.opa import OpaClient
126
126
127
- client = OpaClient() # default host='localhost', port=8181, version='v1'
127
+ client = OpaClient()
128
128
129
129
client.delete_opa_policy(" fromfile" ) # response is True
130
130
131
- client.get_policies_list() # response is [fromurl"]
131
+ client.get_policies_list() # response is []
132
132
133
133
del client
134
134
```
@@ -141,7 +141,7 @@ del client
141
141
142
142
from opa_client.opa import OpaClient
143
143
144
- client = OpaClient() # default host='localhost', port=8181, version='v1'
144
+ client = OpaClient()
145
145
146
146
print (client.get_opa_raw_data(" testapi/testdata" )) # response is {'result': ['world', 'hello']}
147
147
@@ -156,7 +156,7 @@ del client
156
156
157
157
from opa_client.opa import OpaClient
158
158
159
- client = OpaClient() # default host='localhost', port=8181, version='v1'
159
+ client = OpaClient()
160
160
161
161
client.opa_policy_to_file(policy_name = " fromurl" ,path = " /your/path" ,filename = " example.rego" ) # response is True
162
162
@@ -171,7 +171,7 @@ del client
171
171
172
172
from opa_client.opa import OpaClient
173
173
174
- client = OpaClient() # default host='localhost', port=8181, version='v1'
174
+ client = OpaClient()
175
175
176
176
client.delete_opa_data(" testapi" ) # response is True
177
177
@@ -187,7 +187,7 @@ del client
187
187
188
188
from opa_client.opa import OpaClient
189
189
190
- client = OpaClient() # default host='localhost', port=8181, version='v1'
190
+ client = OpaClient()
191
191
192
192
client.get_policies_info()
193
193
@@ -205,7 +205,7 @@ del client
205
205
206
206
from opa_client.opa import OpaClient
207
207
208
- client = OpaClient() # default host='localhost', port=8181, version='v1'
208
+ client = OpaClient()
209
209
210
210
permission_you_want_check = {" input" : {" message" : " hello" }}
211
211
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
215
215
del client
216
216
```
217
217
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
+
218
292
219
293
# Contributing #
220
294
0 commit comments