Skip to content

(MODULES-9895) iis_virtual_directory unable to create directories wit… #403

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

Merged
merged 1 commit into from
Apr 11, 2025
Merged
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
30 changes: 21 additions & 9 deletions lib/puppet/provider/iis_virtual_directory/webadministration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ def create
else
# New-WebVirtualDirectory fails when PhysicalPath is a UNC path that unavailable,
# and UNC paths are inherently not necessarily always available.
cmd << "New-Item -Type VirtualDirectory 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' "
cmd << "New-Item -Type VirtualDirectory 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' "
end
cmd << "-Application \"#{@resource[:application]}\" " if @resource[:application]
cmd << "-PhysicalPath \"#{@resource[:physicalpath]}\" " if @resource[:physicalpath]
cmd << '-ErrorAction Stop;'
if @resource[:user_name]
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' " \
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' " \
"-Name 'userName' -Value '#{@resource[:user_name]}' -ErrorAction Stop;"
end
if @resource[:password]
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' " \
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' " \
"-Name 'password' -Value '#{escape_string(@resource[:password])}' -ErrorAction Stop;"
end
cmd = cmd.join
Expand All @@ -67,10 +67,10 @@ def update

cmd = []

cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' -Name 'physicalPath' -Value '#{@resource[:physicalpath]}';" if @resource[:physicalpath]
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' -Name 'application' -Value '#{@resource[:application]}';" if @resource[:application]
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' -Name 'userName' -Value '#{@resource[:user_name]}';" if @resource[:user_name]
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' -Name 'password' -Value '#{escape_string(@resource[:password])}';" if @resource[:password]
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'physicalPath' -Value '#{@resource[:physicalpath]}';" if @resource[:physicalpath]
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'application' -Value '#{@resource[:application]}';" if @resource[:application]
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'userName' -Value '#{@resource[:user_name]}';" if @resource[:user_name]
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'password' -Value '#{escape_string(@resource[:password])}';" if @resource[:password]

cmd = cmd.join
result = self.class.run(cmd)
Expand All @@ -79,11 +79,11 @@ def update

def destroy
Puppet.debug "Destroying #{@resource[:name]}"
test = self.class.run("Test-Path -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}'")
test = self.class.run("Test-Path -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}'")
if test[:stdout].strip.casecmp('true').zero?
cmd = []
cmd << 'Remove-Item '
cmd << "-Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' "
cmd << "-Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' "
cmd << '-Recurse '
cmd << '-ErrorAction Stop '
cmd = cmd.join
Expand Down Expand Up @@ -131,6 +131,18 @@ def self.instances
end
end

def virt_dir_path(sitename, name)
@cached_virt_dir_path ||= {}

key = "#{sitename}/#{name}"
@cached_virt_dir_path[key] ||= begin
parts = name.tr('/', '\\').split('\\')
parts.shift if parts.first.casecmp?(sitename)
normalized_name = parts.join('\\')
"#{sitename}\\#{normalized_name}"
end
end

def escape_string(value)
value.gsub("'", "''")
end
Expand Down
31 changes: 31 additions & 0 deletions spec/acceptance/iis_virtual_directory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,36 @@
end
end
end

context 'with names prefixed with site name' do
virt_dir_name = SecureRandom.hex(10).to_s
name = "#{site_name}\\#{virt_dir_name}"
manifest = <<-HERE
file{ 'c:/foo':
ensure => 'directory'
}->
iis_virtual_directory { '#{name}':
ensure => 'present',
sitename => '#{site_name}',
physicalpath => 'c:\\foo'
}
HERE

iis_idempotent_apply('create iis virtual dir', manifest)

it 'configures all expected parameters' do
resource_data = resource('iis_virtual_directory', name)
puppet_resource_should_show('ensure', 'present', resource_data)
end

it 'has the correct IIS provider path' do
expected_path = "/#{site_name}/#{virt_dir_name}"
expect(virtual_directory_path_exists?(site_name, expected_path)).to be(true)
end

it 'remove virt dir name' do
remove_vdir(virt_dir_name, site_name)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/support/utilities/iis_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ def create_path(path)
def remove_all_sites
run_shell(format_powershell_iis_command('Get-Website | Remove-Website'))
end

def virtual_directory_path_exists?(site_name, path)
ps_script = %(
$vdir = Get-WebVirtualDirectory -Site "#{site_name}" | Where-Object { $_.Path -eq "#{path}" };
if ($vdir) { Write-Output "true" } else { Write-Output "false" }
).strip

result = run_shell(format_powershell_iis_command(ps_script))
result[:stdout].strip.casecmp('true').zero?
end
Loading