This repository has been archived by the owner on Nov 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from alphagov/vcloud-curl-tidy
vcloud-tool as ruby, shell version as 'example code'
- Loading branch information
Showing
3 changed files
with
134 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|