netscaler-cli 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -15,26 +15,16 @@ module Netscaler
15
15
  end
16
16
 
17
17
  def execute!
18
- parse!(@args.dup)
19
- Netscaler::Logging.configure(options[:debug])
20
-
21
- Netscaler::Transaction.new(netscaler_configuration) do |client|
22
- action = options[:action].keys[0]
23
- args = options[:action][action]
24
- executor = create_executor(client)
25
-
26
- if args.nil?
27
- executor.send(action)
28
- else
29
- executor.send(action, args)
30
- end
31
- end
32
- end
33
-
34
- def parse!(args)
35
18
  begin
36
- parse_options(args)
37
- validate_args(args)
19
+ parse!(@args.dup)
20
+ Netscaler::Logging.configure(options[:debug])
21
+
22
+ Netscaler::Transaction.new(netscaler_configuration) do |client|
23
+ action = options[:action][0]
24
+ executor = create_executor(client)
25
+
26
+ executor.send(action, options)
27
+ end
38
28
  rescue SystemExit => e
39
29
  raise
40
30
  rescue Netscaler::ConfigurationError => e
@@ -47,10 +37,15 @@ module Netscaler
47
37
  end
48
38
  end
49
39
 
40
+ def parse!(args)
41
+ parse_options(args)
42
+ validate_args(args)
43
+ end
44
+
50
45
  def parse_options(args)
51
46
  @options ||= {}
52
47
  if @options.empty?
53
- @options[:action] = Hash.new
48
+ @options[:action] = Array.new
54
49
  end
55
50
  @parsed_options ||= OptionParser.new do |opts|
56
51
  interface_header(opts)
@@ -62,14 +57,16 @@ module Netscaler
62
57
 
63
58
  def interface_configuration(opts)
64
59
  opts.separator " Configuration: "
65
- opts.on('-n', '--netscaler NETSCALER_ADDRESS',
66
- "The IP or hostname of the Netscaler load balancer.",
60
+ opts.on('-n', '--netscaler NETSCALER',
61
+ "The IP or hostname of the Netscaler",
62
+ "load balancer.",
67
63
  "This argument is required.") do |n|
68
64
  options[:netscaler] = n
69
65
  end
70
66
  opts.on('-c', '--config CONFIG',
71
- "The path to the netscaler-cli configuration file.",
72
- "By default, it is ~/.netscaler-cli.yml") do |c|
67
+ "The path to the netscaler-cli configuration",
68
+ "file. By default, it is the ",
69
+ "~/.netscaler-cli.yml") do |c|
73
70
  options[:config] = c
74
71
  end
75
72
  opts.separator ""
@@ -105,8 +102,8 @@ module Netscaler
105
102
  @host = args[0]
106
103
 
107
104
  if options[:action].empty?
108
- options[:action][:status] = nil
109
- elsif options[:action].keys.length != 1
105
+ options[:action] << :status
106
+ elsif options[:action].length != 1
110
107
  raise Netscaler::ConfigurationError.new("Multiple actions specified -- only one action is supported at a time.")
111
108
  end
112
109
 
@@ -30,11 +30,11 @@ DESC
30
30
  opts.separator " Actions: "
31
31
  opts.on('-e', '--enable',
32
32
  "Enables the given server.") do |e|
33
- options[:action][:enable] = nil
33
+ options[:action] << :enable
34
34
  end
35
35
  opts.on('-d', '--disable',
36
36
  "Disables the given server.") do |d|
37
- options[:action][:disable] = nil
37
+ options[:action] << :disable
38
38
  end
39
39
  opts.separator ""
40
40
  end
@@ -7,15 +7,15 @@ module Netscaler::Server
7
7
  @params = { :name => host }
8
8
  end
9
9
 
10
- def enable
10
+ def enable(options)
11
11
  send_request('enableserver', @params)
12
12
  end
13
13
 
14
- def disable
14
+ def disable(options)
15
15
  send_request('disableserver', @params)
16
16
  end
17
17
 
18
- def status
18
+ def status(options)
19
19
  send_request('getserver', @params) do |response|
20
20
  info = response[:return][:list][:item]
21
21
  puts "Name: #{info[:name]}"
@@ -27,19 +27,21 @@ DESC
27
27
  opts.separator " Actions: "
28
28
  opts.on('-e', '--enable',
29
29
  "Enables the given service.") do |e|
30
- options[:action][:enable] = nil
30
+ options[:action] << :enable
31
31
  end
32
32
  opts.on('-d', '--disable',
33
33
  "Disables the given service.") do |d|
34
- options[:action][:disable] = nil
34
+ options[:action] << :disable
35
35
  end
36
36
  opts.on('-b', '--bind VSERVER',
37
37
  "Binds a service to a virtual server.") do |b|
38
- options[:action][:bind] = b
38
+ options[:action] << :bind
39
+ options[:vserver] = b
39
40
  end
40
41
  opts.on('-u', '--unbind VSERVER',
41
42
  "Unbinds a serivce to a virtual server.") do |u|
42
- options[:action][:unbind] = u
43
+ options[:action] << :unbind
44
+ options[:vserver] = u
43
45
  end
44
46
  opts.separator ""
45
47
  end
@@ -7,11 +7,11 @@ module Netscaler::Service
7
7
  @params = { :name => host }
8
8
  end
9
9
 
10
- def enable
10
+ def enable(options)
11
11
  send_request('enableservice', @params)
12
12
  end
13
13
 
14
- def disable
14
+ def disable(options)
15
15
  params = {
16
16
  :name => host,
17
17
  :delay => 0
@@ -19,7 +19,7 @@ module Netscaler::Service
19
19
  send_request('disableservice', params)
20
20
  end
21
21
 
22
- def status
22
+ def status(options)
23
23
  send_request('getservice', @params) do |response|
24
24
  info = response[:return][:list][:item]
25
25
  puts "Name: #{info[:name]}"
@@ -29,9 +29,9 @@ module Netscaler::Service
29
29
  end
30
30
  end
31
31
 
32
- def bind(vserver)
32
+ def bind(options)
33
33
  params = {
34
- :name => vserver,
34
+ :name => options[:vserver],
35
35
  :servicename => host
36
36
  }
37
37
  send_request('bindlbvserver_service', params) do |response|
@@ -40,9 +40,9 @@ module Netscaler::Service
40
40
  end
41
41
  end
42
42
 
43
- def unbind(vserver)
43
+ def unbind(options)
44
44
  params = {
45
- :name => vserver,
45
+ :name => options[:vserver],
46
46
  :servicename => host
47
47
  }
48
48
  send_request('unbindlbvserver_service', params) do |response|
@@ -42,6 +42,8 @@ module Netscaler
42
42
 
43
43
  log.debug("Yielding client control to the calling context")
44
44
  yield client
45
+ rescue SystemExit => e
46
+ raise
45
47
  rescue Exception => e
46
48
  log.fatal(e)
47
49
  log.fatal("Unable to execute transaction.")
@@ -30,21 +30,46 @@ DESC
30
30
  opts.separator " Actions: "
31
31
  opts.on('-e', '--enable',
32
32
  "Enables the given virtual server.") do |e|
33
- options[:action][:enable] = nil
33
+ options[:action] << :enable
34
34
  end
35
35
  opts.on('-d', '--disable',
36
36
  "Disables the given virtual server.") do |d|
37
- options[:action][:disable] = nil
37
+ options[:action] << :disable
38
38
  end
39
39
  opts.on('-b', '--bind POLICY_NAME',
40
- "Binds a policy of a given name to a virtual server.") do |b|
41
- options[:action][:bind] = b
40
+ "Binds a policy of a given name to a",
41
+ "virtual server.") do |b|
42
+ options[:action] << :bind
43
+ options[:policy_name] = b
44
+ end
45
+ opts.on('-p', '--priority NUMBER', Integer,
46
+ "The priority to bind the policy with.",
47
+ "Used only with the --bind flag.") do |p|
48
+ options[:priority] = p
42
49
  end
43
50
  opts.on('-u', '--unbind POLICY_NAME',
44
- "Unbinds a policy of a given name to a virtual server.") do |u|
45
- options[:action][:unbind] = u
51
+ "Unbinds a policy of a given name to a",
52
+ "virtual server.") do |u|
53
+ options[:action] << :unbind
54
+ options[:policy_name] = u
46
55
  end
47
56
  opts.separator ""
48
57
  end
58
+
59
+ def validate_args(args)
60
+ super(args)
61
+
62
+ if options[:action][0] == :bind
63
+ if options[:priority].nil?
64
+ options[:priority] = 1
65
+ elsif options[:priority] <= 0
66
+ raise Netscaler::ConfigurationError.new("The --priority must be greater than 0")
67
+ end
68
+ else
69
+ if options[:priority]
70
+ raise Netscaler::ConfigurationError.new("The --priority flag can only specified with the --bind option")
71
+ end
72
+ end
73
+ end
49
74
  end # CLI
50
75
  end
@@ -1,55 +1,57 @@
1
+ require 'netscaler/logging'
1
2
  require 'netscaler/baseexecutor'
2
3
 
3
4
  module Netscaler::VServer
4
5
  class Executor < Netscaler::BaseExecutor
6
+ include Netscaler::Logging
7
+
5
8
  def initialize(host, client)
