This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
service_broker_cli
executable file
·98 lines (82 loc) · 2.63 KB
/
service_broker_cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env ruby
# Copyright (c) 2009-2011 VMware, Inc.
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)
require "rubygems"
require "bundler/setup"
require "yajl"
require "yaml"
require "uri"
require "optparse"
require "net/http"
DESCRIPTION ="
This tool reads service defination from default configuration
file ../config/services.yml or using the path indicates by -c
The default action is create a service. If -d is given, tool
deletes the given service rather than creates it.
"
# default configuration file
config_file = File.join(File.dirname(__FILE__), "..", "config", "services.yml")
# default action
action = "create"
OptionParser.new do |opts|
opts.banner = "Usage: service_broker_cli [options]"
opts.on("-c", "--config CONFIG" ,"Configuration file") {|opt| config_file = opt}
opts.on("-d", "--delete" ,"Delete the brokered service") {|opt| action = "delete"}
opts.on("-v", "--verbose" ,"Verbose output") {@verbose = true }
opts.on("-h", "--help" ,"Help") { puts opts; puts DESCRIPTION;exit }
end.parse!
def symbolize_keys(hash)
if hash.is_a? Hash
new_hash = {}
hash.each {|k, v| new_hash[k.to_sym] = symbolize_keys(v) }
new_hash
else
hash
end
end
begin
@config = YAML.load_file(config_file)
@config = symbolize_keys(@config)
rescue => e
puts "Can't read config file: #{e}"
end
REQ_OPTS = %w(service_broker token service).map{|e| e.to_sym}
missing_opts = REQ_OPTS.select {|o| !@config.has_key? o}
unless missing_opts.empty?
puts "Missing required options: #{missing_opts.join(", ")}"
exit 1
end
@token = @config[:token]
@broker = @config[:service_broker]
@service = @config[:service]
API_VERSION = "v1"
uri = URI.parse(@broker)
hdrs = {
'Content-Type' => 'application/json',
'X-VCAP-Service-Token' => "#{@token}",
}
label = "#{@service[:name]}-#{@service[:version]}"
host = uri.host
port = uri.port
klass = url = body = nil
case action
when "create"
klass = Net::HTTP::Post
url = "/service-broker/#{API_VERSION}/offerings"
@service[:label] = label
%w(name version).each{|k| @service.delete(k.to_sym)}
body = Yajl::Encoder.encode(@service)
when "delete"
klass = Net::HTTP::Delete
url = "/service-broker/#{API_VERSION}/offerings/#{label}"
body = nil
end
puts "Going to #{action} service to http://#{host}:#{port}#{url}.\nRequest Body:#{body}" if@verbose
req = klass.new(url, initheader=hdrs)
req.body = body if body
resp = Net::HTTP.new(host, port).start {|http| http.request(req)}
if resp.is_a? Net::HTTPOK
puts "Successful to #{action} brokered service #{label}"
else
puts "Failed to #{action} service #{label}: status=#{resp.code}, response body=#{resp.message}"
end