project-fifo-ruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/README.md +4 -0
- data/lib/project-fifo.rb +82 -0
- data/lib/project-fifo/dataset.rb +9 -0
- data/lib/project-fifo/iprange.rb +9 -0
- data/lib/project-fifo/package.rb +9 -0
- data/lib/project-fifo/resource.rb +45 -0
- data/lib/project-fifo/vm.rb +19 -0
- data/project-fifo-ruby.gemspec +19 -0
- data/test.rb +42 -0
- metadata +73 -0
data/.gitignore
ADDED
data/README.md
ADDED
data/lib/project-fifo.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
require 'json'
|
3
|
+
require 'project-fifo/resource'
|
4
|
+
require 'project-fifo/vm'
|
5
|
+
require 'project-fifo/dataset'
|
6
|
+
require 'project-fifo/package'
|
7
|
+
require 'project-fifo/iprange'
|
8
|
+
|
9
|
+
class ProjectFifo
|
10
|
+
|
11
|
+
attr_reader :endpoint, :username, :password, :token, :ssh_keys
|
12
|
+
|
13
|
+
def initialize(endpoint, username, password)
|
14
|
+
@endpoint = endpoint
|
15
|
+
@username = username
|
16
|
+
@password = password
|
17
|
+
@ssh_keys = nil
|
18
|
+
@verbose = true
|
19
|
+
@rest = RestClient::Resource.new(endpoint, :headers => { :content_type => 'application/json', :accept => :json })
|
20
|
+
end
|
21
|
+
|
22
|
+
def connect()
|
23
|
+
response = post('sessions', { 'user' => @username, 'password' => @password })
|
24
|
+
@token = response["session"]
|
25
|
+
@ssh_keys = response["metadata"]["jingles"]["ssh_keys"]
|
26
|
+
@rest.headers[:x_snarl_token] = @token
|
27
|
+
response
|
28
|
+
end
|
29
|
+
|
30
|
+
def post(path_part, payload, additional_headers = {}, &block)
|
31
|
+
api_request { @rest[path_part].post(payload.to_json, additional_headers, &block) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def get(path_part, additional_headers = {}, &block)
|
35
|
+
api_request { @rest[path_part].get(additional_headers, &block) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def put(path_part, payload, additional_headers = {}, &block)
|
39
|
+
api_request { @rest[path_part].put(payload.to_json, additional_headers, &block) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def delete(path_part, additional_headers = {}, &block)
|
43
|
+
api_request { @rest[path_part].delete(additional_headers, &block) }
|
44
|
+
end
|
45
|
+
|
46
|
+
def vms
|
47
|
+
@vms ||= ProjectFifo::VM.new(self)
|
48
|
+
end
|
49
|
+
|
50
|
+
def datasets
|
51
|
+
@datasets ||= ProjectFifo::Dataset.new(self)
|
52
|
+
end
|
53
|
+
|
54
|
+
def packages
|
55
|
+
@packages ||= ProjectFifo::Package.new(self)
|
56
|
+
end
|
57
|
+
|
58
|
+
def ipranges
|
59
|
+
@ipranges ||= ProjectFifo::Iprange.new(self)
|
60
|
+
end
|
61
|
+
|
62
|
+
protected
|
63
|
+
|
64
|
+
# inspired by https://github.com/adamhjk/dynect_rest/blob/master/lib/dynect_rest.rb
|
65
|
+
def api_request(&block)
|
66
|
+
response_body = begin
|
67
|
+
response = block.call
|
68
|
+
if 204 == response.code then
|
69
|
+
'{"success": true}'
|
70
|
+
else
|
71
|
+
response.body
|
72
|
+
end
|
73
|
+
rescue RestClient::Exception => e
|
74
|
+
if @verbose
|
75
|
+
puts "I have #{e.inspect} with #{e.http_code}"
|
76
|
+
pp e
|
77
|
+
end
|
78
|
+
e.response
|
79
|
+
end
|
80
|
+
JSON.parse(response_body)
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class ProjectFifo
|
2
|
+
class Resource
|
3
|
+
|
4
|
+
attr_reader :fifo, :namespace
|
5
|
+
|
6
|
+
def initialize(fifo, namespace)
|
7
|
+
@namespace = namespace
|
8
|
+
@fifo = fifo
|
9
|
+
end
|
10
|
+
|
11
|
+
def list
|
12
|
+
fifo.get(namespace)
|
13
|
+
end
|
14
|
+
|
15
|
+
# alias didn't work. research it
|
16
|
+
def [](uuid)
|
17
|
+
get(uuid)
|
18
|
+
end
|
19
|
+
|
20
|
+
def get(uuid)
|
21
|
+
fifo.get(namespace + '/' + uuid)
|
22
|
+
end
|
23
|
+
|
24
|
+
def metadata(uuid, key, value)
|
25
|
+
fifo.put([ namespace, uuid, 'metadata'].join('/'), { key => value })
|
26
|
+
end
|
27
|
+
|
28
|
+
def create(data)
|
29
|
+
fifo.post(namespace, data)
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete(uuid)
|
33
|
+
fifo.delete(namespace + '/' + uuid)
|
34
|
+
end
|
35
|
+
|
36
|
+
def post(uuid, payload)
|
37
|
+
fifo.post(namespace + '/' + uuid, payload)
|
38
|
+
end
|
39
|
+
|
40
|
+
def put(uuid, payload)
|
41
|
+
fifo.put(namespace + '/' + uuid, payload)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class ProjectFifo
|
2
|
+
class VM < ProjectFifo::Resource
|
3
|
+
|
4
|
+
def initialize(fifo)
|
5
|
+
super(fifo, 'vms')
|
6
|
+
end
|
7
|
+
|
8
|
+
%w{ start stop reboot }.each do |act|
|
9
|
+
define_method(act) { |uuid, force| action(uuid, act, force) }
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def action(uuid, act, force = false)
|
15
|
+
put(uuid, { :action => act, :force => force })
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "project-fifo-ruby"
|
5
|
+
spec.version = '0.1.0'
|
6
|
+
spec.authors = [ "Brian Akins" ]
|
7
|
+
spec.email = [ "brian@akins.org" ]
|
8
|
+
spec.description = %q{A simple incomplete project-fifo client API}
|
9
|
+
spec.summary = spec.description
|
10
|
+
spec.homepage = "https://github.com/bakins/project-fifo-ruby"
|
11
|
+
spec.license = "Apache 2.0"
|
12
|
+
|
13
|
+
spec.files = `git ls-files`.split($/)
|
14
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
15
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
+
spec.require_paths = ["lib"]
|
17
|
+
spec.required_ruby_version = ">= 1.9.1"
|
18
|
+
spec.add_dependency("rest-client", "~> 1.6")
|
19
|
+
end
|
data/test.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
2
|
+
require 'project-fifo'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
fifo = ProjectFifo.new("http://192.168.1.100/api/0.1.0/", "admin", "admin")
|
6
|
+
|
7
|
+
fifo.connect
|
8
|
+
|
9
|
+
#fifo.vms.list.each do |vm|
|
10
|
+
# pp fifo.vms[vm]
|
11
|
+
#end
|
12
|
+
|
13
|
+
package = fifo.packages.list.first
|
14
|
+
dataset = fifo.datasets.list.first
|
15
|
+
iprange = fifo.ipranges.list.first
|
16
|
+
|
17
|
+
data = {
|
18
|
+
dataset: dataset,
|
19
|
+
package: package,
|
20
|
+
config: {
|
21
|
+
alias: "api-test",
|
22
|
+
resolvers: [ "8.8.8.8" ],
|
23
|
+
ssh_keys: fifo.ssh_keys,
|
24
|
+
networks: {
|
25
|
+
net0: iprange
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
|
31
|
+
vm = fifo.vms.create(data)
|
32
|
+
|
33
|
+
pp vm
|
34
|
+
|
35
|
+
id = vm['uuid']
|
36
|
+
sleep 10
|
37
|
+
|
38
|
+
fifo.vms.stop(id, true)
|
39
|
+
|
40
|
+
sleep 10
|
41
|
+
|
42
|
+
fifo.vms.delete(id)
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: project-fifo-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brian Akins
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.6'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.6'
|
30
|
+
description: A simple incomplete project-fifo client API
|
31
|
+
email:
|
32
|
+
- brian@akins.org
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- README.md
|
39
|
+
- lib/project-fifo.rb
|
40
|
+
- lib/project-fifo/dataset.rb
|
41
|
+
- lib/project-fifo/iprange.rb
|
42
|
+
- lib/project-fifo/package.rb
|
43
|
+
- lib/project-fifo/resource.rb
|
44
|
+
- lib/project-fifo/vm.rb
|
45
|
+
- project-fifo-ruby.gemspec
|
46
|
+
- test.rb
|
47
|
+
homepage: https://github.com/bakins/project-fifo-ruby
|
48
|
+
licenses:
|
49
|
+
- Apache 2.0
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 1.9.1
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.23
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: A simple incomplete project-fifo client API
|
72
|
+
test_files: []
|
73
|
+
has_rdoc:
|