Class: Veslo

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/veslo.rb

Constant Summary collapse

SUPPORTED_METHODS =
["put", "get"]
SUPPORTED_RESOURCES =
["configurations"]

Instance Method Summary collapse

Instance Method Details

#parse_commands(commands) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
# File 'lib/veslo.rb', line 23

def parse_commands(commands)
  raise ArgumentError.new("Not the right ammount of arguments") unless (3..4).include?(commands.size)
  @resource = commands.shift
  @method = commands.shift
  @name = commands.shift
  @file = commands.shift
  validate_input
end

#resource_getObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/veslo.rb', line 37

def resource_get
  result = @server["#{@resource}/#{@name}"].get
  $stdout.puts result.to_str
  return 0
rescue RestClient::ExceptionWithResponse => e
  case e.response.code
  when 404
    $stderr.puts("Requested resource not found")
    return 1
  else
    $stderr.puts("Request failed with status: #{e.response.code}")
    return 2
  end
end

#resource_putObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/veslo.rb', line 52

def resource_put
  raise NotImplementedError, "No STDIN yet" unless @file
  file_content = File.open(@file, 'r').read
  put_data = "{\"format\":\"app/octet\", \"body\":#{file_content.to_json}}"
  result = @server["#{@resource}/#{@name}"].put(put_data)
  $stdout.puts "Config uploaded"
  return 0
rescue Errno::ENOENT
  $stderr.puts "File not found: #{@file}"
  return 3
end

#run!(*arguments) ⇒ Object



16
17
18
19
20
21
# File 'lib/veslo.rb', line 16

def run!(*arguments)
  argv = parse_options(arguments)
  @server = RestClient::Resource.new(config[:server_url], :headers => {:accept => "application/octet"})
  parse_commands(argv)
  send(:"resource_#{@method}")
end

#validate_inputObject

Raises:

  • (NotImplementedError)


32
33
34
35
# File 'lib/veslo.rb', line 32

def validate_input
  raise NotImplementedError.new("method #{@method} not supported") unless SUPPORTED_METHODS.include?(@method)
  raise NotImplementedError.new("resource #{@resource} not supported") unless SUPPORTED_RESOURCES.include?(@resource)
end