Skip to content

Commit 02527b9

Browse files
committed
(#1) Update query, snapshot, and revert
1 parent f773e0a commit 02527b9

File tree

4 files changed

+91
-29
lines changed

4 files changed

+91
-29
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ gem install vmfloaty
1818
get Gets a vm or vms based on the os flag
1919
help Display global or [command] help documentation
2020
list Shows a list of available vms from the pooler
21-
modify
22-
query
23-
revert
24-
snapshot
21+
modify Modify a vms tags and TTL
22+
query Get information about a given vm
23+
revert Reverts a vm to a specified snapshot
24+
snapshot Takes a snapshot of a given vm
2525
status Prints the status of vmpooler
2626
summary Prints the summary of vmpooler
2727

lib/vmfloaty.rb

+44-17
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def run
5252
c.option '--filter STRING', String, 'A filter to apply to the list'
5353
c.option '--url STRING', String, 'URL of vmpooler'
5454
c.action do |args, options|
55-
# Do something or c.when_called Floaty::Commands::Query
5655
filter = options.filter
5756
url = options.url ||= config['url']
5857

@@ -62,23 +61,37 @@ def run
6261

6362
command :query do |c|
6463
c.syntax = 'floaty query [options]'
65-
c.summary = ''
64+
c.summary = 'Get information about a given vm'
6665
c.description = ''
67-
c.example 'description', 'command example'
68-
c.option '--some-switch', 'Some switch that does something'
66+
c.example 'Get information about a sample host', 'floaty query --url http://vmpooler.example.com --host myvmhost.example.com'
67+
c.option '--url STRING', String, 'URL of vmpooler'
68+
c.option '--host STRING', String, 'Hostname to query'
6969
c.action do |args, options|
70-
# Do something or c.when_called Floaty::Commands::Query
70+
url = options.url ||= config['url']
71+
hostname = options.hostname
72+
73+
Pooler.query(url, hostname)
7174
end
7275
end
7376

7477
command :modify do |c|
7578
c.syntax = 'floaty modify [options]'
76-
c.summary = ''
79+
c.summary = 'Modify a vms tags and TTL'
7780
c.description = ''
7881
c.example 'description', 'command example'
79-
c.option '--some-switch', 'Some switch that does something'
82+
c.option '--url STRING', String, 'URL of vmpooler'
83+
c.option '--token STRING', String, 'Token for vmpooler'
84+
c.option '--host STRING', String, 'Hostname to modify'
85+
c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)'
86+
c.option '--tags HASH', Hash, 'free-form VM tagging'
8087
c.action do |args, options|
81-
# Do something or c.when_called Floaty::Commands::Modify
88+
url = options.url ||= config['url']
89+
hostname = options.hostname
90+
lifetime = options.lifetime
91+
tags = options.tags
92+
token = options.token
93+
94+
Pooler.modify(url, hostname, token, lifetime, tags)
8295
end
8396
end
8497

@@ -93,29 +106,43 @@ def run
93106
hosts = options.hosts
94107
url = options.url ||= config['url']
95108

96-
Pool.delete(hosts, url)
109+
Pool.delete(url, hosts)
97110
end
98111
end
99112

100113
command :snapshot do |c|
101114
c.syntax = 'floaty snapshot [options]'
102-
c.summary = ''
115+
c.summary = 'Takes a snapshot of a given vm'
103116
c.description = ''
104-
c.example 'description', 'command example'
105-
c.option '--some-switch', 'Some switch that does something'
117+
c.example 'Takes a snapshot for a given host', 'floaty snapshot --url http://vmpooler.example.com --host myvm.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
118+
c.option '--url STRING', String, 'URL of vmpooler'
119+
c.option '--host STRING', String, 'Hostname to modify'
120+
c.option '--token STRING', String, 'Token for vmpooler'
106121
c.action do |args, options|
107-
# Do something or c.when_called Floaty::Commands::Snapshot
122+
url = options.url ||= config['url']
123+
hostname = options.hostname
124+
token = options.token
125+
126+
Pooler.snapshot(url, hostname, token)
108127
end
109128
end
110129

