Browse files

Add ipc gem for talking with the CC

Change-Id: I8560b381dce3483b5f26b28df897e1331d768c3f
  • Loading branch information...
1 parent d8d7fb0 commit 42618750adfaa8b82c70be796914a9c293fb5826 mpage committed Oct 19, 2011
View
4 cloud_controller_ipc/.gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
View
4 cloud_controller_ipc/Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in cloud_controller_ipc.gemspec
+gemspec
View
7 cloud_controller_ipc/Rakefile
@@ -0,0 +1,7 @@
+require "bundler/gem_tasks"
+
+task :spec do
+ desc 'Run tests'
+ sh('bundle install')
+ sh('cd spec && rake spec')
+end
View
2 cloud_controller_ipc/lib/vcap/cloud_controller/ipc.rb
@@ -0,0 +1,2 @@
+require 'vcap/cloud_controller/ipc'
+require 'vcap/cloud_controller/ipc/service_consumer_v1_client'
View
48 cloud_controller_ipc/lib/vcap/cloud_controller/ipc/service_consumer_v1_client.rb
@@ -0,0 +1,48 @@
+require 'net/http'
+require 'yajl'
+
+module VCAP
+ module CloudController
+ module Ipc
+ end
+ end
+end
+
+class VCAP::CloudController::Ipc::ServiceConsumerV1Client
+ attr_reader :host
+ attr_reader :port
+
+ def initialize(host, port, opts={})
+ @host = host
+ @port = port
+ @headers = {'Content-Type' => 'application/json'}
+ @headers['X-VCAP-Staging-Task-ID'] = opts[:staging_task_id] if opts[:staging_task_id]
+ end
+
+ def provision_service(label, name, plan, plan_option=nil)
+ body_hash = {
+ :label => label,
+ :name => name,
+ :plan => plan,
+ :plan_option => plan_option,
+ }
+ perform_request(Net::HTTP::Post, "/services/v1/configurations", body_hash)
+ end
+
+ protected
+
+ def build_uri(path, query=nil)
+
+ end
+
+ def perform_request(net_http_class, path, body_hash=nil)
+ req = net_http_class.new(path, initheaders=@headers)
+ req.body = Yajl::Encoder.encode(body_hash) if body_hash
+ resp = Net::HTTP.new(@host, @port).start {|http| http.request(req) }
+ if resp.kind_of?(Net::HTTPSuccess)
+ {:result => Yajl::Parser.parse(resp.body)}
+ else
+ {:error => resp}
+ end
+ end
+end
View
7 cloud_controller_ipc/lib/vcap/cloud_controller/ipc/version.rb
@@ -0,0 +1,7 @@
+module VCAP
+ module CloudController
+ module Ipc
+ VERSION = '0.0.1'
+ end
+ end
+end
View
16 cloud_controller_ipc/spec/Rakefile
@@ -0,0 +1,16 @@
+require 'rake'
+
+BASE_DIR = File.expand_path(File.join('..', '..'), __FILE__)
+ENV["BUNDLE_GEMFILE"] ||= File.join(BASE_DIR, 'Gemfile')
+require 'rubygems'
+require 'bundler'
+Bundler.setup(:default, :test)
+
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec) do |t|
+ t.pattern = '**/*_spec.rb'
+ t.rspec_opts = ['--color', '--format nested']
+end
+
+task :default => [:spec]
View
54 cloud_controller_ipc/spec/service_consumer_v1_client_spec.rb
@@ -0,0 +1,54 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+describe VCAP::CloudController::Ipc::ServiceConsumerV1Client do
+ before :all do
+ @host = '127.0.0.1'
+ @port = 80
+ @client = VCAP::CloudController::Ipc::ServiceConsumerV1Client.new(@host, @port)
+ end
+
+ describe '#provision_service' do
+ before :all do
+ @uri = build_uri('/services/v1/configurations')
+ @serv_req = {
+ :label => 'fooservice-1.1',
+ :name => 'test',
+ :plan => 'free',
+ :plan_option => nil,
+ }
+ end
+
+ it 'should issue a post request to /services/v1/configurations with the correct request body' do
+ stub_request(:post, @uri).with(:body => @serv_req)
+ @client.provision_service(@serv_req[:label], @serv_req[:name], @serv_req[:plan])
+ end
+
+ it 'should pass along the staging_task_id as a query parameter if supplied' do
+ stub_request(:post, @uri).with(:query => {'staging_task_id' => '5'})
+ @client.provision_service(@serv_req[:label], @serv_req[:name], @serv_req[:plan], nil, 5)
+ end
+
+ it 'should decode the response body on success' do
+ result = {'foo' => 'bar'}
+ enc_result = Yajl::Encoder.encode(result)
+ stub_request(:post, @uri).with(:body => @serv_req).to_return(:status => 200, :body => enc_result)
+ resp = @client.provision_service(@serv_req[:label], @serv_req[:name], @serv_req[:plan])
+ resp[:result].should == result
+ end
+
+ it 'should return the response as an error on non-200 replies' do
+ stub_request(:post, @uri).with(:body => @serv_req).to_return(:status => 400)
+ resp = @client.provision_service(@serv_req[:label], @serv_req[:name], @serv_req[:plan])
+ resp[:error].should_not be_nil
+ resp[:error].code.should == '400'
+ end
+ end
+
+ def build_uri(path, query=nil)
+ URI::HTTP.build(
+ :host => @host,
+ :port => @port,
+ :path => path,
+ :query => query).to_s
+ end
+end
View
5 cloud_controller_ipc/spec/spec_helper.rb
@@ -0,0 +1,5 @@
+$LOAD_PATH << File.expand_path('../../lib', __FILE__)
+
+require 'webmock/rspec'
+
+require 'vcap/cloud_controller/ipc'
View
24 cloud_controller_ipc/vcap_cloud_controller_ipc.gemspec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "vcap/cloud_controller/ipc/version"
+
+Gem::Specification.new do |s|
+ s.name = "vcap_cloud_controller_ipc"
+ s.version = VCAP::CloudController::Ipc::VERSION
+ s.authors = ["mpage"]
+ s.email = ["mpage@vmware.com"]
+ s.homepage = "www.cloudfoundry.com"
+ s.summary = %q{Library for communicating with Cloud Controllers}
+ s.description = %q{Provides clients for ineracting with CCs}
+
+ s.files = %w(Rakefile Gemfile) + Dir.glob("{lib,spec}/**/*")
+ s.executables = []
+ s.bindir = 'bin'
+ s.require_paths = ["lib"]
+
+ s.add_dependency 'rake'
+ s.add_dependency 'yajl-ruby'
+
+ s.add_development_dependency 'rspec'
+ s.add_development_dependency 'webmock'
+end
View
1 stager/Gemfile
@@ -11,6 +11,7 @@ gem 'vcap_common'
gem 'vcap_logging', '>= 0.1.1'
gem 'vcap_staging', '>= 0.1.11'
gem 'vcap_stager_ipc'
+gem 'vcap_cloud_controller_ipc', :path => '../cloud_controller_ipc'
group :test do
gem 'rspec'
View
8 stager/Gemfile.lock
@@ -1,3 +1,10 @@
+PATH
+ remote: ../cloud_controller_ipc
+ specs:
+ vcap_cloud_controller_ipc (0.0.1)
+ rake
+ yajl-ruby
+
GEM
remote: http://rubygems.org/
specs:
@@ -73,6 +80,7 @@ DEPENDENCIES
rest-client (= 1.6.7)
rspec
sinatra
+ vcap_cloud_controller_ipc!
vcap_common
vcap_logging (>= 0.1.1)
vcap_stager_ipc
View
4 stager/lib/vcap/stager/plugin_action_proxy.rb
@@ -20,8 +20,8 @@ def initialize(start_script_path, stop_script_path, droplet)
# @param name String What to call the provisioned service
# @param plan String Which plan should be provisioned
# @param plan_option String Optional plan option to select.
- def create_service(label, name, plan, plan_option=nil)
- raise NotImplementedError
+ def provision_service(label, name, plan, plan_option=nil)
+ @services_client.provision_service(label, name, plan, plan_option, @staging_task_id)
end
# Binds a service to the application being staged

0 comments on commit 4261875

Please sign in to comment.