Skip to content

Commit dd373d1

Browse files
tn5993aniranc
authored andcommitted
Refactor clean with a RackHD 2.0 server
Signed-off-by: Aniran Chandravongsri <aniran.chandravongsri@gmail.com>
1 parent 7cbd8fc commit dd373d1

File tree

6 files changed

+234
-6
lines changed

6 files changed

+234
-6
lines changed

fixtures/node_tags.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
"reserved",
3+
"4c9289ae-f1da"
4+
]

fixtures/workflow.json

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
[{
2+
"definition": {
3+
"injectableName": "W1",
4+
"friendlyName": "Testing",
5+
"tasks": [
6+
{
7+
"taskName": "Task.Obm.Node.PxeBoot",
8+
"label": "set-boot-pxe",
9+
"waitOn": {}
10+
}
11+
],
12+
"options": {
13+
"defaults": {
14+
"obmServiceName": null
15+
},
16+
"default": {
17+
"graphOptions": {},
18+
"nodeId": "57f41596d02e1a5c5df6cfa1"
19+
}
20+
}
21+
},
22+
"instanceId": "638c735d-c4a7-42e4-88ac-e7650d2a2f3f",
23+
"context": {
24+
"graphId": "638c735d-c4a7-42e4-88ac-e7650d2a2f3f"
25+
},
26+
"domain": "default",
27+
"name": "Testing",
28+
"injectableName": "W1",
29+
"tasks": {
30+
"145c0e5d-0c08-4d77-a909-bd939825d838": {
31+
"friendlyName": "Set Node Pxeboot",
32+
"injectableName": "Task.Obm.Node.PxeBoot",
33+
"implementsTask": "Task.Base.Obm.Node",
34+
"schemaRef": "obm-control.json",
35+
"options": {
36+
"action": "setBootPxe",
37+
"obmServiceName": null,
38+
"_taskTimeout": 86400000
39+
},
40+
"properties": {
41+
"power": {}
42+
},
43+
"instanceId": "145c0e5d-0c08-4d77-a909-bd939825d838",
44+
"runJob": "Job.Obm.Node",
45+
"label": "set-boot-pxe",
46+
"name": "Task.Obm.Node.PxeBoot",
47+
"waitingOn": {},
48+
"ignoreFailure": false,
49+
"state": "pending",
50+
"terminalOnStates": [
51+
"succeeded",
52+
"timeout",
53+
"cancelled",
54+
"failed"
55+
]
56+
}
57+
},
58+
"_status": "running",
59+
"logContext": {
60+
"graphInstance": "638c735d-c4a7-42e4-88ac-e7650d2a2f3f",
61+
"graphName": "Testing"
62+
},
63+
"updatedAt": "2016-10-05T15:14:27.460Z",
64+
"createdAt": "2016-10-05T15:14:27.460Z"
65+
},
66+
{
67+
"definition": {
68+
"injectableName": "W2",
69+
"friendlyName": "Testing",
70+
"tasks": [
71+
{
72+
"taskName": "Task.Obm.Node.PxeBoot",
73+
"label": "set-boot-pxe",
74+
"waitOn": {}
75+
}
76+
],
77+
"options": {
78+
"defaults": {
79+
"obmServiceName": null
80+
},
81+
"default": {
82+
"graphOptions": {},
83+
"nodeId": "57f41596d02e1a5c5df6cfa1"
84+
}
85+
}
86+
},
87+
"instanceId": "638c735d-c4a7-42e4-88ac-e7650d2a2f3f",
88+
"context": {
89+
"graphId": "638c735d-c4a7-42e4-88ac-e7650d2a2f3f"
90+
},
91+
"domain": "default",
92+
"name": "Testing",
93+
"injectableName": "W2",
94+
"tasks": {
95+
"145c0e5d-0c08-4d77-a909-bd939825d838": {
96+
"friendlyName": "Set Node Pxeboot",
97+
"injectableName": "Task.Obm.Node.PxeBoot",
98+
"implementsTask": "Task.Base.Obm.Node",
99+
"schemaRef": "obm-control.json",
100+
"options": {
101+
"action": "setBootPxe",
102+
"obmServiceName": null,
103+
"_taskTimeout": 86400000
104+
},
105+
"properties": {
106+
"power": {}
107+
},
108+
"instanceId": "145c0e5d-0c08-4d77-a909-bd939825d838",
109+
"runJob": "Job.Obm.Node",
110+
"label": "set-boot-pxe",
111+
"name": "Task.Obm.Node.PxeBoot",
112+
"waitingOn": {},
113+
"ignoreFailure": false,
114+
"state": "pending",
115+
"terminalOnStates": [
116+
"succeeded",
117+
"timeout",
118+
"cancelled",
119+
"failed"
120+
]
121+
}
122+
},
123+
"_status": "running",
124+
"logContext": {
125+
"graphInstance": "638c735d-c4a7-42e4-88ac-e7650d2a2f3f",
126+
"graphName": "Testing"
127+
},
128+
"updatedAt": "2016-10-05T15:14:27.460Z",
129+
"createdAt": "2016-10-05T15:14:27.460Z"
130+
}
131+
]

lib/rackhd/api.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,17 @@ def self.clean_files(config)
262262
raise 'Please specify a target.' unless config['target']
263263

264264
http = Net::HTTP.new(config['target'], config['port'])
265-
request = Net::HTTP::Get.new('/api/common/files/list/all')
265+
request = Net::HTTP::Get.new('/api/2.0/files')
266266
response = http.request(request)
267267

