summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2021-06-13 13:34:41 -0700
committerJohn McCrae <john.mccrae@progress.com>2021-06-14 19:01:15 -0700
commitafb4e9f3aa532ee7020389baa1eaa3e6f474f45e (patch)
tree6d856ea4cc9b90ec29807e8cba9254a9d3bfe26e
parent9b64362e19ba184ee48fbefb362b0fab0898dbf1 (diff)
downloadchef-afb4e9f3aa532ee7020389baa1eaa3e6f474f45e.tar.gz
Try to handle the driver installation
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--kitchen-tests/cookbooks/end_to_end/recipes/_windows_printer.rb2
-rw-r--r--lib/chef/resource/windows_printer.rb17
2 files changed, 14 insertions, 5 deletions
diff --git a/kitchen-tests/cookbooks/end_to_end/recipes/_windows_printer.rb b/kitchen-tests/cookbooks/end_to_end/recipes/_windows_printer.rb
index 18da8af3b0..64a05c05c9 100644
--- a/kitchen-tests/cookbooks/end_to_end/recipes/_windows_printer.rb
+++ b/kitchen-tests/cookbooks/end_to_end/recipes/_windows_printer.rb
@@ -33,6 +33,6 @@ end
windows_printer "HP LaserJet 5th Floor" do
ipv4_address "10.4.64.41"
driver_name "Generic / Text Only"
- port_name "My awesome printer port"
+ port_name "My awesome port"
create_port false
end
diff --git a/lib/chef/resource/windows_printer.rb b/lib/chef/resource/windows_printer.rb
index b64f67a142..24d746b648 100644
--- a/lib/chef/resource/windows_printer.rb
+++ b/lib/chef/resource/windows_printer.rb
@@ -22,6 +22,10 @@ require_relative "../resource"
class Chef
class Resource
+ # @todo
+ # 1. Allow updating the printer properties
+ # 2. Fail with a warning if the port can't be found and create_port is false
+ # 3. Fail with helpful messaging if the printer driver can't be installed
class WindowsPrinter < Chef::Resource
unified_mode true
@@ -29,7 +33,7 @@ class Chef
provides(:windows_printer) { true }
- description "Use the **windows_printer** resource to setup Windows printers. Note that this doesn't currently install a printer driver. You must already have the driver installed on the system."
+ description "Use the **windows_printer** resource to setup Windows printers. This resource will automatically install the driver specified in the `driver_name` property and will automatically create a printer port using either the `ipv4_address` property or the `port_name property."
introduced "14.0"
examples <<~DOC
**Create a printer**:
@@ -67,7 +71,6 @@ class Chef
create_port false
end
```
-
DOC
property :device_id, String,
@@ -122,13 +125,15 @@ class Chef
device_id new_resource.device_id
comment printer_data["Comment"]
default printer_data["Default"]
- driver_name printer_data["DriverName"]
location printer_data["Location"]
shared printer_data["Shared"]
share_name printer_data["ShareName"]
port_name printer_data["PortName"]
- # @todo handle the port current_resource
+ driver_data = powershell_exec(%Q{Get-PrinterDriver -Name="#{new_resource.driver_name}"}).result
+ unless driver_data.empty?
+ driver_name new_resource.driver_name
+ end
end
end
@@ -144,6 +149,10 @@ class Chef
end
end
+ converge_by("install driver #{new_resource.driver_name}") do
+ powershell_exec!("Add-PrinterDriver -Name '#{new_resource.driver_name}'")
+ end
+
converge_by("create #{@new_resource.device_id}") do
powershell_exec! <<-EOH
Set-WmiInstance -class Win32_Printer `