|
1 | 1 | import logging |
2 | 2 | from json import load |
3 | | -from tb_rest_client import * |
| 3 | +from tb_rest_client.rest_client_pe import * |
4 | 4 | from tb_rest_client.rest import ApiException |
5 | 5 |
|
6 | 6 |
|
|
13 | 13 | username = "tenant@thingsboard.org" |
14 | 14 | password = "tenant" |
15 | 15 |
|
16 | | -rest_client = RestClient(base_url=url) |
17 | | -rest_client.login(username=username, password=password) |
18 | | - |
19 | | -current_user = rest_client.get_user() |
20 | | - |
21 | | -try: |
22 | | - # Creating Dashboard Group on the Tenant Level |
23 | | - shared_dashboards_group = EntityGroup(name="Shared Dashboards", type="DASHBOARD") |
24 | | - shared_dashboards_group = rest_client.save_entity_group(shared_dashboards_group) |
25 | | - |
26 | | - # Loading Dashboard from file |
27 | | - dashboard_json = None |
28 | | - with open("watermeters.json", "r") as dashboard_file: |
29 | | - dashboard_json = load(dashboard_file) |
30 | | - dashboard = Dashboard(title=dashboard_json["title"], configuration=dashboard_json["configuration"]) |
31 | | - dashboard = rest_client.save_dashboard(dashboard) |
32 | | - |
33 | | - # Adding Dashboard to the Shared Dashboards Group |
34 | | - rest_client.add_entities_to_entity_group(shared_dashboards_group.id, [dashboard.id.id]) |
35 | | - |
36 | | - # Creating Customer 1 |
37 | | - customer1 = Customer(title="Customer 1") |
38 | | - customer1 = rest_client.save_customer(customer1) |
39 | | - |
40 | | - # Creating Device |
41 | | - device = Device(name="WaterMeter1", type="waterMeter") |
42 | | - device = rest_client.save_device(device) |
43 | | - |
44 | | - # Fetching automatically created "Customer Administrators" Group. |
45 | | - customer1_administrators = rest_client.get_entity_group_info_by_owner_and_name_and_type(customer1.id, "USER", "Customer Administrators") |
46 | | - |
47 | | - # Creating Read-Only Role |
48 | | - read_only_role = Role(name="Read-Only", permissions=['READ', 'READ_ATTRIBUTES', 'READ_TELEMETRY', 'READ_CREDENTIALS'], type="GROUP") |
49 | | - read_only_role = rest_client.save_role(read_only_role) |
50 | | - |
51 | | - # Assigning Shared Dashboards to the Customer 1 Administrators |
52 | | - tenant_id = current_user.tenant_id |
53 | | - group_permission = GroupPermission(role_id=read_only_role.id, |
54 | | - name="Read Only Permission", |
55 | | - is_public=False, |
56 | | - user_group_id=customer1_administrators.id, |
57 | | - tenant_id=tenant_id, |
58 | | - entity_group_id=shared_dashboards_group.id, |
59 | | - entity_group_type=shared_dashboards_group.type) |
60 | | - group_permission = rest_client.save_group_permission(group_permission) |
61 | | - |
62 | | - # Creating User for Customer 1 with default dashboard from Tenant "Shared Dashboards" group. |
63 | | - user_email = "user@thingsboard.org" |
64 | | - user_password = "secret" |
65 | | - |
66 | | - additional_info = { |
67 | | - "defaultDashboardId": dashboard.id.id, |
68 | | - "defaultDashboardFullscreen": False |
69 | | - } |
70 | | - user = User(authority="CUSTOMER_USER", |
71 | | - customer_id=customer1.id, |
72 | | - email=user_email, |
73 | | - additional_info=additional_info) |
74 | | - user = rest_client.save_user(user, send_activation_mail=False) |
75 | | - rest_client.activate_user(user.id, user_password) |
76 | | - rest_client.add_entities_to_entity_group(customer1_administrators.id, [user.id.id]) |
77 | | - |
78 | | -except ApiException as e: |
79 | | - logging.exception(e) |
| 16 | + |
| 17 | +with RestClientPE(base_url=url) as rest_client: |
| 18 | + try: |
| 19 | + rest_client.login(username=username, password=password) |
| 20 | + current_user = rest_client.get_user() |
| 21 | + # Creating Dashboard Group on the Tenant Level |
| 22 | + shared_dashboards_group = EntityGroup(name="Shared Dashboards", type="DASHBOARD") |
| 23 | + shared_dashboards_group = rest_client.save_entity_group(shared_dashboards_group) |
| 24 | + |
| 25 | + # Loading Dashboard from file |
| 26 | + dashboard_json = None |
| 27 | + with open("watermeters.json", "r") as dashboard_file: |
| 28 | + dashboard_json = load(dashboard_file) |
| 29 | + dashboard = Dashboard(title=dashboard_json["title"], configuration=dashboard_json["configuration"]) |
| 30 | + dashboard = rest_client.save_dashboard(dashboard) |
| 31 | + |
| 32 | + # Adding Dashboard to the Shared Dashboards Group |
| 33 | + rest_client.add_entities_to_entity_group(shared_dashboards_group.id, [dashboard.id.id]) |
| 34 | + |
| 35 | + # Creating Customer 1 |
| 36 | + customer1 = Customer(title="Customer 1") |
| 37 | + customer1 = rest_client.save_customer(customer1) |
| 38 | + |
| 39 | + # Creating Device |
| 40 | + device = Device(name="WaterMeter1", type="waterMeter") |
| 41 | + device = rest_client.save_device(device) |
| 42 | + |
| 43 | + # Fetching automatically created "Customer Administrators" Group. |
| 44 | + customer1_administrators = rest_client.get_entity_group_info_by_owner_and_name_and_type(customer1.id, "USER", "Customer Administrators") |
| 45 | + |
| 46 | + # Creating Read-Only Role |
| 47 | + read_only_role = Role(name="Read-Only", permissions=['READ', 'READ_ATTRIBUTES', 'READ_TELEMETRY', 'READ_CREDENTIALS'], type="GROUP") |
| 48 | + read_only_role = rest_client.save_role(read_only_role) |
| 49 | + |
| 50 | + # Assigning Shared Dashboards to the Customer 1 Administrators |
| 51 | + tenant_id = current_user.tenant_id |
| 52 | + group_permission = GroupPermission(role_id=read_only_role.id, |
| 53 | + name="Read Only Permission", |
| 54 | + is_public=False, |
| 55 | + user_group_id=customer1_administrators.id, |
| 56 | + tenant_id=tenant_id, |
| 57 | + entity_group_id=shared_dashboards_group.id, |
| 58 | + entity_group_type=shared_dashboards_group.type) |
| 59 | + group_permission = rest_client.save_group_permission(group_permission) |
| 60 | + |
| 61 | + # Creating User for Customer 1 with default dashboard from Tenant "Shared Dashboards" group. |
| 62 | + user_email = "user@thingsboard.org" |
| 63 | + user_password = "secret" |
| 64 | + |
| 65 | + additional_info = { |
| 66 | + "defaultDashboardId": dashboard.id.id, |
| 67 | + "defaultDashboardFullscreen": False |
| 68 | + } |
| 69 | + user = User(authority="CUSTOMER_USER", |
| 70 | + customer_id=customer1.id, |
| 71 | + email=user_email, |
| 72 | + additional_info=additional_info) |
| 73 | + user = rest_client.save_user(user, send_activation_mail=False) |
| 74 | + rest_client.activate_user(user.id, user_password) |
| 75 | + rest_client.add_entities_to_entity_group(customer1_administrators.id, [user.id.id]) |
| 76 | + |
| 77 | + except ApiException as e: |
| 78 | + logging.exception(e) |
0 commit comments