Commit 8db102c1 by John Donnal

updated workflow for adding nilm nodes to allow custom URL's

parent 8583c8d7
...@@ -38,12 +38,11 @@ class NilmsController < ApplicationController ...@@ -38,12 +38,11 @@ class NilmsController < ApplicationController
@service = AddNilmByKey.new @service = AddNilmByKey.new
end end
@service.run(params, request.remote_ip) @service.run(params, request.remote_ip)
if @service.success? errors = @service.warnings+@service.errors
@nilm = @service.nilm if errors.empty?
@role = 'owner'
render plain: "ok" render plain: "ok"
else else
render plain: @service.errors.join(", "), status: :unprocessable_entity render plain: errors.join(", "), status: :unprocessable_entity
end end
end end
......
...@@ -12,10 +12,9 @@ class AddNilmByKey ...@@ -12,10 +12,9 @@ class AddNilmByKey
required_keys = required_keys =
[:port, :scheme, :name, :api_key, :auth_key] [:port, :scheme, :name, :api_key, :auth_key]
# sanitize parameters so the next line doesn't raise an exception: # sanitize parameters so the next line doesn't raise an exception:
request_params.slice!(*required_keys+[:name_is_host, :base_uri]) request_params.slice!(*required_keys+[:name_is_host, :base_uri, :return_address])
joule_params = request_params.permit(required_keys+[:name_is_host, :base_uri]) joule_params = request_params.permit(required_keys+[:name_is_host, :base_uri, :return_address])
# since we're not explicitly checking for base_uri, give it a default value # since we're not explicitly checking for base_uri, give it a default value
# it should always be present but may be "" which causes the require action to fail # it should always be present but may be "" which causes the require action to fail
joule_params[:base_uri]="" if joule_params[:base_uri].nil? joule_params[:base_uri]="" if joule_params[:base_uri].nil?
...@@ -33,26 +32,32 @@ class AddNilmByKey ...@@ -33,26 +32,32 @@ class AddNilmByKey
add_error("invalid authorization key") add_error("invalid authorization key")
return self return self
end end
#2 Figure out the remote URL (resolve IP address to domain name for SSL) auth_key.destroy
if joule_params[:name_is_host].nil? #2 Figure out the Joule URL if it is not specified
host = remote_ip # (resolve IP address to domain name for SSL)
if joule_params.has_key?('return_address')
url = URI(joule_params[:return_address])
else else
host = joule_params[:name] if joule_params[:name_is_host].nil?
host = remote_ip
else
host = joule_params[:name]
end
url = URI("http://temp")
url.host = host
url.port = joule_params[:port]
url.scheme = joule_params[:scheme]
url.path = joule_params[:base_uri]
end end
url = URI("http://temp") #2 Check to see if the URL works
url.host = host verified_url = verify_url(url,request_params[:api_key])
url.port = joule_params[:port] url = verified_url unless verified_url.nil?
url.scheme = joule_params[:scheme]
url.path = joule_params[:base_uri]
# check to see if this is a valid URL for Joule
url = verify_url(url,request_params[:api_key])
#3 Create the Nilm #3 Create the Nilm
adapter = Joule::Adapter.new(url, joule_params[:api_key]) adapter = Joule::Adapter.new(url, joule_params[:api_key])
service = CreateNilm.new(adapter) service = CreateNilm.new(adapter)
absorb_status(service.run(name: joule_params[:name], url:url, absorb_status(service.run(name: joule_params[:name], url:url,
key: joule_params[:api_key], key: joule_params[:api_key],
owner: auth_key.user)) owner: auth_key.user))
auth_key.destroy if service.success?
@nilm = service.nilm @nilm = service.nilm
self self
end end
......
...@@ -18,7 +18,7 @@ class AddNilmByUser ...@@ -18,7 +18,7 @@ class AddNilmByUser
[:port, :scheme, :name, :api_key] + [:port, :scheme, :name, :api_key] +
[:first_name, :last_name, :email, :password] [:first_name, :last_name, :email, :password]
request_params = request_params.permit(required_keys+[:name_is_host, :base_uri]) request_params = request_params.permit(required_keys+[:name_is_host, :base_uri, :return_address])
# since we're not explicitly checking for base_uri, give it a default value # since we're not explicitly checking for base_uri, give it a default value
# it should always be present but may be "" which causes the require action to fail # it should always be present but may be "" which causes the require action to fail
request_params[:base_uri]="" if request_params[:base_uri].nil? request_params[:base_uri]="" if request_params[:base_uri].nil?
...@@ -36,19 +36,25 @@ class AddNilmByUser ...@@ -36,19 +36,25 @@ class AddNilmByUser
add_errors(owner.errors.full_messages) add_errors(owner.errors.full_messages)
return self return self
end end
#2 Figure out the remote URL (resolve IP address to domain name for SSL) #2 Figure out the remote URL if it is not specified
if request_params[:name_is_host].nil? # (resolve IP address to domain name for SSL)
host = remote_ip if request_params.has_key?('return_address')
url = URI(request_params[:return_address])
else else
host = request_params[:name] if request_params[:name_is_host].nil?
host = remote_ip
else
host = request_params[:name]
end
url = URI("http://temp")
url.host = host
url.port = request_params[:port]
url.scheme = request_params[:scheme]
url.path = request_params[:base_uri]
end end
url = URI("http://temp")
url.host = host
url.port = request_params[:port]
url.scheme = request_params[:scheme]
url.path = request_params[:base_uri]
# check to see if this is a valid URL for Joule # check to see if this is a valid URL for Joule
url = verify_url(url,request_params[:api_key]) verified_url = verify_url(url,request_params[:api_key])
url = verified_url unless verified_url.nil?
#3 Create the Nilm #3 Create the Nilm
adapter = Joule::Adapter.new(url, request_params[:api_key]) adapter = Joule::Adapter.new(url, request_params[:api_key])
service = CreateNilm.new(adapter) service = CreateNilm.new(adapter)
......
...@@ -2,10 +2,6 @@ module VerifyUrl ...@@ -2,10 +2,6 @@ module VerifyUrl
def verify_url(orig_url, key) def verify_url(orig_url, key)
url = orig_url.dup url = orig_url.dup
# only verify when the host is 127.0.0.1
if orig_url.host!='127.0.0.1'
return orig_url
end
begin begin
resp = HTTParty.get(url, verify: false, resp = HTTParty.get(url, verify: false,
headers: {'X-API-KEY': key}) headers: {'X-API-KEY': key})
...@@ -23,6 +19,6 @@ module VerifyUrl ...@@ -23,6 +19,6 @@ module VerifyUrl
return url if resp.parsed_response.downcase == 'joule server' return url if resp.parsed_response.downcase == 'joule server'
rescue StandardError #ignore exceptions rescue StandardError #ignore exceptions
end end
return orig_url # unsuccessful modification, return the original url nil # url is not valid and cannot be successfully modified
end end
end end
\ No newline at end of file
...@@ -182,7 +182,9 @@ RSpec.describe NilmsController, type: :request do ...@@ -182,7 +182,9 @@ RSpec.describe NilmsController, type: :request do
scheme: "http", base_uri: "/joule"} scheme: "http", base_uri: "/joule"}
post "/nilms.json", post "/nilms.json",
params: user_params.merge(nilm_params) params: user_params.merge(nilm_params)
expect(response).to have_http_status(:ok) # since there is no NILM at this address the response is a 422 error
expect(response.body).to include("cannot contact node at")
expect(response).to have_http_status(:unprocessable_entity)
# make sure the NILM was built # make sure the NILM was built
nilm = Nilm.find_by_name('Test Node') nilm = Nilm.find_by_name('Test Node')
expect(nilm).to_not be nil expect(nilm).to_not be nil
...@@ -216,7 +218,9 @@ RSpec.describe NilmsController, type: :request do ...@@ -216,7 +218,9 @@ RSpec.describe NilmsController, type: :request do
scheme: "http", base_uri: "/joule"} scheme: "http", base_uri: "/joule"}
post "/nilms.json", post "/nilms.json",
params: user_params.merge(nilm_params) params: user_params.merge(nilm_params)
expect(response).to have_http_status(:ok) # since there is no NILM at this address the response is a 422 error
expect(response.body).to include("cannot contact node at")
expect(response).to have_http_status(:unprocessable_entity)
# make sure the NILM was built # make sure the NILM was built
nilm = Nilm.find_by_name('Test Node') nilm = Nilm.find_by_name('Test Node')
expect(nilm).to_not be nil expect(nilm).to_not be nil
......
...@@ -84,8 +84,8 @@ RSpec.describe 'AddNilmByKey' do ...@@ -84,8 +84,8 @@ RSpec.describe 'AddNilmByKey' do
expect(Nilm.count).to eq 1 expect(Nilm.count).to eq 1
# owner doesn't get permissions on the existing nilm # owner doesn't get permissions on the existing nilm
expect(owner.admins_nilm?(nilm)).to be false expect(owner.admins_nilm?(nilm)).to be false
# auth key is not deleted # auth key is deleted
expect(NilmAuthKey.count).to eq 1 expect(NilmAuthKey.count).to eq 0
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment