Skip to content

Commit c90b576

Browse files
author
Dan Bunker
committed
Fix global to tenant conversion script to output correct namespace
1 parent 414cb88 commit c90b576

File tree

2 files changed

+180
-65
lines changed

2 files changed

+180
-65
lines changed

convert-xdm-to-tenant.sh

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,35 +101,53 @@ process_schema_file() {
101101
# 4. Strip "xdm:" namespace from property keys
102102
# 5. Remove meta:intendedToExtend from fieldgroups (not needed for tenant schemas)
103103

104-
jq --arg tenant_id "$TENANT_ID" '
105-
# Function to recursively update all $ref values and replace xdm: with tenant: in property keys
106-
def update_refs_and_keys:
104+
jq --arg tenant_id "$TENANT_ID" --arg file_type "$file_type" '
105+
# Function to recursively update all $ref values
106+
def update_refs:
107107
if type == "object" then
108-
# First handle $ref if present
108+
# Handle $ref if present
109109
(if has("$ref") then
110110
.["$ref"] |= (
111111
gsub("https://ns\\.adobe\\.com/xdm/datatypes/paid-media"; "https://ns.adobe.com/\($tenant_id)/datatypes/paid-media") |
112112
gsub("https://ns\\.adobe\\.com/xdm/mixins/paid-media"; "https://ns.adobe.com/\($tenant_id)/mixins/paid-media")
113113
)
114114
else . end) |
115-
# Then recursively process all values and rename keys
115+
# Recursively process all values
116116
to_entries |
117+
map(.value |= update_refs) |
118+
from_entries
119+
elif type == "array" then
117120
map(
118-
# Replace "xdm:" prefix with tenant ID prefix for custom fields
121+
if type == "object" or type == "array" then
122+
update_refs
123+
else
124+
.
125+
end
126+
)
127+
else
128+
.
129+
end;
130+
131+
# Function to strip xdm: prefix from property keys (but not from values)
132+
def strip_xdm_prefix:
133+
if type == "object" then
134+
to_entries |
135+
map(
136+
# Strip xdm: prefix from keys
119137
if .key | startswith("xdm:") then
120-
.key |= gsub("^xdm:"; "\($tenant_id):")
138+
.key |= gsub("^xdm:"; "")
121139
else . end |
122140
# Recursively process the value
123-
.value |= update_refs_and_keys
141+
.value |= strip_xdm_prefix
124142
) |
125143
from_entries
126144
elif type == "array" then
127145
# For arrays, check if they contain strings that need replacement (like in "required" arrays)
128146
map(
129147
if type == "string" and startswith("xdm:") then
130-
gsub("^xdm:"; "\($tenant_id):")
148+
gsub("^xdm:"; "")
131149
elif type == "object" or type == "array" then
132-
update_refs_and_keys
150+
strip_xdm_prefix
133151
else
134152
.
135153
end
@@ -138,6 +156,24 @@ process_schema_file() {
138156
.
139157
end;
140158
159+
# Function to add tenant namespace prefix to property keys
160+
# Only for fieldgroups, not for datatypes (datatypes are reusable type definitions)
161+
# This produces Standard XDM format (tenantId:fieldName) for API upload
162+
# Note: Remove leading underscore from tenant_id when using as namespace prefix
163+
def add_tenant_prefix:
164+
if $file_type == "fieldgroup" and has("definitions") and (.definitions | type == "object") then
165+
.definitions |= (
166+
to_entries | map(
167+
.value.properties |= (
168+
to_entries | map(
169+
# Remove leading underscore from tenant_id for namespace prefix
170+
.key = "\($tenant_id | ltrimstr("_")):\(.key)"
171+
) | from_entries
172+
)
173+
) | from_entries
174+
)
175+
else . end;
176+
141177
# Update top-level $id
142178
if has("$id") then
143179
.["$id"] |= gsub("https://ns\\.adobe\\.com/xdm/"; "https://ns.adobe.com/\($tenant_id)/")
@@ -180,8 +216,14 @@ process_schema_file() {
180216
)
181217
else . end |
182218
183-
# Recursively update all $ref values and strip xdm: from property keys throughout the document
184-
update_refs_and_keys
219+
# First strip xdm: prefix from all property keys
220+
strip_xdm_prefix |
221+
222+
# Then add tenant prefix to property keys (for fieldgroups only)
223+
add_tenant_prefix |
224+
225+
# Finally update all $ref values
226+
update_refs
185227
' "$input_file" > "$output_file"
186228

187229
if [ $? -eq 0 ]; then

upload-tenant-schemas.sh

Lines changed: 126 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -151,60 +151,13 @@ upload_schema() {
151151
return 0
152152
fi
153153

154+
# Note: If DELETE_EXISTING is true, resources should have been deleted in bulk before upload
155+
# So if we still get "already exists", something went wrong with the bulk delete
154156
if [ "$DELETE_EXISTING" = true ]; then
155-
echo -e " ${YELLOW}${NC} Already exists, deleting and recreating (--delete-existing flag set)..."
156-
157-
# URL-encode the schema $id for use in the DELETE endpoint
158-
local encoded_id=$(printf '%s' "$schema_id" | jq -sRr @uri)
159-
160-
# Delete the existing resource
161-
delete_response=$(curl -s -w "\n%{http_code}" -X DELETE \
162-
"${PLATFORM_URL}/data/foundation/schemaregistry/tenant/${endpoint}/${encoded_id}" \
163-
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
164-
-H "x-api-key: ${CLIENT_ID}" \
165-
-H "x-gw-ims-org-id: ${ORG_ID}" \
166-
-H "x-sandbox-name: ${SANDBOX_NAME}")
167-
168-
delete_http_code=$(echo "$delete_response" | tail -n1)
169-
delete_response_body=$(echo "$delete_response" | sed '$d')
170-
171-
if [[ "$delete_http_code" =~ ^2[0-9][0-9]$ ]]; then
172-
echo -e " ${GREEN}${NC} Deleted (HTTP ${delete_http_code})"
173-
elif [ "$delete_http_code" = "404" ]; then
174-
echo -e " ${YELLOW}${NC} Not found for deletion (HTTP 404), will try to create anyway"
175-
else
176-
echo -e " ${RED}${NC} Delete failed (HTTP ${delete_http_code})"
177-
echo -e "${RED}Response:${NC}"
178-
echo "$delete_response_body" | jq '.' 2>/dev/null || echo "$delete_response_body"
179-
return 1
180-
fi
181-
182-
# Now try to create it (again) with POST
183-
response=$(curl -s -w "\n%{http_code}" -X POST \
184-
"${PLATFORM_URL}/data/foundation/schemaregistry/tenant/${endpoint}" \
185-
-H 'Content-Type: application/json' \
186-
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
187-
-H "x-api-key: ${CLIENT_ID}" \
188-
-H "x-gw-ims-org-id: ${ORG_ID}" \
189-
-H "x-sandbox-name: ${SANDBOX_NAME}" \
190-
-d @"${file_path}")
191-
192-
http_code=$(echo "$response" | tail -n1)
193-
response_body=$(echo "$response" | sed '$d')
194-
195-
if [[ "$http_code" =~ ^2[0-9][0-9]$ ]]; then
196-
echo -e " ${GREEN}${NC} Recreated (HTTP ${http_code})"
197-
schema_id=$(echo "$response_body" | grep -o '"$id":"[^"]*"' | cut -d'"' -f4)
198-
if [ -n "$schema_id" ]; then
199-
echo -e " ${BLUE}ID:${NC} ${schema_id}"
200-
fi
201-
return 0
202-
else
203-
echo -e " ${RED}${NC} Recreate failed (HTTP ${http_code})"
204-
echo -e "${RED}Response:${NC}"
205-
echo "$response_body" | jq '.' 2>/dev/null || echo "$response_body"
206-
return 1
207-
fi
157+
echo -e " ${RED}${NC} Already exists even after bulk delete attempt"
158+
echo -e "${RED}Response:${NC}"
159+
echo "$response_body" | jq '.' 2>/dev/null || echo "$response_body"
160+
return 1
208161
fi
209162

210163
echo -e " ${YELLOW}${NC} Already exists, attempting to update with PUT..."
@@ -251,6 +204,126 @@ upload_schema() {
251204
fi
252205
}
253206

207+
# Function to delete a schema resource
208+
delete_schema() {
209+
local file_path="$1"
210+
local endpoint="$2"
211+
local file_name=$(basename "$file_path")
212+
213+
echo -e " Deleting: ${YELLOW}${file_name}${NC}"
214+
215+
# Extract the $id from the schema file
216+
local schema_id=$(jq -r '.["$id"]' "$file_path")
217+
218+
# URL-encode the schema $id for use in the DELETE endpoint
219+
local encoded_id=$(printf '%s' "$schema_id" | jq -sRr @uri)
220+
221+
# Delete the resource
222+
response=$(curl -s -w "\n%{http_code}" -X DELETE \
223+
"${PLATFORM_URL}/data/foundation/schemaregistry/tenant/${endpoint}/${encoded_id}" \
224+
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
225+
-H "x-api-key: ${CLIENT_ID}" \
226+
-H "x-gw-ims-org-id: ${ORG_ID}" \
227+
-H "x-sandbox-name: ${SANDBOX_NAME}")
228+
229+
http_code=$(echo "$response" | tail -n1)
230+
response_body=$(echo "$response" | sed '$d')
231+
232+
if [[ "$http_code" =~ ^2[0-9][0-9]$ ]]; then
233+
echo -e " ${GREEN}${NC} Deleted (HTTP ${http_code})"
234+
return 0
235+
elif [ "$http_code" = "404" ]; then
236+
echo -e " ${YELLOW}${NC} Not found (HTTP 404), already deleted or never existed"
237+
return 0
238+
else
239+
echo -e " ${RED}${NC} Delete failed (HTTP ${http_code})"
240+
echo -e "${RED}Response:${NC}"
241+
echo "$response_body" | jq '.' 2>/dev/null || echo "$response_body"
242+
return 1
243+
fi
244+
}
245+
246+
# If --delete-existing flag is set, delete all resources in reverse order first
247+
if [ "$DELETE_EXISTING" = true ]; then
248+
echo -e "${BLUE}Deleting existing resources in reverse order...${NC}"
249+
echo ""
250+
251+
# Delete schemas first (they depend on fieldgroups)
252+
if [ -d "schemas/paid-media" ]; then
253+
echo -e "${GREEN}Deleting schemas...${NC}"
254+
file_count=0
255+
for file in schemas/paid-media/*.schema.json; do
256+
if [ -f "$file" ]; then
257+
if ! delete_schema "$file" "schemas"; then
258+
echo -e "${RED}Delete failed. Exiting.${NC}"
259+
exit 1
260+
fi
261+
((file_count++))
262+
echo ""
263+
fi
264+
done
265+
echo -e " Deleted ${GREEN}${file_count}${NC} schema files"
266+
echo ""
267+
fi
268+
269+
# Delete fieldgroups (they depend on datatypes)
270+
if [ -d "components/fieldgroups/paid-media" ]; then
271+
echo -e "${GREEN}Deleting fieldgroups...${NC}"
272+
file_count=0
273+
for file in components/fieldgroups/paid-media/*.schema.json; do
274+
if [ -f "$file" ]; then
275+
if ! delete_schema "$file" "fieldgroups"; then
276+
echo -e "${RED}Delete failed. Exiting.${NC}"
277+
exit 1
278+
fi
279+
((file_count++))
280+
echo ""
281+
fi
282+
done
283+
echo -e " Deleted ${GREEN}${file_count}${NC} fieldgroup files"
284+
echo ""
285+
fi
286+
287+
# Delete datatypes (they depend on nothing, but classes might depend on them)
288+
if [ -d "components/datatypes/paid-media" ]; then
289+
echo -e "${GREEN}Deleting datatypes...${NC}"
290+
file_count=0
291+
for file in components/datatypes/paid-media/*.schema.json; do
292+
if [ -f "$file" ]; then
293+
if ! delete_schema "$file" "datatypes"; then
294+
echo -e "${RED}Delete failed. Exiting.${NC}"
295+
exit 1
296+
fi
297+
((file_count++))
298+
echo ""
299+
fi
300+
done
301+
echo -e " Deleted ${GREEN}${file_count}${NC} datatype files"
302+
echo ""
303+
fi
304+
305+
# Delete classes last
306+
if [ -d "components/classes" ]; then
307+
echo -e "${GREEN}Deleting classes...${NC}"
308+
class_count=0
309+
for file in components/classes/*.schema.json; do
310+
if [ -f "$file" ]; then
311+
if ! delete_schema "$file" "classes"; then
312+
echo -e "${RED}Delete failed. Exiting.${NC}"
313+
exit 1
314+
fi
315+
((class_count++))
316+
echo ""
317+
fi
318+
done
319+
echo -e " Deleted ${GREEN}${class_count}${NC} class files"
320+
echo ""
321+
fi
322+
323+
echo -e "${GREEN}✓ All resources deleted successfully!${NC}"
324+
echo ""
325+
fi
326+
254327
# Upload classes first (needed by fieldgroups and schemas)
255328
if [ -d "components/classes" ]; then
256329
echo -e "${GREEN}Uploading classes...${NC}"

0 commit comments

Comments
 (0)