---

- name: set connection information for all tasks
  set_fact:
    aws_connection_info: &aws_connection_info
      aws_access_key: "{{ aws_access_key }}"
      aws_secret_key: "{{ aws_secret_key }}"
      region: "{{ aws_region }}"
    dms_identifier: "{{ resource_prefix }}-dms"
  no_log: yes

- block:
    - name: create endpoints
      dms_endpoint:
          state: present
          endpointidentifier: "{{ dms_identifier }}"
          endpointtype: source
          enginename: aurora
          username: testing
          password: testint1234
          servername: "{{ resource_prefix }}.exampledomain.com"
          port: 3306
          databasename: 'testdb'
          sslmode: none
          <<: *aws_connection_info
      register: result

    - assert:
        that:
          - result is changed
          - result is not failed

    - name: create endpoints no change
      dms_endpoint:
          state: present
          endpointidentifier: "{{ dms_identifier }}"
          endpointtype: source
          enginename: aurora
          username: testing
          password: testint1234
          servername: "{{ resource_prefix }}.exampledomain.com"
          port: 3306
          databasename: 'testdb'
          sslmode: none
          <<: *aws_connection_info
      register: result

    - assert:
        that:
          - result is not changed
          - result is not failed

    - name: update endpoints
      dms_endpoint:
        state: present
        endpointidentifier: "{{ dms_identifier }}"
        endpointtype: source
        enginename: aurora
        username: testing
        password: testint1234
        servername: "{{ resource_prefix }}.exampledomain.com"
        port: 3306
        databasename: 'testdb2'
        sslmode: none
        <<: *aws_connection_info
      register: result

    - assert:
        that:
          - result is changed
          - result is not failed

    - name: update endpoints no change
      dms_endpoint:
        state: present
        endpointidentifier: "{{ dms_identifier }}"
        endpointtype: source
        enginename: aurora
        username: testing
        password: testint1234
        servername: "{{ resource_prefix }}.exampledomain.com"
        port: 3306
        databasename: 'testdb2'
        sslmode: none
        <<: *aws_connection_info
      register: result

    - assert:
        that:
          - result is not changed
          - result is not failed

  always:
    - name: delete endpoints
      dms_endpoint:
        state: absent
        endpointidentifier: "{{ dms_identifier }}"
        endpointtype: source
        enginename: aurora
        username: testing
        password: testint1234
        servername: "{{ resource_prefix }}.exampledomain.com"
        port: 3306
        databasename: 'testdb'
        sslmode: none
        wait: True
        timeout: 60
        retries: 10
        <<: *aws_connection_info
      register: result

    - assert:
        that:
          - result is changed
          - result is not failed

    - name: delete endpoints no change
      dms_endpoint:
        state: absent
        endpointidentifier:  "{{ dms_identifier }}"
        endpointtype: source
        enginename: aurora
        username: testing
        password: testint1234
        servername: "{{ resource_prefix }}.exampledomain.com"
        port: 3306
        databasename: 'testdb'
        sslmode: none
        wait: False
        <<: *aws_connection_info
      register: result

    - assert:
        that:
          - result is not changed
          - result is not failed