-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopulate.py
More file actions
394 lines (305 loc) · 12.1 KB
/
populate.py
File metadata and controls
394 lines (305 loc) · 12.1 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import json
import requests
REMOTE_URL = 'https://dome-marketplace.eu'
LOCAL_URL = 'http://proxy.docker:8004'
LOCAL_TOKEN = ''
PAGE_SIZE = 50
PARTIES = {}
CATALOGS = {}
PRICES = {}
RESOURCES = {}
SERVICES = {}
PRODUCTS = {}
CATEGORIES = {}
def fetch_all(url):
all_items = []
page = 0
while True:
params = {
"offset": page * PAGE_SIZE,
"limit": PAGE_SIZE
}
print(f"Fetching page {page + 1}...")
response = requests.get(url, headers={
"Accept": "application/json"
}, params=params)
response.raise_for_status()
data = response.json()
if not data:
print("No more data.")
break
all_items.extend(data)
page += 1
return all_items
def create_party(party_data):
url = f"{LOCAL_URL}/admin/party/organization"
del party_data['id']
del party_data['href']
response = requests.post(url, headers={
"Accept": "application/json",
"Authorization": f"Bearer {LOCAL_TOKEN}"
}, json=party_data)
response.raise_for_status()
return response.json()['id']
def create_catalog(catalog_data):
url = f"{LOCAL_URL}/admin/catalog/catalog"
del catalog_data['id']
del catalog_data['href']
# Update the party IDs in the catalog
for party in catalog_data.get('relatedParty', []):
remote_id = party.get('id')
if remote_id in PARTIES:
party['id'] = PARTIES[remote_id]
party['href'] = PARTIES[remote_id]
response = requests.post(url, headers={
"Accept": "application/json",
"Authorization": f"Bearer {LOCAL_TOKEN}"
}, json=catalog_data)
response.raise_for_status()
return response.json()['id']
def create_service(service_data):
url = f"{LOCAL_URL}/admin/service/serviceSpecification"
del service_data['id']
del service_data['href']
# Update the party IDs in the catalog
for party in service_data.get('relatedParty', []):
remote_id = party.get('id')
if remote_id in PARTIES:
party['id'] = PARTIES[remote_id]
party['href'] = PARTIES[remote_id]
response = requests.post(url, headers={
"Accept": "application/json",
"Authorization": f"Bearer {LOCAL_TOKEN}"
}, json=service_data)
response.raise_for_status()
return response.json()['id']
def create_resource(resource_data):
import ipdb; ipdb.sset_trace()
url = f"{LOCAL_URL}/admin/resource/resourceSpecification"
del resource_data['id']
del resource_data['href']
# Update the party IDs in the catalog
for party in resource_data.get('relatedParty', []):
remote_id = party.get('id')
if remote_id in PARTIES:
party['id'] = PARTIES[remote_id]
party['href'] = PARTIES[remote_id]
response = requests.post(url, headers={
"Accept": "application/json",
"Authorization": f"Bearer {LOCAL_TOKEN}"
}, json=resource_data)
response.raise_for_status()
return response.json()['id']
def create_product(product_data):
url = f"{LOCAL_URL}/admin/catalog/productSpecification"
del product_data['id']
del product_data['href']
# Update the party IDs in the catalog
for party in product_data.get('relatedParty', []):
remote_id = party.get('id')
if remote_id in PARTIES:
party['id'] = PARTIES[remote_id]
party['href'] = PARTIES[remote_id]
for service in product_data.get('serviceSpecification', []):
remote_id = service.get('id')
if remote_id in SERVICES:
service['id'] = SERVICES[remote_id]
service['href'] = SERVICES[remote_id]
for resource in product_data.get('resourceSpecification', []):
remote_id = resource.get('id')
if remote_id in RESOURCES:
resource['id'] = RESOURCES[remote_id]
resource['href'] = RESOURCES[remote_id]
if "productSpecificationRelationship" in product_data:
product_data["productSpecificationRelationship"] = []
response = requests.post(url, headers={
"Accept": "application/json",
"Authorization": f"Bearer {LOCAL_TOKEN}"
}, json=product_data)
response.raise_for_status()
return response.json()['id']
def create_pop(price_data):
url = f"{LOCAL_URL}/admin/catalog/productOfferingPrice"
del price_data['id']
del price_data['href']
response = requests.post(url, headers={
"Accept": "application/json",
"Authorization": f"Bearer {LOCAL_TOKEN}"
}, json=price_data)
response.raise_for_status()
return response.json()['id']
def create_offering(offering_data):
url = f"{LOCAL_URL}/admin/catalog/productOffering"
del offering_data['id']
del offering_data['href']
for price in offering_data.get('productOfferingPrice', []):
remote_id = price.get('id')
if remote_id in PRICES:
price['id'] = PRICES[remote_id]
price['href'] = PRICES[remote_id]
for category in offering_data.get('category', []):
remote_id = category.get('id')
if remote_id in CATEGORIES:
category['id'] = CATEGORIES[remote_id]
category['href'] = CATEGORIES[remote_id]
if 'productSpecification' in offering_data:
remote_id = offering_data['productSpecification']['id']
offering_data['productSpecification']['id'] = PRODUCTS[remote_id]
offering_data['productSpecification']['href'] = PRODUCTS[remote_id]
response = requests.post(url, headers={
"Accept": "application/json",
"Authorization": f"Bearer {LOCAL_TOKEN}"
}, json=offering_data)
response.raise_for_status()
return response.json()['id']
def create_category(category_data):
url = f"{LOCAL_URL}/admin/catalog/category"
del category_data['id']
del category_data['href']
if not category_data["isRoot"]:
category_data["parentId"] = CATEGORIES[category_data["parentId"]]
response = requests.post(url, headers={
"Accept": "application/json",
"Authorization": f"Bearer {LOCAL_TOKEN}"
}, json=category_data)
response.raise_for_status()
return response.json()['id']
if __name__ == "__main__":
# Get all the parties
party_url = f"{REMOTE_URL}/party/organization"
parties = fetch_all(party_url)
print(f"Fetched {len(parties)} parties from remote.")
# Create a map with the party IDs
for party in parties:
remote_id = party.get('id')
try:
if remote_id:
# Create new parties in the local environment
local_id = create_party(party)
PARTIES[remote_id] = local_id
print(f"Created party {remote_id} with local ID {local_id}")
except Exception as e:
print(f"Error creating party {remote_id}")
# Get all the root categories
category_url = f"{REMOTE_URL}/catalog/category?isRoot=true"
categories = fetch_all(category_url)
print(f"Fetched {len(categories)} root categories from remote.")
for category in categories:
remote_id = category.get('id')
try:
if remote_id:
# Create new parties in the local environment
local_id = create_category(category)
CATEGORIES[remote_id] = local_id
print(f"Created category {remote_id} with local ID {local_id}")
except Exception as e:
print(f"Error creating category {remote_id}")
# Get all the child categories
category_url = f"{REMOTE_URL}/catalog/category?isRoot=false"
categories = fetch_all(category_url)
print(f"Fetched {len(categories)} child categories from remote.")
for category in categories:
remote_id = category.get('id')
try:
if remote_id:
# Create new parties in the local environment
local_id = create_category(category)
CATEGORIES[remote_id] = local_id
print(f"Created category {remote_id} with local ID {local_id}")
except Exception as e:
print(f"Error creating category {remote_id}")
# Get all the catalogs
catalog_url = f"{REMOTE_URL}/catalog/catalog"
catalogs = fetch_all(catalog_url)
print(f"Fetched {len(catalogs)} catalogs from remote.")
# Update the catalog with the new IDs
for catalog in catalogs:
remote_id = catalog.get('id')
try:
if remote_id:
# Create new catalogs in the local environment
local_id = create_catalog(catalog)
CATALOGS[remote_id] = local_id
print(f"Created catalog {remote_id} with local ID {local_id}")
except Exception as e:
print(f"Error creating catalog {remote_id}")
# Get all the service specs
service_url = f"{REMOTE_URL}/service/serviceSpecification"
services = fetch_all(service_url)
print(f"Fetched {len(services)} services from remote.")
# Update the product specs with the new party IDs
# Create a map with the product spec IDs
for service in services:
remote_id = service.get('id')
try:
if remote_id:
# Create new catalogs in the local environment
local_id = create_service(service)
SERVICES[remote_id] = local_id
print(f"Created service {remote_id} with local ID {local_id}")
except Exception as e:
print(f"Error creating service {remote_id}")
# Get all the resource specs
resource_url = f"{REMOTE_URL}/resource/resourceSpecification"
resources = fetch_all(resource_url)
print(f"Fetched {len(resources)} resources from remote.")
# Update the product specs with the new party IDs
# Create a map with the product spec IDs
for resource in resources:
remote_id = resource.get('id')
try:
if remote_id:
# Create new catalogs in the local environment
local_id = create_resource(resource)
RESOURCES[remote_id] = local_id
print(f"Created resource {remote_id} with local ID {local_id}")
except Exception as e:
print(f"Error creating resource {remote_id}")
# Get all the product specs
product_url = f"{REMOTE_URL}/catalog/productSpecification"
products = fetch_all(product_url)
print(f"Fetched {len(products)} products from remote.")
# Update the product specs with the new party IDs
# Create a map with the product spec IDs
for product in products:
remote_id = product.get('id')
try:
if remote_id:
# Create new catalogs in the local environment
local_id = create_product(product)
PRODUCTS[remote_id] = local_id
print(f"Created product {remote_id} with local ID {local_id}")
except Exception as e:
print(f"Error creating product {remote_id}")
# Get all the product offering prices
price_url = f"{REMOTE_URL}/catalog/productOfferingPrice"
pops = fetch_all(price_url)
print(f"Fetched {len(pops)} prices from remote.")
# Create a map with the product offering prices IDs
for pop in pops:
remote_id = pop.get('id')
try:
if remote_id:
# Create new catalogs in the local environment
local_id = create_pop(pop)
PRICES[remote_id] = local_id
print(f"Created price {remote_id} with local ID {local_id}")
except Exception as e:
print(f"Error creating price {remote_id}")
print('============')
print(PRICES)
print('============')
# Get all the product offerings
offering_url = f"{REMOTE_URL}/catalog/productOffering"
offerings = fetch_all(offering_url)
print(f"Fetched {len(offerings)} offerings from remote.")
# Create prodict offerings
for offering in offerings:
remote_id = offering.get('id')
try:
if remote_id:
local_id = create_offering(offering)
print(f"Created offering {remote_id} with local ID {local_id}")
except Exception as e:
print(f"Error creating offering {remote_id}")
print(str(e))