Skip to content
This repository has been archived by the owner on Dec 9, 2018. It is now read-only.

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
frodenas committed Dec 2, 2011
0 parents commit 168c641
Show file tree
Hide file tree
Showing 74 changed files with 7,002 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.bundle
.DS_Store
.idea
.rbenv-version
.yardoc
Gemfile.lock
doc/*
log/*
pkg/*
vendor/*


2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--colour
--format=nested
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
branches:
only:
- master

rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- jruby
- rbx
- rbx-19mode
- ree
9 changes: 9 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--no-private
--protected
--output-dir doc/yard
--tag authenticated:"Requires a user logged in"
--tag admin:"Requires an admin user logged in"
--markup markdown
-
CHANGELOG.md
LICENSE
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2011 Ferran Rodenas

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
122 changes: 122 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
CloudFoundry Ruby Client Gem
============================

This is a Ruby wrapper for the [CloudFoundry](http://cloudfoundry.org/) API, the industry’s first open Platform as a
Service (PaaS) offering.

Installation
------------

gem install cloudfoundry-client

Continuous Integration
----------------------

[![Build Status](https://secure.travis-ci.org/frodenas/cloudfoundry-client.png)](http://travis-ci.org/frodenas/cloudfoundry-client)

Documentation
-------------

[http://rdoc.info/gems/cloudfoundry-client](http://rdoc.info/gems/cloudfoundry-client)

Usage Examples
--------------

### Connection and login:

require "cloudfoundry-client"
cf_client = CloudFoundry::Client.new({:target_url => "https://api.cloudfoundry.com"})
cf_client.login("user@vcap.me", "password")

### Retrieve information from target cloud:

cloud_info = cf_client.cloud_info()
cloud_runtimes_info = cf_client.cloud_runtimes_info()
cloud_services_info = cf_client.cloud_services_info()

### Actions for applications:

apps = cf_client.list_apps()
app_info = cf_client.app_info("appname")
app_instances = cf_client.app_instances("appname")
app_stats = cf_client.app_stats("appname")
app_crashes = cf_client.app_crashes("appname")
app_files = cf_client.app_files("appname", "/")
app_files = cf_client.app_files("appname", "/logs/stdout.log")
created = cf_client.create_app("appname", manifest)
updated = cf_client.update_app("appname", manifest)
update_info = cf_client.update_app_info("appname")
deleted = cf_client.delete_app("appname")
uploaded = cf_client.upload_app("appname", zipfile, manifest)
zipfile = cf_client.download_app("appname")

### Actions for services:

services = cf_client.list_services()
service_info = cf_client.service_info("redis-12345")
created = cf_client.create_service("redis", "redis-12345")
deleted = cf_client.delete_service("redis-12345")
binded = cf_client.bind_service("redis-12345", "appname")
unbinded = cf_client.unbind_service("redis-12345", "appname")

### Actions for users (some of them require an admin user):

users = cf_client.list_users()
user_info = cf_client.user_info("user@vcap.me")
created = cf_client.create_user("user@vcap.me", "password")
updated = cf_client.update_user("user@vcap.me", "new_password")
deleted = cf_client.delete_user("user@vcap.me")

Contributing
------------
In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help
improve this project.

Here are some ways *you* can contribute:

* by using alpha, beta, and prerelease versions
* by reporting bugs
* by suggesting new features
* by writing or editing documentation
* by writing specifications
* by writing code (**no patch is too small**: fix typos, add comments, clean up inconsistent whitespace)
* by refactoring code
* by closing [issues](http://github.com/frodenas/cloudfoundry-client/issues)
* by reviewing patches


Submitting an Issue
-------------------
We use the [GitHub issue tracker](http://github.com/frodenas/cloudfoundry-client/issues) to track bugs and features.
Before submitting a bug report or feature request, check to make sure it hasn't already been submitted. You can indicate
support for an existing issuse by voting it up. When submitting a bug report, please include a
[Gist](http://gist.github.com/) that includes a stack trace and any details that may be necessary to reproduce the bug,
including your gem version, Ruby version, and operating system. Ideally, a bug report should include a pull request with
failing specs.


Submitting a Pull Request
-------------------------
1. Fork the project.
2. Create a topic branch.
3. Implement your feature or bug fix.
4. Add documentation for your feature or bug fix.
5. Run <tt>rake doc:yard</tt>. If your changes are not 100% documented, go back to step 4.
6. Add specs for your feature or bug fix.
7. Run <tt>rake spec</tt>. If your changes are not 100% covered, go back to step 6.
8. Commit and push your changes.
9. Submit a pull request. Please do not include changes to the gemspec, version, or history file. (If you want to create
your own version for some reason, please do so in a separate commit.)


Authors
-------

By [Ferran Rodenas](http://www.rodenas.org/) <frodenas@gmail.com>
Based on the [VMC - VMware Cloud CLI](https://github.com/cloudfoundry/vmc)

Copyright
---------

See [LICENSE](https://github.com/frodenas/cloudfoundry-client/blob/master/LICENSE) for details.
Copyright (c) 2011 [Ferran Rodenas](http://www.rodenas.org/).
28 changes: 28 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task :test => :spec
task :default => :spec

namespace :doc do
begin
require 'yard'
rescue LoadError
# ignore
else
YARD::Rake::YardocTask.new do |task|
task.files = ["lib/**/*.rb", "-", "CHANGELOG.md", "LICENSE"]
task.options = [
"--no-private",
"--protected",
"--output-dir", "doc/yard",
"--tag", "authenticated:Requires a user logged in",
"--tag", "admin:Requires an admin user logged in",
"--markup", "markdown",
]
end
end
end
29 changes: 29 additions & 0 deletions cloudfoundry.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "cloudfoundry/version"

