HPE OneView
1753278 Members
5640 Online
108792 Solutions
New Discussion

REST API getting list of current hardware

 
SOLVED
Go to solution
BradV
Esteemed Contributor

REST API getting list of current hardware

I'm working towards creating server profile templates.  In order to do that, I need to get a current listing of all hardware types.  That call is only pulling back 32 and not all.  I am using: 

 

# First, get a list of all hardware:
HARDW=hardw
curl --insecure \
     --header "auth: ${sessionID}" \
     --header "X-API-Version: ${currentVersion}" \
     --output ${HARDW} \
     --request GET ${OneView}/rest/server-hardware?start=0&count=-1

 

I thought by adding count=-1 that was supposed to return all?  (I am using API version 1000).  Anyone have a clue what I am doing wrong?

FYI, I changed the count from -1, to 20, 200, 1000 and it always just returns 32 servers.

4 REPLIES 4
ChrisLynch
HPE Pro

Re: REST API getting list of current hardware

What you are getting is a paginated return.  We limit the number of objects on the return, so that it does not overwhelm the appliance.  So, you need to look at the nextPageUri value if it is not null or empty.  If it has a value, make a looping call until it is, storing each array of members into a new local copy of all members.

Just to reiterate a prior statement of mine, our scripting tool kits would handle a lot of this for you automatically.


I am an HPE employee

Accept or Kudo

BradV
Esteemed Contributor

Re: REST API getting list of current hardware

Hi Chris,

OK.  Thanks.  That helped.  I can now get them all with: 

# First, get a list of all hardware:
HARDW=hardw
curl --insecure \
     --header "auth: ${sessionID}" \
     --header "X-API-Version: ${currentVersion}" \
     --output ${HARDW} \
     --request GET ${OneView}/rest/server-hardware?start=0&count=-1
# That only pulls down the first 32.  To get the rest, do:
i=1
NEXT=$(/bin/sed -e 's|^.*nextPageUri":"\(/rest/server-hardware.*\):,"prevPageUri.*|\1|' ${HARDW})
while [[ ${NEXT != *"null"* ]]; do
   curl --insecure \
     --header "auth: ${sessionID}" \
     --header "X-API-Version: ${currentVersion}" \
     --output ${HARDW}${i} \
     --request GET ${OneView}${NEXT}
   NEXT=$(/bin/sed -e 's|^.*nextPageUri":"\(/rest/server-hardware.*\):,"prevPageUri.*|\1|' ${HARDW}${i})
   if [[ ${#NEXT} -gt 50 ]]; then
      NEXT$(/bin/sed -e 's|^.*nextPageUri":\(.*\),"prevPageUri.*|\1|' ${HARDW}${i})
   fi
   ((i++))
done

I just need to then concatenate to a single file.

 

I am trying to make openview-python work, but not having a lot of luck yet.

Thanks!

BradV
Esteemed Contributor
Solution

Re: REST API getting list of current hardware

Couldn't come up with a clean method to concatenate programatically, but did it just be looking at how many hardware output files there were.  This will get them into a json formatted file.  

 

 

# Follow instructions in: OneView-API_Get_Session_Credentials.txt
#
# To get a list of the current hardware,  run:
# Create a variable pointing to a file to hold the hardware output:
HARDW=hardw
# Get the list of current server hardware:
curl --insecure \
     --header "auth: ${sessionID}" \
     --header "X-API-Version: ${currentVersion}" \
     --output ${HARDW} \
     --request GET ${OneView}/rest/server-hardware?start=0&count=-1
#
# That only pulls down 32 devices.  Need to look for nextPageUri:
i=1
NEXT=$(/bin/sed -e 's|^.*nextPageUri":"\(/rest/server-hardware.*\)","prevPageUri.*|\1|' ${HARDW})
while [[ ${NEXT} != *"null"* ]]; do
   curl --insecure \
     --header "auth: ${sessionID}" \
     --header "X-API-Version: ${currentVersion}" \
     --output ${HARDW}${i} \
     --request GET ${OneView}${NEXT}
   NEXT=$(/bin/sed -e 's|^.*nextPageUri":"\(/rest/server-hardware.*\)","prevPageUri.*|\1|' ${HARDW}${i})
   if [[ ${#NEXT} -gt 50 ]]; then
      NEXT=$(/bin/sed -e 's|^.*nextPageUri":\(.*\)","prevPageUri.*|\1|' ${HARDW}${i})
   fi
   ((i++))
done
cat ${HARDW} ${HARDW}1 ${HARDW}2 ${HARDW}3 ${HARDW}4 ${HARDW}5 > hardw-all-raw
cat hardw-all-raw | jq -r '.' > hardw-all

 

Once you have the full hardware list, can then get a listing of the different hardware types with: 

# To get a list of server hardware type uri's:
RAW_DATA=$(grep -E 'serverHardwareTypeUri|model' hardw-all | sed -e 's|^\s\(.*\)$|\1' | sed -e '{$!{ N;s|","model|}}' | sed -e 's|": "|":"|g' | tr ' ' '_' | 

sort -u)
i=0
declare -A HWR
for f in $(echo ${RAW_DATA}); do
   echo "f = ${f}"
   HWR[${i},0]=$(echo ${f} | cut -d ':' -f2 | cut -d ',' -f1)
   HWR[${i},1]=$(echo ${f} | cut -d ':' -f3 | tr '_' ' ')
   ((i++))
done

Hopefully that will help someone else.

BradV
Esteemed Contributor

Re: REST API getting list of current hardware

Updated my procedure a little to programmaticaly concatenate the different intermediate files:

# Need to know the number of iterations and build a command to concatentate all into a single file:
echo ${i}
j=0
while [[ $[j} -lt ${i} ]]; do
   echo ${j}
   if [[ ${j} -eq 0 ]]; then
      CMD="cat ${HARDW}"
   else
      CMD="${CMD} ${HARDW}${j}"
   fi
   ((j++))
done
echo ${CMD}
$(echo ${CMD}) > hardw-all-raw
# Now process into a readable json format:
cat hardw-all-raw | jq -r '.' > hardw-all
# Clean up
/bin/rm hardw hardw? hardw-all-raw