forked from gooddata/gooddata-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_demo_layout.py
More file actions
121 lines (93 loc) · 4.15 KB
/
upload_demo_layout.py
File metadata and controls
121 lines (93 loc) · 4.15 KB
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
115
116
117
118
119
120
121
# (C) 2022 GoodData Corporation
import json
import os
import time
from pathlib import Path
import requests
fixtures_dir = Path(os.environ.get("FIXTURES_DIR", Path(os.path.curdir) / "fixtures"))
host = os.environ.get("HOST", "http://localhost:3000")
token = os.environ.get("TOKEN", "YWRtaW46Ym9vdHN0cmFwOmFkbWluMTIz")
header_host = os.environ.get("HEADER_HOST", None)
content_type_jsonapi = "application/vnd.gooddata.api+json"
content_type_default = "application/json"
headers = {"Host": header_host}
api_version = "v1"
def rest_op(op, url_path, data=None, raise_ex=True):
all_headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
**headers,
}
url = f"{host}/{url_path}"
kwargs = {
"url": url,
"headers": all_headers,
}
if data:
kwargs["json"] = data
op_fnc = getattr(requests, op)
response = op_fnc(**kwargs)
if response.status_code < 200 or response.status_code > 299:
if raise_ex:
raise Exception(f"Call to {url} failed - {str(response.text)}")
else:
return None
if response.status_code == 200:
return response.json()
else:
return None
def read_data_from_file(data_json_path):
with open(data_json_path) as f:
return json.load(f)
def rest_op_jsonapi(op, url_path, data_json_path=None, raise_ex=True):
headers["Content-Type"] = content_type_jsonapi
return rest_op(op, url_path, data_json_path, raise_ex)
def rest_op_default(op, url_path, data_json_path=None, raise_ex=True):
headers["Content-Type"] = content_type_default
return rest_op(op, url_path, data_json_path, raise_ex)
def wait_platform_up():
# wait till GD.CN is up and ready to receive requests
print("Waiting till AIO GD.CN is up", flush=True)
while True:
try:
result = rest_op_jsonapi("get", f"api/{api_version}/entities/admin/organizations/default", raise_ex=False)
if result is not None:
print("AIO GD.CN is up", flush=True)
break
print("AIO GD.CN metadata does not responding", flush=True)
except requests.exceptions.ConnectionError:
print("AIO GD.CN is not available", flush=True)
time.sleep(4)
def create_entity(entity_id, entity_data, entity_type, api_path, action):
print(f"Creating {entity_type} id={entity_id}", flush=True)
result = action("get", f"{api_path}/{entity_id}", raise_ex=False)
if not result:
return action("post", f"{api_path}", entity_data)
else:
print(f"Entity {entity_type} already exists id={entity_id} result={result}")
return result
def update_layout():
user_groups = read_data_from_file(fixtures_dir / "user_groups.json")
user_auth = read_data_from_file(fixtures_dir / "user_auth.json")
user = read_data_from_file(fixtures_dir / "user.json")
data_sources = read_data_from_file(fixtures_dir / "demo_data_sources.json")
hierarchy = read_data_from_file(fixtures_dir / "demo_declarative_hierarchy.json")
permissions = read_data_from_file(fixtures_dir / "workspace_permissions.json")
# TODO: use python-sdk support
wait_platform_up()
print("Uploading userGroups", flush=True)
rest_op_default("put", f"api/{api_version}/layout/userGroups", user_groups)
response = create_entity(
user_auth["email"], user_auth, "user auth", f"api/{api_version}/auth/users", rest_op_default
)
user["data"]["attributes"]["authenticationId"] = response["authenticationId"]
create_entity(user["data"]["id"], user, "user", f"api/{api_version}/entities/users", rest_op_jsonapi)
print("Uploading test DS with physical model for demo", flush=True)
rest_op_default("put", f"api/{api_version}/layout/dataSources", data_sources)
print("Uploading demo workspaces", flush=True)
rest_op_default("put", f"api/{api_version}/layout/workspaces", hierarchy)
print("Uploading permissions for demo workspace", flush=True)
rest_op_default("put", f"api/{api_version}/layout/workspaces/demo/permissions", permissions)
print("Layout configuration done successfully!", flush=True)
if __name__ == "__main__":
update_layout()