Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
wattsworth
/
lumen-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8db102c1
authored
Nov 08, 2023
by
John Donnal
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
updated workflow for adding nilm nodes to allow custom URL's
parent
8583c8d7
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
50 additions
and
39 deletions
app/controllers/nilms_controller.rb
app/services/nilm/add_nilm_by_key.rb
app/services/nilm/add_nilm_by_user.rb
app/services/nilm/verify_url.rb
spec/controllers/nilms_controller_spec.rb
spec/services/nilm/add_nilm_by_key_spec.rb
app/controllers/nilms_controller.rb
View file @
8db102c1
...
...
@@ -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
...
...
app/services/nilm/add_nilm_by_key.rb
View file @
8db102c1
...
...
@@ -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,26 +32,32 @@ class AddNilmByKey
add_error
(
"invalid authorization key"
)
return
self
end
#2 Figure out the remote URL (resolve IP address to domain name for SSL)
if
joule_params
[
:name_is_host
].
nil?
host
=
remote_ip
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
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
url
=
URI
(
"http://temp"
)
url
.
host
=
host
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
])
#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
...
...
app/services/nilm/add_nilm_by_user.rb
View file @
8db102c1
...
...
@@ -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,19 +36,25 @@ 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)
if
request_params
[
:name_is_host
].
nil?
host
=
remote_ip
#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
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
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
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
)
...
...
app/services/nilm/verify_url.rb
View file @
8db102c1
...
...
@@ -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
spec/controllers/nilms_controller_spec.rb
View file @
8db102c1
...
...
@@ -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
...
...
spec/services/nilm/add_nilm_by_key_spec.rb
View file @
8db102c1
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment