Skip to content
This repository has been archived by the owner on Nov 23, 2020. It is now read-only.

Commit

Permalink
Merge pull request #52 from alphagov/vcloud-curl-tidy
Browse files Browse the repository at this point in the history
vcloud-tool as ruby, shell version as 'example code'
  • Loading branch information
Dan Abel committed Dec 5, 2013
2 parents 8c78133 + 15c10ab commit f57cf13
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 52 deletions.
8 changes: 8 additions & 0 deletions lib/vcloud/fog_service_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ def put_vm id, name, options
@vcloud.process_task(task)
end

def vcloud_token
@vcloud.vcloud_token
end

def end_point
@vcloud.end_point
end

private
def extract_id(link)
link[:href].split('/').last
Expand Down
112 changes: 60 additions & 52 deletions utils/vcloud-curl
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
#!/bin/sh

usage() {
echo "vcloud-curl: simple curl wrapper to allow GET requests to vcloud API"
echo
echo "Usage:"
echo " vcloud-curl {request}"
echo
echo "where {request} is the resource required after the /api part of the url"
echo "eg: vcloud-curl 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

CURL_OPTS="-k -s"

SESSION_KEY=`curl -i $CURL_OPTS \
-H 'Accept:application/*+xml;version=1.5' \
-u "${VCLOUD_USER}@${VCLOUD_ORG}:${VCLOUD_PASS}" \
-X POST \
"https://${VCLOUD_HOST}/api/sessions" \
| grep '^x-vcloud-authorization:' \
| tr -d '\r' \
| awk '{print $2}'
`

if [ -z "${SESSION_KEY}" ]; then
echo "Failed to get vCloud session. Bailing"
exit 2
fi

curl $CURL_OPTS \
-H 'Accept:application/*+xml;version=1.5' \
-H "x-vcloud-authorization: ${SESSION_KEY}" \
"https://${VCLOUD_HOST}/api/$1"

# Log out -- fails with 401
#curl $CURL_OPTS \
# -H 'Accept:application/*+xml;version=1.5' \
# -H "x-vcloud-authorization: ${SESSION_KEY}" \
# -X DELETE \
# "https://${VCLOUD_HOST}/api/sessions"
#!/usr/bin/env ruby

require 'rubygems'
require 'bundler/setup'
require 'pp'
require 'vcloud'
require 'methadone'

ENV['FOG_MOCK'] && Fog.mock!

class App

include Methadone::Main
include Methadone::CLILogging

main do

fsi = Vcloud::FogServiceInterface.new()
token = fsi.vcloud_token
end_point = fsi.end_point

rel_url = ARGV.shift
url = end_point + rel_url

system("/usr/bin/curl #{options[:curl_base_opts]} \
-H 'Accept:#{options[:content_type]}' \
-H 'x-vcloud-authorization: #{token}' \
#{ARGV.join(' ')} #{url}")

end

options[:content_type] = "application/*+xml;version=5.1"
options[:curl_base_opts] = '-s'

on("--content_type", "=ATTRIBUTE",
"Override default content_type (#{options[:content_type]})" ) do |v|
options[:content_type] = v
end

on("--curl_base_opts", "=ATTRIBUTE",
"Override base curl options (#{options[:curl_base_opts]})" ) do |v|
options[:curl_base_opts] = v
end

on("--verbose", "Verbose output")
on("--debug", "Debugging output")

arg :relative_url, :optional

description '
vcloud-curl takes a relative URL for a query (to the base api url), then
shells out to cURL with the remainder of ARGV
See https://github.com/alphagov/vcloud-tools for more info'

version Vcloud::VERSION

go!
end


66 changes: 66 additions & 0 deletions utils/vcloud-curl-example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/sh

# This is an illustrative (functional) example of how to use just curl
# to talk to the vCloud API.
#
# The rest of the tools in vcloud-tools use Fog and hence have a unified
# credential/session handling system - and should as a result be used instead
# of this example.
#

usage() {
echo "vcloud-curl: simple curl wrapper to allow GET requests to vcloud API"
echo
echo "Usage:"
echo " vcloud-curl-example.sh {request}"
echo
echo "where {request} is the resource required after the /api end point"
echo "eg: vcloud-curl-example.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

CURL_OPTS=${CURL_OPTS-'--silent'}
MAIN_QUERY_CURL_OPTS=${MAIN_QUERY_CURL_OPTS-''}

api_version=${VCLOUD_API_VERSION-'5.1'}

MAIN_QUERY_CONTENT_TYPE=${MAIN_QUERY_CONTENT_TYPE-"application/*+xml;version=${api_version}"}

SESSION_KEY=`curl --include $CURL_OPTS \
-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}'
`

if [ -z "${SESSION_KEY}" ]; then
echo "Failed to get vCloud session. Bailing"
exit 2
fi

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"

0 comments on commit f57cf13

Please sign in to comment.