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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#serverObject

Returns the value of attribute server.



11
12
13
# File 'lib/veslo.rb', line 11

def server
  @server
end

Class Method Details

.client(server) ⇒ Object



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

def self.client(server)
  client = self.new
  client.server = RestClient::Resource.new(server, :headers => {:accept => "application/octet"})
  client
end

Instance Method Details

#delete(resource, name, data) ⇒ Object



79
80
81
82
83
# File 'lib/veslo.rb', line 79

def delete(resource, name, data)
  @resource = resource
  @name = name
  resource_delete(data)
end

#get(resource, name) ⇒ Object



44
45
46
47
48
# File 'lib/veslo.rb', line 44

def get(resource, name)
  @resource = resource
  @name = name
  resource_get
end

#parse_commands(commands) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
# File 'lib/veslo.rb', line 30

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

#put(resource, name, data) ⇒ Object



69
70
71
72
73
# File 'lib/veslo.rb', line 69

def put(resource, name, data)
  @resource = resource
  @name = name
  resource_put(data)
end

#resource_delete(data) ⇒ Object



85
86
87
# File 'lib/veslo.rb', line 85

def resource_delete(data)
  @server["#{@resource}/#{@name}"].delete(data)
end

#resource_getObject



50
51
52
# File 'lib/veslo.rb', line 50

def resource_get
  @server["#{@resource}/#{@name}"].get
end

#resource_get_cliObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/veslo.rb', line 54

def resource_get_cli
  result = resource_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_put(data) ⇒ Object



75
76
77
# File 'lib/veslo.rb', line 75

def resource_put(data)
  @server["#{@resource}/#{@name}"].put(data)
end

#resource_put_cliObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/veslo.rb', line 90

def resource_put_cli
  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 = resource_put(put_data)
  $stdout.puts "Config uploaded"
  return 0
rescue Errno::ENOENT
  $stderr.puts "File not found: #{@file}"
  return 3
end

#run!(*arguments) ⇒ Object



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

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

#validate_inputObject

Raises:

  • (NotImplementedError)


39
40
41
42
# File 'lib/veslo.rb', line 39

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