Skip to content

Commit 0367564

Browse files
authored
Merge pull request #9 from jamf/gpugh-pagination-1
Gpugh pagination 1
2 parents 2b2852a + c13133e commit 0367564

1 file changed

Lines changed: 45 additions & 13 deletions

File tree

services/Framework-Fixer/Framework-Fixer.sh

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,24 +231,56 @@ group_name_prompt() {
231231
# Check if the command was successful
232232
if [[ "${raw_JSON}" =~ ^(200|201|202|204)$ ]]
233233
then
234-
raw_JSON=$("${curl_path}" -s -H "Authorization: Bearer ${bearer_token}" "${jamf_pro_url}/api/v2/computer-groups/smart-groups")
234+
# Collect all group data
235+
page=0
236+
page_size=100
237+
total_count=0
238+
239+
# Get first page to determine total count
240+
echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Fetching page ${page} of Smart Computer Groups" >> "${log_file}"
241+
page_response=$("${curl_path}" -s -H "Authorization: Bearer ${bearer_token}" "${jamf_pro_url}/api/v2/computer-groups/smart-groups?page=${page}&page-size=${page_size}&sort=name:asc")
242+
243+
# Extract total count
244+
total_count=$(echo "${page_response}" | grep -o '"totalCount"[[:space:]]*:[[:space:]]*[0-9]*' | grep -o '[0-9]*')
245+
246+
# Store the full first page response
247+
raw_JSON="${page_response}"
248+
249+
# If there are more than 100 entries, we need to fetch additional pages and merge results
250+
if [[ ${total_count} -gt ${page_size} ]]; then
251+
echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Found ${total_count} total groups, fetching all pages" >> "${log_file}"
252+
253+
# Calculate total pages needed
254+
total_pages=$(( (total_count + page_size - 1) / page_size ))
255+
256+
# Create a temporary file to collect all results
257+
temp_file=$(mktemp)
258+
259+
# Extract names from first page
260+
echo "${page_response}" | grep '"name"[[:space:]]*:' | sed 's/.*"name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/' > "${temp_file}"
261+
262+
# Loop through remaining pages and collect names
263+
for (( page=1; page<total_pages; page++ )); do
264+
echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Fetching page ${page} of Smart Computer Groups" >> "${log_file}"
265+
page_response=$("${curl_path}" -s -H "Authorization: Bearer ${bearer_token}" "${jamf_pro_url}/api/v2/computer-groups/smart-groups?page=${page}&page-size=${page_size}&sort=name:asc")
266+
267+
# Extract and append names from this page
268+
echo "${page_response}" | grep '"name"[[:space:]]*:' | sed 's/.*"name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/' >> "${temp_file}"
269+
done
270+
271+
# Read all group names from temp file
272+
smart_group_names=$(cat "${temp_file}")
273+
rm "${temp_file}"
274+
else
275+
# Single page, extract names directly
276+
smart_group_names=$(echo "${raw_JSON}" | grep '"name"[[:space:]]*:' | sed 's/.*"name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')
277+
fi
235278
else
236279
# If not, present an error message
237280
error_prompt
238281
fi
239-
# Convert JSON to XML plist for xpath parsing
240-
plist_data=$(echo "${raw_JSON}" | "${plutil_path}" -convert xml1 -o - - 2>/dev/null)
241-
# Check if plist contains data
242-
if [[ -z "${plist_data}" ]]
243-
then
244-
echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: Failed to convert JSON to plist" >> "${log_file}"
245-
# If not, present an error message
246-
error_prompt
247-
fi
248-
249-
# Extract Smart Computer Group names
250-
smart_group_names=$(echo "${plist_data}" | xpath -q -e "//key[text()='name']/following-sibling::string[1]/text()")
251282

283+
# Check if we extracted group names successfully
252284
if [[ -z "${smart_group_names}" ]]
253285
then
254286
echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No Smart Computer Groups found" >> "${log_file}"

0 commit comments

Comments
 (0)