- name: Prepare random number
  set_fact:
    rpfx: "{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
  run_once: yes

- name: Create instance of PostgreSQL Server -- check mode
  azure_rm_postgresqlserver:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}
    sku:
      name: B_Gen5_1
      tier: Basic
    location: westus2
    storage_mb: 51200
    enforce_ssl: True
    admin_username: zimxyz
    admin_password: Testpasswordxyz12!
  check_mode: yes
  register: output
- name: Assert the resource instance is well created
  assert:
    that:
      - output.changed

- name: Create instance of PostgreSQL Server
  azure_rm_postgresqlserver:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}
    sku:
      name: B_Gen5_1
      tier: Basic
    location: westus2
    storage_mb: 51200
    enforce_ssl: True
    admin_username: zimxyz
    admin_password: Testpasswordxyz12!
  register: output
- name: Assert the resource instance is well created
  assert:
    that:
      - output.changed
      - output.state == 'Ready'

- name: Create again instance of PostgreSQL Server
  azure_rm_postgresqlserver:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}
    sku:
      name: B_Gen5_1
      tier: Basic
    location: westus2
    storage_mb: 51200
    enforce_ssl: True
    admin_username: zimxyz
    admin_password: Testpasswordxyz12!
  register: output
- name: Assert the state has not changed
  assert:
    that:
      - output.changed == false
      - output.state == 'Ready'

- name: Create second instance of PostgreSQL Server
  azure_rm_postgresqlserver:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}second
    sku:
      name: B_Gen5_1
      tier: Basic
    location: westus2
    storage_mb: 51200
    enforce_ssl: True
    admin_username: zimxyz
    admin_password: Testpasswordxyz12!
    tags:
      aaa: bbb

- name: Create second instance of PostgreSQL Server -- add tags
  azure_rm_postgresqlserver:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}second
    sku:
      name: B_Gen5_1
      tier: Basic
    location: westus2
    storage_mb: 51200
    enforce_ssl: True
    admin_username: zimxyz
    admin_password: Testpasswordxyz12!
    tags:
      ccc: ddd

- name: Gather facts PostgreSQL Server
  azure_rm_postgresqlserver_facts:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}second
  register: output 

- name: Assert that facts are returned
  assert:
    that:
      - output.changed == False
      - output.servers[0]['id'] != None
      - output.servers[0]['name'] != None
      - output.servers[0]['location'] != None
      - output.servers[0]['sku']['name'] != None
      - output.servers[0]['sku']['tier'] != None
      - output.servers[0]['sku']['capacity'] != None
      - output.servers[0]['version'] != None
      - output.servers[0]['user_visible_state'] != None
      - output.servers[0]['fully_qualified_domain_name'] != None
      - output.servers[0]['tags']['aaa'] == 'bbb'
      - output.servers[0]['tags']['ccc'] == 'ddd'

- name: Gather facts PostgreSQL Server
  azure_rm_postgresqlserver_facts:
    resource_group: "{{ resource_group }}"
  register: output
- name: Assert that facts are returned
  assert:
    that:
      - output.changed == False
      - output.servers[0]['id'] != None
      - output.servers[0]['name'] != None
      - output.servers[0]['location'] != None
      - output.servers[0]['sku']['name'] != None
      - output.servers[0]['sku']['tier'] != None
      - output.servers[0]['sku']['capacity'] != None
      - output.servers[0]['version'] != None
      - output.servers[0]['user_visible_state'] != None
      - output.servers[0]['fully_qualified_domain_name'] != None
      - output.servers[1]['id'] != None
      - output.servers[1]['name'] != None
      - output.servers[1]['location'] != None
      - output.servers[1]['sku']['name'] != None
      - output.servers[1]['sku']['tier'] != None
      - output.servers[1]['sku']['capacity'] != None
      - output.servers[1]['version'] != None
      - output.servers[1]['user_visible_state'] != None
      - output.servers[1]['fully_qualified_domain_name'] != None


