kostenloser Webspace werbefrei: lima-city


vCloud API ansprechen per Bash Script

lima-cityForumProgrammiersprachenSonstige Programmiersprachen

  1. Autor dieses Themas

    fourflames

    fourflames hat kostenlosen Webspace.

    Hallo Freunde,

    ich bin gerade dabei mich mit curl und de REST API von vCloud vertraut zu machen.
    Das Skript habe ich als Bash Script angefangen.
    Ziel ist es, per curl command die notwendigen Informationen (org, vapp, etc.) abzuholen um dann letztendlich eine vm per Skript in einer existierenden "leeren" vApp anzulegen.

    Hat hierbei jemand bereits Erfahrungen sammeln können? Gibt es vielleicht andere nützliche Alternativen, die das Handling deutlich erleichtern?

    #!/bin/bash
    
    LOGFILE=vcloud-cli.sh.log
    RETAIN_NUM_LINES=1000
    
    function logsetup {
        TMP=$(tail -n $RETAIN_NUM_LINES $LOGFILE 2>/dev/null) && echo "${TMP}" > $LOGFILE
        exec > >(tee -a $LOGFILE)
        exec 2>&1
    }
    
    function log {
        echo "[$(date)]: $*"
    }
    
    logsetup
    
    showenv() {
    
    	echo "$VCLOUD_HOST"
    	echo "$VCLOUD_ORG"
    	echo "$VCLOUD_USER"
    	echo "$VCLOUD_PASS"
    }
    
    usage() {
      echo "vcloud-curl: simple curl wrapper to allow GET requests to vcloud API"
      echo
      echo "Usage:"
      echo "  vcloud-cli.sh {request}"
      echo 
      echo "where {request} is the resource required after the /api end point"
      echo "eg: vcloud-cli.sh vApp/{vm-id}/virtualHardwareSection/cpu"
      echo 
      echo "You must set in environment:"
      echo "  VCLOUD_HOST"
      echo "  VCLOUD_ORG"
      echo "  VCLOUD_USER"
      echo "  VCLOUD_PASS"
      exit 1
    }
    
    [ -z "$VCLOUD_HOST" ] && usage
    [ -z "$VCLOUD_ORG" ]  && usage
    [ -z "$VCLOUD_USER" ] && usage
    [ -z "$VCLOUD_PASS" ] && usage
    
    showenv
    
    log "vcloud-host: "$VCLOUD_HOST
    log "vcloud-org: "$VCLOUD_ORG
    log "vcloud-user: "$VCLOUD_USER
    log "vcloud-pass: "$VCLOUD_PASS
    
    CURL_OPTS=${CURL_OPTS-'--silent'} 
    MAIN_QUERY_CURL_OPTS=${MAIN_QUERY_CURL_OPTS-''} 
    
    api_version=${VCLOUD_API_VERSION-'1.5'} 
    
    MAIN_QUERY_CONTENT_TYPE=${MAIN_QUERY_CONTENT_TYPE-"application/*+xml;version=${api_version}"}
    
    function callcurl() {
    	CALLCURL=$(curl -i -k -H "Accept:application/*+xml;version=${api_version}" -H "x-vcloud-authorization:${SESSION_KEY}" -u "${VCLOUD_USER}@${VCLOUD_ORG}:${VCLOUD_PASS}" $*)
    	echo $CALLCURL
    }
    
    # set up the session key and organization URL -> ORGLINK
    ANTWORT=$(curl -i -k -H "Accept:application/*+xml;version=${api_version}" -u "${VCLOUD_USER}@${VCLOUD_ORG}:${VCLOUD_PASS}" -X POST https://${VCLOUD_HOST}/api/sessions) 
    SESSION_KEY=$(curl -i -k -H "Accept:application/*+xml;version=${api_version}" -u "${VCLOUD_USER}@${VCLOUD_ORG}:${VCLOUD_PASS}" -X POST https://${VCLOUD_HOST}/api/sessions \
    	| grep '^x-vcloud-authorization:' \
    	| tr -d '\r' \
    	| awk '{print $2}')
    
    ORGLINK=$(echo $ANTWORT |awk -F"href=" '{print $6}' | cut -d " " -f 1 | tr --delete "\"" | tr --delete "\n")
    
    echo "Session-key: "$SESSION_KEY 
    echo "ORGLINK="$ORGLINK
    
    callcurl "-X GET" $ORGLINK
    
    # retrieve catalogs from org
    CATALOG=$(callcurl "-X GET https://${VCLOUD_HOST}/api/query?type=catalog")
    echo $CATALOG
    
    # retrieve catalog ID for Artur Test
    KATALOG=$(echo $CATALOG | grep /api/catalog | grep Artur | awk -F'/api/catalog' '{print $2}' | tr --delete "\/" | tr --delete "\"" | tr --delete ">" | cut -d " " -f 1)
    
    echo "->> Catalog-ID: "$KATALOG
    
    # retreive Template-ID for deployment (looking up CentOS66-Final)
    #TEMPLATE=$(callcurl "-X GET https://${VCLOUD_HOST}/api/catalog/${KATALOG}" | awk -F'CentOS' '{print $1}')
    
    #TEMPLATE=b119b8aa-745f-4b7f-aad8-a86500c09dcd
    echo "->> TEMPLATE: "$TEMPLATE
    
    # set the vApp ID for API-Test vApp
    #VAPPID=7a403246-5ed2-4cde-b77b-a5b2c5638f70
    
    echo "->> VAPPID: "$VAPPID
    echo "before catalogitem "$TEMPLATE
    # CATITEM=$(callcurl "-X GET https://${VCLOUD_HOST}/api/catalogItem/${TEMPLATE}")
    echo "->> CATITEM: "$CATITEM
    
    
    # check if session_key is set
    if [ -z "${SESSION_KEY}" ]; then
      echo "Failed to get vCloud session. Bailing"
      log "Exit 2 because Session key is empty."
      exit 2
    fi
    
    
    
    # run main request via curl
    #curl $CURL_OPTS $MAIN_QUERY_CURL_OPTS \
    #   -H "Accept:${MAIN_QUERY_CONTENT_TYPE}" \
    #   -H "x-vcloud-authorization: ${SESSION_KEY}" \
    #   "https://${VCLOUD_HOST}/api/$1"
    
    # Log out
    curl $CURL_OPTS \
       -H "Accept:application/*+xml;version=${api_version}" \
       -H "x-vcloud-authorization: ${SESSION_KEY}" \
       -X DELETE \
       "https://${VCLOUD_HOST}/api/session"


    Beitrag zuletzt geändert: 7.4.2015 20:06:25 von fourflames
  2. Diskutiere mit und stelle Fragen: Jetzt kostenlos anmelden!

    lima-city: Gratis werbefreier Webspace für deine eigene Homepage

Dir gefällt dieses Thema?

Über lima-city

Login zum Webhosting ohne Werbung!