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

- name: Create instance of Key Vault -- check mode
  azure_rm_keyvault:
    resource_group: "{{ resource_group }}"
    vault_name: "vault{{ rpfx }}"
    vault_tenant: 11111111-1111-1111-1111-111122223333
    enabled_for_deployment: yes
    sku:
      name: standard
      family: A
    access_policies:
      - object_id: 99998888-8666-4144-9199-2d7cd0111111
        keys:
          - get
          - list
  check_mode: yes
  register: output
- name: Assert the resource instance is well created
  assert:
    that:
      - output.changed

- name: Create instance of Key Vault
  azure_rm_keyvault:
    resource_group: "{{ resource_group }}"
    vault_name: "vault{{ rpfx }}"
    vault_tenant: 11111111-1111-1111-1111-111122223333
    enabled_for_deployment: yes
    sku:
      name: standard
      family: A
    access_policies:
      - object_id: 99998888-8666-4144-9199-2d7cd0111111
        keys:
          - get
          - list
  register: output
- name: Assert the resource instance is well created
  assert:
    that:
      - output.changed

- name: Create again instance of Key Vault
  azure_rm_keyvault:
    resource_group: "{{ resource_group }}"
    vault_name: "vault{{ rpfx }}"
    vault_tenant: 11111111-1111-1111-1111-111122223333
    enabled_for_deployment: yes
    sku:
      name: standard
      family: A
    access_policies:
      - object_id: 99998888-8666-4144-9199-2d7cd0111111
        keys:
          - get
          - list
  register: output
- name: Assert the state has not changed
  assert:
    that:
      - output.changed == false

- name: Update existing Key Vault (add a rule)
  azure_rm_keyvault:
    resource_group: "{{ resource_group }}"
    vault_name: "vault{{ rpfx }}"
    vault_tenant: 11111111-1111-1111-1111-111122223333
    enabled_for_deployment: yes
    sku:
      name: standard
      family: A
    access_policies:
      - object_id: 99998888-8666-4144-9199-2d7cd0111111
        certificates:
          - get
          - list
      - object_id: 11112222-8666-4144-9199-2d7cd0111111
        keys:
          - get
          - list
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed == true

- name: Delete instance of Key Vault -- check mode
  azure_rm_keyvault:
    resource_group: "{{ resource_group }}"
    vault_name: "vault{{ rpfx }}"
    state: absent
  check_mode: yes
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed

- name: Delete instance of Key Vault
  azure_rm_keyvault:
    resource_group: "{{ resource_group }}"
    vault_name: "vault{{ rpfx }}"
    state: absent
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed

- name: Delete unexisting instance of Key Vault
  azure_rm_keyvault:
    resource_group: "{{ resource_group }}"
    vault_name: "vault{{ rpfx }}"
    state: absent
  register: output
- name: Assert the state has changed
  assert:
    that:
      - output.changed == false