111130
command :revert do |c|
112131
c.syntax = 'floaty revert [options]'
113-
c.summary = ''
132+
c.summary = 'Reverts a vm to a specified snapshot'
114133
c.description = ''
115-
c.example 'description', 'command example'
116-
c.option '--some-switch', 'Some switch that does something'
134+
c.example 'Reverts to a snapshot for a given host', 'floaty revert --url http://vmpooler.example.com --host myvm.example.com --token a9znth9dn01t416hrguu56ze37t790bl --snapshot n4eb4kdtp7rwv4x158366vd9jhac8btq'
135+
c.option '--url STRING', String, 'URL of vmpooler'
136+
c.option '--host STRING', String, 'Hostname to modify'
137+
c.option '--token STRING', String, 'Token for vmpooler'
138+
c.option '--snapshot STRING', String, 'SHA of snapshot'
117139
c.action do |args, options|
118-
# Do something or c.when_called Floaty::Commands::Revert
140+
url = options.url ||= config['url']
141+
hostname = options.hostname
142+
token = options.token
143+
snapshot_sha = options.snapshot
144+
145+
Pooler.revert(url, hostname, token, snapshot_sha)
119146
end
120147
end
121148

lib/vmfloaty/auth.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
require 'faraday'
2+
require 'json'
23
require 'vmfloaty/http'
34

45
class Auth
56
def self.get_token(user, url, password)
67
conn = Http.get_conn(url)
78

8-
#resp = conn.post do |req|
9-
# req.url '/v1/token'
10-
# req.headers['Content-Type'] = 'application/json'
11-
# req.user = user
12-
# end
9+
resp = conn.post do |req|
10+
req.url '/v1/token'
11+
req.headers['Content-Type'] = 'application/json'
12+
req.user = user
13+
end
1314
# if ok: true, return token
14-
puts 'Got token'
15+
resp_body = JSON.parse(resp.body)
16+
resp_body
1517
end
1618

1719
def self.delete_token(user, token)

lib/vmfloaty/pooler.rb

+35-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,18 @@ def self.retrieve(os_type, token, url)
4040
puts JSON.parse(response.body)
4141
end
4242

43-
def self.modify(hostname, token, url, lifetime, tags)
43+
def self.modify(url, hostname, token, lifetime, tags)
4444
modify_body = {'lifetime'=>lifetime, 'tags'=>tags}
4545
conn = Http.get_conn(url)
46+
47+
# need to use token
48+
response = conn.put "/v1/#{hostname}"
49+
res_body = JSON.parse(response.body)
50+
51+
puts res_body
4652
end
4753

48-
def self.delete(hostnames, url)
54+
def self.delete(url, hostname)
4955
hosts = hostnames.split(',')
5056
conn = Http.get_conn(url)
5157

@@ -72,4 +78,31 @@ def self.summary(url)
7278
res_body = JSON.parse(response.body)
7379
puts res_body
7480
end
81+
82+
def self.query(url, hostname)
83+
conn = Http.get_conn(url)
84+
85+
response = conn.get "/v1/vm/#{hostname}"
86+
res_body = JSON.parse(response.body)
87+
88+
puts res_body
89+
end
90+
91+
def self.snapshot(url, hostname, token)
92+
conn = Http.get_conn(url)
93+
94+
# need to use token
95+
response = conn.post "/v1/#{hostname}/snapshot"
96+
res_body = JSON.parse(response.body)
97+
puts res_body
98+
end
99+
100+
def self.revert(url, hostname, token, snapshot_sha)
101+
conn = Http.get_conn(url)
102+
103+
# need to use token
104+
response = conn.post "/v1/#{hostname}/snapshot/#{snapshot}"
105+
res_body = JSON.parse(response.body)
106+
puts res_body
107+
end
75108
end

0 commit comments

Comments
 (0)