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
@service = AddNilmByKey.new
end
@service.run(params, request.remote_ip)
if @service.success?
@nilm = @service.nilm
@role = 'owner'
errors = @service.warnings+@service.errors
if errors.empty?
render plain: "ok"
else
render plain: @service.errors.join(", "), status: :unprocessable_entity
render plain: errors.join(", "), status: :unprocessable_entity
end
end
......
......@@ -12,10 +12,9 @@ class AddNilmByKey
required_keys =
[:port, :scheme, :name, :api_key, :auth_key]
# sanitize parameters so the next line doesn't raise an exception:
request_params.slice!(*required_keys+[:name_is_host, :base_uri])
joule_params = request_params.permit(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, :return_address])
# 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
joule_params[:base_uri]="" if joule_params[:base_uri].nil?
......@@ -33,7 +32,12 @@ class AddNilmByKey
add_error("invalid authorization key")
return self
end
#2 Figure out the remote URL (resolve IP address to domain name for SSL)
auth_key.destroy
#2 Figure out the Joule URL if it is not specified
# (resolve IP address to domain name for SSL)
if joule_params.has_key?('return_address')
url = URI(joule_params[:return_address])
else
if joule_params[:name_is_host].nil?
host = remote_ip
else
......@@ -44,15 +48,16 @@ class AddNilmByKey
url.port = joule_params[:port]
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])
end
#2 Check to see if the URL works
verified_url = verify_url(url,request_params[:api_key])
url = verified_url unless verified_url.nil?
#3 Create the Nilm
adapter = Joule::Adapter.new(url, joule_params[:api_key])
service = CreateNilm.new(adapter)
absorb_status(service.run(name: joule_params[:name], url:url,
key: joule_params[:api_key],
owner: auth_key.user))
auth_key.destroy if service.success?
@nilm = service.nilm
self
end
......
......@@ -18,7 +18,7 @@ class AddNilmByUser
[:port, :scheme, :name, :api_key] +
[: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
# 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?
......@@ -36,7 +36,11 @@ class AddNilmByUser
add_errors(owner.errors.full_messages)
return self
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
# (resolve IP address to domain name for SSL)
if request_params.has_key?('return_address')
url = URI(request_params[:return_address])
else
if request_params[:name_is_host].nil?
host = remote_ip
else
......@@ -47,8 +51,10 @@ class AddNilmByUser
url.port = request_params[:port]
url.scheme = request_params[:scheme]
url.path = request_params[:base_uri]
end
# 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
adapter = Joule::Adapter.new(url, request_params[:api_key])
service = CreateNilm.new(adapter)
......
......@@ -2,10 +2,6 @@ module VerifyUrl
def verify_url(orig_url, key)
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
resp = HTTParty.get(url, verify: false,
headers: {'X-API-KEY': key})
......@@ -23,6 +19,6 @@ module VerifyUrl
return url if resp.parsed_response.downcase == 'joule server'
rescue StandardError #ignore exceptions
end
return orig_url # unsuccessful modification, return the original url
nil # url is not valid and cannot be successfully modified
end
end
\ No newline at end of file
......@@ -182,7 +182,9 @@ RSpec.describe NilmsController, type: :request do
scheme: "http", base_uri: "/joule"}
post "/nilms.json",
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
nilm = Nilm.find_by_name('Test Node')
expect(nilm).to_not be nil
......@@ -216,7 +218,9 @@ RSpec.describe NilmsController, type: :request do
scheme: "http", base_uri: "/joule"}
post "/nilms.json",
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
nilm = Nilm.find_by_name('Test Node')
expect(nilm).to_not be nil
......
......@@ -84,8 +84,8 @@ RSpec.describe 'AddNilmByKey' do
expect(Nilm.count).to eq 1
# owner doesn't get permissions on the existing nilm
expect(owner.admins_nilm?(nilm)).to be false
# auth key is not deleted
expect(NilmAuthKey.count).to eq 1
# auth key is deleted
expect(NilmAuthKey.count).to eq 0
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