WIP
parent
cf84348f4f
commit
08738e70e7
|
@ -61,7 +61,8 @@ class NervesHub
|
|||
NERVES_HUB_ERROR = "NervesHub request failed: %s: %s"
|
||||
|
||||
# HEADERS for HTTP requests to NervesHub
|
||||
HEADERS = {"Content-Type" => "application/json"}
|
||||
HEADERS = {"Content-Type" => "application/json"}
|
||||
DEFAULT_HTTP = Net::HTTP.new(NERVES_HUB_URI.host, NERVES_HUB_URI.port)
|
||||
|
||||
# Raises an exception for when NervesHub API requests fail.
|
||||
def self.bad_http(code, body)
|
||||
|
@ -153,37 +154,40 @@ class NervesHub
|
|||
!(current_cert.nil? && current_key.nil?)
|
||||
end
|
||||
|
||||
def self.set_conn(obj = DEFAULT_HTTP)
|
||||
@conn = obj
|
||||
# Setting the contents of this
|
||||
# in the CA store doesn't work for some reason?
|
||||
@conn.ca_file = self.current_ca_file
|
||||
# Don't think this is absolutely needed.
|
||||
@conn.cert_store = nil
|
||||
@conn = obj
|
||||
@conn.use_ssl = true
|
||||
@conn.cert = current_cert
|
||||
@conn.key = current_key
|
||||
@conn
|
||||
end
|
||||
|
||||
# HTTP connection.
|
||||
def self.conn
|
||||
if active? && !@conn
|
||||
@conn = Net::HTTP.new(NERVES_HUB_URI.host, NERVES_HUB_URI.port)
|
||||
@conn.use_ssl = true
|
||||
@conn.cert = current_cert
|
||||
@conn.key = current_key
|
||||
# Setting the contents of this
|
||||
# in the CA store doesn't work for some reason?
|
||||
@conn.ca_file = self.current_ca_file
|
||||
# Don't think this is absolutely needed.
|
||||
@conn.cert_store = nil
|
||||
end
|
||||
@conn
|
||||
active? && !@conn ? set_conn : @conn
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Helper for making requests to a device url on NervesHub
|
||||
def self.devices_path
|
||||
"/orgs/#{NERVES_HUB_ORG}/devices"
|
||||
def self.devices_path(*chunks)
|
||||
["/orgs", NERVES_HUB_ORG, "devices"].concat(chunks).join("/")
|
||||
end
|
||||
|
||||
# Helper for making requests to a particular device on NervesHub
|
||||
def self.device_path(serial_number)
|
||||
"/orgs/#{NERVES_HUB_ORG}/devices/#{serial_number}"
|
||||
devices_path(serial_number)
|
||||
end
|
||||
|
||||
# Helper for making signing requests for a device on NervesHub
|
||||
def self.device_sign_path(serial_number)
|
||||
"#{devices_path}/#{serial_number}/certificates/sign"
|
||||
devices_path(serial_number, "certificates", "sign")
|
||||
end
|
||||
|
||||
# Generates a key on behalf of a NervesHub device
|
||||
|
|
|
@ -12,7 +12,7 @@ class Device < ApplicationRecord
|
|||
"Resuming log storage."
|
||||
CACHE_KEY = "devices.%s"
|
||||
|
||||
has_many :farmware_envs, dependent: :destroy
|
||||
has_many :farmware_envs, dependent: :destroy
|
||||
has_many :farm_events, dependent: :destroy
|
||||
has_many :farmware_installations, dependent: :destroy
|
||||
has_many :images, dependent: :destroy
|
||||
|
|
|
@ -21,6 +21,7 @@ module Devices
|
|||
end
|
||||
device
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def merge_default_values
|
||||
|
|
|
@ -1,6 +1,18 @@
|
|||
require "spec_helper"
|
||||
|
||||
describe NervesHub do
|
||||
class StubConn
|
||||
attr_accessor :ca_file, :cert_store, :use_ssl, :cert, :key
|
||||
end
|
||||
|
||||
class StubResp
|
||||
attr_accessor :code, :body
|
||||
|
||||
def initialize(code, body)
|
||||
@code, @body = code, body
|
||||
end
|
||||
end
|
||||
|
||||
it "generates HTTP failure messages" do
|
||||
status = "800"
|
||||
msg = "failed to reticulate splines."
|
||||
|
@ -8,4 +20,22 @@ describe NervesHub do
|
|||
.to raise_error(NervesHub::NervesHubHTTPError)
|
||||
end
|
||||
|
||||
it "generates URL paths" do
|
||||
expect(NervesHub.devices_path).to eq "/orgs/farmbot/devices"
|
||||
expect(NervesHub.device_path("foo")).to eq "/orgs/farmbot/devices/foo"
|
||||
expect(NervesHub.device_sign_path(123)).to eq "/orgs/farmbot/devices/123/certificates/sign"
|
||||
end
|
||||
|
||||
it "gets a device via .device" do
|
||||
conn = StubConn.new
|
||||
resp = StubResp.new("200", { "data" => { hello: :world } }.to_json)
|
||||
ser = "f1o2o3"
|
||||
url = NervesHub.device_path(ser)
|
||||
|
||||
allow(conn).to receive(:get).with(url).and_return(resp)
|
||||
|
||||
NervesHub.set_conn(conn)
|
||||
|
||||
expect(NervesHub.device(ser)).to eq(hello: "world")
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue