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


- name: Create a CDN profile(check mode)
  azure_rm_cdnprofile:
      resource_group: "{{ resource_group }}"
      name: "{{ cdnprofilename }}"
      sku: standard_akamai
      tags:
          testing: testing
          delete: on-exit
          foo: bar
  check_mode: yes

- name: Check there is no CDN profile created
  azure_rm_cdnprofile_info:
      resource_group: "{{ resource_group }}"
      name: "{{ cdnprofilename }}"
  register: fact

- name: Check there is no CDN profile created
  assert: { that: "{{ fact.cdnprofiles | length }} == 0" }

- name: Create a CDN profile
  azure_rm_cdnprofile:
      resource_group: "{{ resource_group }}"
      name: "{{ cdnprofilename }}"
      sku: standard_akamai
      tags:
          testing: testing
          delete: on-exit
          foo: bar
  register: output

- name: Assert the CDN profile is well created
  assert:
    that:
      - output.changed
      - output.id != ''

- name: Gather CDN profile facts
  azure_rm_cdnprofile_info:
      resource_group: "{{ resource_group }}"
      name: "{{ cdnprofilename }}"
  register: fact

- name: Assert fact returns the created one
  assert:
      that:
        - "fact.cdnprofiles | length == 1"
        - fact.cdnprofiles[0].sku == 'Standard_Akamai'
        - fact.cdnprofiles[0].tags.foo == 'bar'

- name: Create a CDN profile (idempotent)
  azure_rm_cdnprofile:
      resource_group: "{{ resource_group }}"
      name: "{{ cdnprofilename }}"
      sku: standard_akamai
      tags:
          testing: testing
          delete: on-exit
          foo: bar
  register: output

- name: Assert idempotent
  assert:
    that:
      - not output.changed

- name: Update the CDN profile
  azure_rm_cdnprofile:
      resource_group: "{{ resource_group }}"
      name: "{{ cdnprofilename }}"
      sku: standard_akamai
      tags:
          testing: testing
          delete: on-exit
          foo: bar
          baz: qux
  register: output

- name: Assert the CDN profile is updated
  assert:
    that:
      - output.changed

- name: Delete the CDN profile(check mode)
  azure_rm_cdnprofile:
    resource_group: "{{ resource_group }}"
    name: "{{ cdnprofilename }}"
    state: absent
  check_mode: yes

- name: Gather CDN profile facts
  azure_rm_cdnprofile_info:
      resource_group: "{{ resource_group }}"
      name: "{{ cdnprofilename }}"
  register: fact

- name: Assert the CDN is still there
  assert:
      that:
        - "fact.cdnprofiles | length == 1"
        - fact.cdnprofiles[0].sku == 'Standard_Akamai'
        - fact.cdnprofiles[0].tags.foo == 'bar'
        - fact.cdnprofiles[0].tags.baz == 'qux'

- name: Create a Azure CDN endpoint(check mode)
  azure_rm_cdnendpoint:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
      origins:
        - name: "org{{ endpointname }}"
          host_name: "www.google.com"
      tags:
          testing: testing
          delete: on-exit
          foo: bar
  check_mode: yes

- name: Create a Azure CDN endpoint
  azure_rm_cdnendpoint:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
      origins:
        - name: "org{{ endpointname }}"
          host_name: "www.google.com"
      tags:
          testing: testing
          delete: on-exit
          foo: bar
  register: output

- name: Assert the Azure CDN endpoint is well created
  assert:
    that:
      - output.changed
      - output.id

- name: Get facts of a Azure CDN endpoint
  azure_rm_cdnendpoint_info:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
  register: facts

- name: Assert facts output
  assert:
    that:
      - facts['cdnendpoints'] | length == 1
      - facts['cdnendpoints'][0]['id']
      - facts['cdnendpoints'][0]['name']
      - facts['cdnendpoints'][0]['profile_name']
      - facts['cdnendpoints'][0]['origin']
      - facts['cdnendpoints'][0]['location']
      - facts['cdnendpoints'][0]['provisioning_state']
      - facts['cdnendpoints'][0]['resource_state']

- name: Create a Azure CDN endpoint(idempotent)
  azure_rm_cdnendpoint:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
      origins:
        - name: "org{{ endpointname }}"
          host_name: "www.google.com"
      tags:
          testing: testing
          delete: on-exit
          foo: bar
  register: output

- name: Assert idempotent
  assert:
    that:
      - not output.changed

- name: Stop a Azure CDN endpoint
  azure_rm_cdnendpoint:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
      started: False
  register: output

- name: Assert stopped
  assert:
    that:
      - output.changed

- name: Stop a Azure CDN endpoint(idempotent)
  azure_rm_cdnendpoint:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
      started: False
  register: output

- name: Assert still stopped and not changed
  assert:
    that:
      - not output.changed

- name: Start a Azure CDN endpoint
  azure_rm_cdnendpoint:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
      started: True
  register: output

- name: Assert started
  assert:
    that:
      - output.changed

- name: Update the Azure CDN endpoint
  azure_rm_cdnendpoint:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
      origin_path: /test/
      tags:
          testing: testing
          delete: on-exit
          foo: baz
  register: output

- name: Assert the Azure CDN endpoint is updated
  assert:
    that:
      - output.changed

- name: Delete a Azure CDN endpoint(check mode)
  azure_rm_cdnendpoint:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
      state: absent
  check_mode: yes

- name: Delete a Azure CDN endpoint
  azure_rm_cdnendpoint:
      resource_group: "{{ resource_group }}"
      name: "{{ endpointname }}"
      profile_name: "{{ cdnprofilename }}"
      state: absent

- name: Delete the CDN profile
  azure_rm_cdnprofile:
    resource_group: "{{ resource_group }}"
    name: "{{ cdnprofilename }}"
    state: absent
  register: output

- name: Assert the CDN profile is well deleted
  assert:
    that:
      - output.changed

- name: Get CDN profile fact
  azure_rm_cdnprofile_info:
      resource_group: "{{ resource_group }}"
      name: "{{ cdnprofilename }}"
  register: fact

- name: Assert fact returns empty
  assert:
    that:
      - "fact.cdnprofiles | length == 0"