#
# azure_rm_postgresqldatabase tests below
#
- name: Create instance of PostgreSQL Database -- check mode
  azure_rm_postgresqldatabase:
    resource_group: "{{ resource_group }}"
    server_name: postgresqlsrv{{ rpfx }}
    name: testdatabase
  check_mode: yes
  register: output
- name: Assert the resource instance is well created
  assert:
    that:
      - output.changed

- name: Create instance of PostgreSQL Database
  azure_rm_postgresqldatabase:
    resource_group: "{{ resource_group }}"
    server_name: postgresqlsrv{{ rpfx }}
    name: testdatabase
  register: output
- name: Assert the resource instance is well created
  assert:
    that:
      - output.changed
      - output.name == 'testdatabase'

- name: Create again instance of PostgreSQL Database
  azure_rm_postgresqldatabase:
    resource_group: "{{ resource_group }}"
    server_name: postgresqlsrv{{ rpfx }}
    name: testdatabase
  register: output
- name: Assert the state has not changed
  assert:
    that:
      - output.changed == false
      - output.name == 'testdatabase'

- name: Create second instance of PostgreSQL Database
  azure_rm_postgresqldatabase:
    resource_group: "{{ resource_group }}"
    server_name: postgresqlsrv{{ rpfx }}
    name: testdatabase2

- name: Gather facts PostgreSQL Database
  azure_rm_postgresqldatabase_facts:
    resource_group: "{{ resource_group }}"
    server_name: postgresqlsrv{{ rpfx }}
    name: testdatabase
  register: output 

- name: Assert that facts are returned
  assert:
    that:
      - output.changed == False
      - output.databases[0]['server_name'] != None
      - output.databases[0]['name'] != None
      - output.databases[0]['charset'] != None
      - output.databases[0]['collation'] != None

- name: Gather facts PostgreSQL Database
  azure_rm_postgresqldatabase_facts:
    resource_group: "{{ resource_group }}"
    server_name: postgresqlsrv{{ rpfx }}
  register: output
- name: Assert that facts are returned
  assert:
    that:
      - output.changed == False
      - output.databases[0]['server_name'] != None
      - output.databases[0]['name'] != None
      - output.databases[0]['charset'] != None
      - output.databases[0]['collation'] != None
      - output.databases[1]['server_name'] != None
      - output.databases[1]['name'] != None
      - output.databases[1]['charset'] != None
      - output.databases[1]['collation'] != None

- name: Delete instance of PostgreSQL Database -- check mode
  azure_rm_postgresqldatabase:
    resource_group: "{{ resource_group }}"
    server_name: postgresqlsrv{{ rpfx }}
    name: testdatabase
    state: absent
  check_mode: yes
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed

- name: Delete instance of PostgreSQL Database
  azure_rm_postgresqldatabase:
    resource_group: "{{ resource_group }}"
    server_name: postgresqlsrv{{ rpfx }}
    name: testdatabase
    state: absent
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed

- name: Delete unexisting instance of PostgreSQL Database
  azure_rm_postgresqldatabase:
    resource_group: "{{ resource_group }}"
    server_name: postgresqlsrv{{ rpfx }}
    name: testdatabase
    state: absent
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed == false

#
# azure_rm_postgresqlserver continuation / clean up
#

- name: Delete instance of PostgreSQL Server -- check mode
  azure_rm_postgresqlserver:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}
    state: absent
  check_mode: yes
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed

- name: Delete instance of PostgreSQL Server
  azure_rm_postgresqlserver:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}
    state: absent
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed

- name: Delete unexisting instance of PostgreSQL Server
  azure_rm_postgresqlserver:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}
    state: absent
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed == false

- name: Delete second instance of PostgreSQL Server
  azure_rm_postgresqlserver:
    resource_group: "{{ resource_group }}"
    name: postgresqlsrv{{ rpfx }}second
    state: absent
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed