Skip to content

Instantly share code, notes, and snippets.

@fairchild
Created September 11, 2012 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fairchild/3696455 to your computer and use it in GitHub Desktop.
Save fairchild/3696455 to your computer and use it in GitHub Desktop.
sample openstack api interaction
require 'rubygems'
require 'faraday'
require 'json'
require 'pry'
require 'openssl'
require 'hashie'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
class Nova < Hash
include Hashie::Extensions::IndifferentAccess
def initialize(opts={})
@nova_version = opts[:nova_version] || ENV['NOVA_VERSION']
@nova_url = opts[:nova_url] || ENV['NOVA_URL']
@nova_username = opts[:nova_username] || ENV['NOVA_USERNAME']
@endpoint = opts[:nova_url] || ENV['NOVA_URL']
@nova_api_keu = opts[:nova_api_key] || ENV['NOVA_API_KEY']
end
def conn
Faraday.new(:url => @nova_url) do |faraday|
faraday.response :logger # log requests to STDOUT
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
end
def get(path, options={})
conn.get path, options
end
def post(path, options={})
conn.post path, options
end
def put(path, options={})
conn.put path, options
end
def delete(path, options={})
conn.delete path, options
end
end
@nova = Nova.new
@json={"auth"=>{
"passwordCredentials"=>{
"username"=>ENV['NOVA_USERNAME'],
"password"=>ENV['NOVA_API_KEY']
},
"tenantName"=>ENV['NOVA_PROJECT_ID']
}
}
@result = @nova.conn.post do |req|
req.url '/tokens'
req.headers['Content-Type'] = 'application/json'
req.body = @json.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment