---
- name: create default pool check
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: started
  register: create_default_check
  check_mode: yes

- name: get actual of create default pool check
  win_command: powershell.exe "Import-Module WebAdministration; Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}"
  register: create_default_actual_check
  failed_when: False

- name: assert create default pool check
  assert:
    that:
    - create_default_check|changed
    - create_default_actual_check.rc == 1

- name: create default pool
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
  register: create_default

- name: get actual of create default pool
  win_command: powershell.exe "Import-Module WebAdministration; Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}"
  register: create_default_actual
  failed_when: False

- name: assert create default pool
  assert:
    that:
    - create_default|changed
    - create_default.info.attributes.name == test_iis_webapppool_name
    - create_default.info.attributes.startMode == 'OnDemand'
    - create_default.info.attributes.state == 'Started'
    - create_default_actual.rc == 0

- name: change attributes of pool check
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes:
      managedPipelineMode: 1 # Using an enum value
      cpu.limit: 95 # Nested values
      processModel.identityType: LocalSystem # Using an enum name
      processModel.loadUserProfile: True
  register: change_pool_attributes_check
  check_mode: yes

- name: assert change attributes of pool check
  assert:
    that:
    - change_pool_attributes_check|changed
    - change_pool_attributes_check.info == create_default.info

- name: change attributes of pool
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes:
      managedPipelineMode: 1 # Using an enum value
      cpu.limit: 95 # Nested values
      processModel.identityType: LocalSystem # Using an enum name
      processModel.loadUserProfile: True
    test: True
  register: change_pool_attributes

- name: assert change attributes of pool
  assert:
    that:
    - change_pool_attributes|changed
    - change_pool_attributes.info.attributes.managedPipelineMode == 'Classic'
    - change_pool_attributes.info.cpu.limit == 95
    - change_pool_attributes.info.processModel.identityType == 'LocalSystem'
    - change_pool_attributes.info.processModel.loadUserProfile == True

- name: change attributes of pool again
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes:
      managedPipelineMode: 1 # Using an enum value
      cpu.limit: 95 # Nested values
      processModel.identityType: LocalSystem # Using an enum name
      processModel.loadUserProfile: True
  register: change_pool_attributes_again

- name: assert change attributes of pool again
  assert:
    that:
    - not change_pool_attributes_again|changed
    - change_pool_attributes_again.info == change_pool_attributes.info

- name: change attributes of pool (old style) check
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes: "managedPipelineMode:0|startMode:OnDemand|cpu.limit:0|processModel.identityType:NetworkService"
  register: change_pool_attributes_deprecated_check
  check_mode: yes

- name: assert change attributes of pool (old style) check
  assert:
    that:
    - change_pool_attributes_deprecated_check|changed
    - change_pool_attributes_deprecated_check.info == change_pool_attributes_again.info

- name: change attributes of pool (old style)
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes: "managedPipelineMode:0|startMode:OnDemand|cpu.limit:10|processModel.identityType:NetworkService"
  register: change_pool_attributes_deprecated

- name: assert change attributes of pool (old style)
  assert:
    that:
    - change_pool_attributes_deprecated|changed
    - change_pool_attributes_deprecated.info.attributes.managedPipelineMode == 'Integrated'
    - change_pool_attributes_deprecated.info.attributes.startMode == 'OnDemand'
    - change_pool_attributes_deprecated.info.cpu.limit == 10
    - change_pool_attributes_deprecated.info.processModel.identityType == 'NetworkService'

- name: change attributes of pool (old style) again
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes: "managedPipelineMode:0|startMode:OnDemand|cpu.limit:10|processModel.identityType:NetworkService"
  register: change_pool_attributes_deprecated_again

- name: assert change attributes of pool (old style) again
  assert:
    that:
    - not change_pool_attributes_deprecated_again|changed
    - change_pool_attributes_deprecated_again.info == change_pool_attributes_deprecated.info

- name: change more complex variables check
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes:
      queueLength: 500
      recycling.periodicRestart.requests: 10 # Deeply nested attribute
      recycling.periodicRestart.time: "00:00:05:00.000000" # Timespan with string
      processModel.pingResponseTime: "00:03:00" # Timespan without days or milliseconds
  register: change_complex_attributes_check
  check_mode: yes

- name: assert change more complex variables check
  assert:
    that:
    - change_complex_attributes_check|changed
    - change_complex_attributes_check.info == change_pool_attributes_deprecated_again.info

