Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use RSAPI Transports #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/puppet/provider/hue_light/hue_light.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def set(context, changes)

def get(context)
instances = []
lights = context.device.hue_get('lights', context.device.connection)
lights = context.transport.hue_get('lights', context.transport.connection)

return instances if lights.nil?

Expand All @@ -32,6 +32,6 @@ def get(context)
end

def update(context, name, should)
context.device.hue_put("lights/#{name}/state", context.device.connection, should)
context.transport.hue_put("lights/#{name}/state", context.transport.connection, should)
end
end
48 changes: 48 additions & 0 deletions lib/puppet/transport/hue_light.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Provides a Transport for making calls to Philips Hue lights device(s)
module Puppet::Transport
# A basic device class, that reads its configuration from the provided URL.
# The URL has to be a local file URL.
class HueLight
attr_reader :connection

def initialize(url, _options = {})
@url = URI.parse(url)
raise Puppet::DevError, "Unexpected url '#{url}' found. Only file:/// URLs for configuration supported at the moment." unless @url.scheme == 'file'

Puppet.debug "Trying to connect to #{config['default']['node']['ip']} with dev key #{config['default']['node']['key']}"
@connection = Faraday.new(url: "http://#{config['default']['node']['ip']}/api/#{config['default']['node']['key']}", ssl: { verify: false })
end

def facts
{ 'operatingsystem' => 'philips_hue' }
end

def config
raise "Trying to load config from '#{@url.path}, but file does not exist." unless File.exist? @url.path
@config ||= Hocon.load(@url.path, syntax: Hocon::ConfigSyntax::HOCON)
end

def hue_get(url, connection, args = nil)
url = URI.escape(url) if url
result = connection.get(url, args)
JSON.parse(result.body)
rescue JSON::ParserError => e
raise Puppet::ResourceError, "Unable to parse JSON response from HUE API: #{e}"
end

def hue_put(url, connection, message)
message = message.to_json
connection.put(url, message)
end

def transport
require 'puppet/resource_api/transport'

@transport[@url] ||= Puppet::ResourceApi::Transport.connect(@url)
end

class << self
attr_reader :connection
end
end
end
45 changes: 45 additions & 0 deletions lib/puppet/transport/schema/hue_light.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'puppet/resource_api'

Puppet::ResourceApi.register_transport(
name: 'hue_light',
docs: <<-EOS,
This transport provides Puppet with the capabilities to manage Philips Hue lights
EOS
attributes: {
name: {
type: 'String',
desc: 'The name of the light',
behaviour: :namevar,
},
on: {
type: 'Optional[Boolean]',
desc: 'Switches the light on or off',
},
hue: {
type: 'Optional[Integer]',
desc: 'The colour the light.',
},
bri: {
type: 'Optional[Integer]',
desc: <<DESC,
This is the brightness of a light from its minimum brightness 0 to its maximum brightness 254
(note minimum brightness is not off, and the light will actually return 1 when set to 0 and return 254 when set to 255).
This range has been calibrated so there a perceptually similar steps in brightness over the range.
You can set the “bri” resource to a specific value
DESC
},
sat: {
type: 'Optional[Integer]',
desc: 'The saturation of the hue colour',
},
effect: {
type: 'Optional[Enum[none, colorloop]]',
desc: 'Enables built in effect',
},
alert: {
type: 'Optional[Enum[none, select]]',
desc: 'Enables special blink feature',
},
},
features: ['remote_resource'],
)
12 changes: 6 additions & 6 deletions spec/unit/puppet/provider/hue_light/hue_light_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module Puppet::Provider::HueLight; end
subject(:provider) { described_class.new }

let(:context) { instance_double('Puppet::ResourceApi::BaseContext', 'context') }
let(:hue_device) { instance_double('Puppet::Util::NetworkDevice::Philips_hue::device', 'hue_device') }
let(:hue_transport) { instance_double('Puppet::Transport::hueLight', 'hue_transport') }

before(:each) do
allow(context).to receive(:device).and_return(hue_device)
allow(context).to receive(:transport).and_return(hue_transport)
end

describe '#set' do
Expand Down Expand Up @@ -67,8 +67,8 @@ module Puppet::Provider::HueLight; end

describe '#get' do
before(:each) do
allow(hue_device).to receive(:connection)
allow(hue_device).to receive(:hue_get).and_return(api_data)
allow(hue_transport).to receive(:connection)
allow(hue_transport).to receive(:hue_get).and_return(api_data)
end

context 'when no data is returned from HUE API' do
Expand Down Expand Up @@ -149,8 +149,8 @@ module Puppet::Provider::HueLight; end
end

it 'calls the hue_put method' do
expect(hue_device).to receive(:connection)
expect(hue_device).to receive(:hue_put).with('lights/test/state', anything, should_hash)
expect(hue_transport).to receive(:connection)
expect(hue_transport).to receive(:hue_put).with('lights/test/state', anything, should_hash)
provider.update(context, should_hash[:name], should_hash)
end
end
Expand Down