6
9
  super(host, client)
7
10
  @params = { :name => host }
8
11
  end
9
12
 
10
- def enable
13
+ def enable(options)
11
14
  send_request('enablelbvserver', @params)
12
15
  end
13
16
 
14
- def disable
17
+ def disable(options)
15
18
  send_request('disablelbvserver', @params)
16
19
  end
17
20
 
18
- def status
21
+ def status(options)
19
22
  send_request('getlbvserver', @params) do |response|
20
- info = response[:return][:list][:item]
21
- puts "Name: #{info[:name]}"
22
- puts "IP Address: #{info[:svcipaddress][:item]}"
23
- puts "Port: #{info[:svcport][:item]}"
24
- puts "State: #{info[:svcstate][:item]}"
23
+ begin
24
+ info = response[:return][:list][:item]
25
+ puts "Name: #{info[:name]}"
26
+ puts "IP Address: #{info[:svcipaddress][:item]}"
27
+ puts "Port: #{info[:svcport][:item]}"
28
+ puts "State: #{info[:svcstate][:item]}"
29
+ rescue Exception => e
30
+ log.fatal "Unable to lookup any information for host: #{host}"
31
+ exit(1)
32
+ end
25
33
  end
26
34
  end
27
35
 
28
- def bind(policy_name)
36
+ def bind(options)
29
37
  params = {
30
38
  :name => host,
31
- :policyname => policy_name,
32
- :priority => 1,
39
+ :policyname => options[:policy_name],
40
+ :priority => options[:priority],
33
41
  :gotopriorityexpression => 'END'
34
42
  }
35
43
 
36
- send_request('bindlbvserver_policy', params) do |response|
37
- #require 'pp'
38
- #pp response
39
- end
44
+ send_request('bindlbvserver_policy', params)
40
45
  end
41
46
 
42
- def unbind(policy_name)
47
+ def unbind(options)
43
48
  params = {
44
49
  :name => host,
45
- :policyname => policy_name,
50
+ :policyname => options[:policy_name],
46
51
  :type => 'REQUEST'
47
52
  }
48
53
 
49
- send_request('unbindlbvserver_policy', params) do |response|
50
- #require 'pp'
51
- #pp response
52
- end
54
+ send_request('unbindlbvserver_policy', params)
53
55
  end
54
56
  end
55
57
  end
data/spec/helpers.rb CHANGED
@@ -3,3 +3,7 @@
3
3
  def attempting(&block)
4
4
  lambda &block
5
5
  end
6
+
7
+ def attempting_to(&block)
8
+ lambda &block
9
+ end
@@ -0,0 +1,33 @@
1
+ require 'helpers'
2
+ require 'netscaler/server/cli'
3
+
4
+ module Netscaler::Server
5
+
6
+ module CLIHelper
7
+ def parse(*args)
8
+ cli = CLI.new(args)
9
+ cli.parse_options(args)
10
+ cli.options
11
+ end
12
+
13
+ def validate(*args)
14
+ cli = CLI.new(args)
15
+ cli.parse_options(args)
16
+ cli.validate_args(args)
17
+ end
18
+ end
19
+
20
+ describe "The CLI interface" do
21
+ include CLIHelper
22
+
23
+ describe "while parsing" do
24
+ it "should set the --enable flag correctly." do
25
+ parse('--enable')[:action][0].should eql(:enable)
26
+ end
27
+
28
+ it "should set the --disable flag correctly." do
29
+ parse('--disable')[:action][0].should eql(:disable)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ require 'helpers'
2
+ require 'netscaler/service/cli'
3
+
4
+ module Netscaler::Service
5
+
6
+ module CLIHelper
7
+ def parse(*args)
8
+ cli = CLI.new(args)
9
+ cli.parse_options(args)
10
+ cli.options
11
+ end
12
+
13
+ def validate(*args)
14
+ cli = CLI.new(args)
15
+ cli.parse_options(args)
16
+ cli.validate_args(args)
17
+ end
18
+ end
19
+
20
+ describe "The CLI interface" do
21
+ include CLIHelper
22
+
23
+ describe "while parsing" do
24
+ it "should set the --enable flag correctly." do
25
+ parse('--enable')[:action][0].should eql(:enable)
26
+ end
27
+
28
+ it "should set the --disable flag correctly." do
29
+ parse('--disable')[:action][0].should eql(:disable)
30
+ end
31
+
32
+ it "should set the --bind flag correctly." do
33
+ opts = parse('--bind', 'some-vserver')
34
+ opts[:action][0].should eql(:bind)
35
+ opts[:vserver].should eql('some-vserver')
36
+ end
37
+
38
+ it "should set the --unbind flag correctly." do
39
+ opts = parse('--unbind', 'some-vserver')
40
+ opts[:action][0].should eql(:unbind)
41
+ opts[:vserver].should eql('some-vserver')
42
+ end
43
+ end
44
+ end
45
+ end
@@ -9,37 +9,67 @@ module Netscaler::VServer
9
9
  cli.parse_options(args)
