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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# import requests
# import json
#
# def extract_snippet_fields(flow):
# return {
# key: flow.get(key, [])
# for key in [
# "processors",
# "connections",
# "funnels",
# "inputPorts",
# "outputPorts",
# "labels",
# "controllerServices",
# "processGroups"
# ]
# }
#
# def upload_nifi_exported_flow(
# nifi_host: str,
# username: str,
# password: str,
# json_file_path: str,
# verify_ssl: bool = False
# ):
# try:
# token_resp = requests.post(
# f"{nifi_host}/nifi-api/access/token",
# data={"username": username, "password": password},
# verify=verify_ssl
# )
# token_resp.raise_for_status()
# token = token_resp.text
#
# headers = {
# "Authorization": f"Bearer {token}",
# "Content-Type": "application/json"
# }
#
# root_resp = requests.get(f"{nifi_host}/nifi-api/flow/process-groups/root", headers=headers, verify=verify_ssl)
# root_resp.raise_for_status()
# root_pg_id = root_resp.json()["processGroupFlow"]["id"]
#
# with open(json_file_path, "r") as f:
# raw = json.load(f)
#
# flow = raw.get("flowContents")
# if not flow:
# raise ValueError("Missing 'flowContents' in provided file.")
#
# snippet = extract_snippet_fields(flow)
#
# payload = {
# "revision": { "version": 0 },
# "component": {
# "name": flow.get("name", "ImportedGroup"),
# "position": { "x": 0.0, "y": 0.0 },
# "flowSnippet": snippet
# }
# }
#
# url = f"{nifi_host}/nifi-api/process-groups/{root_pg_id}/process-groups"
# resp = requests.post(url, headers=headers, json=payload, verify=verify_ssl)
#
# if resp.status_code == 201:
# print("✅ Flow uploaded successfully!")
# pg_id = resp.json()["component"]["id"]
# print(f"🔗 View it at: {nifi_host}/nifi/#/process-groups/{root_pg_id}/process-group/{pg_id}")
# else:
# print(f"❌ Upload failed: {resp.status_code} - {resp.text}")
#
# except Exception as e:
# print(f"🚨 Error: {e}")
#
#
#
#
# #####################################################################################
#
#
# def get_nifi_token(nifi_url, username, password, verify_ssl=False):
# """
# Get authentication token from NiFi.
# """
# token_url = f"{nifi_url}/access/token"
# headers = {
# "Content-Type": "application/x-www-form-urlencoded"
# }
# data = {
# "username": username,
# "password": password
# }
#
# response = requests.post(token_url, headers=headers, data=data, verify=verify_ssl)
#
# if response.ok:
# return response.text
# else:
# print(f"Failed to get token: {response.status_code}")
# print(response.text)
# response.raise_for_status()
#
# def get_root_process_group_id(nifi_url, token, verify_ssl=False):
# """
# Get the root process group ID from NiFi.
# """
# root_url = f"{nifi_url}/flow/process-groups/root"
# headers = {
# "Authorization": f"Bearer {token}"
# }
#
# response = requests.get(root_url, headers=headers, verify=verify_ssl)
#
# if response.ok:
# return response.json()["processGroupFlow"]["id"]
# else:
# print(f"Failed to get root process group ID: {response.status_code}")
# print(response.text)
# response.raise_for_status()
#
#
# def upload_nifi_pipeline_nifi_2_1(file_path, nifi_url, username, password, process_group_id=None, verify_ssl=False):
# """
# Authenticate to NiFi 2.1+, fetch root process group ID, and upload the flow definition JSON.
# """
# token = get_nifi_token(nifi_url, username, password, verify_ssl)
#
# if not process_group_id:
# process_group_id = get_root_process_group_id(nifi_url, token, verify_ssl)
#
# with open(file_path, 'r') as f:
# pipeline_json = json.load(f)
#
# import_endpoint = f"{nifi_url}/versions/process-groups/{process_group_id}/import"
# headers = {
# "Content-Type": "application/json",
# "Accept": "application/json",
# "Authorization": f"Bearer {token}"
# }
#
# response = requests.post(
# import_endpoint,
# headers=headers,
# data=json.dumps(pipeline_json),
# verify=verify_ssl
# )
#
# if response.ok:
# print("✅ Pipeline uploaded successfully (NiFi 2.1).")
# return response.json()
# else:
# print(f"❌ Failed to upload pipeline: {response.status_code}")
# print(response.text)
# response.raise_for_status()
#
#
#
# #################### Testing ###################
# upload_nifi_pipeline_nifi_2_1(
# file_path="templates/basic_ETL.json",
# nifi_url="https://127.0.0.1.nip.io/nifi-api",
# username="lab08nifiuser",
# password="tartunifi2023",
# #process_group_id="5bb69687-0194-1000-ce98-931e7e192b3d",
# verify_ssl=False # set to True if using trusted SSL certs
# )
|