Gem::Specification.new do |s|
s.name = "cloudfoundry-client"
s.version = CloudFoundry::Client::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Ferran Rodenas"]
s.email = ["frodenas@gmail.com"]
s.homepage = "http://github.com/frodenas/cloudfoundry-client"
s.summary = %q{A Ruby wrapper for the CloudFoundry API PaaS}
s.description = %q{This is a Ruby wrapper for the CloudFoundry API, the industry’s first open Platform as a Service (PaaS) offering.}
s.licenses = ["MIT"]

s.add_development_dependency("rspec", ">= 2.7.0")
s.add_development_dependency("webmock", ">= 1.7.8")
s.add_development_dependency("vcr", ">= 2.0.0.beta2")

s.add_runtime_dependency("faraday", ">= 0.7.5")
s.add_runtime_dependency("json_pure", ">= 1.6.2")

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- spec/*`.split("\n")
s.require_paths = ["lib"]

s.rdoc_options = ["--charset=UTF-8"]
s.extra_rdoc_files = ["CHANGELOG.md", "LICENSE", "README.md"]
end
34 changes: 34 additions & 0 deletions lib/cloudfoundry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'faraday'
require 'json/pure'

# This is a Ruby wrapper for the CloudFoundry API, the industry’s first open Platform as a Service (PaaS) offering.
module CloudFoundry
require 'cloudfoundry/client'
require 'cloudfoundry/constants'
require 'cloudfoundry/exception'
require 'cloudfoundry/version'

# @private
VERSION = CloudFoundry::Client::VERSION #:nodoc;

class << self
# Alias for CloudFoundry::Client.new
#
# @return [CloudFoundry::Client] A CloudFoundry::Client Object.
# @see CloudFoundry::Client#initialize CloudFoundry::Client.new()
def new(options = {})
CloudFoundry::Client.new(options)
end

# Delegate all methods to CloudFoundry::Client.
def method_missing(method, *arguments, &block)
return super unless new.respond_to?(method)
new.send(method, *arguments, &block)
end

# Delegate all methods to CloudFoundry::Client.
def respond_to?(method, include_private = false)
new.respond_to?(method, include_private) || super(method, include_private)
end
end
end
93 changes: 93 additions & 0 deletions lib/cloudfoundry/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require 'cloudfoundry/client/apps'
require 'cloudfoundry/client/info'
require 'cloudfoundry/client/request'
require 'cloudfoundry/client/resources'
require 'cloudfoundry/client/response'
require 'cloudfoundry/client/services'
require 'cloudfoundry/client/users'

module CloudFoundry
# This is a Ruby wrapper for the CloudFoundry API, the industry’s first open Platform as a Service (PaaS) offering.
class Client
include CloudFoundry::Client::Apps
include CloudFoundry::Client::Info
include CloudFoundry::Client::Request
include CloudFoundry::Client::Resources
include CloudFoundry::Client::Services
include CloudFoundry::Client::Users

# Returns the HTTP connection adapter that will be used to connect.
attr_reader :net_adapter
# Returns the Proxy URL that will be used to connect.
attr_reader :proxy_url
# Returns the CloudFoundry API Target URL.
attr_reader :target_url
# Returns the CloudFoundry API Trace Key.
attr_reader :trace_key
# Returns the CloudFoundry API Authorization Token.
attr_reader :auth_token
# Returns the CloudFoundry Logged User.
attr_reader :user
# Returns the CloudFoundry Proxied User.
attr_reader :proxy_user

# Creates a new CloudFoundry::Client object.
#
# @param [Hash] options
# @option options [Faraday::Adapter] :adapter The HTTP connection adapter that will be used to connect.
# @option options [String] :proxy_url The Proxy URL that will be used to connect.
# @option options [String] :target_url The CloudFoundry API Target URL.
# @option options [String] :trace_key The CloudFoundry API Trace Key.
# @option options [String] :auth_token The CloudFoundry API Authorization Token.
# @return [CloudFoundry::Client] A CloudFoundry::Client Object.
# @raise [CloudFoundry::Client::Exception::BadParams] when target_url is not a valid CloudFoundry API URL.
# @raise [CloudFoundry::Client::Exception::AuthError] when auth_token is not a valid CloudFoundry API authorization token.
def initialize(options = {})
@net_adapter = options[:adapter] || DEFAULT_ADAPTER
@proxy_url = options[:proxy_url] || nil
@target_url = options[:target_url] || DEFAULT_TARGET
@target_url = sanitize_url(@target_url)
@trace_key = options[:trace_key] || nil
@auth_token = options[:auth_token] || nil
@user = nil
@proxy_user = nil

raise CloudFoundry::Client::Exception::BadParams, "Invalid CloudFoundry API URL: " + @target_url unless valid_target_url?
if @auth_token
raise CloudFoundry::Client::Exception::AuthError, "Invalid CloudFoundry API authorization token" unless logged_in?
end
end

private

# Sanitizes an URL.
#
# @param [String] url URL to sanitize.
# @return [String] URL sanitized.
def sanitize_url(url)
url = url =~ /^(http|https).*/i ? url : "http://#{url}"
url = url.gsub(/\/+$/, "")
end

# Checks if the target_url is a valid CloudFoundry target.
#
# @return [Boolean] Returns true if target_url is a valid CloudFoundry API URL, false otherwise.
def valid_target_url?
return false unless cloud_info = cloud_info()
return false unless cloud_info[:name]
return false unless cloud_info[:build]
return false unless cloud_info[:support]
return false unless cloud_info[:version]
true
rescue
false
end

# Requires a logged in user.
#
# @raise [CloudFoundry::Client::Exception::AuthError] if user is not logged in.
def require_login
raise CloudFoundry::Client::Exception::AuthError unless @user || logged_in?
end
end
end
Loading

0 comments on commit 168c641

Please sign in to comment.