Skip to content

Commit 80fc3ce

Browse files
authored
Merge pull request #403 from imaqsood/MODULES-9895
(MODULES-9895) iis_virtual_directory unable to create directories wit…
2 parents 0ddb526 + cc3e75e commit 80fc3ce

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

lib/puppet/provider/iis_virtual_directory/webadministration.rb

Lines changed: 21 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(@resource[: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(@resource[: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(@resource[: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(@resource[:sitename], @resource[:name])}' -Name 'physicalPath' -Value '#{@resource[:physicalpath]}';" if @resource[:physicalpath]
71+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'application' -Value '#{@resource[:application]}';" if @resource[:application]
72+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[:sitename], @resource[:name])}' -Name 'userName' -Value '#{@resource[:user_name]}';" if @resource[:user_name]
73+
cmd << "Set-ItemProperty -Path 'IIS:\\Sites\\#{virt_dir_path(@resource[: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(@resource[: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(@resource[:sitename], @resource[:name])}' "
8787
cmd << '-Recurse '
8888
cmd << '-ErrorAction Stop '
8989
cmd = cmd.join
@@ -131,6 +131,18 @@ 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+
"#{sitename}\\#{normalized_name}"
143+
end
144+
end
145+
134146
def escape_string(value)
135147
value.gsub("'", "''")
136148
end

spec/acceptance/iis_virtual_directory_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,36 @@
214214
end
215215
end
216216
end
217+
218+
context 'with names prefixed with site name' do
219+
virt_dir_name = SecureRandom.hex(10).to_s
220+
name = "#{site_name}\\#{virt_dir_name}"
221+
manifest = <<-HERE
222+
file{ 'c:/foo':
223+
ensure => 'directory'
224+
}->
225+
iis_virtual_directory { '#{name}':
226+
ensure => 'present',
227+
sitename => '#{site_name}',
228+
physicalpath => 'c:\\foo'
229+
}
230+
HERE
231+
232+
iis_idempotent_apply('create iis virtual dir', manifest)
233+
234+
it 'configures all expected parameters' do
235+
resource_data = resource('iis_virtual_directory', name)
236+
puppet_resource_should_show('ensure', 'present', resource_data)
237+
end
238+
239+
it 'has the correct IIS provider path' do
240+
expected_path = "/#{site_name}/#{virt_dir_name}"
241+
expect(virtual_directory_path_exists?(site_name, expected_path)).to be(true)
242+
end
243+
244+
it 'remove virt dir name' do
245+
remove_vdir(virt_dir_name, site_name)
246+
end
247+
end
217248
end
218249
end

spec/support/utilities/iis_site.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ def create_path(path)
2020
def remove_all_sites
2121
run_shell(format_powershell_iis_command('Get-Website | Remove-Website'))
2222
end
23+
24+
def virtual_directory_path_exists?(site_name, path)
25+
ps_script = %(
26+
$vdir = Get-WebVirtualDirectory -Site "#{site_name}" | Where-Object { $_.Path -eq "#{path}" };
27+
if ($vdir) { Write-Output "true" } else { Write-Output "false" }
28+
).strip
29+
30+
result = run_shell(format_powershell_iis_command(ps_script))
31+
result[:stdout].strip.casecmp('true').zero?
32+
end

0 commit comments

Comments
 (0)