I am working on a new custom provider for IIS, forked from the forge module puppet/iis. I am trying to make the module work for some of the older OS that I have to manage, which unfortunately are running Powershell 2.0. I have had it working in Puppet 2016.2 but everything fell apart when I upgraded the Master to 2016.5.1, and rather than keep my 'hacked together' version of the module I've started to re-do it to my needs from the ground up (re-using what I can, learning as I go) following the rather handy "Puppet Types & Providers" book by Dan Bode & Nan Liu.
Okay enough pretext. I am running into an issue implementing the *self.instances* method. Here is what I have been working with.
require 'json'
# snap_mod global variable decides whether to import the WebAdministration module, or add the WebAdministration snap-in.
$snap_mod = "$psver = [int]$PSVersionTable.PSVersion.Major; If($psver -lt 4){Add-PSSnapin WebAdministration}else{Import-Module WebAdministration}"
...
def self.instances
# Powershell could convert this output to JSON, but only from PS3.0 onwards (ConvertTo-JSON).
# For maximum compatibility we will have Ruby do it.
inst_cmd = "#$snap_mod; Get-ChildItem 'IIS:\\sites' | ForEach-Object {Get-ItemProperty $_.PSPath | Select name, physicalPath, applicationPool, hostHeader, state, bindings}"
# Must pass the .parse method an array, so we turn inst_cmd into an array, run it, convert the result to JSON and select the first array result...
resp = Puppet::Type::Iis_site::ProviderPowershell.run(inst_cmd)
site_json = JSON.parse(["#{resp}"].to_json).first
site_json = [site_json] #if site_json.is_a?(Hash)
site_json.map do |site|
site_hash = {}
site_hash[:ensure] = :present
site_hash[:state] = site['state']
site_hash[:name] = site['name']
site_hash[:path] = site['physicalPath']
site_hash[:app_pool] = site['applicationPool']
binding_collection = site['bindings']['Collection']
bindings = binding_collection.first['bindingInformation']
site_hash[:protocol] = site['bindings']['Collection'].first['protocol']
site_hash[:ip] = bindings.split(':')[0]
site_hash[:port] = bindings.split(':')[1]
site_hash[:host_header] = bindings.split(':')[2]
site_hash[:ssl] = if site['bindings']['Collection'].first['sslFlags'].zero?
:false
else
:true
end
new(site_hash)
end
end
The trouble I've encountered is that the *.to_json* method [needs to be given an array](https://makandracards.com/makandra/15611-how-to-fix-unexpected-token-error-for-json-parse). However no matter how I try to do this, something else doesn't work. When I provide it an array (as per example above) the *.first* method breaks. When I also provide that an array, the *.map* method breaks.
I am new to Ruby as I'm sure you've guessed. I am well aware there are likely several issues here rather than one. Any assistance would be a huge help.
----------
----------
My trouble has been
↧