- name: change more complex variables
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes:
      queueLength: 500
      recycling.periodicRestart.requests: 10 # Deeply nested attribute
      recycling.periodicRestart.time: "00:00:05:00.000000" # Timespan with string
      processModel.pingResponseTime: "00:03:00" # Timespan without days or milliseconds
  register: change_complex_attributes

- name: assert change more complex variables
  assert:
    that:
    - change_complex_attributes|changed
    - change_complex_attributes.info.attributes.queueLength == 500
    - change_complex_attributes.info.recycling.periodicRestart.requests == 10
    - change_complex_attributes.info.recycling.periodicRestart.time.TotalSeconds == 300
    - change_complex_attributes.info.processModel.pingResponseTime.TotalSeconds == 180

- name: change more complex variables again
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes:
      queueLength: 500
      recycling.periodicRestart.requests: 10 # Deeply nested attribute
      recycling.periodicRestart.time: "00:00:05:00.000000" # Timespan with string
      processModel.pingResponseTime: "00:03:00" # Timespan without days or milliseconds
  register: change_complex_attributes_again

- name: assert change more complex variables again
  assert:
    that:
    - not change_complex_attributes_again|changed
    - change_complex_attributes_again.info == change_complex_attributes.info

- name: stop web pool check
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: stopped
  register: stop_pool_check
  check_mode: yes

- name: get actual status of pool check
  win_command: powershell.exe "Import-Module WebAdministration; (Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}).state"
  register: stop_pool_actual_check

- name: assert stop web pool check
  assert:
    that:
    - stop_pool_check|changed
    - stop_pool_actual_check.stdout == 'Started\r\n'

- name: stop web pool
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: stopped
  register: stop_pool

- name: get actual status of pool
  win_command: powershell.exe "Import-Module WebAdministration; (Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}).state"
  register: stop_pool_actual

- name: assert stop web pool
  assert:
    that:
    - stop_pool|changed
    - stop_pool_actual.stdout == 'Stopped\r\n'

- name: stop web pool again
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: stopped
  register: stop_pool_again

- name: get actual status of pool again
  win_command: powershell.exe "Import-Module WebAdministration; (Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}).state"
  register: stop_pool_actual_again

- name: assert stop web pool again
  assert:
    that:
    - not stop_pool_again|changed
    - stop_pool_actual_again.stdout == 'Stopped\r\n'

- name: start web pool check
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: started
  register: start_pool_check
  check_mode: yes

- name: get actual status of pool check
  win_command: powershell.exe "Import-Module WebAdministration; (Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}).state"
  register: start_pool_actual_check

- name: assert start web pool check
  assert:
    that:
    - start_pool_check|changed
    - start_pool_actual_check.stdout == 'Stopped\r\n'

- name: start web pool
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: started
  register: start_pool

- name: get actual status of pool
  win_command: powershell.exe "Import-Module WebAdministration; (Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}).state"
  register: start_pool_actual

- name: assert start web pool
  assert:
    that:
    - start_pool|changed
    - start_pool_actual.stdout == 'Started\r\n'

- name: start web pool again
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: started
  register: start_pool_again

- name: get actual status of pool again
  win_command: powershell.exe "Import-Module WebAdministration; (Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}).state"
  register: start_pool_actual_again

- name: assert start web pool again
  assert:
    that:
    - not start_pool_again|changed
    - start_pool_actual_again.stdout == 'Started\r\n'

- name: restart web pool
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: restarted
  register: restart_pool

- name: get actual status of pool
  win_command: powershell.exe "Import-Module WebAdministration; (Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}).state"
  register: restart_pool_actual

- name: assert restart web pool
  assert:
    that:
    - restart_pool|changed
    - restart_pool_actual.stdout == 'Started\r\n'

- name: stop pool before restart on stop test
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: stopped

- name: restart from stopped web pool check
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: restarted
  register: restart_from_stop_pool_check
  check_mode: yes

- name: get actual status of pool check
  win_command: powershell.exe "Import-Module WebAdministration; (Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}).state"
  register: restart_from_stop_pool_actual_check

- name: assert restart from stopped web pool check
  assert:
    that:
    - restart_from_stop_pool_check|changed
    - restart_from_stop_pool_actual_check.stdout == 'Stopped\r\n'

- name: restart from stopped web pool
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: restarted
  register: restart_from_stop_pool