10
10
  cli.options
11
11
  end
12
+
13
+ def validate(*args)
14
+ cli = CLI.new(args)
15
+ cli.parse_options(args)
16
+ cli.validate_args(args)
17
+ end
12
18
  end
13
19
 
14
20
  describe "The CLI interface" do
15
21
  include CLIHelper
16
22
 
17
- it "should set the --netscaler flag correctly." do
18
- parse('--netscaler', 'localhost')[:netscaler].should eql('localhost')
19
- end
23
+ describe "while parsing" do
24
+ it "should set the --netscaler flag correctly." do
25
+ parse('--netscaler', 'localhost')[:netscaler].should eql('localhost')
26
+ end
20
27
 
21
- it "should set the --debug flag correctly." do
22
- parse('--debug')[:debug].should be(true)
23
- end
28
+ it "should set the --debug flag correctly." do
29
+ parse('--debug')[:debug].should be(true)
30
+ end
24
31
 
25
- it "should set the --enable flag correctly." do
26
- parse('--enable')[:action].has_key?(:enable).should be(true)
27
- end
32
+ it "should set the --enable flag correctly." do
33
+ parse('--enable')[:action][0].should eql(:enable)
34
+ end
28
35
 
29
- it "should set the --disable flag correctly." do
30
- parse('--disable')[:action].has_key?(:disable).should be(true)
31
- end
36
+ it "should set the --disable flag correctly." do
37
+ parse('--disable')[:action][0].should eql(:disable)
38
+ end
32
39
 
33
- it "should set the --bind flag correctly." do
34
- parse('--bind', 'some-policy')[:action][:bind].should eql('some-policy')
35
- end
40
+ it "should set the --bind flag correctly." do
41
+ opts = parse('--bind', 'some-policy')
42
+ opts[:action][0].should eql(:bind)
43
+ opts[:policy_name].should eql('some-policy')
44
+ end
45
+
46
+ it "should set the --unbind flag correctly." do
47
+ opts = parse('--unbind', 'some-policy')
48
+ opts[:action][0].should eql(:unbind)
49
+ opts[:policy_name].should eql('some-policy')
50
+ end
36
51
 
37
- it "should set the --unbind flag correctly." do
38
- parse('--unbind', 'some-policy')[:action][:unbind].should eql('some-policy')
52
+ it "should fail when the --priority flag is not an integer" do
53
+ attempting_to { parse('--priority', 'blah') }.should raise_error(OptionParser::InvalidArgument)
54
+ end
55
+
56
+ it "should set the --priority flag correctly." do
57
+ parse('--priority', '6')[:priority].should eql(6)
58
+ end
59
+
60
+ it "should set the --config flag correctly." do
61
+ parse('--config', 'CONFIG')[:config].should eql('CONFIG')
62
+ end
39
63
  end
40
64
 
41
- it "should set the --config flag correctly." do
42
- parse('--config', 'CONFIG')[:config].should eql('CONFIG')
65
+ describe "while validating" do
66
+ it "should fail when the priority flag is set with unbind" do
67
+ attempting_to { validate('-n', 'net', '--unbind', 'policy', '--priority', '5', 'host') }.should raise_error(Netscaler::ConfigurationError, /priority/)
68
+ end
69
+
70
+ it "should set the priority when the bind flag is set." do
71
+ validate('-n', 'net', '--bind', 'policy', '--priority', '5', 'host')
72
+ end
43
73
  end
44
74
  end
45
75
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netscaler-cli
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gabe McArthur
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-30 00:00:00 -07:00
18
+ date: 2010-10-12 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -118,6 +118,8 @@ files:
118
118
  - spec/configs/missing-username.yml
119
119
  - spec/configs/simple-config.yml
120
120
  - spec/helpers.rb
121
+ - spec/server/cli_spec.rb
122
+ - spec/service/cli_spec.rb
121
123
  - spec/vserver/cli_spec.rb
122
124
  has_rdoc: true
123
125
  homepage: http://github.com/gabemc/netscaler-cli
@@ -156,4 +158,6 @@ summary: Simple command line utilities for interacting remotely with a Netscaler
156
158
  test_files:
157
159
  - spec/vserver/cli_spec.rb
158
160
  - spec/helpers.rb
161
+ - spec/server/cli_spec.rb
162
+ - spec/service/cli_spec.rb
159
163
  - spec/config_spec.rb