-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_connections_dev.py
More file actions
194 lines (162 loc) · 6.36 KB
/
Copy pathget_connections_dev.py
File metadata and controls
194 lines (162 loc) · 6.36 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import requests
import csv
import os
import json
import time
from typing import List, Dict, Any
def load_config(config_path: str) -> Dict[str, Any]:
"""Load configuration from a JSON file."""
with open(config_path) as f:
return json.load(f)
def fetch_data(session: requests.Session, url: str) -> Dict[str, Any]:
"""Fetch data from the API."""
response = session.get(url, allow_redirects=False, timeout=10)
if response.status_code == 307:
redirect_url = response.headers.get("Location")
response = session.get(redirect_url)
response.raise_for_status()
return response.json()
def process_items(items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Process and extract necessary fields from items."""
processed_items = []
for item in items:
processed_item = {
"code": item.get("id", ""),
"adapter_type": item.get("adapterType", {}).get("displayName", ""),
"policy": item.get("securityPolicy", ""),
"status": item.get("status", ""),
"usage": item.get("usage", ""),
"usage_active": item.get("usageActive", ""),
"host_url": next(
(
prop.get("propertyValue")
for prop in item.get("connectionProperties", [])
if prop.get("displayName")
in [
"WSDL URL",
"Connection URL",
"Host",
"ERP Cloud Host",
"FTP Server Host Address",
]
),
"",
),
}
processed_items.append(processed_item)
return processed_items
def fetch_service_account(
session: requests.Session, base_url: str, code: str, instance: str
) -> str:
"""Fetch the service account for a given code."""
url = f"{base_url}/ic/api/integration/v1/connections/{code}?integrationInstance={instance}"
response_data = fetch_data(session, url)
return next(
(
prop.get("propertyValue", "")
for prop in response_data.get("securityProperties", [])
if prop.get("propertyName") == "username"
),
"",
)
def write_to_csv(
file_path: str, rows: List[Dict[str, Any]], fieldnames: List[str]
) -> None:
"""Write processed rows to a CSV file."""
with open(file_path, mode="w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
def main(config_path: str) -> None:
"""Main function to fetch API data and write to CSV."""
config = load_config(config_path)
access_token = config["environments"]["dev"]["authorization"]["bearer_token"]
instance = config["environments"]["dev"]["instance"]
api_path = config["api_uris"]["retrieve_connections"]
region = config['region']
base_url = config['base_url']
output_directory = os.path.join(os.getcwd(), "conn_inventory_dev")
os.makedirs(output_directory, exist_ok=True)
output_file = os.path.join(output_directory, "oic_dev_conn_extract.csv")
headers = {"Authorization": f"Bearer {access_token}"}
session = requests.Session()
session.headers.update(headers)
limit = 100
offset = 0
fieldnames = [
"code",
"adapter_type",
"policy",
"status",
"usage",
"usage_active",
"host_url",
"service_account",
]
all_processed_items = []
errors = []
try:
while True:
print(f"Fetching data with offset: {offset}")
old_url = f"https://{instance}.integration.{region}.ocp.oraclecloud.com{api_path}?limit={limit}&offset={offset}"
try:
response_data = fetch_data(session, old_url)
except requests.exceptions.RequestException as err:
errors.append(f"Error fetching data from old URL: {err}")
break
items = response_data.get("items", [])
if items:
all_processed_items.extend(process_items(items))
else:
print("No more items to process. Stopping execution.")
break
if not response_data.get("hasMore", False):
break
offset += limit
new_url = f"https://design.integration.{region}.ocp.oraclecloud.com{api_path}?limit={limit}&offset={offset}&integrationInstance={instance}"
try:
response_data = fetch_data(session, new_url)
except requests.exceptions.RequestException as err:
errors.append(f"Error fetching data from new URL: {err}")
break
items = response_data.get("items", [])
if items:
all_processed_items.extend(process_items(items))
else:
print("No more items to process. Stopping execution.")
break
if not response_data.get("hasMore", False):
break
offset += limit
except requests.exceptions.RequestException as err:
errors.append(f"General request error occurred: {err}")
if errors:
print("Errors encountered during execution:")
for error in errors:
print(f"- {error}")
else:
write_to_csv(output_file, all_processed_items, fieldnames)
print(f"Data successfully written to {output_file}")
# Fetch service accounts
for item in all_processed_items:
code = item["code"]
try:
item["service_account"] = fetch_service_account(
session,
f"https://{instance}.integration.{region}.ocp.oraclecloud.com",
code,
instance,
)
except requests.exceptions.RequestException as err:
print(f"Error fetching service account for {code}: {err}")
# Write updated data back to CSV
write_to_csv(output_file, all_processed_items, fieldnames)
print(f"Service accounts successfully added to {output_file}")
if __name__ == "__main__":
start_time = time.time()
main("config.json")
end_time = time.time()
execution_time = end_time - start_time
print(
f"Execution time: {execution_time:.2f} seconds ({execution_time/60:.2f} minutes)"
)