- name: get actual status of pool
  win_command: powershell.exe "Import-Module WebAdministration; (Get-Item -Path IIS:\AppPools\{{test_iis_webapppool_name}}).state"
  register: restart_from_stop_pool_actual

- name: assert restart from stopped web pool
  assert:
    that:
    - restart_from_stop_pool|changed
    - restart_from_stop_pool_actual.stdout == 'Started\r\n'

- name: set web pool attribute that is a collection (check mode)
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes:
      recycling.periodicRestart.schedule: "00:10:00,10:10:00"
  register: collection_change_check
  check_mode: yes

- name: get result of set web pool attribute that is a collection (check mode)
  win_shell: |
    Import-Module WebAdministration
    (Get-ItemProperty -Path "IIS:\AppPools\{{test_iis_webapppool_name}}" -Name recycling.periodicRestart.schedule).Collection | ForEach-Object { $_.value.ToString() }
  register: collection_change_result_check

- name: assert results of set web pool attribute that is a collection (check mode)
  assert:
    that:
    - collection_change_check|changed
    - collection_change_result_check.stdout == ""

- name: set web pool attribute that is a collection
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes:
      recycling.periodicRestart.schedule: "00:10:00,10:10:00"
  register: collection_change

- name: get result of set web pool attribute that is a collection
  win_shell: |
    Import-Module WebAdministration
    (Get-ItemProperty -Path "IIS:\AppPools\{{test_iis_webapppool_name}}" -Name recycling.periodicRestart.schedule).Collection | ForEach-Object { $_.value.ToString() }
  register: collection_change_result

- name: assert results of set web pool attribute that is a collection
  assert:
    that:
    - collection_change|changed
    - collection_change_result.stdout_lines == [ "00:10:00", "10:10:00" ]

- name: set web pool attribute that is a collection (idempotent)
  win_iis_webapppool:
    name: '{{test_iis_webapppool_name}}'
    state: present
    attributes:
      recycling.periodicRestart.schedule: [ "00:10:00", "10:10:00" ]
  register: collection_change_again

- name: assert results of set web pool attribute that is a collection (idempotent)
  assert:
    that:
    - not collection_change_again|changed

# The following tests are only for IIS versions 8.0 or newer
- block:
  - name: delete test pool
    win_iis_webapppool:
      name: '{{test_iis_webapppool_name}}'
      state: absent

  - name: create test pool
    win_iis_webapppool:
      name: '{{test_iis_webapppool_name}}'
      state: present
    register: iis_attributes_blank

  - name: change attributes for newer IIS version check
    win_iis_webapppool:
      name: '{{test_iis_webapppool_name}}'
      state: present
      attributes:
        startMode: AlwaysRunning
        processModel.identityType: 3
        processModel.userName: '{{ansible_user}}'
        processModel.password: '{{ansible_password}}'
    register: iis_attributes_new_check
    check_mode: yes
  
  - name: assert change attributes for newer IIS version check
    assert:
      that:
      - iis_attributes_new_check|changed
      - iis_attributes_new_check.info == iis_attributes_blank.info

  - name: change attributes for newer IIS version
    win_iis_webapppool:
      name: '{{test_iis_webapppool_name}}'
      state: present
      attributes:
        startMode: AlwaysRunning
        processModel.identityType: 3
        processModel.userName: '{{ansible_user}}'
        processModel.password: '{{ansible_password}}'
    register: iis_attributes_new
  
  - name: assert change attributes for newer IIS version
    assert:
      that:
      - iis_attributes_new|changed
      - iis_attributes_new.info.attributes.startMode == 'AlwaysRunning'
      - iis_attributes_new.info.processModel.identityType == 'SpecificUser'
      - iis_attributes_new.info.processModel.userName == ansible_user
      - iis_attributes_new.info.processModel.password == ansible_password

  - name: change attributes for newer IIS version again
    win_iis_webapppool:
      name: '{{test_iis_webapppool_name}}'
      state: present
      attributes:
        startMode: AlwaysRunning
        processModel.identityType: 3
        processModel.userName: '{{ansible_user}}'
        processModel.password: '{{ansible_password}}'
    register: iis_attributes_new_again
  
  - name: assert change attributes for newer IIS version again
    assert:
      that:
      - not iis_attributes_new_again|changed
      - iis_attributes_new_again.info == iis_attributes_new.info

  when: iis_version.win_file_version.file_major_part|int > 7
