Adding idempotency fixes for alerting, cloud and folder modules

* Fixing cloud_plugin idempotency

* Sanity check fixes

* fix test

* fix

* reset test

* Add 9dempotency check to test

* Working cloud_plugin with idempotency

* Adding idempotency for contactPoint

* fixes

* Tests update

* sanity fix

* Fixed

* Fixes

* sanity fix

* Fix sanity

* fixes

* fixes

* Fixed idempotency

* fix sanity

* alert_notification update

* fix notification

* fixes

* folder idempotency

* folder test

Co-authored-by: ishanjainn <ishan.jain@gmail.com>
This commit is contained in:
Ishan Jain 2022-11-01 12:52:19 +05:30 committed by GitHub
parent e103856767
commit 8eeaea2aa8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 106 additions and 41 deletions

View File

@ -148,7 +148,26 @@ def present_alert_contact_point(module):
if result.status_code == 202:
return False, True, result.json()
elif result.status_code == 500:
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points/' + module.params['uid']
sameConfig = False
contactPointInfo = {}
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
for contact_points in result.json():
if contact_points['uid'] == module.params['uid']:
if (contact_points['name'] == module.params['name'] and contact_points['type'] == module.params['type'] and contact_points['settings']
and contact_points['settings'] == module.params['settings']
and contact_points['disableResolveMessage'] == module.params['disableResolveMessage']):
sameConfig = True
contactPointInfo = contact_points
if sameConfig:
return False, False, contactPointInfo
else:
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/contact-points/' + \
module.params['uid']
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
@ -157,20 +176,11 @@ def present_alert_contact_point(module):
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
contactPointFound = False
contactPointInfo = {}
for contact_points in result.json():
if contact_points['uid'] == module.params['uid']:
contactPointFound = True
contactPointInfo = contact_points
if contactPointFound:
return False, True, contactPointInfo
else:
return True, False, "Contact Point not found"
return False, True, contact_points
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}

View File

@ -179,13 +179,18 @@ def alert_notification_policy(module):
'repeat_interval': module.params['repeatInterval']}
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/policies'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
if (result.json()['receiver'] == module.params['rootPolicyReceiver'] and result.json()['routes'] == module.params['routes']
and result.json()['group_wait'] == module.params['groupWait'] and result.json()['group_interval'] == module.params['groupInterval']
and result.json()['repeat_interval'] == module.params['repeatInterval']):
return False, False, result.json()
else:
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/policies'
result = requests.put(api_url, json=body, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
if result.status_code == 202:
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/v1/provisioning/policies'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
return False, True, result.json()
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}

View File

@ -115,6 +115,12 @@ def present_cloud_plugin(module):
if result.status_code == 200:
return False, True, result.json()
elif result.status_code == 409:
api_url = 'https://grafana.com/api/instances/' + module.params['stack_slug'] + '/plugins/' + module.params['name']
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['cloud_api_key']})
if result.json()['pluginSlug'] == module.params['name'] and result.json()['version'] == module.params['version']:
return False, False, result.json()
else:
api_url = 'https://grafana.com/api/instances/' + module.params['stack_slug'] + '/plugins/' + module.params[
'name']
result = requests.post(api_url, json={'version': module.params['version']},

View File

@ -176,6 +176,20 @@ def present_folder(module):
if result.status_code == 200:
return False, True, result.json()
elif result.status_code == 412:
sameConfig = False
folderInfo = {}
api_url = 'https://' + module.params['stack_slug'] + '.grafana.net/api/folders'
result = requests.get(api_url, headers={"Authorization": 'Bearer ' + module.params['grafana_api_key']})
for folder in result.json():
if folder['uid'] == module.params['uid'] and folder['title'] == module.params['title']:
sameConfig = True
folderInfo = folder
if sameConfig:
return False, False, folderInfo
else:
body = {
'uid': module.params['uid'],
'title': module.params['title'],
@ -189,7 +203,6 @@ def present_folder(module):
return False, True, result.json()
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}
else:
return True, False, {"status": result.status_code, 'response': result.json()['message']}

View File

@ -16,6 +16,24 @@
- add_result.failed == false
- add_result.output.provenance == "api"
- name: Idempotency Check
grafana.grafana.alert_contact_point:
name: ops-email
uid: opsemail
type: email
settings: {
addresses: "ops@mydomain.com,devs@mydomain.com"
}
stack_slug: "{{ stack_name }}"
grafana_api_key: "{{ grafana_api_key }}"
state: present
register: idempotent_result
- assert:
that:
- idempotent_result.changed == false
- idempotent_result.output.provenance == "api"
- name: Update Alerting contact point
grafana.grafana.alert_contact_point:
name: ops-email

View File

@ -12,7 +12,21 @@
- add_result.changed == true
- add_result.pluginName == "GitHub"
- name: update a plugin
- name: Idempotency Check
grafana.grafana.cloud_plugin:
name: grafana-github-datasource
version: 1.0.14
stack_slug: "{{ stack_name }}"
cloud_api_key: "{{ grafana_cloud_api_key }}"
state: present
register: idempotency_result
- assert:
that:
- idempotency_result.changed == false
- idempotency_result.pluginName == "GitHub"
- name: Update a plugin
grafana.grafana.cloud_plugin:
name: grafana-github-datasource
version: 1.0.15

View File

@ -10,8 +10,7 @@
- assert:
that:
- create_result.changed == true
- create_result.output.url == "/dashboards/f/test123/ansible-integration-test"
- create_result.failed == false
- name: Delete a folder
grafana.grafana.folder: