Permalink
Browse files

Adding the servicegroups and altering how the JSON output gets displa…

…yed. Broke the other view. Will need to come back and fix.
  • Loading branch information...
1 parent 9a9c847 commit f384956dbe7d97027adc8fc286f01086813fcfe6 Gabe McArthur committed Mar 18, 2011
View
@@ -4,6 +4,7 @@ gem 'log4r', '~> 1.1'
gem 'savon', '~> 0.7.0'
gem 'highline', '>= 1.6'
gem 'choosy', '~> 0.2'
+gem 'json_pure', '~> 1.5.1', :require => 'json'
group :development do
gem 'rspec', :require => "spec"
View
@@ -7,10 +7,12 @@ GEM
autotest-notification (2.3.1)
autotest (~> 4.3)
builder (3.0.0)
- choosy (0.2.3)
+ choosy (0.2.5)
crack (0.1.8)
diff-lcs (1.1.2)
highline (1.6.1)
+ hirb (0.4.0)
+ json_pure (1.5.1)
log4r (1.1.9)
rspec (2.5.0)
rspec-core (~> 2.5.0)
@@ -32,6 +34,8 @@ DEPENDENCIES
autotest-notification
choosy (~> 0.2)
highline (>= 1.6)
+ hirb (~> 0.4.0)
+ json_pure (~> 1.5.1)
log4r (~> 1.1)
rspec
savon (~> 0.7.0)
View
@@ -1 +1 @@
-0.4.4
+0.5.0
@@ -30,7 +30,10 @@ def send_request(name, params, &block)
soap.body = body
end
- log.debug(result)
+ if log.debug?
+ require 'pp'
+ PP::pp(result.to_hash, $stderr, 80)
+ end
response = result.to_hash["#{name.to_s}_response".to_sym]
msg = response[:return][:message]
View
@@ -5,6 +5,7 @@
require 'netscaler/server/request'
require 'netscaler/vserver/request'
require 'netscaler/service/request'
+require 'netscaler/servicegroup/request'
require 'choosy'
module Netscaler
@@ -35,7 +36,7 @@ def execute!
protected
def command
- cmds = [servers, vservers, services]
+ cmds = [vservers, servers, services, servicegroups]
@command ||= Choosy::SuperCommand.new :netscaler do
printer :standard, :color => true, :headers => [:bold, :blue], :max_width => 80
@@ -47,7 +48,7 @@ def command
alias: is_optional
usernamd: is_required
password: is_optional_but_querried_if_not_found"
-
+
# COMMANDS
header 'Commands:'
cmds.each do |cmd|
@@ -98,7 +99,7 @@ def servers
end
arguments do
count 0..1 #:at_least => 0, :at_most => 1
- metaname 'SERVER'
+ metaname '[SERVER]'
validate do |args, options|
if args.length == 0
die "no server given to act upon" unless options[:action] == :list
@@ -147,7 +148,7 @@ def vservers
end
arguments do
count 0..1 #:at_least => 0, :at_most => 1
- metaname 'SERVER'
+ metaname '[SERVER]'
validate do |args, options|
if args.length == 0
die "no virtual server given to act upon" unless options[:action] == :list
@@ -181,7 +182,7 @@ def services
end
arguments do
count 0..1 #:at_least => 0, :at_most => 1
- metaname 'SERVICE'
+ metaname '[SERVICE]'
validate do |args, options|
if args.length == 0
die "no services given to act on" unless options[:action] == :list
@@ -191,6 +192,40 @@ def services
end
end
+ def servicegroups
+ Choosy::Command.new :servicegroup do
+ executor Netscaler::Executor.new(Netscaler::ServiceGroup::Request)
+ summary "Enables, disables, binds or unbinds from a virtual server, a given service group."
+ header 'Description:'
+ para "This is a tool for enabling and disabling service groups in a Netscaler load balancer. The name of the service group is required, as is the address of the Netscaler load balancer."
+
+ header 'Options:'
+ enum :action, [:enable, :disable, :bind, :unbind, :status], "Either [enable, disable, bind, unbind, status] of a service group. 'bind' and 'unbind' require the '--vserver' flag. Default is 'status'." do
+ default :status
+ end
+ string :vserver, "The virtual server to bind/unbind this service to/from." do
+ depends_on :action
+ default :unset
+ validate do |arg, options|
+ if [:bind, :unbind].include?(options[:action])
+ die "requires the -v/--vserver flag" if arg == :unset
+ else
+ die "only used with bind/unbind" unless arg == :unset
+ end
+ end
+ end
+ arguments do
+ count 0..1 #:at_least => 0, :at_most => 1
+ metaname '[SERVICEGROUP]'
+ validate do |args, options|
+ if args.length == 0
+ die "no service group given to act on" unless options[:action] == :list
+ end
+ end
+ end
+ end
+ end
+
private
def print_error(e)
STDERR.puts "#{File.basename($0)}: #{e}"
View
@@ -1,7 +1,7 @@
require 'netscaler/errors'
require 'netscaler/logging'
require 'netscaler/transaction'
-require 'netscaler/extensions'
+require 'json'
module Netscaler
class Executor
@@ -16,16 +16,10 @@ def execute!(args, options)
Netscaler::Transaction.new options[:netscaler] do |client|
@request_class.new(client).send(options[:action], args[0], options) do |response|
if options[:json]
- puts response.to_json
+ puts JSON.pretty_generate(response)
else
- if response.is_a?(Array)
- if response.length > 0
- puts response[0].header
- end
- else
- puts response.header
- end
- puts response.to_s
+ STDERR.puts "Tempoararily disabled stdout. Use --json instead. Sorry...."
+ # puts Hirb::Helpers::Tree.render(response)
end
end
end
@@ -1,47 +0,0 @@
-
-# Alter the standard Array class
-class Array
- def to_s
- base = ""
- each do |e|
- base << e.to_s
- base << "\n"
- end
- base
- end
-
- def to_json(prefix=nil)
- value = if prefix
- prefix.dup
- else
- ""
- end
- if empty?
- return value << "[]"
- end
-
- indent = if prefix
- " " << prefix
- else
- " "
- end
-
- value << "[\n"
-
- each_with_index do |e, i|
- value << indent
- value << e.to_json(indent)
- value << ",\n" unless i == length - 1
- end
-
- value << "\n"
- value << prefix if prefix
- value << "]\n"
- end
-end
-
-class String
- def to_json(prefix=nil)
- "\"#{self}\""
- end
-end
@@ -15,15 +15,15 @@ def list(server, options)
responses = []
send_request('getserver', {:empty => :ok}) do |response|
response_part(response).each_with_index do |server, i|
- responses << Response.new(server)
+ responses << Response.new(server).to_hash
end
end
yield responses if block_given?
end
def status(server, options)
send_request('getserver', {:name => server }) do |response|
- yield Response.new(response_part(response)) if block_given?
+ yield Response.new(response_part(response)).to_hash if block_given?
end
end
@@ -1,67 +1,46 @@
module Netscaler::Server
class Response
- FORMAT = "%-30s %15s %10s"
-
- attr_reader :info
-
def initialize(raw_response)
@info = raw_response
end
def name
- info[:name]
+ @info[:name]
end
def ip_address
- info[:ipaddress]
+ @info[:ipaddress]
end
def state
- info[:state]
- end
-
- def header
- line = sprintf FORMAT, 'Name', 'IP Address', 'State'
- eqls = '=' * line.length
- line + "\n" + eqls
+ @info[:state]
end
def services
- if info[:servicename]
- res = info[:servicename][:item]
- if !res.is_a?(Array)
- [res]
- else
- res
- end
- else
- []
- end
- end
+ @services ||= if @info[:servicename]
+ res = @info[:servicename][:item]
+ if res.is_a?(Array)
+ res
+ else
+ [res]
+ end
+ else
+ []
+ end
+ end
+
+ def to_hash
+ hash = {
+ :name => name,
+ :ip_address => ip_address,
+ :state => state,
+ }
- def to_s
- base = sprintf FORMAT, name, ip_address, state
if !services.empty?
- base << "\n"
- services.each do |s|
- base << "| service: #{s}\n"
- end
+ hash[:services] = services
end
- base
- end
- def to_json(prefix=nil)
- indent = if prefix
- prefix + " "
- else
- " "
- end
- base = "{\n#{indent}'name': '#{name}',\n#{indent}'ip_address': '#{ip_address}',\n#{indent}'state': '#{state}'"
- if !services.empty?
- base << ",\n#{indent}'services':\n#{services.to_json(indent)}"
- end
- base.chomp!
- base << "\n#{prefix}}"
+ hash
end
end
end
@@ -35,7 +35,7 @@ def unbind(service, options)
def status(service, options)
params = { :name => service }
send_request('getservice', params) do |response|
- yield Response.new(response) if block_given?
+ yield Response.new(response).to_hash if block_given?
end
end
end
@@ -1,7 +1,5 @@
module Netscaler::Service
class Response
- FORMAT = "%-30s %15s %10s %10s"
-
def initialize(raw_response)
@info = raw_response[:return][:list][:item]
end
@@ -19,26 +17,20 @@ def state
end
def port
- @info[:port]
+ @info[:port].to_i
end
- def header
- line = sprintf FORMAT, 'Name', 'IP Address', 'State', 'Port'
- eqls = '=' * line.length
- line + "\n" + eqls
- end
-
- def to_s
- sprintf FORMAT, name, ip_address, state, port
+ def server
+ @info[:servername]
end
- def to_json(prefix=nil)
- indent = if prefix
- ' ' + prefix
- else
- ' '
- end
- "{\n#{indent}'name': '#{name}',\n#{indent}'ip_address': '#{ip_address}',\n#{indent}'state': '#{state}',\n#{indent}'port': #{port}\n#{prefix}}"
+ def to_hash
+ { :name => name,
+ :server => server,
+ :ip_address => ip_address,
+ :state => state,
+ :port => port
+ }
end
end
end
Oops, something went wrong.

0 comments on commit f384956

Please sign in to comment.