Skip to content

Commit a4b6cfe

Browse files
authored
Add methods for bulk policy operations (#229)
* New methods * Tests * Update policies.py example * Remove value from testing * Update docstrings
1 parent 68ab134 commit a4b6cfe

3 files changed

Lines changed: 91 additions & 0 deletions

File tree

duo_client/admin.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3396,6 +3396,34 @@ def update_policy_v2(self, policy_key, json_request):
33963396
path = "/admin/v2/policies/" + self._quote_policy_id(policy_key)
33973397
response = self.json_api_call("PUT", path, json_request)
33983398
return response
3399+
3400+
def update_policies_v2(self, sections, sections_to_delete,
3401+
edit_list, edit_all_policies=False):
3402+
"""
3403+
Update the contents of multiple policies.
3404+
3405+
Args:
3406+
sections (dict): policy content to update
3407+
sections_to_delete (list): List of section names to delete
3408+
edit_list (list): List of new policy keys to apply the changes to.
3409+
Ignored if edit_all_policies is True.
3410+
edit_all_policies (bool, optional): Apply changes to all policies.
3411+
Defaults to False.
3412+
Returns (list): all updated policies
3413+
"""
3414+
path = "/admin/v2/policies/update"
3415+
params = {
3416+
"policies_to_update": {
3417+
"edit_all_policies": edit_all_policies,
3418+
"edit_list": edit_list,
3419+
},
3420+
"policy_changes": {
3421+
"sections": sections,
3422+
"sections_to_delete": sections_to_delete,
3423+
},
3424+
}
3425+
response = self.json_api_call("PUT", path, params)
3426+
return response
33993427

34003428
def create_policy_v2(self, json_request):
34013429
"""
@@ -3407,6 +3435,25 @@ def create_policy_v2(self, json_request):
34073435
path = "/admin/v2/policies"
34083436
response = self.json_api_call("POST", path, json_request)
34093437
return response
3438+
3439+
def copy_policy_v2(self, policy_key, new_policy_names_list):
3440+
"""
3441+
Copy policy to multiple new policies.
3442+
3443+
Args:
3444+
policy_key (str): Unique id of the policy to copy from
3445+
new_policy_names_list (array): The policy specified by policy_key
3446+
will be copied once for each name
3447+
in the list
3448+
Returns (list): all new policies
3449+
"""
3450+
path = "/admin/v2/policies/copy"
3451+
params = {
3452+
"policy_key": policy_key,
3453+
"new_policy_names_list": new_policy_names_list
3454+
}
3455+
response = self.json_api_call("POST", path, params)
3456+
return response
34103457

34113458
def get_policy_v2(self, policy_key):
34123459
"""

examples/policies.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ def create_policy_browsers(name, print_response=False):
5959
print(pretty)
6060
return response.get("policy_key")
6161

62+
def copy_policy(name1, name2, copy_from, print_response=False):
63+
"""
64+
Copy the policy `copy_from` to two new policies.
65+
"""
66+
response = admin_api.copy_policy_v2(copy_from, [name1, name2])
67+
if print_response:
68+
pretty = json.dumps(response, indent=4, sort_keys=True, default=str)
69+
print(pretty)
70+
policies = response.get("policies")
71+
return (policies[0].get("policy_key"), policies[1].get("policy_key"))
72+
73+
def bulk_delete_section(policy_keys, print_response=False):
74+
"""
75+
Delete the section "browsers" from the provided policies.
76+
"""
77+
response = admin_api.update_policies_v2("", ["browsers"], policy_keys)
78+
if print_response:
79+
pretty = json.dumps(response, indent=4, sort_keys=True, default=str)
80+
print(pretty)
6281

6382
def update_policy_with_device_health_app(policy_key, print_response=False):
6483
"""
@@ -131,6 +150,12 @@ def main():
131150
# Create a policy with browser restriction settings.
132151
policy_key_d = create_policy_browsers("Test New Policy - d")
133152

153+
# Copy a policy to 2 new policies.
154+
policy_key_e, policy_key_f = copy_policy("Test New Policy - e", "Test New Policy - f", policy_key_d)
155+
156+
# Delete the browser restriction settings from 2 policies.
157+
bulk_delete_section([policy_key_e, policy_key_f])
158+
134159
# Fetch the global and other custom policy.
135160
get_policy("global")
136161
get_policy(policy_key_b)

tests/admin/test_policies.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def test_update_policy_v2(self):
3939
self.assertEqual(response["method"], "PUT")
4040
self.assertEqual(response["uri"], "/admin/v2/policies/{}".format(policy_key))
4141

42+
def test_update_policies_v2(self):
43+
edit_list = ["POSTGY2G0HVWJR4JO1XT", "POSTGY2G0HVWJR4JO1XU"]
44+
sections = {
45+
"browsers": {
46+
"blocked_browsers_list": ["ie"],
47+
}
48+
}
49+
response = self.client.update_policies_v2(sections, [], edit_list)
50+
self.assertEqual(response["method"], "PUT")
51+
self.assertEqual(response["uri"], "/admin/v2/policies/update")
52+
4253
def test_create_policy_v2(self):
4354
policy_settings = {
4455
"name": "my new policy",
@@ -53,6 +64,14 @@ def test_create_policy_v2(self):
5364
self.assertEqual(response["method"], "POST")
5465
self.assertEqual(response["uri"], "/admin/v2/policies")
5566

67+
def test_copy_policy_v2(self):
68+
policy_key = "POSTGY2G0HVWJR4JO1XT"
69+
new_policy_names_list = ["Copied Pol 1", "Copied Pol 2"]
70+
response = self.client.copy_policy_v2(policy_key, new_policy_names_list)
71+
72+
self.assertEqual(response["method"], "POST")
73+
self.assertEqual(response["uri"], "/admin/v2/policies/copy")
74+
5675
def test_get_policies_v2(self):
5776
response = self.client.get_policies_v2(limit=3, offset=0)
5877
uri, args = response["uri"].split("?")

0 commit comments

Comments
 (0)