- name: Create cloudwatch datasource
  register: result
  grafana_datasource:
    name: datasource-cloudwatch
    grafana_url: "{{ grafana_url }}"
    grafana_user: "{{ grafana_username }}"
    grafana_password: "{{ grafana_password}}"
    org_id: '1'
    ds_type: cloudwatch
    ds_url: http://monitoring.us-west-1.amazonaws.com
    aws_auth_type: keys
    aws_default_region: us-west-1
    aws_access_key: speakFriendAndEnter
    aws_secret_key: mel10n
    aws_custom_metrics_namespaces: n1,n2

- debug:
    var: result

- assert:
    that:
    - result.changed
    - "result.msg == 'Datasource datasource-cloudwatch created'"
    - result.datasource.access == 'proxy'
    - result.datasource.basicAuth == false
    - result.datasource.database == ''
    - result.datasource.isDefault == false
    - result.datasource.jsonData.authType == 'keys'
    - result.datasource.jsonData.customMetricsNamespaces == 'n1,n2'
    - result.datasource.jsonData.defaultRegion == 'us-west-1'
    - result.datasource.jsonData.tlsAuth == false
    - result.datasource.jsonData.tlsAuthWithCACert == false
    - result.datasource.name == 'datasource-cloudwatch'
    - result.datasource.orgId == 1
    - result.datasource.password == ''
    - result.datasource.type == 'cloudwatch'
    - result.datasource.url == 'http://monitoring.us-west-1.amazonaws.com'
    - result.datasource.user == ''
    - result.datasource.withCredentials == false

- name: Check cloudwatch datasource creation idempotency
  register: result
  grafana_datasource:
    name: datasource-cloudwatch
    grafana_url: "{{ grafana_url }}"
    grafana_user: "{{ grafana_username }}"
    grafana_password: "{{ grafana_password }}"
    org_id: '1'
    ds_type: cloudwatch
    ds_url: http://monitoring.us-west-1.amazonaws.com
    aws_auth_type: keys
    aws_default_region: us-west-1
    aws_access_key: speakFriendAndEnter
    aws_secret_key: mel10n
    aws_custom_metrics_namespaces: n1,n2

- debug:
    var: result

- assert:
    that:
    - not result.changed
    - result.datasource.access == 'proxy'
    - result.datasource.basicAuth == false
    - result.datasource.database == ''
    - result.datasource.isDefault == false
    - result.datasource.jsonData.authType == 'keys'
    - result.datasource.jsonData.customMetricsNamespaces == 'n1,n2'
    - result.datasource.jsonData.defaultRegion == 'us-west-1'
    - result.datasource.jsonData.tlsAuth == false
    - result.datasource.jsonData.tlsAuthWithCACert == false
    - result.datasource.name == 'datasource-cloudwatch'
    - result.datasource.orgId == 1
    - result.datasource.password == ''
    - result.datasource.type == 'cloudwatch'
    - result.datasource.url == 'http://monitoring.us-west-1.amazonaws.com'
    - result.datasource.user == ''
    - result.datasource.withCredentials == false

- name: Delete cloudwatch datasource
  register: result
  grafana_datasource:
    name: datasource-cloudwatch
    grafana_url: "{{ grafana_url }}"
    grafana_user: "{{ grafana_username }}"
    grafana_password: "{{ grafana_password}}"
    org_id: '1'
    ds_type: cloudwatch
    ds_url: http://monitoring.us-west-1.amazonaws.com
    aws_auth_type: keys
    aws_default_region: us-west-1
    aws_access_key: speakFriendAndEnter
    aws_secret_key: mel10n
    aws_custom_metrics_namespaces: n1,n2
    state: absent

- debug:
    var: result

- assert:
    that:
    - result.changed
    - "result.msg == 'Datasource datasource-cloudwatch deleted.'"

- name: Delete cloudwatch datasource (idempotency)
  register: result
  grafana_datasource:
    name: datasource-cloudwatch
    grafana_url: "{{ grafana_url }}"
    grafana_user: "{{ grafana_username }}"
    grafana_password: "{{ grafana_password}}"
    org_id: '1'
    ds_type: cloudwatch
    ds_url: http://monitoring.us-west-1.amazonaws.com
    aws_auth_type: keys
    aws_default_region: us-west-1
    aws_access_key: speakFriendAndEnter
    aws_secret_key: mel10n
    aws_custom_metrics_namespaces: n1,n2
    state: absent

- debug:
    var: result

- assert:
    that:
    - not result.changed