268268
files = JSON.parse(response.body)
269269
files.each do |file|
270-
request = Net::HTTP::Delete.new("/api/common/files/#{file['uuid']}")
270+
request = Net::HTTP::Delete.new("/api/2.0/files/#{file['uuid']}")
271271
response = http.request(request)
272272
raise("Error deleting file: #{file['uuid']}") unless response.kind_of? Net::HTTPNoContent
273273
end
274274

275-
request = Net::HTTP::Get.new('/api/common/files/list/all')
275+
request = Net::HTTP::Get.new('/api/2.0/files')
276276
response = http.request(request)
277277

278278
if JSON.parse(response.body).length != 0

lib/rackhd/json_helpers.rb

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'table_print'
2+
require_relative 'api'
3+
module RackHD
4+
class JsonHelper
5+
def self.get_nodes_table(config, nodes)
6+
node_names = config['node_names']
7+
8+
nodes.each do |node|
9+
mac_addr = node['name']
10+
node['name'] = node_names && node_names[mac_addr] ? "#{node_names[mac_addr]}" : "#{mac_addr}"
11+
tags = RackHD::API.get_node_tags(config, node['id'])
12+
node['tags'] = tags
13+
#//Do logic to get status and cid
14+
# node['status'], node['cid'] = self.get_status_and_cid(tags)
15+
# node['obm'].each do |obmSetting|
16+
# end
17+
18+
# node['cid'] = 'n/a' unless node['cid']
19+
# node['status'] = 'n/a' unless node['status']
20+
# if node['persistent_disk'] && node['persistent_disk']['disk_cid']
21+
# node['disk cid'] = node['persistent_disk']['disk_cid']
22+
# else
23+
# node['disk cid'] = 'n/a'
24+
# end
25+
26+
node['active workflow'] = RackHD::API.get_active_workflow(config, node['id'])
27+
end
28+
29+
# if config['with_ips']
30+
# tp nodes, 'id', 'name', 'obm', 'cid', 'status', 'disk cid', 'active workflow', 'ip'
31+
# else
32+
tp nodes, 'id', 'name', 'tags', 'active workflow'
33+
# end
34+
end
35+
36+
private
37+
def self.get_status_and_cid(tags)
38+
if tags.size == 2
39+
return "n/a", tags[1]
40+
elsif tags.size == 1
41+
return tags[0], "n/a"
42+
else # size =0
43+
return "n/a", "n/a"
44+
end
45+
end
46+
end
47+
end

spec/api_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,12 @@
352352

353353
resp2 = [].to_json
354354

355-
stub_request(:get, "#{config['target']}/api/common/files/list/all")
355+
stub_request(:get, "#{config['target']}/api/2.0/files")
356356
.to_return({ body: resp1 }, { body: resp2 })
357357

358-
stub1 = stub_request(:delete, "#{config['target']}/api/common/files/78e53b30-98dc-4daf-89fb-fe34e1d10cb7")
358+
stub1 = stub_request(:delete, "#{config['target']}/api/2.0/files/78e53b30-98dc-4daf-89fb-fe34e1d10cb7")
359359
.to_return(status: 204)
360-
stub2 = stub_request(:delete, "#{config['target']}/api/common/files/8d8792d5-e1ab-419d-9aff-c49c2a29624a")
360+
stub2 = stub_request(:delete, "#{config['target']}/api/2.0/files/8d8792d5-e1ab-419d-9aff-c49c2a29624a")
361361
.to_return(status: 204)
362362

363363
deleted_files = subject.clean_files(config)

spec/json_helpers_spec.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'rspec'
2+
require 'webmock/rspec'
3+
require_relative '../lib/rackhd/json_helpers'
4+
require 'net/ssh'
5+
6+
7+
describe RackHD::JsonHelper do
8+
subject { RackHD::JsonHelper }
9+
let (:rackhd_gateway) { 'fake-gateway'}
10+
let (:rackhd_host) {'my.server:8080'}
11+
let (:rackhd_username) {'fake-username'}
12+
let (:rackhd_password) {'fake-password'}
13+
let(:config) { {
14+
'target' => rackhd_host,
15+
'obm_user' => 'root',
16+
'password' => 'password',
17+
'server_username' => rackhd_username,
18+
'server_password' => rackhd_password,
19+
'server_gateway' => rackhd_gateway,
20+
'node_names' => {"00:50:56:b8:08:91" => "fakenodealias"}
21+
}}
22+
23+
before(:each) do
24+
$stdout = StringIO.new
25+
tp.set :io, $stdout
26+
end
27+
28+
after(:each) do
29+
tp.clear :io
30+
end
31+
32+
context 'when given a nodes json response' do
33+
it 'displays a nodes table' do
34+
tag_json = File.read('fixtures/node_tags.json')
35+
workflow_json = File.read('fixtures/workflow.json')
36+
stub_request(:get, "#{config['target']}/api/2.0/nodes/583dee4f65617a2013493904/tags")
37+
.to_return(body: tag_json)
38+
stub_request(:get, "#{config['target']}/api/2.0/nodes/583dee4f65617a2013493904/workflows?active=true")
39+
.to_return(body: workflow_json)
40+
nodes_response_body = JSON.parse(File.read('fixtures/nodes.json'))
41+
subject.get_nodes_table(config, nodes_response_body)
42+
43+
expect($stdout.string).to include('583dee4f65617a2013493904 | fakenodealias | n/a | 4c9289ae-f1da | ["W1", "W2"]')
44+
end
45+
end
46+
end

0 commit comments

Comments
 (0)