razor-client 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,7 @@
1
1
  config.yaml
2
2
  log/*
3
+ pkg/
3
4
  /.yardoc
4
5
  /Gemfile.local*
5
6
  /coverage
7
+ /.project
data/bin/razor CHANGED
@@ -15,6 +15,9 @@ begin
15
15
  puts "From #{url}:\n\n#{format_document document}\n\n"
16
16
  rescue Razor::CLI::NavigationError => e
17
17
  puts "Error: #{e}\n#{parse.help}\n\n"
18
+ rescue SocketError, Errno::ECONNREFUSED => e
19
+ puts "Error: Could not connect to the server at #{parse.api_url}"
20
+ puts " #{e}\n"
18
21
  rescue RestClient::Exception => e
19
22
  r = e.response
20
23
  puts "Error from doing #{r.args[:method].to_s.upcase} #{r.args[:url]}"
@@ -1,5 +1,6 @@
1
1
  require 'rest-client'
2
2
  require 'json'
3
+ require 'yaml'
3
4
 
4
5
  module Razor::CLI
5
6
  class Navigate
@@ -63,10 +64,10 @@ module Razor::CLI
63
64
  body = {}
64
65
  until @segments.empty?
65
66
  if @segments.shift =~ /\A--([a-z-]+)(=(\S+))?\Z/
66
- body[$1] = ($3 || @segments.shift)
67
+ body[$1] = convert_arg(cmd["name"], $1, ($3 || @segments.shift))
67
68
  end
68
69
  end
69
- # Special treatment for tag rules
70
+ # Parse JSON-valued arguments
70
71
  if cmd["name"] == "create-tag" && body["rule"]
71
72
  body["rule"] = JSON::parse(body["rule"])
72
73
  end
@@ -114,5 +115,20 @@ module Razor::CLI
114
115
  puts "POST #{url.to_s}\n#{body}\n-->\n#{response.body}" if @parse.dump_response?
115
116
  JSON::parse(response.body)
116
117
  end
118
+
119
+ private
120
+ def self.annotations
121
+ @@annotations ||=
122
+ YAML::load_file(File::join(File::dirname(__FILE__), "navigate.yaml"))
123
+ end
124
+
125
+ def self.arg_type(cmd_name, arg_name)
126
+ cmd = annotations["commands"][cmd_name]
127
+ cmd && cmd["args"][arg_name]
128
+ end
129
+
130
+ def convert_arg(cmd_name, arg_name, value)
131
+ self.class.arg_type(cmd_name, arg_name) == "json" ? JSON::parse(value) : value
132
+ end
117
133
  end
118
134
  end
@@ -0,0 +1,14 @@
1
+ ---
2
+
3
+ # This file helps Razor::CLI::Navigate with a few things by annotating the
4
+ # API. Ultimately, this should be something the server provides. But until
5
+ # we better understand what sort of annotation we need from the server,
6
+ # we'll keep this with the client.
7
+
8
+ commands:
9
+ create-tag:
10
+ args:
11
+ rule: json
12
+ update-tag-rule:
13
+ args:
14
+ rule: json
@@ -4,6 +4,8 @@ require 'optparse'
4
4
  module Razor::CLI
5
5
 
6
6
  class Parse
7
+ DEFAULT_RAZOR_API = "http://localhost:8080/api"
8
+
7
9
  def get_optparse
8
10
  @optparse ||= OptionParser.new do |opts|
9
11
  opts.banner = "Usage: razor [FLAGS] NAVIGATION\n"
@@ -13,8 +15,11 @@ module Razor::CLI
13
15
  @dump = true
14
16
  end
15
17
 
16
- opts.on "-U", "--url URL", "The full Razor API URL (default #{@api_url})" do |url|
17
- @api_url = URI.parse(url)
18
+ opts.on "-U", "--url URL",
19
+ "The full Razor API URL, can also be set\n" + " "*37 +
20
+ "with the RAZOR_API environment variable\n" + " "*37 +
21
+ "(default #{DEFAULT_RAZOR_API})" do |url|
22
+ parse_and_set_api_url(url, :opts)
18
23
  end
19
24
 
20
25
  opts.on "-h", "--help", "Show this screen" do
@@ -33,8 +38,13 @@ module Razor::CLI
33
38
 
34
39
  def help
35
40
  output = get_optparse.to_s
36
- output << list_things("collections", navigate.collections)
37
- output << list_things("commands", navigate.commands)
41
+ begin
42
+ output << list_things("collections", navigate.collections)
43
+ output << list_things("commands", navigate.commands)
44
+ rescue
45
+ output << "\nCould not connect to the server at #{@api_url}. More help is available after "
46
+ output << "pointing\nthe client to a Razor server"
47
+ end
38
48
  output
39
49
  end
40
50
 
@@ -49,7 +59,7 @@ module Razor::CLI
49
59
  attr_reader :api_url
50
60
 
51
61
  def initialize(args)
52
- @api_url = URI.parse("http://localhost:8080/api")
62
+ parse_and_set_api_url(ENV["RAZOR_API"] || DEFAULT_RAZOR_API, :env)
53
63
  @args = args.dup
54
64
  rest = get_optparse.order(args)
55
65
  if rest.any?
@@ -62,5 +72,14 @@ module Razor::CLI
62
72
  def navigate
63
73
  @navigate ||=Navigate.new(self, @navigation)
64
74
  end
75
+
76
+ private
77
+ def parse_and_set_api_url(url, source)
78
+ begin
79
+ @api_url = URI.parse(url)
80
+ rescue URI::InvalidURIError => e
81
+ raise Razor::CLI::InvalidURIError.new(url, source)
82
+ end
83
+ end
65
84
  end
66
85
  end
data/lib/razor/cli.rb CHANGED
@@ -12,6 +12,20 @@ module Razor
12
12
  end
13
13
  end
14
14
  end
15
+
16
+ class InvalidURIError < Error
17
+ def initialize(url, type)
18
+ case type
19
+ when :env
20
+ super "URL '#{url}' in ENV variable RAZOR_API is not valid"
21
+ when :opts
22
+ super "URL '#{url}' provided by -U or --url is not valid"
23
+ else
24
+ super "URL '#{url}' is not valid"
25
+ end
26
+ end
27
+ end
28
+
15
29
  end
16
30
  end
17
31
 
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "razor-client"
7
+ spec.version = "0.11.0"
8
+ spec.authors = ["Puppet Labs"]
9
+ spec.email = ["info@puppetlabs.com"]
10
+ spec.description = "The client for the Razor server"
11
+ spec.summary = "The client for everybody's favorite provisioning tool"
12
+ spec.homepage = "https://github.com/puppetlabs/razor-client"
13
+ spec.license = "ASL2"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.bindir = "bin"
17
+ spec.executables = ['razor']
18
+ spec.test_files = spec.files.grep(%r{^spec/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rest-client"
22
+ spec.add_dependency "terminal-table"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -1,3 +1,4 @@
1
+ require "rspec/expectations"
1
2
  require_relative '../spec_helper'
2
3
 
3
4
  describe Razor::CLI::Parse do
@@ -24,6 +25,30 @@ describe Razor::CLI::Parse do
24
25
  url = 'http://razor.example.com:2150/path/to/api'
25
26
  parse('-U',url).api_url.to_s.should == url
26
27
  end
28
+
29
+ it "should terminate with an error if an invalid URL is provided" do
30
+ expect{parse('-U','not valid url')}.to raise_error(Razor::CLI::InvalidURIError)
31
+ end
32
+ end
33
+
34
+ context "with ENV RAZOR_API set" do
35
+ it "should use the given URL" do
36
+ url = 'http://razor.example.com:2150/env/path/to/api'
37
+ ENV["RAZOR_API"] = url
38
+ parse.api_url.to_s.should == url
39
+ end
40
+
41
+ it "should use -U before ENV" do
42
+ env_url = 'http://razor.example.com:2150/env/path/to/api'
43
+ url = 'http://razor.example.com:2150/path/to/api'
44
+ ENV["RAZOR_API"] = env_url
45
+ parse('-U',url).api_url.to_s.should == url
46
+ end
47
+
48
+ it "should terminate with an error if an invalid URL is provided" do
49
+ ENV["RAZOR_API"] = 'not valid url'
50
+ expect{parse}.to raise_error(Razor::CLI::InvalidURIError)
51
+ end
27
52
  end
28
53
 
29
54
  describe "#help", :vcr do
data/spec/spec_helper.rb CHANGED
@@ -30,4 +30,7 @@ end
30
30
  # Record one cassette for each test
31
31
  RSpec.configure do |c|
32
32
  c.treat_symbols_as_metadata_keys_with_true_values = true
33
+ c.before(:each) do
34
+ ENV::delete('RAZOR_API')
35
+ end
33
36
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: razor-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - David Lutterkort
8
+ - Puppet Labs
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-19 00:00:00.000000000 Z
12
+ date: 2013-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -77,7 +77,7 @@ dependencies:
77
77
  version: '0'
78
78
  description: The client for the Razor server
79
79
  email:
80
- - lutter@watzmann.net
80
+ - info@puppetlabs.com
81
81
  executables:
82
82
  - razor
83
83
  extensions: []
@@ -95,7 +95,9 @@ files:
95
95
  - lib/razor/cli.rb
96
96
  - lib/razor/cli/format.rb
97
97
  - lib/razor/cli/navigate.rb
98
+ - lib/razor/cli/navigate.yaml
98
99
  - lib/razor/cli/parse.rb
100
+ - razor-client.gemspec
99
101
  - spec/cli/navigate_spec.rb
100
102
  - spec/cli/parse_spec.rb
101
103
  - spec/fixtures/vcr/Razor_CLI_Navigate/with_a_single_item_path/.yml
@@ -129,7 +131,7 @@ rubyforge_project:
129
131
  rubygems_version: 1.8.23
130
132
  signing_key:
131
133
  specification_version: 3
132
- summary: The client for everyvody's favorite provisioning tool
134
+ summary: The client for everybody's favorite provisioning tool
133
135
  test_files:
134
136
  - spec/cli/navigate_spec.rb
135
137
  - spec/cli/parse_spec.rb