forked from Swansoffie02/esper-api-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanaged-configuration-chrome.py
115 lines (93 loc) · 4.43 KB
/
managed-configuration-chrome.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Sample code to use managed configuration APIs of Esper.
# This code run the APIs on the chrome browser and help in
# 1. Place specific URLs on an allowlist
# 2. Place specific URLs on a blocklist
# 3. Disable browser incognito mode on Android
# 4. Enable forced Safe Search on a device
# Code can be run either on set of devices or set of groups
# provided as a argument to the command request
import esperclient
from esperclient.rest import ApiException
from esperclient.models.v0_command_args import V0CommandArgs as CommandArgs
# Configure API key authorization: apiKey
configuration = esperclient.Configuration()
configuration.host = 'https://<endpoint-name>-api.esper.cloud/api'
configuration.api_key['Authorization'] = '<API-Key>'
configuration.api_key_prefix['Authorization'] = 'Bearer'
# create an instance of the API class
api_instance = esperclient.CommandsV2Api(esperclient.ApiClient(configuration))
enterprise_id = '<Enterprise ID>' # str | ID of the enterprise
# Modify this Python Dict to set your own managed configurations
command_args = CommandArgs(
custom_settings_config={
"managedAppConfigurations": {
"com.android.chrome": {
"URLBlacklist": ["https://www.omegle.com/","facebook.com","instagram.com"], # not allow these websites to work
"URLWhitelist": ["*"], # rest all allowed to open
"IncognitoModeAvailability": "1", # disable incognito mode
"ForceGoogleSafeSearch" : "true", # enable safe search to work
"HomepageLocation": "https://esper.io" # set chrome home page
}
}
}
)
# int | Number of results to return per page. (optional) (default to 20)
configuration.per_page_limit = 20
# int | The initial index from which to return the results.(optional) default to 0)
configuration.per_page_offset = 0
# get all the groups present in the enterprise and execute run_managed_configuration on each of them
def managed_configuration_all_groups():
# create an instance of the API class
api_instance = esperclient.DeviceGroupApi(esperclient.ApiClient(configuration))
try:
api_response = api_instance.get_all_groups(enterprise_id,
limit=configuration.per_page_limit,
offset=configuration.per_page_offset)
if len(api_response.results):
for group in api_response.results:
# add all the devices in this group to global list of devices
run_managed_configuration(group.id)
#print(allgroups)
except ApiException as e:
print("Exception when calling DeviceGroupApi->get_all_groups: %s\n" % e)
# V0CommandRequest | The request body to create a command for set of devices or groups
## command_type ->
## * DEVICE: command request is meant for devices
## * GROUP: command request is meant for groups
## * DYNAMIC: command request is meant for dynamic set of devices i.e subset of devices from different groups or otherwise.
## Type of devices to run commands on
## * active: Run commands on currently online devices
## * inactive: Run commands on currently offline devices
## * all: Run commands on all the devices. Commands will be queued for offline devices until they come back online.
# run this on each group one by one
def run_managed_configuration(group_id):
request = esperclient.V0CommandRequest(
enterprise=enterprise_id,
command_type="GROUP",
device_type="all",
groups = [group_id],
command="UPDATE_DEVICE_CONFIG",
command_args=command_args
)
try:
# Create a command request
api_response = api_instance.create_command(enterprise_id, request)
#print(api_response)
request_id = api_response.id
response = api_instance.get_command_request_status(enterprise_id, request_id)
status = response.results[0]
#print(status)
while status.state not in ["Command Success", "Command Failure", "Command TimeOut", "Command Cancelled", "Command Queued"]:
response = api_instance.get_command_request_status(enterprise_id, request_id)
status = response.results[0]
time.sleep(1)
#print(status)
except ApiException as e:
print("Exception when calling CommandsV2Api->create_command: %s\n" % e)
"""
Main Function
"""
def main():
managed_configuration_all_groups()
if __name__ == "__main__":
main()