Skip to content

Commit 2a20451

Browse files
committed
(MODULES-9895) iis_virtual_directory unable to create directories with the same name on different sites
1 parent 0ddb526 commit 2a20451

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

lib/puppet/provider/iis_virtual_directory/webadministration.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ def create
4040
else
4141
# New-WebVirtualDirectory fails when PhysicalPath is a UNC path that unavailable,
4242
# and UNC paths are inherently not necessarily always available.
43-
cmd << "New-Item -Type VirtualDirectory 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' "
43+
cmd << "New-Item -Type VirtualDirectory 'IIS:\\Sites\\#{virt_dir_path(@resouce[:sitename], @resource[:name]}' "
4444
end
4545
cmd << "-Application \"#{@resource[:application]}\" " if @resource[:application]
4646
cmd << "-PhysicalPath \"#{@resource[:physicalpath]}\" " if @resource[:physicalpath]
4747
cmd << '-ErrorAction Stop;'
4848
if @resource[:user_name]
49-
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' " \
49+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resouce[:sitename], @resource[:name]}' " \
5050
"-Name 'userName' -Value '#{@resource[:user_name]}' -ErrorAction Stop;"
5151
end
5252
if @resource[:password]
53-
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' " \
53+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resouce[:sitename], @resource[:name]}' " \
5454
"-Name 'password' -Value '#{escape_string(@resource[:password])}' -ErrorAction Stop;"
5555
end
5656
cmd = cmd.join
@@ -67,10 +67,10 @@ def update
6767

6868
cmd = []
6969

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

7575
cmd = cmd.join
7676
result = self.class.run(cmd)
@@ -79,11 +79,11 @@ def update
7979

8080
def destroy
8181
Puppet.debug "Destroying #{@resource[:name]}"
82-
test = self.class.run("Test-Path -Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}'")
82+
test = self.class.run("Test-Path -Path 'IIS:\\Sites\\#{virt_dir_path(@resouce[:sitename], @resource[:name]}'")
8383
if test[:stdout].strip.casecmp('true').zero?
8484
cmd = []
8585
cmd << 'Remove-Item '
86-
cmd << "-Path 'IIS:\\Sites\\#{@resource[:sitename]}\\#{@resource[:name]}' "
86+
cmd << "-Path 'IIS:\\Sites\\#{virt_dir_path(@resouce[:sitename], @resource[:name]}' "
8787
cmd << '-Recurse '
8888
cmd << '-ErrorAction Stop '
8989
cmd = cmd.join
@@ -131,6 +131,19 @@ def self.instances
131131
end
132132
end
133133

134+
def virt_dir_path(sitename, name)
135+
@cached_virt_dir_path ||= {}
136+
137+
key = "#{sitename}/#{name}"
138+
@cached_virt_dir_path[key] ||= begin
139+
parts = name.tr('/', '\\').split('\\')
140+
parts.shift if parts.first.casecmp?(sitename)
141+
normalized_name = parts.join('\\')
142+
site_path = "#{sitename}\\#{normalized_name}"
143+
[normalized_name, site_path]
144+
end
145+
end
146+
134147
def escape_string(value)
135148
value.gsub("'", "''")
136149
end

spec/acceptance/iis_virtual_directory_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,30 @@
214214
end
215215
end
216216
end
217+
218+
context 'with same names under different sites.' do
219+
site_name1 = SecureRandom.hex(10)
220+
site_name2 = SecureRandom.hex(10)
221+
222+
manifest = <<-HERE
223+
224+
iis_virtual_directory { "#{site_name1}/includes":
225+
ensure => present,
226+
name => 'includes',
227+
sitename => "#{site_name1}",
228+
physicalpath => 'G:\inetpub\includes',
229+
}
230+
231+
iis_virtual_directory { "#{site_name2}includes":
232+
  ensure => present,
233+
  name => 'includes',
234+
  sitename => "#{site_name2}",
235+
  physicalpath => 'G:\inetpub\includes',
236+
end
237+
end
238+
HERE
239+
240+
iis_idempotent_apply('create iis virtual dir', manifest)
241+
end
217242
end
218243
end

0 commit comments

Comments
 (0)