-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate_catalogs.py
More file actions
236 lines (176 loc) · 9.73 KB
/
migrate_catalogs.py
File metadata and controls
236 lines (176 loc) · 9.73 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
import requests
# BASE = 'http://proxy.docker:8004'
BASE = 'https://dome-marketplace.eu'
TOKEN = 'eyJraWQiOiJkaWQ6a2V5OnpEbmFlVlluV1RadTVuYnJIMXFtQlZNdk53U3J0S25rUmJDWjR4SDVoMkxRUG56ZHIiLCJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJkaWQ6a2V5OnpEbmFlVFUzOVd4OUtYZ21Fd21mWHNaU3lFVnhnQ3F3Q1Ztb1B5VlFVVEQ4YmhXOGEiLCJzdWIiOiJkaWQ6a2V5OnpEbmFlcnRtQ0RhR055ekZQdkNlNmtWSFNLYVFkcHV5RGVyMU5TWTVBSFI0UWNKMlciLCJzY29wZSI6Im9wZW5pZCBsZWFyY3JlZGVudGlhbCIsImlzcyI6Imh0dHBzOi8vdmVyaWZpZXIuZG9tZS1tY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DQ2ZjM5MiIsImNsaWVudF9pZCI6Imh0dHBzOi8vdmVyaWZpZXIuZG9tZS1tYXJrZXRwbGFjZS5ldSJ9.GglplFROPEE1IRUc73W6TIPa64kK244y7rLlmni0Kx9vXtM4k8AyDhLgK_3s0axIdf2xEA8D3iadJIKmrTqzMA'
PAGE_SIZE = 50
PARTIES = {}
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 get_product_spec(spec_id):
url = f"{BASE}/catalog/productSpecification/{spec_id}"
resp = requests.get(url)
resp.raise_for_status()
return resp.json()
def create_category(name, party):
url = f'{BASE}/admin/catalog/category'
cat = {
"isRoot": True,
"lifecycleStatus": "Launched",
"name": name
# "relatedParty": [{
# "id": party,
# "href": party,
# "role": "Owner"
# }]
}
resp = requests.post(url, json=cat, headers={
'Authorization': 'Bearer ' + TOKEN
})
resp.raise_for_status()
return resp.json()
def update_catalog(catalog_id, category_id, category_name):
url = f'{BASE}/admin/catalog/catalog/{catalog_id}'
catalog = {
"category": [
{
"id": category_id,
"href": category_id,
"name": category_name
}
]
}
resp = requests.patch(url, json=catalog, headers={
'Authorization': 'Bearer ' + TOKEN
})
resp.raise_for_status()
return resp.json()
def update_offering(offer, category_id, category_name):
url = f'{BASE}/admin/catalog/productOffering/{offer["id"]}'
categories = offer.get('category', [])
categories.append({
'id': category_id,
'href': category_id,
'name': category_name
})
resp = requests.patch(url, json={
'category': categories
}, headers={
'Authorization': 'Bearer ' + TOKEN
})
resp.raise_for_status()
if __name__ == "__main__":
catalog_url = f"{BASE}/catalog/catalog"
catalogs = fetch_all(catalog_url)
for catalog in catalogs:
if 'name' not in catalog:
continue
if 'relatedParty' not in catalog or len(catalog['relatedParty']) == 0:
continue
catalog_id = catalog['id']
catalog_name = catalog['name']
party_id = catalog['relatedParty'][0]['id']
if party_id not in PARTIES:
PARTIES[catalog['relatedParty'][0]['id']] = {
'migrated': [],
'pending': []
}
if 'category' in catalog and len(catalog['category']) > 0:
category = catalog['category'][0]['id']
print(f'-----> Found catalog {catalog_name} ({catalog_id}) with category {category} and status {catalog["lifecycleStatus"]}')
PARTIES[party_id]['migrated'].append({
'id': catalog_id,
'name': catalog_name,
'status': catalog["lifecycleStatus"],
'categoryId': category
})
else:
print(f'No category found for catalog {catalog_name} ({catalog_id}) with status {catalog["lifecycleStatus"]}')
PARTIES[party_id]['pending'].append({
'id': catalog_id,
'name': catalog_name,
'status': catalog["lifecycleStatus"]
})
print('=========================================================================')
print('=========================================================================')
for party_id, data in PARTIES.items():
print('--------------------------------------------------------------')
print(f'Party {party_id} has {len(data["migrated"])} migrated catalogs and {len(data["pending"])} pending catalogs')
print('Migrated catalogs: ' + ', '.join([f'{c["name"]} ({c["id"]})' for c in data['migrated']]))
print('Pending catalogs: ' + ', '.join([f'{c["name"]} ({c["id"]})' for c in data['pending']]))
for catalog in data['pending']:
try:
# Create a new catalog category
category = create_category(catalog['name'], party_id)
# Update the catalog with the new category
update_catalog(catalog['id'], category['id'], category['name'])
catalog['categoryId'] = category['id']
data['migrated'].append(catalog)
except Exception as e:
print(f'Error creating category for catalog {catalog["name"]}: {e}')
continue
print(f'Created category {category["name"]} ({category["id"]}) for catalog {catalog["name"]} ({catalog["id"]})')
print('=========================================================================')
print('=========================================================================')
offerings_url = f'{BASE}/catalog/productOffering'
offers = fetch_all(offerings_url)
for offer in offers:
try:
spec = get_product_spec(offer['productSpecification']['id'])
provider = spec['relatedParty'][0]['id']
# Get the category ID to be included
category_id = None
category_name = None
for cat in PARTIES[provider]['migrated']:
if cat['status'].lower() == 'launched':
category_id = cat['categoryId']
category_name = cat['name']
print(f'Found Launched catalog {cat["name"]}')
break
else:
print(f'Not Found any Launched catalog for offer {offer["name"]}')
if category_id is None:
for cat in PARTIES[provider]['migrated']:
if cat['status'].lower() == 'active':
category_id = cat['categoryId']
category_name = cat['name']
print(f'Found Active catalog {cat["name"]}')
break
else:
print(f'Not Found any Active catalog for offer {offer["name"]}')
if category_id is None:
cat = PARTIES[provider]['migrated'][0]
category_id = cat['categoryId']
category_name = cat['name']
print(f'Using catalog {cat["name"]} with status {cat["status"]}')
update_offering(offer, category_id, category_name)
print(f'Updated product offer {offer["id"]} - {offer["name"]}')
except Exception as e:
print(f'Error updating product offer {offer["id"]} - {offer["name"]}: {e}')
continue
# Get all the catalogs
# Check if the same provider has more than 1 catalog
# Create a category for each catalog
# Update catalog with the category information
# Save category info for later use
# Get all the product offerings
# Get the provider info
# Choose a catalog from the provider
# Add the catalog category to the product offering